Thanks! I am using IB and so all are supported at broker side. But I am just doing local backtesting/playback only now. I will verify the broker later.
But about my puzzle around "logical error". I do place my OCO inside a IF as you can see above (and another bigger "if within trading hours"). Does OCO not work all the time or just randomly inside a condition?
If you put a code inside a condition,
it does not matter if the code is a variable assignment or a stop loss,
that piece of code will only execute
IF the condition is met.
For a stop loss, it is a Global order, it is only in effect if there is an open position, there is really no reason to put it in a condition.
Hi TJ,
I know what a IF condition is. My IF condition above is not just about MP but other conditions too. I made it simple to use “BarNumber” for illustration. My original intention is to use the past pivot point at the point of the order to determine the stoploss price. As pivot points will move, I need to call it right after the order is placed, not at every current bar or the deduced SL price will got changed.
Based all your replies so far and my testing today, my guess is that:
1. In real-time mode, setstoploss() does trigger to broker immediately within the same bar/tick iteration, no matter you call it under a condition or not. The stoploss exit will be fulfilled by the broker.
2. In backtest/playback, the stop loss exit will be evaluated by a call to setstoploss() (as an internal emulator of the broker). So if I skip it at the bar where the stop loss should be fullfilled, no stop loss will be filled. That's why in your FAQ article you said "To avoid logical errors, DO NOT place these keywords inside a condition", to avoid this kind of situation.
Any comment? In fact the problem I am facing now is with backtesting.
And Happy New Year to you and your loved ones!!!