• Get $30 off VIP with code SUMMER30. Ends July 27. Happy trading!

Clear variable based on a condition

rktrader

New member
VIP
Hello Everyone,

I am using below code based on moving averages to know when they are aligned. I am using this code in a watch list column on a 1min time frame.

def EMA8 = ExpAverage(close, 8);
def EMA21 = ExpAverage(close, 21);
def EMA50 = ExpAverage(close, 50);
def EMA100 = ExpAverage(close, 100);
def EMA200 = ExpAverage(close, 200);

def ct_5_up = CompoundValue(1, if EMA8 > EMA21 and EMA21 > EMA50 and EMA50 > EMA100 and EMA100 > EMA200 then ct_5_up[1] + 1 else 0, 1);
def ct_3_up = CompoundValue(1, if EMA50 > EMA100 and EMA100 > EMA200 then ct_5_up[1] + 1 else 0, 1);

addlabel(1, if EMA8 > EMA21 and EMA21 > EMA50 and EMA50 > EMA100 and EMA100 > EMA200 then "5-UP-" + ct_5_up else if EMA50 > EMA100 and EMA100 > EMA200 then "3-UP-" + ct_3_up else "", if EMA8 > EMA21 and EMA21 > EMA50 and EMA50 > EMA100 and EMA100 > EMA200 then color.green else if EMA50 > EMA100 and EMA100 > EMA200 then color.green else color.black);

Basically, I am trying to count the number of bars how long the moving averages are aligned as per the condition above. First I am checking for the ct_5_up and if it is not true, then I am checking next one, ct_3_up. But I want to re-initialize the other one whenever the other condition is true.

It is working fine as expected - but the ct_5_up should be reset to 0, when the ct_3_up condition is met. I tried to use if-else condition but not able to clear the other variable.

Hope I am able to explain what I am trying to achieve and where I am fumbling.

Please suggest. Thank you for your time and help in this regard.
 

Similar threads

Top