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

Error Using - SystemPerformance.AllTrades.Count - 1

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

    Error Using - SystemPerformance.AllTrades.Count - 1

    When running my strategy with live data I am getting an error after an order closes that the code is trying to access an index with an invalid range at the OnOrderUpdate.

    This is the code within OnOrderUpdate
    Code:
    if (order.Name == "StopExitLongFloor3b1" && orderState == OrderState.Filled || order.Name == "ExitProfitLongEntryFloor3b1" && orderState == OrderState.Filled
    || order.Name == "StopExitLongFloor3b1a" && orderState == OrderState.Filled || order.Name == "StopExitLongFloor3b1b" && orderState == OrderState.Filled
    )
    {
    Floor3bActive = false;
    LongFloor3bEntry = null;
    LongFloor3bEntryActive = false;
    stopOrderLongEntryFloor3b1a = null;
    stopOrderLongBreakOutFloor3b = null;
    stopOrderLongEntryFloor3b1 = null;
    entryLongFloor3b1 = null;
    Floor3bLossCount += SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count - 1].ProfitCurrency;
    }
    This is a MTF strategy.

    I read here that there was a problem with the AllTrades.Count when apply the strategy from the strategy tab. I am doing the same. Does this issue still exist?
    https://ninjatrader.com/support/foru...h-live-trading

    Is there a better way to pull last trades profit/loss?

    Thanks
    Last edited by mlprice12; 07-27-2021, 02:50 PM.

    #2
    Hello mlprice12,

    The SystemPerformance is updated after the position update.

    If this is moved to OnPositionUpdate() and you first confirm that SystemPerformance.AllTrades.Count is greater than 1, and then print SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count - 1].ProfitCurrency is there an indexing error?

    When adding a strategy directly to the Strategies tab of the Control Center instead of to a chart, set IncludeTradeHistoryInBacktest to true.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thanks for the response Chelsea,

      I have 4 separate entries so I need to distinguish the order name to make sure I can separate trade performance for each order name. ie lossCount1, lossCount2, lossCount3, and lossCount4.
      Would this still be possible in OnPositionUpdate? My understanding is I wouldn't be able to distinguish from which entry the position update is referencing.

      Now that I enabled the strategy from the chart the TradeCount is printing correctly.
      I also moved the code logic to OnExecutionUpdate. This seems to be working.

      Comment


        #4
        Hello mlprice12,

        Yes, each trade in the performance collection will have an Entry and Exit.

        if (SystemPerformance.AllTrades.Count > 0)
        Print(SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count - 1].Entry.Name);


        Chelsea B.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by GussJ, 03-04-2020, 03:11 PM
        11 responses
        3,227 views
        0 likes
        Last Post xiinteractive  
        Started by andrewtrades, Today, 04:57 PM
        1 response
        13 views
        0 likes
        Last Post NinjaTrader_Manfred  
        Started by chbruno, Today, 04:10 PM
        0 responses
        7 views
        0 likes
        Last Post chbruno
        by chbruno
         
        Started by josh18955, 03-25-2023, 11:16 AM
        6 responses
        440 views
        0 likes
        Last Post Delerium  
        Started by FAQtrader, Today, 03:35 PM
        0 responses
        12 views
        0 likes
        Last Post FAQtrader  
        Working...
        X