Multi timeframe buy/sell signal coding help

Plot( C, _DEFAULT_NAME(), IIf( C > Ref(C, -1), colorBlue, IIf(C < Ref(C, -1), colorRed, colorGrey40)), styleBar | styleThick );

_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
SetChartOptions(0,chartShowArrows|chartShowDates);  //Enable X-Axis (Date/Time Axis)


Plot(EMA(Close,20),"EMA-20",colorGreen , styleLine | styleThick); //Plot Historical Short EMA lines
Plot(EMA(Close,50),"EMA-50",colorBlue , styleLine | styleThick); //Plot Historical Long EMA Lines


//Trading Logic
Buy = Cross(EMA(Close,20),EMA(Close,50)); //Positive EMA Crossover (True/False) 
Sell = Cross(EMA(Close,50),EMA(Close,20)); //Negative EMA Crossover (True/False)

Buy=ExRem(Buy,Sell);
Sell=ExRem(Sell,Buy);
Short=Sell;
Cover=Buy;

TimeFrameSet(inDaily); //move to higher timeframe - 60min
ema13 = EMA(C,13);
ema52 = EMA(C,52);
ema100 =EMA(C,100);   //Extract data from higher timeframe
TimeFrameRestore(); //get back to old timeframe

ema13d = TimeFrameExpand(ema13,inDaily); //data expand to match lower timframe dataset
ema52d = TimeFrameExpand(ema52,inDaily);
ema100d = TimeFrameExpand(ema100,inDaily);

Plot(ema13d, "EMA 13",colorRed);  //60min timeframe
Plot(ema52d, "EMA 52",colorYellow);
Plot(ema100d, "EMA 100",colorOrange);


/* Plot Buy and Sell Signal Arrows */
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);
PlotShapes(IIf(Sell, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);


amiemaafl

i have written an afl to explain my problem

Trading logic: buy on 20/50 ema cross over

sell on 20/50 ema cross down

condition : for buy to trigger in 15 min chart (or any time frame) daily ema13>ema52>ema100

for sell to trigger in 15 min chart daily ema100>ema52>ema13

values used here have no trading significance i am using random numbers to learn.

Please help me how can i remove incorrect sell signals from below chart( i know i can write an exploration, filter stocks add them to a watch-list and apply my system on the watch-list, i am looking for a better solution)

Simply change from this one

//Trading Logic
Buy = Cross(EMA(Close,20),EMA(Close,50)); //Positive EMA Crossover (True/False) 
Sell = Cross(EMA(Close,50),EMA(Close,20)); //Negative EMA Crossover (True/False)

to this one

//Trading Logic
Buy = Cross(EMA(Close,20),EMA(Close,50)); //Positive EMA Crossover (True/False) 
Sell = Cross(EMA(Close,50),EMA(Close,20)); //Negative EMA Crossover (True/False)

Buy = Buy AND ema13d > ema52d AND ema52d > ema100d;
Sell = Sell AND ema13d < ema52d AND ema52d < ema100d;

Buy = ExRem(Buy, Sell);
Sell = ExRem(Sell, Buy);

-->

Plot( C, _DEFAULT_NAME(), IIf( C > Ref(C, -1), colorBlue, IIf(C < Ref(C, -1), colorRed, colorGrey40)), styleBar | styleThick );

_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
SetChartOptions(0,chartShowArrows|chartShowDates);  //Enable X-Axis (Date/Time Axis)

Plot(EMA(Close,20),"EMA-20",colorGreen , styleLine | styleThick); //Plot Historical Short EMA lines
Plot(EMA(Close,50),"EMA-50",colorBlue , styleLine | styleThick); //Plot Historical Long EMA Lines

TimeFrameSet(inDaily); //move to higher timeframe - 60min
ema13 = EMA(C,13);
ema52 = EMA(C,52);
ema100 =EMA(C,100);   //Extract data from higher timeframe
TimeFrameRestore(); //get back to old timeframe

ema13d = TimeFrameExpand(ema13,inDaily); //data expand to match lower timframe dataset
ema52d = TimeFrameExpand(ema52,inDaily);
ema100d = TimeFrameExpand(ema100,inDaily);

//Trading Logic
Buy = Cross(EMA(Close,20),EMA(Close,50)); //Positive EMA Crossover (True/False) 
Sell = Cross(EMA(Close,50),EMA(Close,20)); //Negative EMA Crossover (True/False)

Buy = Buy AND ema13d > ema52d AND ema52d > ema100d;
Sell = Sell AND ema13d < ema52d AND ema52d < ema100d;

Buy = ExRem(Buy, Sell);
Sell = ExRem(Sell, Buy);

Short = Sell;
Cover = Buy;

Plot(ema13d, "EMA 13",colorRed);  //60min timeframe
Plot(ema52d, "EMA 52",colorYellow);
Plot(ema100d, "EMA 100",colorOrange);


/* Plot Buy and Sell Signal Arrows */
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);
PlotShapes(IIf(Sell, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);
2 Likes

Thank you for the prompt response, my query is resolved. i have made some small changes in the final solution code , removed daily ema plots as we trust the code now, and changed buy/sell and short/cover rules as we are using the larger time frame condition , we need to add some more lines to back test the code but i have not reached to that complexity in my coding journey. cheers!!

Plot( C, _DEFAULT_NAME(), IIf( C > Ref(C, -1), colorBlue, IIf(C < Ref(C, -1), colorRed, colorGrey40)), styleBar | styleThick );

_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
SetChartOptions(0,chartShowArrows|chartShowDates);  //Enable X-Axis (Date/Time Axis)


Plot(EMA(Close,20),"EMA-20",colorGreen , styleLine | styleThick); //Plot Historical Short EMA lines
Plot(EMA(Close,50),"EMA-50",colorBlue , styleLine | styleThick); //Plot Historical Long EMA Lines


TimeFrameSet(inDaily); //move to higher timeframe 
ema13 = EMA(C,13);
ema52 = EMA(C,52);
ema100 =EMA(C,100);   //Extract data from higher timeframe
TimeFrameRestore(); //get back to old timeframe

ema13d = TimeFrameExpand(ema13,inDaily); //data expand to match lower timframe dataset
ema52d = TimeFrameExpand(ema52,inDaily);
ema100d = TimeFrameExpand(ema100,inDaily);

//Plot(ema13d, "EMA 13",colorRed);  //daily  timeframe
//Plot(ema52d, "EMA 52",colorYellow);
//Plot(ema100d, "EMA 100",colorOrange);

//Trading Logic
Buy = Cross(EMA(Close,20),EMA(Close,50)) AND ema13d > ema52d AND ema52d > ema100d; //Positive EMA Crossover (True/False) 
Sell = Cross(EMA(Close,50),EMA(Close,20)); //Negative EMA Crossover (True/False)


Short = Cross(EMA(Close,50),EMA(Close,20)) AND ema13d < ema52d AND ema52d < ema100d;
Cover = Cross(EMA(Close,20),EMA(Close,50));

Buy = ExRem(Buy, Sell);
Sell = ExRem(Sell, Buy);

Short = ExRem(Short,Cover);
Cover = ExRem(Cover,Short);


/* Plot Buy and Sell Signal Arrows */
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45);

PlotShapes(IIf(Sell, shapeStar, shapeNone),colorGold, 0, L, Offset=-15);

PlotShapes(IIf(Short, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);                      
PlotShapes(IIf(Short, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);

PlotShapes(IIf(Cover, shapeStar, shapeNone),colorGold, 0,L, Offset=-15);