Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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 RaddiFX, Today, 10:15 AM
    2 responses
    14 views
    0 likes
    Last Post RaddiFX
    by RaddiFX
     
    Started by patrickmlee007, Today, 09:33 AM
    2 responses
    17 views
    0 likes
    Last Post patrickmlee007  
    Started by magnatauren, 08-15-2020, 02:12 PM
    5 responses
    206 views
    0 likes
    Last Post RaddiFX
    by RaddiFX
     
    Started by rene69851, 05-02-2024, 03:25 PM
    1 response
    22 views
    0 likes
    Last Post rene69851  
    Started by ETFVoyageur, Yesterday, 07:05 PM
    5 responses
    46 views
    0 likes
    Last Post ETFVoyageur  
    Working...
    X