I am developing a strategy in NinjaTrader 8 that includes daily profit and loss limits (DailyProfit and DailyLoss). The goal is to stop only the specific instance of the strategy when it reaches its daily PnL limit, while allowing other running instances of the same strategy to continue trading independently.
The issue:
- When one instance of the strategy reaches its PnL limit, all instances stop trading, instead of only the one that hit the limit.
- I suspect the issue might be related to the use of SystemPerformance.AllTrades.TradesPerformance.Curr ency.CumProfit, as it might be considering all trades across the account, rather than just those of the individual strategy instance.
What I have tried:
- Using SystemPerformance.StrategyTrades.TradesPerformance .Currency.CumProfit, but I want to ensure this works across all versions of NinjaTrader 8 (both old and new).
- Implementing this.Disable(), but I need confirmation that it stops only the current strategy instance and does not impact others.
How can I modify my strategy to ensure that each instance tracks its own PnL separately and stops only when its own daily limit is hit, without affecting other running instances?
Below is my current code for pnl
// PnL
YesterdaysPNL[0] = YesterdaysPNL[1];
StrategyTotallPNL[0] = SystemPerformance.AllTrades.TradesPerformance.Curr ency.CumProfit;
if (CurrentBars[0] < 1)
return;
//
if (Bars.IsFirstBarOfSession)
{
YesterdaysPNL[0] = SystemPerformance.AllTrades.TradesPerformance.Curr ency.CumProfit;
Print(@"*** First Bar of Session ***");
Draw.Text(this, @"DailyPNLexample Text_1 " + Convert.ToString(CurrentBars[0]),
@"TotalRealizedPNL= " + Convert.ToString(SystemPerformance.AllTrades.Trade sPerformance.Currency.CumProfit),
0, (Low[0] + (-9 * TickSize)));
Draw.Text(this, @"DailyPNLexample Text_2 " + Convert.ToString(CurrentBars[0]),
@"Prior SessionPNL = " + Convert.ToString(DailyPNL),
0, (Low[0] + (-5 * TickSize)));
DailyPNL = 0;
}
else
{
// PnL
DailyPNL = (StrategyTotallPNL[0] - (YesterdaysPNL[0]));
}
// DailyLoss и DailyProfit
if (DailyPNL <= DailyLoss || DailyPNL >= DailyProfit)
{
Print($"Strategy stopped for this instance. Account: {Account.Name}, DailyPNL: {DailyPNL}, DailyLoss: {DailyLoss}, DailyProfit: {DailyProfit}");
return; //
}
//
Print(Convert.ToString(Times[0][0]) + @" TotalPNL: " + Convert.ToString(StrategyTotallPNL[0]) +
@" YestredaysPNL: " + Convert.ToString(YesterdaysPNL[0]) +
@" DailyPNL: " + Convert.ToString(DailyPNL));
Any guidance or best practices would be greatly appreciated! Thank you.
Comment