Toward Lu
Toward Lu

Reputation: 1

Hi guys, I wanna ask a question about how to use 'for' function for each day loop

here is my code.

int count_long = 0
bool long_trend = false
for i = 0 to lookback by 1
    if ta.ema(close[i], 120) < ta.ema(close[i], 60) and ta.ema(close[i], 60) < ta.ema(close[i], 20)
        count_long += 1
if count_long >= num_day
    long_trend := true

And I got a warning that The function 'ta.ema' should be called on each calculation for consistency. It is recommended to extract the call from this scope.

I wanna get each day test, if ema20 > ema60 and ema60 > ema120 then count 1, if last 10 days there are 7 days match my rule. then I mark it as long_trend. Please help me solve it. Many thanks

Upvotes: 0

Views: 72

Answers (1)

rumpypumpydumpy
rumpypumpydumpy

Reputation: 3803

float ema20 = ta.ema(close, 20)
float ema60 = ta.ema(close, 60)
float ema120 = ta.ema(close, 120)

bool ema_condition = ema20 > ema60 and ema60 > ema120

int count = 0

for i = 0 to 9
    if ema_condition[i]
        count += 1

bool long_trend = count >= 7

Upvotes: 1

Related Questions