Reputation: 569
I have developed a pinescript for plotting average candle length for 5 minute candles. but it is incorrect. can somebody help me. The day starts at 9:05am but the average candle at start is not 1 as it is in the code.
The code plots the average candle length of x number of candles with x varying from one for first candle at start of day that is 9:15 am to 72 for last candle at 3:15 pm assuming five minute candle.
indicator("My script")
candle_length = math.abs(high - low)
start_time = timestamp("GMT+0530",year, month, dayofmonth, 9, 05)
x = math.floor(minute(timenow-start_time)/5)
sum_length = 0.0
average_length=0.0
if x > 0
for i = 0 to x - 1
sum_length := sum_length + candle_length[i]
average_length := sum_length / x
else
average_length := 1.0
sum_length := 0.0
plot(average_length, title="Average Length", color=color.red, linewidth=2)
Upvotes: 0
Views: 406
Reputation: 21121
Below line is causing the issues.
x = math.floor(minute(timenow-start_time)/5)
Your use of minute()
and timenow
are wrong.
minute()
returns minute in exchange timezone. You are trying to use it as it would give you the minute between timenow-start_time
.
timenow
also refers to current time. So, using it on historical bars does not make any sense. You should be using time
instead.
The correct statement should be:
x = math.floor((time-start_time)/60000/5)
Upvotes: 1