I’m currently running into an issue with my strategy that works well in real-time but causes problems during backtesting in the Strategy Analyzer. Here’s the situation:
Problem Overview:
- My strategy involves placing trades with two profit targets, stop-loss adjustments, and breakeven logic. In live trading, I manage these using OnExecutionUpdate to dynamically handle stop-loss adjustments, partial exits, and moving stops to breakeven + pips.
- I’ve used the State.Realtime condition in parts of my code to handle execution and position management for live trades.
When I remove the State.Realtime condition to enable backtesting, the Strategy Analyzer runs into issues where:
- Partial trades or incorrect position sizes appear in historical data.
- Breakeven adjustments, stop-loss movements, and profit target hits are not simulated properly since these are handled in real-time with dynamic logic.
- Accurate Backtesting: I need my backtests to simulate the behavior of live trading as closely as possible, including proper handling of stop-loss, profit targets, and breakeven logic without relying on real-time execution conditions.
- Keep Real-Time Logic: I want to retain the real-time execution logic for live trading, but I’m not sure how to properly isolate it without affecting backtesting results.
- How can I simulate my real-time order management logic (breakeven, stop-loss adjustments, etc.) accurately during backtesting, given that I rely on OnExecutionUpdate and dynamic position management in live trading?
Any advice on structuring the strategy to accommodate both backtesting and live trading modes without losing accuracy would be greatly appreciated!

Comment