Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

"Error calling onBarUpdate" problem

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

    "Error calling onBarUpdate" problem

    On a strategy with one additional instrument, I consistently receive this error in the log, although the strat compiles correctly:

    Error on calling "onBarUpdate" You must use the overload that has a "BarsInProgress" parameter.

    I am following the approach listed in this help page:



    However there is no BarsInProgress overload parameter for OnBarUpdate described.

    Am I doing something wrong in my strat?

    Thanks

    Chris

    #2
    Please provide the code you are currently using and then we can advise you accordingly.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Hi Josh

      Sure. It's shown below. There is a lot of commenting out as I'm testing it with various parameters.

      It's based on your SampleOnOrderUpdate example.

      Code:
      // 
      // Copyright (C) 2008, NinjaTrader LLC <www.ninjatrader.com>.
      // NinjaTrader reserves the right to modify or overwrite this NinjaScript component with each release.
      //
      
      #region Using declarations
      using System;
      using System.ComponentModel;
      using System.Diagnostics;
      using System.Drawing;
      using System.Drawing.Drawing2D;
      using System.Xml.Serialization;
      using NinjaTrader.Cbi;
      using NinjaTrader.Data;
      using NinjaTrader.Indicator;
      using NinjaTrader.Strategy;
      #endregion
      
      // This namespace holds all strategies and is required. Do not change it.
      namespace NinjaTrader.Strategy
      {
          /// <summary>
          /// Sample demonstrating the use of the OnOrderUpdate() method.
          /// </summary>
          [Description("Sample strategy demonstrating a use case involving the OnOrderUpdate() method")]
          public class multi_Strategy : Strategy
          {
              #region Variables
              private IOrder entryOrder     = null; // This variable holds an object representing our entry order
              private IOrder stopOrder     = null; // This variable holds an object representing our stop loss order
              private IOrder targetOrder     = null; // This variable holds an object representing our profit target order
              #endregion
      
              /// <summary>
              /// This method is used to configure the strategy and is called once before any strategy method is called.
              /// </summary>
              protected override void Initialize()
              {
                  //Add new instrument.  Barsinprogress == 1
                  Add("QQQQ", PeriodType.Minute,1);
                  CalculateOnBarClose = false;
                  TraceOrders = true;
                  ClearOutputWindow();
              }
              
              /// <summary>
              /// Called on each bar update event (incoming tick)
              /// </summary>
              protected override void OnBarUpdate()
              {
                  //Only process information for the primary instrument
                  if (BarsInProgress != 0)
                      {
                      return;
                      }    
                      
                  // Submit an entry limit order if we currently don't have an entry order open
                  //if (entryOrder == null && Close[0] > Open[0]) CDR 15/9/08
                  if (entryOrder == null )
                  {
                      /* The entryOrder object will take on a unique ID from our EnterLong()
                      that we can use later for order identification purposes in the OnOrderUpdate() method. */
                                      
                      //Enter long position
                      //if (EMA(Close,9)[1] > (EMA(Close, 34)[1] + 0.25) 
                      if ( Low[1] >= EMA(Close,9)[1] && Low[0] >= EMA(Close, 9)[0] 
                          && Rising(EMA(Close,9)) && Close[1] >=Open[1]
                          && Lows[1][1] < EMA(BarsArray[1],9)[1] && Lows[1][0] < EMA(BarsArray[1], 9)[0] 
                          //&& EMA(BarsArray[1],9)[0] < EMA(BarsArray[1],9)[1] & EMA(BarsArray[1],9)[1] < EMA(BarsArray[1],9)[2]
                          && Falling(EMA(BarsArray[1],20)))
                          //&& Close[1] >= ((High[1] - Low[1]) * 0.7 ) + Low[1])
                      {
                          //if (Open[0] >= Close[1] && Close[0] >= Open[0])
                          //{
                              entryOrder = EnterLongLimit(0,true,100, Close[0], "LongEntry");
                              Print("Long Position at " + Time[0]);
                          
                          //}        
                      }    
                      
                      //Enter Short position
                      if ( High[1] <= EMA(Close,9)[1] && High[0] <= EMA(Close, 9)[0] 
                          && Falling(EMA(Close,9)) && Close[1] <=Open[1]) 
                      /*
                      if (Close[1] <=Open[1] 
                          && Close[1] <= ((High[1] - Low[1]) * 0.2 ) + Low[1])
                      {
                          if (Open[0] <= Close[1] && Close[0] <= Open[0])
                      */    
                          {
                              entryOrder = EnterShortLimit(0,true,100, Close[0], "ShortEntry");
                              Print("Short Position at " + Time[0]);                        
                          }        
                      //}    
                  //    }    
                  }
                  
                  /* If we have a long position and the current price is in profit, raise the stop-loss order to breakeven.
                  */            
                  //Process long position
                  if (Positions[0].MarketPosition == MarketPosition.Long && Close[0] >= Position.AvgPrice + 0.30)
                  {
                      // Checks to see if our Stop Order has been submitted already
                      if (stopOrder != null && stopOrder.StopPrice < Position.AvgPrice)
                      {
                          // Modifies stop-loss to breakeven
                          stopOrder = ExitLongStop(0, true, stopOrder.Quantity, Position.AvgPrice - 0.50, "MyStop", "LongEntry");
                      }
                  }
                  
                  //Process short position
                  if (Positions[0].MarketPosition == MarketPosition.Short && Close[0] <= Position.AvgPrice - 0.30)
                  {
                      // Checks to see if our Stop Order has been submitted already
                      if (stopOrder != null && stopOrder.StopPrice > Position.AvgPrice)
                      {
                          // Modifies stop-loss to breakeven
                          stopOrder = ExitShortStop(0, true, stopOrder.Quantity, Position.AvgPrice + 0.50, "MyStop", "ShortEntry");
                      }
                  }
                  
                  // Do we need to close short position early as current price is higher than entry price?
                  if (Position.MarketPosition == MarketPosition.Long && Close[0] < Position.AvgPrice)
                  {
                      if ( BarsSinceEntry() > 1 & Open[0] < Close[1] )
                      {
                          ExitLong(0,stopOrder.Quantity,"EarlyStop","LongEntry");
                          Print("End Long Position Early " + Time[0]);
                      }        
                  }
      
                  // Do we need to close short position early as current price is higher than entry price?
                  if (Positions[0].MarketPosition == MarketPosition.Short && Close[0] > Position.AvgPrice)
                  {
                      if ( BarsSinceEntry() > 1 & Open[0] > Close[1] )
                      {
                          ExitShort(0,stopOrder.Quantity,"EarlyStop","ShortEntry");
                          Print("End Short Position Early " + Time[0]);
                      }        
                  }    
              
              }
      
              /// <summary>
              /// Called on each incoming order event
              /// </summary>
              protected override void OnOrderUpdate(IOrder order)
              {
                  // Handle entry orders here. The entryOrder object allows us to identify that the order that is calling the OnOrderUpdate() method is the entry order.
                  if (entryOrder != null && entryOrder.Token == order.Token)
                  {    
                      // Reset the entryOrder object to null if order was cancelled without any fill
                      if (order.OrderState == OrderState.Cancelled && order.Filled == 0)
                      {
                          entryOrder = null;
                      }
                  }
              }
              
              /// <summary>
              /// Called on each incoming execution
              /// </summary>
              protected override void OnExecution(IExecution execution)
              {
                  if (entryOrder != null && entryOrder.Token == execution.Order.Token)
                  {
                      if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
                      {
                          if (Positions[0].MarketPosition == MarketPosition.Long )
                          {
                          // Long Stop-Loss order
                          stopOrder     = ExitLongStop(0, true, execution.Order.Filled, execution.Order.AvgFillPrice - 1.00, "MyStop", "LongEntry");
                          
                          // Target order
                          targetOrder = ExitLongLimit(0, true, execution.Order.Filled, execution.Order.AvgFillPrice + 0.60, "MyTarget", "LongEntry");
                          }
      
                          if (Positions[0].MarketPosition == MarketPosition.Short )
                          {
                          // Short Stop-Loss order 
                          stopOrder     = ExitShortStop(0, true, execution.Order.Filled, execution.Order.AvgFillPrice + 1.00, "MyStop", "ShortEntry");
                          
                          // Target order
                          targetOrder = ExitShortLimit(0, true, execution.Order.Filled, execution.Order.AvgFillPrice - 0.60, "MyTarget", "ShortEntry");
                          }
      
                          
                          // Resets the entryOrder object to null after the order has been filled or partially filled
                          if (execution.Order.OrderState != OrderState.PartFilled)
                          {
                              entryOrder     = null;
                          }
                      }
                  }
                  
                  // Reset our stop order and target orders' IOrder objects after our position is closed.
                  if ((stopOrder != null && stopOrder.Token == execution.Order.Token) || (targetOrder != null && targetOrder.Token == execution.Order.Token))
                  {
                      if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled)
                      {
                          stopOrder = null;
                          targetOrder = null;
                      }
                  }
              }
      
              #region Properties
              #endregion
          }
      }
      Last edited by Operandi; 09-17-2008, 08:55 AM.

      Comment


        #4
        Can you tell me which line is the one triggering the error?

        From a brief scan, it may be the BarsSinceEntry() causing the error. Please see the multi-instrument syntax for it here: http://www.ninjatrader-support.com/H...inceEntry.html

        If this is not the line please inform me. Thanks.
        Last edited by NinjaTrader_JoshP; 09-17-2008, 08:55 AM.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          Hi Josh

          I don't know what line is triggering the error as the strat compiles OK. The only error message is that displayed in the logs, which is as per my first post in this thread.

          Thanks for helping

          Chris

          Comment


            #6
            Please try my suggested changes in my previous post. If that does not work then I suggest you use try-catch blocks to find the error. You can see a reference sample for try-catch blocks here: http://www.ninjatrader-support.com/v...ead.php?t=9825
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              Hi Josh

              I've just amended the BarsSinceEntry code, as you suggested, and you were right. It does work now.

              Many thanks for such fast and effective support (as usual)

              Chris

              For reference, the Multi instrument code for BarSinceEntry should be this:

              Code:
              // Do we need to close short position early as current price is higher than entry price?
                          if (Position.MarketPosition == MarketPosition.Long && Close[0] < Position.AvgPrice)
                          {
                              if ( BarsSinceEntry(0, "LongEntry", 0) > 1 & Open[0] < Close[1] )
                              {
                                  ExitLong(0,stopOrder.Quantity,"EarlyStop","LongEntry");
                                  Print("End Long Position Early " + Time[0]);
                              }        
                          }
              
                          // Do we need to close short position early as current price is higher than entry price?
                          if (Positions[0].MarketPosition == MarketPosition.Short && Close[0] > Position.AvgPrice)
                          {
                              if ( BarsSinceEntry(0,"ShortEntry",0) > 1 & Open[0] > Close[1] )
                              {
                                  ExitShort(0,stopOrder.Quantity,"EarlyStop","ShortEntry");
                                  Print("End Short Position Early " + Time[0]);
                              }        
                          }

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
              0 responses
              572 views
              0 likes
              Last Post Geovanny Suaza  
              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
              0 responses
              331 views
              1 like
              Last Post Geovanny Suaza  
              Started by Mindset, 02-09-2026, 11:44 AM
              0 responses
              101 views
              0 likes
              Last Post Mindset
              by Mindset
               
              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
              0 responses
              549 views
              1 like
              Last Post Geovanny Suaza  
              Started by RFrosty, 01-28-2026, 06:49 PM
              0 responses
              550 views
              1 like
              Last Post RFrosty
              by RFrosty
               
              Working...
              X