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

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.
    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.
        Chelsea B.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by GLFX005, Today, 03:23 AM
        0 responses
        1 view
        0 likes
        Last Post GLFX005
        by GLFX005
         
        Started by XXtrader, Yesterday, 11:30 PM
        2 responses
        11 views
        0 likes
        Last Post XXtrader  
        Started by Waxavi, Today, 02:10 AM
        0 responses
        6 views
        0 likes
        Last Post Waxavi
        by Waxavi
         
        Started by TradeForge, Today, 02:09 AM
        0 responses
        14 views
        0 likes
        Last Post TradeForge  
        Started by Waxavi, Today, 02:00 AM
        0 responses
        3 views
        0 likes
        Last Post Waxavi
        by Waxavi
         
        Working...
        X