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 SalmaTrader, 07-07-2026, 10:26 PM
    0 responses
    46 views
    0 likes
    Last Post SalmaTrader  
    Started by CarlTrading, 07-05-2026, 01:16 PM
    0 responses
    22 views
    0 likes
    Last Post CarlTrading  
    Started by CaptainJack, 06-17-2026, 10:32 AM
    0 responses
    14 views
    0 likes
    Last Post CaptainJack  
    Started by kinfxhk, 06-17-2026, 04:15 AM
    0 responses
    20 views
    0 likes
    Last Post kinfxhk
    by kinfxhk
     
    Started by kinfxhk, 06-17-2026, 04:06 AM
    0 responses
    22 views
    0 likes
    Last Post kinfxhk
    by kinfxhk
     
    Working...
    X