Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Number of consecutive losses of a strategy

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Number of consecutive losses of a strategy

    Hi,

    I try to find out the number of consecutive losses that a strategy had. Currently I use the code below, which works fine in backtests when there is just one strategy running at a time. But on a live system that runs several strategies, it returns the number of consecutive lossses of all strategies and not just of the strategy were I execute this code.

    How can I select the trades by strategy (e.g. strategy name or SignalName), so that I get a losscount for that strategy only?

    Ana help will be greatly appreciated.

    Thanks in advance,

    Till

    Code:
    int losses = 0;
    if (Performance.AllTrades.Count > 0)
    {
    	for(int n = 1; n <= Performance.AllTrades.Count; n++) {
    		// Get the last completed trade (at index 0)
            Trade lastTrade = Performance.AllTrades[Performance.AllTrades.Count - n];
     
            // Calculate the PnL for the last completed real-time trade
            double lastProfit = lastTrade.ProfitCurrency * lastTrade.Quantity;
    		if(lastProfit < 0) losses++;
    		if(lastProfit > 0) break;
    	}
    }

    #2
    Hello tadorna,

    Thank you for your post and welcome to the NinjaTrader Support Forum!

    The Performance.AllTrades.Count is specific to the strategy, what you are seeing is the historical trades that would have occurred historically being counted in the Performance.AllTrades.Count for the strategy. To avoid this add the following line to the start of the OnBarUpdate() method:
    Code:
    if(Historical)
    return;
    Keep in mind this will need to be removed for backtesting as backtesting is done on historical data. For information on Historical please visit the following link: http://www.ninjatrader.com/support/h...historical.htm

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
    0 responses
    590 views
    0 likes
    Last Post Geovanny Suaza  
    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
    0 responses
    342 views
    1 like
    Last Post Geovanny Suaza  
    Started by Mindset, 02-09-2026, 11:44 AM
    0 responses
    103 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
    0 responses
    555 views
    1 like
    Last Post Geovanny Suaza  
    Started by RFrosty, 01-28-2026, 06:49 PM
    0 responses
    552 views
    1 like
    Last Post RFrosty
    by RFrosty
     
    Working...
    X