I am trying to capture the current candle's High and Low and compare that with current day's open price.
My buy condition : If Open = Low and current High crosses Previous days High or Previous week's high then Buy.
Pasting the code below, which is giving me completely wrong backtested result. Any suggestion to fix this would be greatly appreciated.
NewDay = Day()!= Ref(Day(), -1) OR BarIndex() == 0;
Open_price = SelectedValue(TimeFrameGetPrice("O", inDaily,0));
LAST_HIGH = SelectedValue(TimeFrameGetPrice("H", inDaily, -1));
Week_High = SelectedValue(TimeFrameGetPrice("H", inWeekly,-1));
Open_LOW = IIf(Open_price==TODAY_L, True, False);
Open_HIGH = IIf(Open_price==TODAY_H, True, False);
Buy = (Open_LOW AND H>LAST_HIGH) OR (Open_LOW AND H>Week_High);
BuyPrice = ValueWhen(Buy,C);
Backtest using daily periodicity.
cond1 = O==L;
cond2 = Cross(H,Ref(H,-1));
pwh = H; // previous week's high
cond3 = Cross(H,TimeFrameExpand(pwh,inweekly));
Buy = cond1 and (cond2 OR cond3);
@C_M thanks for your quick reply, I am looking for a 15 min periodicity, and challenge I am facing while checking the Open price is low till that candle.
I could fix this with the below changes in my first few lines.
Open_price = Valuewhen(NewDay, TimeFrameGetPrice("O", inDaily,0));
LAST_HIGH = Valuewhen(Newday, TimeFrameGetPrice("H", inDaily, -1));
Week_High = Valuewhen(Newday, TimeFrameGetPrice("H", inWeekly,-1));
Thank you all...
Why don't you just use
Open_price = TimeFrameGetPrice( "O", inDaily, 0)); instead ?
Both arrays appear to be the same.
Thanks Clark, you are right this is simpler. Thanks for responding
Regardless of time frame, you can only run an accurate exploration when the bar is complete. The only exception is when you want to query the open price, which will remian fixed until the bar is complete. Use 'code check' to see if there's a look-ahead bias.
If you're using your exploration to trade and want to estimate the close and enter at or near close, that's ok in my book. But on a small timeframe like 15 minutes, you may need to add several ticks to your backtest Buyprice for a reasonable estimate of performance (one component of 'slippage'). The contract needs to be liquid at the time you want to trade it.
For really accurate backtesting on smaller time frames you probably need millisecond price and depth data.