Well, the snippet above is an example of "peeking". The logic says, "if I sell at my stopPrice on the next bar, buy that bar on the open". That's not possible to do in real life, and likely inflated your backtest results.
There are 3 ways I can think of to do the stop and reverse, depending on what you're really trying to achieve.
1. Use a BuyAtStop order if you really want to execute "at market" when the stopPrice trigger is
touched. The code -
CODE:
Please log in to see this code.
2. Another way is to operate on a closing value and use market order for both -
CODE:
Please log in to see this code.
3. Finally, you could execute the touch stop, but enter at market on the following bar if the Close exceeds the stop price. The logic for this would have to be more integrated with the script.
The problem with the first two methods is that I'm not sure if the Fidelity backend will allow simultaneous Cover and Buy orders. I think stop and reverse is supported, but maybe their telephone trading support can answer this question.