Also, the strategy uses "NumContracts" to set the default number of contracts to trade.
Here is an example:
if (Bars.IsFirstBarOfSession) { PriorWinningTrades = SystemPerformance.AllTrades.WinningTrades.Count; } CurrentWins = SystemPerformance.AllTrades.WinningTrades.Count - PriorWinningTrades; // Stop trading if MaxWinners reached if (CurrentWins >= MaxWinners) { Print(Time[0] + " Trading Stopped... Max Winners Reached: " + CurrentWins); return; } // Entry logic if (Close[0] < Close[1]) { EnterShortLimit(NumContracts, Close[0], @"Short"); }
In backtesting and optimization, everything works as intended and can be verified in the NinjaScript Output window. For example, when NumContracts is set to "4" and MaxWinners is set to "2" and the strategy executes two separate winning trades of 4 contracts each, the strategy will not place any more trades, and outputs:
"4/12/2023 2:29:10 PM Trading Stopped... Max Winners Reached: 2"
However, in LIVE trading using the same parameters, one winning trade made with 4 contracts sets "CurrentWins" to 4, instead of 1 like in backtesting. Live excution interprets one 4 contract winning trade as 4 separate winning trades instead of a single winning trade, which is my intention. This messes up my strategy in live trading since it will stop trading after one winning trade since CurrentWins=4 is greater than MaxWinners=2. Here is the output in LIVE trading after one single winning trade of 4 contracts:
"7/17/2023 11:35:30 AM Trading Stopped... Max Winners Reached: 4"
What can I do to have "CurrentWins" behave the same in live trading and backtesting?
EDIT: I have run this through Market Replay and discovered that sometimes a single trade of 4 contracts will result in a different number of trades, such as 2 contracts + 2 contracts, 3 contacts + 1 contract, etc.
Comment