How to code first golden cross after dead cross?

Hello, I have this simple question:
I want to find a candle that cross above ema 20, and count how many time that condition happend after the last time ema 100 is cross above ema 200 ( my code error because it counts even after ema 100 cross below ema 200)

so the sequence should be like this =
ema100 cross below ema200
-counting is zero
ema100 cross above ema200

  • start count from 1 how many time candle cross above ema20
    ema100 cross below ema200
  • stop counting reset to zero
    ema100 cross above ema200
  • start counting again from 1

here is the code I tried to write

thanks

bc = L<(EMA(C,20) AND Cross(H,EMA(C,20)));
gc = Cross(EMA(C,100),EMA(C,200));
dc = Cross(EMA(C,200),EMA(C,100));

it = Cum(bc AND BarsSince(gc)< BarsSince(dc));

PlotShapes(IIf(bc AND it==1,shapeDigit1,Null),colorBlack,0,L,-15);

I forgot to ask = please any body can help to fix the code? thanks

Use SumSince not Cum.
Also your brackets of bc are wrong.

So something like this...

/// @link https://forum.amibroker.com/t/how-to-code-first-golden-cross-after-dead-cross/14244
ema20 = EMA(C,20);
prev_ema20 = Ref(ema20, -1);
bc = L<prev_ema20 AND Cross(H,prev_ema20);

ema100 = EMA(C,100);
ema200 = EMA(C,200);
gc = Cross(ema100,ema200);
dc = Cross(ema200,ema100);

it = IIf(ema100 > ema200, SumSince(gc, bc), 0);

Plot( C, "Price", colorDefault, styleBar );
Plot( it, "it", colorDefault, styleOwnScale );
Plot( ema20, "ema20", colorRed );
Plot( ema100, "ema100", colorBlue );
Plot( ema200, "ema200", colorGreen );
PlotShapes((bc AND it==1)*shapeDigit1,colorBlack,0,L,-15);

6

7 Likes

very quick and precise!
thank you very much for your kind heart.... :slight_smile: