To: Qone0 who wrote (84658 ) 2/25/2024 2:18:23 PM From: Sun Tzu 1 RecommendationRecommended By ajtj99
Respond to of 98150 Let's talk some more about backtesting. The reason that backtest engines rightly treat the trade as an event that gets executed on the next bar is to prevent the effect of future data creeping into the test. This is harder than people normally expect. For example, suppose that a strategy says buy a stock any time it moves above its 13 day moving average and sell it if it moves below it. When they look at the chart they see it works well, but when you actually want to trade it the price fluctuates above and below that 13DMA. Worse, the moving average itself is also changing depending on what the stock is doing. Backtest engines solve this by entering the order the next day. As you said, the trade is an event and that event has not completed yet. But there is a better, though more complex way, to do it. You can limit your exposure to only non-repainting data. That is data which does not change during the current bar. Examples are the open or the value of the MA as of the close yesterday. The above trade strategy then changes to something like, if open is below yesterday's moving average sell the stock with a stoploss of X. Here then backtest engine should have an easier job handling it. The open price does not change during the day, neither does the previous day's MA. The engine should register the sell at the open or the midway between VWAP of the bar and the open, whichever is lower (this allows for the case where a lot of selling prevents you from grabbing the open price). Then if the high/low of the day is above/below your stoploss, it should register a loss for you equal to the difference between your stoploss the VWAP of the day, again whichever is worse for you. Otherwise, the trade should stand. In my case I test it even more deeply. If the day has conditions that meet my trade criteria, for example the low of the day is below the moving average by some margin that would trigger a sell (or buy). Rather than issuing a buy/sell order to the backtest engine on the daily bar (which would be wrong because we don't what and when the low of the day happens), I trigger a trade routine that operates on intraday data and is limited to only past data. That routine then executes trades during that day according to its logic. So when I issue a buy/sell order to the backtest engine at some price, I really want that order to register at the price I gave it. I don't want it to wait for the next day's bar. At most it should wait for the next intraday bar, not the next day's. I think the problem I am encountering is because their engine can't distinguish between the multiple timeframes coming out of the same script and executes on the daily bar. I need to find a way around this.