• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

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)
                {
     if (s.Name == "Workbench")
                    {
                        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:
    https://ninjatrader.com/support/help...rderupdate.htm

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

    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.
        https://ninjatrader.com/support/foru...245#post712245
        Chelsea B.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Brillo, Today, 12:40 PM
        0 responses
        2 views
        0 likes
        Last Post Brillo
        by Brillo
         
        Started by authenticx, 05-29-2020, 03:51 PM
        3 responses
        60 views
        0 likes
        Last Post NinjaTrader_ChrisL  
        Started by nikolaalx, Today, 07:31 AM
        10 responses
        36 views
        0 likes
        Last Post bltdavid  
        Started by HiddenPhilosopher, Today, 09:28 AM
        0 responses
        11 views
        0 likes
        Last Post HiddenPhilosopher  
        Started by tipslark, Today, 07:23 AM
        0 responses
        24 views
        0 likes
        Last Post tipslark  
        Working...
        X