Reputation: 544
I am writing a basic strategy in pine script which triggers a buy position when ema is pointing upwards and any of the last 5 green volume candle is greater than avg 10 day volume.
Issue:
I am not able to match if the highest volume in the last 5 candle is from a green candle or a red candle. I tried looping over but that also did not work.
Code I came up with:
getHighestVol(fastLength) =>
var float highestVolume = na
var bool isGreenVol = na
// Loop through last 5 candles
for i = 1 to fastLength
// Determine if current candle is green
isGreenVol := close[i-1] >= close[i]
// Update highest volume if current candle's volume is higher than previous highest
if (volume[i-1] > highestVolume and isGreenVol)
highestVolume := volume[i-1]
highestVolume
But this gives me empty result. Image attached.
Any help would be appreciated. Thanks
Upvotes: 0
Views: 781
Reputation: 21342
You can store the volume data in a var
array when it is a green candle. Then use array.max()
to get the max value in your array.
//@version=5
indicator("My script", overlay=true)
in_len = input.int(5, "Length")
f_add_to_arr(p_arr, p_val, p_max) =>
len = array.size(p_arr)
if ((len + 1) > p_max)
array.shift(p_arr)
array.push(p_arr, p_val)
var green_vol_arr = array.new_float()
is_green_candle = (close >= open)
if (is_green_candle)
f_add_to_arr(green_vol_arr, volume, in_len)
max_green_vol = array.max(green_vol_arr)
plotchar(max_green_vol, "Max Green Volume", "")
Upvotes: 0