I am modeling a stock using 60-min bars along with buy limit and sell limit prices. Given there are many instances of price lows hitting my buy limit prices and price highs hitting my sell limit prices that never actually fill in real trading, my goal is to eliminate any such simulated backtesting trades whereby long entries never occur exactly at a bar's low price and long exits never occur exactly at the bar's high price.
As such, I simply added a condition that checks to see if the next bar's low occurs exactly at the model's buy limit price if currently flat (if true, no long position is taken in the simulation). I performed something similar with long exits using high prices, though I will focus just on the long entry side.
My code for the conditional backtested market entry is provided below.
Oddly, simulated long entry (exit) trades are still taken exactly a bar's low (high) price in the event the 2 prices coincide on the same bar. It's important to note that many trades which were meant to be skipped were skipped, while others were not.
When I change "<" to "==" in the code below, it appears to eliminate the trades that should have been eliminated when using "<." It also appears to reinstate those that were eliminated correctly using the "<" sign.
It seems simple enough, though my coding error escapes me. My guess is it has something to do with rounding.
Prices are rounded to 2 decimals in Preferences.
CODE:
Please log in to see this code.