Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Accessing Order Details in Backtesting

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

    Accessing Order Details in Backtesting

    Hi,
    I'm trying to print Order details of a strategy in backtesting. But the following code is recognizing only those strategies that have been enabled but not the strategy that I'm backtesting. Could you help me understand what I'm missing?

    Code:
    ...
            else if (State == State.DataLoaded)
            {    
            ...
                allAccts = Account.All;
                foreach (Account acct in allAccts)
                {
                    if (acct.Name == "Sim101") 
                    {                        
                        strategyAcct = acct;
                    }
                }
    
                foreach (StrategyBase s in strategyAcct.Strategies)
                {
    [B] if (s.Name == "Workbench")[/B]
                    {
                        thisStrategy = s;
                        Print(thisStrategy.Name);
                    }
                }        
            }
    
            protected override void OnBarUpdate()
            {    
            ...        
                if (strategyAcct != null)
                {                
                    if (thisStrategy != null)
                    {
                        Collection<Order> orders = thisStrategy.Orders;
                        Position position = thisStrategy.Position;
    
                        foreach (Order order in orders) 
                        {
                           if (order.OrderType == OrderType.Limit && position != null)     
                           {         
                               Print(Time[0] + "\t" + order.FromEntrySignal + "\t" + order.Instrument + "\t" + order.LimitPrice);
                            } 
                        }
                    }
                }    
            }
    Thanks,
    CV

    #2
    Hi chaitunt, thanks for your post.

    The Account.Strategies collection will only include enabled strategies on the account. It's not looking at the backtest account. We have a variable called "IsInStrategyAnalyzer" where you can filter code to only run if the strategy is instantiated in the strategy analyzer. If the code is being run in the strategy analyzer, you can use the Strategie's Account object:
    https://ninjatrader.com/support/help...ount_class.htm

    The OnOrderUpdate method can also be used to keep track of order events:


    Please let me know if I can assist any further.
    Last edited by NinjaTrader_ChrisL; 03-31-2020, 09:10 AM.

    Comment


      #3
      Chris:
      Thanks for the reply. How do I access the strategy object that is being backtested? I have the following code in a strategy called Test and it's only printing "In Strategy Analyzer..." but nothing else. I have tried the OnAccountItemUpdate method as well without success. I'm basically trying to backtest some trade management techniques and improve my strategy but am stuck here. Thanks for your help.

      Code:
         if (IsInStrategyAnalyzer)
         {
          Print("In Strategy Analyzer...");
          foreach (StrategyBase s in StrategyBase.All)
          {    
           if (s.Name == "Test")
           {      
            Print("Strategy name: " + s.Name + " Account name: " + s.Account.Name);
            realizedPnL = s.Account.Get(AccountItem.RealizedProfitLoss, Currency.UsDollar);
            unRealizedPnL = s.Account.Get(AccountItem.UnrealizedProfitLoss, Currency.UsDollar);
            totalPnL = realizedPnL + unRealizedPnL;
      
            Print(string.Format("Strategy: {0}, Account: {1}, Realized PnL: {2}, Unrealized PnL: {3}, Total PnL: {4}", s.Name, s.Account.Name, realizedPnL, unRealizedPnL, totalPnL));      
           }
          }
         }
      I have also tried to access the Orders object from the Backtest account but it's not getting anything. It does print the account name (represented by
      strategyAcct in the code snippet below)
      as Backtest.

      Code:
          Print("In Strategy Analyzer..." + strategyAcct.Name);
          foreach (Order order in strategyAcct.Orders)
            {
               Print(String.Format("Order placed: {0}, {1}, {2}, {3}, {4}", order.Name, order.OrderAction, order.OrderType, order.Quantity, order.AverageFillPrice));
            }
      Thanks,
      CV
      Last edited by chaitunt; 03-31-2020, 01:26 PM.

      Comment


        #4
        Hello chaitunt,

        Unfortunately, as scripts that have run in the Strategy Analyzer do not get enabled on an account, these are not accessible from an Addon.

        Any information you want from the strategy would need to come from the strategy.

        You could look into pushing information into static collection in an external namespace.
        Chelsea B.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by argusthome, 03-08-2026, 10:06 AM
        0 responses
        90 views
        0 likes
        Last Post argusthome  
        Started by NabilKhattabi, 03-06-2026, 11:18 AM
        0 responses
        48 views
        0 likes
        Last Post NabilKhattabi  
        Started by Deep42, 03-06-2026, 12:28 AM
        0 responses
        31 views
        0 likes
        Last Post Deep42
        by Deep42
         
        Started by TheRealMorford, 03-05-2026, 06:15 PM
        0 responses
        34 views
        0 likes
        Last Post TheRealMorford  
        Started by Mindset, 02-28-2026, 06:16 AM
        0 responses
        69 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Working...
        X