Enter trade x days after signal

Hi - I want to be able to enter a trade day after a signal (in this case a 3x MA crossover) has triggered AND only if the next days high is higher than the previous day. the code I am using doesn't generate a any buy signals. Thee cross over code works and will generate signals but it looks like there is something wrong with my code entering the next day when the hihg is higher than previous day.

buytriggered=  EMA(P1, Periods1) > EMA(P2, Periods2) AND EMA(P1, Periods1) > EMA(P3, Periods3);
BS= BarsSince(buytriggered);
Buy= BS==1 AND H > Ref(High,-1);

You need to post full relevant code details for others to be able to help you.

What have you assigned to: P1, Periods1, P2, Periods2, P3 and Periods3?

I agree with TrendSurfer.

Anyway, did you try an impulse ?

Buy = BS == 1 AND Cross( C, Ref( H, -1) );

Hi,

Code below including P1, Periods1, P2, Periods2, P3 and Periods3

P1 = ParamField("Price field",-1);
Periods1 = Param("Periods1", 5, 2, 300, 1, 10 );

P2 = ParamField("Price field",-1);
Periods2 = Param("Periods2", 21, 2, 300, 1, 10 );

P3 = ParamField("Price field",-1);
Periods3 = Param("Periods3", 55, 2, 300, 1, 10 );

buytriggered= H==FastRes AND EMA(P1, Periods1) > EMA(P2, Periods2) AND EMA(P1, Periods1) > EMA(P3, Periods3);
BS= BarsSince(buytriggered);
Buy= BS==1 AND H > Ref(High,-1);

@easypipsy
Hope this help……

AfterDownCond=  (EMA(C, 5) < EMA(C, 21));
BS= BarsSince(AfterDownCond);
Buy= BS==1 AND H > Ref(High,-1);

What have you assigned to FastRes? Doesn't matter.

You need to understand more about 'BarsSince'. For example, make the following change to your code to see what I mean.

Replace:

BS= BarsSince(buytriggered);

With:

BS = BarsSince(Not buytriggered);

You don't need any barssince to delay entry.
Simply use Ref() for entry delay.

P1 = ParamField("Price field",-1);
Periods1 = Param("Periods1", 5, 2, 300, 1, 10 );

P2 = ParamField("Price field",-1);
Periods2 = Param("Periods2", 21, 2, 300, 1, 10 );

P3 = ParamField("Price field",-1);
Periods3 = Param("Periods3", 55, 2, 300, 1, 10 );

BuyPrice = Open;

buytriggered = EMA(P1, Periods1) > EMA(P2, Periods2) AND EMA(P1, Periods1) > EMA(P3, Periods3);
Buy = Ref(buytriggered, -1) AND H > Ref(High,-1);// No BarSince required!
Buy = Ref(Buy, -1);// Adding bar delay if entering next day/bar at Open!

Besides if using code in analysis backtester there is SetTradeDelays() function:

P1 = ParamField("Price field",-1);
Periods1 = Param("Periods1", 5, 2, 300, 1, 10 );

P2 = ParamField("Price field",-1);
Periods2 = Param("Periods2", 21, 2, 300, 1, 10 );

P3 = ParamField("Price field",-1);
Periods3 = Param("Periods3", 55, 2, 300, 1, 10 );

SetTradeDelays(1, 1, 1, 1);// Adding bar delay if entering next day/bar at Open!

BuyPrice = Open;

buytriggered = EMA(P1, Periods1) > EMA(P2, Periods2) AND EMA(P1, Periods1) > EMA(P3, Periods3);
Buy = Ref(buytriggered, -1) AND H > Ref(High,-1);// No BarSince required!

And some code using some Sell rule in addition and if not using Cross() function

P1 = ParamField("Price field",-1);
Periods1 = Param("Periods1", 5, 2, 300, 1, 10 );

P2 = ParamField("Price field",-1);
Periods2 = Param("Periods2", 21, 2, 300, 1, 10 );

P3 = ParamField("Price field",-1);
Periods3 = Param("Periods3", 55, 2, 300, 1, 10 );

BuyPrice = SellPrice = Open;

buytriggered = EMA(P1, Periods1) > EMA(P2, Periods2) AND EMA(P1, Periods1) > EMA(P3, Periods3);
Buy = Ref(buytriggered, -1) AND H > Ref(High,-1);// No BarSince required!
Buy = Ref(Buy, -1);// Adding bar delay if entering next day/bar at Open!

Sell = EMA(P1, Periods1) < EMA(P2, Periods2);
Sell = Ref(Sell, -1);// Adding bar delay if exiting next day/bar at Open!

// remove excessive signals
Buy = ExRem( Buy, Sell );
Sell = ExRem( Sell, Buy );

Plot( C, "Price", colorDefault, styleCandle );
PlotShapes( Buy * shapeUpArrow, colorGreen, layer = 0, y = L, dist=-12 );
PlotShapes( Sell * shapeDownArrow, colorRed, layer = 0, y = H, dist=-12 );
2 Likes

OP didn't clarify where the code is being used which made me realize the above posts before @fxshrat all assumed a certain context.

If it is a chart, then using LastValue() is very important in many such calculations otherwise the values will returned will keep changing based on the bar selector. Ofc if you're checking the most recent event.

@easypipsy TRACEF() the BS variable and you'll see what I mean, until then you'll keep wondering why its not working.