SetChartOptions(0,chartShowArrows|chartShowDates);
e1=EMA(C,9);
e2=EMA(C,45);
Plot(e1,"",colorRed);
Plot(e2,"",colorYellow);
BuyCrossover=Cross(e1,e2);
SellCrossover=Cross(e2,e1);
BuyCrossover = Flip( BuyCrossover, SellCrossover );
SellCrossover = Flip( SellCrossover, BuyCrossover );
//BuyCrossover = ExRem( BuyCrossover, SellCrossover );
//SellCrossover = ExRem( SellCrossover, BuyCrossover );
//printf(WriteIf(BuyCrossover,"Buy Signal Generated","") + " \n");
BI = BarIndex();
CrossoverIndex = ValueWhen(BuyCrossover, BI,1);
//printf("Buy Signal Generated bar index" + LastValue(CrossoverIndex,False) + " \n");
//NoteSet("",WriteIf(Buy,"Buy Signal Generated","") + " \n");
//printf("Bar Since buy signal " + BarsSince(BuyCrossover)+ " \n");
//Price should pullback to 45EMA
BarBelow45 = IIf(BuyCrossover, IIf(BarsSince(BuyCrossover) <= 5 AND L <= e2,1,0),0); //this is for buy signal, pullback to 15EMA
BarBelow45 = Flip(BarBelow45,SellCrossover);
//printf("Bar Since BarBelow15 " + BarsSince(BarBelow15)+ " \n");
//printf(WriteIf((BarsSince(BuyCrossover) - BarsSince(BarBelow15) >= 2),"Bar closed below 15EMA after buy signal","") + " \n");
//BarBelow15Confirmed = IIf(BarsSince(BuyCrossover) - BarsSince(BarBelow15) >= 2,1,0);
//PlotShapes(IIf(BarBelow15Confirmed AND (ValueWhen(BarBelow15,L) == L), shapeSmallCircle, shapeNone),colorGreen, 0, L, Offset=-10);
BarCloseAbove9 = IIf(C >= e1,1,0); //this is for buy signal, close above 3EMA
//printf(WriteIf((BarsSince(BuyCrossover) - BarsSince(BarBelow15) >= 2),"Bar closed below 15EMA after buy signal","") + " \n");
BarCloseAbove9 = IIf((BarsSince(BarBelow45) - BarsSince(BarCloseAbove9)) <= 5,1,0);
BarCloseAbove9 = Flip(BarCloseAbove9, SellCrossover);
//BarBelow15 = Flip(BarBelow15, BuyCrossover);
PlotShapes(IIf(BuyCrossover, shapeSmallSquare, shapeNone),colorGreen, 0, L, Offset=-10);
PlotShapes(IIf(BarBelow45, shapeSmallCircle, shapeNone),colorGreen, 0, L, Offset=-10);
//PlotShapes(IIf(BarCloseAbove9, shapeUpArrow, shapeNone),colorGreen, 0, L, Offset=-10);