Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Enter long stop limit

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

    #16
    Hello speedytrade02,

    Thank you for your reply.

    The snippet you have provided does not show any of your order entry logic, so I wouldn't be able to tell you what to change or not change. In the first link I shared, if you download the SampleIntrabarBacktest script, there are comments throughout the strategy that explain what is happening. For example, in State.Configure the 1 tick series is added as follows:
    Code:
                else if(State == State.Configure)
                {
                    /* Add a secondary bar series.
                    Very Important: This secondary bar series needs to be smaller than the primary bar series.
    
                    Note: The primary bar series is whatever you choose for the strategy at startup. In this example I will
                    reference the primary as a 5min bars series. */
                    AddDataSeries(Data.BarsPeriodType.Tick, 1);​
    For this example, the primary series is BarsInProgress 0 and the 1 tick series is BarsInProgress 1. In your case, I see the following from your snippet:
    Code:
     else if (State == State.Configure)
    {
    Calculate = Calculate.OnBarClose;
    
    AddDataSeries("YM SEP23", Data.BarsPeriodType.Minute, 1, Data.MarketDataType.Last);
    AddDataSeries("YM SEP23", Data.BarsPeriodType.Second, 15, Data.MarketDataType.Last);
    AddDataSeries("YM SEP23", Data.BarsPeriodType.Tick, 1, Data.MarketDataType.Last);
    AddDataSeries("YM SEP23", Data.BarsPeriodType.Volume, 1, Data.MarketDataType.Last);
    }​
    Based on the order you have added the series, the primary series is BarsInProgress 0, 1 minute is BarsInProgress 1, 15 second is BarsInProgress 2, 1 tick is BarsInProgress 3, and 1 volume is BarsInProgress 4. This is important to keep in mind when you use order methods to submit orders. For example, if you wanted to EnterLong() on the 1 tick data series, you would use the following overload that allows you to define the BarsInProgressIndex:
    EnterLong(int barsInProgressIndex, int quantity, string signalName)

    Let's say you want to enter long on the 1 tick series, which is BarsInProgress 3, with a quantity of 5 and a signal name of myEnter. It would look like the following:
    Code:
    EnterLong(3, 5, "myEntry");
    All of the topics discussed on the following page may also be helpful in gaining a deeper understanding and should be kept in mind while developing your strategy:


    I am not able to answer your question "You thing that If I try my actual code on live trading, the orders should work nice?" This is subjective and up to you whether the orders are working nicely or not. I am not able to offer trading advice, and as far as the strategy running as expected without errors, you will need to test your strategy. I suggest testing your strategy in various ways, such as on the Playback Connection, running backtests in the Strategy Analyzer, and on live data using a simulated account. Once you are confident that your strategy is behaving as expected and submitting orders in the way you'd like it to in your tests, it is up to you whether you have the confidence to run it on a live account or not.

    Please let us know if we may be of further assistance.

    Comment


      #17
      Thanks for your response Emily, If updated my order execution code as you said, and it looks like this:


      Code:
      if(entrada == true){
      
          Print("Hola Short 2");
      
          if(lastOrder != null){
      
              if(lastOrder.OrderState ==  OrderState.Submitted){
                  CancelOrder(lastOrder);
              }
          }
      
          // generate a name for the signal with the number of the zone and the length of the position
          string signalName = "Short entry zone " + iAux;
          // We enter the position
          TP = takeProfitPrice;
          Order myOrder = EnterShortStopLimit(1, true, 1, entryPrice, stopPrice, signalName);
      
          Print("---------------------------------");
          Print(Time[0]);
          Print("Short2 ");
          Print("Entry Price --> " + entryPrice);
          Print("TP --> " + takeProfitPrice);
          Print("SL --> " + stopPrice);
          Print("---------------------------------");
      
          // save last position to zonas[i]
          lastOrder = myOrder;
          Print ("Ultima orden --> " + lastOrder);
      }​

      So this way I've indicated that the order should be placed on data serie 1 which is (AddDataSeries("YM SEP23", Data.BarsPeriodType.Minute, 1, Data.MarketDataType.Last)

      But I still getting the following error:

      Code:
      01/05/2023 16:12:00 Strategy 'test4/-1': Entered internal SubmitOrderManaged() method at 01/05/2023 16:12:00: BarsInProgress=1 Action=Buy OrderType=StopLimit Quantity=1 LimitPrice=34558 StopPrice=34538 SignalName='Long entry zone 1' FromEntrySignal=''
      
      Strategy 'test4/-1': An order has been ignored since the stop price ‘34538’ near the bar stamped ‘05/01/2023 16:12:00’ is invalid based on the price range of the bar. This is an invalid order and subsequent orders may also be ignored.
      
      01/05/2023 16:12:00 Strategy 'test4/-1': Ignored SubmitOrderManaged() method at 01/05/2023 16:12:00: BarsInProgress=1 Action=Buy OrderType=StopLimit Quantity=1 LimitPrice=34558 StopPrice=34538 SignalName='Long entry zone 1' FromEntrySignal='' Reason='Invalid order price, please see log tab'
      ​
      Let me know if I need you to provide any other part of the code

      Thanks again emily

      Comment


        #18
        Hello speedytrade02,

        Thanks for your reply.

        Are you still testing this in the Strategy Analyzer? Please provide a screenshot of your backtest settings so I may better assist you. If you need to scroll down and take additional screenshots to capture all of the backtest settings, that is alright.
        • To send a screenshot with Windows 10 or newer I would recommend using the Windows Snipping Tool.
        • Alternatively to send a screenshot press Alt + PRINT SCREEN to take a screenshot of the selected window. Then go to Start--> Accessories--> Paint, and press CTRL + V to paste the image. Lastly, save it as a jpeg file and send the file as an attachment.
        ​I look forward to your reply.

        Comment


          #19
          Hi Emily,

          Yes I'm still testing the strategy with Strategy Analyzer. Here you have the backtest settings that I'm using:


          Click image for larger version  Name:	image.png Views:	0 Size:	302.2 KB ID:	1269820

          On the other hand If i don't select the "Tick Replay" option, I get the following error:

          Code:
          01/05/2023 14:25:00 Strategy 'test4/-1': Entered internal SubmitOrderManaged() method at 01/05/2023 14:25:00: BarsInProgress=0 Action=SellShort OrderType=StopLimit Quantity=1 LimitPrice=34507 StopPrice=34511 SignalName='Short entry zone 0' FromEntrySignal=''
          
          01/05/2023 14:25:00 Strategy 'test4/-1': Ignored SubmitOrderManaged() method at 01/05/2023 14:25:00: BarsInProgress=0 Action=SellShort OrderType=StopLimit Quantity=1 LimitPrice=34507 StopPrice=34511 SignalName='Short entry zone 0' FromEntrySignal='' Reason='Invalid order price, please see log tab'
          ​
          Last edited by speedytrade02; 09-21-2023, 09:10 AM.

          Comment


            #20
            Hello speedytrade02,

            Thank you for your reply.

            Is your strategy a multi-instrument/multi-series script? If not, you could also prevent this error from happening by disabling Tick Replay and instead using a High order fill resolution of 1-tick. I addressed this error in my previous post Reports of this thread:
            Hi, I'm creating a strategy, and when I perform it manually I'm sending a "BuyStopLimit" or "SellStopLimit" order depending if I want to make a short or a long entry. How should I do it on ninjascript? I'm using the following code: string signalName = "Short entry zone " + i; // We enter the


            Here is a screenshot showing where you may change to High order fill resolution if you are using a single series script without Tick Replay:
            World's leading screen capture + recorder from Snagit + Screencast by Techsmith. Capture, edit and share professional-quality content seamlessly.


            Otherwise, if you use a single series script without Tick Replay and keep it on Standard resolution, it is more common for this error to occur.

            Please let me know if I may be of further assistance.

            Comment


              #21
              Hi Emily,

              Yes I'm using a multi-series script , and when I disable Tick Replay, and try to use High order fill resolution of 1-tick as you can see:

              Click image for larger version

Name:	image.png
Views:	169
Size:	59.7 KB
ID:	1269904



              I get the following error:

              Click image for larger version

Name:	image.png
Views:	159
Size:	28.0 KB
ID:	1269905


              Any other alternative that should I try?

              Thanks again

              Comment


                #22
                Hello speedytrade02,

                Multi series scripts cannot be used with high order fill resolution, you need to implement that on your own by adding a 1 tick series to your script and submitting orders to that BarsInProgress. You can find a sample of that concept here: https://ninjatrader.com/support/help...ipt_strate.htm

                Comment


                  #23
                  Thanks for the response Jesse.

                  Based on what I've seen on the code shown on the link you provided to me, I can see that I need to change "BarsInProgress == x" (where x is the desired dataSeries) in order to work with different dataSeries.

                  The only thing that I don't understand at all is how can I submit an order into an specific dataseries. For example I want to send the order (EnterLongStopLimit or EnterShortStopLimit) into 1m chart.

                  Here you have some parts of my code, so you can understand better what I've done.

                  Code:
                  protected override void OnStateChange()
                  {
                      if (State == State.SetDefaults)
                      {
                          Description                                 = @"Joan strategy";
                          Name                                        = "test4";
                          Calculate                                   = Calculate.OnBarClose;
                          EntriesPerDirection                         = 1;
                          EntryHandling                               = EntryHandling.AllEntries;
                          IsExitOnSessionCloseStrategy                = false;
                          ExitOnSessionCloseSeconds                   = 0;
                          IsFillLimitOnTouch                          = true;
                          MaximumBarsLookBack                         = MaximumBarsLookBack.Infinite;
                          OrderFillResolution                         = OrderFillResolution.High;
                          Slippage                                    = 0;
                          StartBehavior                               = StartBehavior.WaitUntilFlat;
                          TimeInForce                                 = TimeInForce.Day;
                          TraceOrders                                 = true;
                          RealtimeErrorHandling                       = RealtimeErrorHandling.StopCancelClose;
                          StopTargetHandling                          = StopTargetHandling.PerEntryExecution;
                          BarsRequiredToTrade                         = 1;
                          // Disable this property for performance gains in Strategy Analyzer optimizations
                          // See the Help Guide for additional information
                          IsInstantiatedOnEachOptimizationIteration   = true;
                      }
                      else if (State == State.Configure)
                      {
                          Calculate = Calculate.OnBarClose;
                  
                          AddDataSeries("YM SEP23", Data.BarsPeriodType.Minute, 1, Data.MarketDataType.Last);
                          AddDataSeries("YM SEP23", Data.BarsPeriodType.Second, 15, Data.MarketDataType.Last);
                          AddDataSeries("YM SEP23", Data.BarsPeriodType.Tick, 1, Data.MarketDataType.Last);
                          AddDataSeries("YM SEP23", Data.BarsPeriodType.Volume, 1, Data.MarketDataType.Last);
                      }
                      else if (State == State.DataLoaded)
                      {              
                          CumDelta1m = OrderFlowCumulativeDelta(BarsArray[1], CumulativeDeltaType.BidAsk, CumulativeDeltaPeriod.Bar, 0);
                          CumDelta15s = OrderFlowCumulativeDelta(BarsArray[2], CumulativeDeltaType.BidAsk, CumulativeDeltaPeriod.Bar, 0);
                      }
                  }
                  
                  protected override void OnBarUpdate()
                  {
                      if (BarsInProgress == 0 && CurrentBar > 0){
                          // strategy logic. I also access some 15s and 1m bars here
                          // Something Like this: Highs[2][0]
                          // And here I place the order
                          if(entrada == true){
                  
                              if(lastOrder != null){
                  
                                  if(lastOrder.OrderState ==  OrderState.Submitted){
                                      CancelOrder(lastOrder);
                                  }
                              }
                              // generate a name for the signal with the number of the zone and the length of the position
                              string signalName = "Long entry zone " + i;
                              // We enter the position
                              TP = takeProfitPrice;
                              Order myOrder = EnterLongStopLimit(1, entryPrice, stopPrice, signalName);
                  
                              // save last position to the variable
                              lastOrder = myOrder;
                              Print ("Ultima orden --> " + lastOrder);
                          }
                      }
                  }
                      ​
                  Thanks in advance!

                  Comment


                    #24
                    Hello speedytrade02,

                    Thank you for your reply.

                    I have mentioned this already in posts NinjaTrader 7 and News and Announcements of this thread.
                    Hi, I'm creating a strategy, and when I perform it manually I'm sending a "BuyStopLimit" or "SellStopLimit" order depending if I want to make a short or a long entry. How should I do it on ninjascript? I'm using the following code: string signalName = "Short entry zone " + i; // We enter the

                    Hi, I'm creating a strategy, and when I perform it manually I'm sending a "BuyStopLimit" or "SellStopLimit" order depending if I want to make a short or a long entry. How should I do it on ninjascript? I'm using the following code: string signalName = "Short entry zone " + i; // We enter the


                    Based on the order you have added the series, the primary series is BarsInProgress 0, 1 minute is BarsInProgress 1, 15 second is BarsInProgress 2, 1 tick is BarsInProgress 3, and 1 volume is BarsInProgress 4. This is important to keep in mind when you use order methods to submit orders. For example, if you wanted to EnterLong() on the 1 tick data series, you would use the following overload that allows you to define the BarsInProgressIndex:
                    EnterLong(int barsInProgressIndex, int quantity, string signalName)

                    Let's say you want to enter long on the 1 tick series, which is BarsInProgress 3, with a quantity of 5 and a signal name of myEnter. It would look like the following:
                    Code:EnterLong(3, 5, "myEntry");
                    All of the topics discussed on the following page may also be helpful in gaining a deeper understanding and should be kept in mind while developing your strategy:
                    https://ninjatrader.com/support/help...r_handling.htm
                    In your snippet I see the following:
                    Code:
                    Order myOrder = EnterLongStopLimit(1, entryPrice, stopPrice, signalName);
                    This uses the following overload for EnterLongStopLimit that does not include the barsInProgressIndex:
                    EnterLongStopLimit(int quantity, double limitPrice, double stopPrice, string signalName)

                    You need to modify this to use the following syntax:
                    EnterLongStopLimit(int barsInProgressIndex, bool isLiveUntilCancelled, int quantity, double limitPrice, double stopPrice, string signalName)

                    If you want to send the order to the 1-minute series, that would be barsInProgressIndex 1 because it is the first added data series. Here is an example that uses barsInProgressIndex 1 and sets isLiveUntilCancelled to true:
                    Code:
                    Order myOrder = EnterLongStopLimit(1, true, 1, entryPrice, stopPrice, signalName);
                    Please read the notes about live until cancelled orders and working with a multi-instrument strategy here for additional explanation and examples:


                    Thank you for your patience.

                    Comment


                      #25
                      Hi Emily, thanks for your response!

                      I've just tryied to change the syntax of the EnterLongStopLimit order as you said, and also I've changed BarsInProgress == 1 so I can work with the desired dataseries. But I still get the same error as the beginning.

                      01/05/2023 16:12:00 Strategy 'test4/-1': Entered internal SubmitOrderManaged() method at 01/05/2023 16:12:00: BarsInProgress=1 Action=Buy OrderType=StopLimit Quantity=1 LimitPrice=34885 StopPrice=34865 SignalName='Long entry zone 1' FromEntrySignal=''

                      Strategy 'test4/-1': An order has been ignored since the stop price ‘34865’ near the bar stamped ‘05/01/2023 16:12:00’ is invalid based on the price range of the bar. This is an invalid order and subsequent orders may also be ignored.

                      01/05/2023 16:12:00 Strategy 'test4/-1': Ignored SubmitOrderManaged() method at 01/05/2023 16:12:00: BarsInProgress=1 Action=Buy OrderType=StopLimit Quantity=1 LimitPrice=34885 StopPrice=34865 SignalName='Long entry zone 1' FromEntrySignal='' Reason='Invalid order price, please see log tab'
                      What could I be missing?

                      Code:
                      protected override void OnStateChange()
                      {
                          if (State == State.SetDefaults)
                          {
                              Description                                 = @"Joan strategy";
                              Name                                        = "test4";
                              Calculate                                   = Calculate.OnBarClose;
                              EntriesPerDirection                         = 1;
                              EntryHandling                               = EntryHandling.AllEntries;
                              IsExitOnSessionCloseStrategy                = false;
                              ExitOnSessionCloseSeconds                   = 0;
                              IsFillLimitOnTouch                          = true;
                              MaximumBarsLookBack                         = MaximumBarsLookBack.Infinite;
                              OrderFillResolution                         = OrderFillResolution.High;
                              Slippage                                    = 0;
                              StartBehavior                               = StartBehavior.WaitUntilFlat;
                              TimeInForce                                 = TimeInForce.Day;
                              TraceOrders                                 = true;
                              RealtimeErrorHandling                       = RealtimeErrorHandling.StopCancelClose;
                              StopTargetHandling                          = StopTargetHandling.PerEntryExecution;
                              BarsRequiredToTrade                         = 1;
                              // Disable this property for performance gains in Strategy Analyzer optimizations
                              // See the Help Guide for additional information
                              IsInstantiatedOnEachOptimizationIteration   = true;
                          }
                          else if (State == State.Configure)
                          {
                              Calculate = Calculate.OnBarClose;
                      
                              AddDataSeries("YM DEC23", Data.BarsPeriodType.Minute, 1, Data.MarketDataType.Last);
                              AddDataSeries("YM DEC23", Data.BarsPeriodType.Second, 15, Data.MarketDataType.Last);
                              AddDataSeries("YM DEC23", Data.BarsPeriodType.Tick, 1, Data.MarketDataType.Last);
                              AddDataSeries("YM DEC23", Data.BarsPeriodType.Volume, 1, Data.MarketDataType.Last);
                          }
                          else if (State == State.DataLoaded)
                          {              
                              CumDelta1m = OrderFlowCumulativeDelta(BarsArray[1], CumulativeDeltaType.BidAsk, CumulativeDeltaPeriod.Bar, 0);
                              CumDelta15s = OrderFlowCumulativeDelta(BarsArray[2], CumulativeDeltaType.BidAsk, CumulativeDeltaPeriod.Bar, 0);
                          }
                      }
                      protected override void OnBarUpdate()
                      {
                          try{
                      
                          // Check if current bar is 1m full candle
                              if (BarsInProgress == 1 && CurrentBar > 0){
                                  if(entrada == true){
                      
                                      Print("Hola Long 1");
                                      if(lastOrder != null){
                      
                                          if(lastOrder.OrderState ==  OrderState.Submitted){
                                              CancelOrder(lastOrder);
                                          }
                                      }
                                      // generate a name for the signal with the number of the zone and the length of the position
                                      string signalName = "Long entry zone " + i;
                                      // We enter the position
                                      TP = takeProfitPrice;
                                      //Order myOrder = EnterLongStopLimit(1, entryPrice, stopPrice, signalName);
                                      Order myOrder = EnterLongStopLimit(1, true, 1, entryPrice, stopPrice, signalName);
                                      // save last position to the variable
                                      lastOrder = myOrder;
                                      Print ("Ultima orden --> " + lastOrder);
                                  }
                              }
                          }
                      }​
                      Thanks again and sorry for asking so many questions.

                      Comment


                        #26
                        Hello speedytrade02,

                        Thank you for your reply.

                        I have already addressed the error message "An order has been ignored since the stop price ‘____’ near the bar stamped ‘____’ is invalid based on the price range of the bar." in post Reports of this thread. As this is occurring when submitting orders to the 1-minute data series, you may need to try submitting orders to a 1-tick data series instead. Based on your snippet, this would be BarsInProgress 3. Please try submitting orders to that data series (by changing the barsInProgressIndex from 1 to 3) to see if you are able to prevent the error message, which only occurs in historical processing.

                        Thank you for using NinjaTrader.

                        Comment


                          #27
                          Perfect Emily, thanks so much for your rapid response.

                          Now I think it's working fine on "live" mode. On the other side, have you any suggestion in order to make it working with historical data?

                          Thanks again.

                          Comment


                            #28
                            Originally posted by speedytrade02 View Post
                            Perfect Emily, thanks so much for your rapid response.

                            Now I think it's working fine on "live" mode. On the other side, have you any suggestion in order to make it working with historical data?

                            Thanks again.
                            You will need to consider the differences between a live/real-time environment and a historical/backtest environment. The following post goes into detail about these differences and the different options available to try and obtain more accurate backtest results:


                            Please let us know if we may be of further assistance.

                            Comment


                              #29
                              Hello speedytrade02,

                              From the code you have suggested in post # 25.

                              AddDataSeries("YM SEP23", Data.BarsPeriodType.Minute, 1, Data.MarketDataType.Last); <--- this is BarsInProgress index 1
                              AddDataSeries("YM SEP23", Data.BarsPeriodType.Second, 15, Data.MarketDataType.Last); <--- this is BarsInProgress index 2
                              AddDataSeries("YM SEP23", Data.BarsPeriodType.Tick, 1, Data.MarketDataType.Last); <--- this is BarsInProgress index 3
                              AddDataSeries("YM SEP23", Data.BarsPeriodType.Volume, 1, Data.MarketDataType.Last);​ <--- this is BarsInProgress index 4

                              The 1 tick series is BarsInProgress 3. Use 3 for the order submissions to place orders to the 1 tick series.
                              EnterLongStopLimit(3, true, 1, entryPrice, stopPrice, signalName)

                              EnterLongStopLimit(int barsInProgressIndex, bool isLiveUntilCancelled, int quantity, double limitPrice, double stopPrice, string signalName)
                              https://ninjatrader.com/support/help...gstoplimit.htm
                              https://ninjatrader.com/support/help...inprogress.htm


                              Where you have:
                              Order myOrder = EnterLongStopLimit(1, true, 1, entryPrice, stopPrice, signalName);

                              Please do not assign orders to order variables from the order method. Assign the order from OnOrderUpdate

                              Code:
                              private Order myOrder;
                              
                              protected override void OnBarUpdate()
                              {
                                  EnterLongStopLimit(1, true, 1, entryPrice, stopPrice, signalName);
                              }
                              
                              protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string comment)
                              {
                                  if (order.Name == signalName && myOrder != order)
                                  {
                                      myOrder = order;
                                  }
                              }​
                              From the help guide:
                              "Notes:
                              OnOrderUpdate() will run inside of order methods such as EnterLong() or SubmitOrderUnmanaged(), therefore attempting to assign an order object outside of OnOrderUpdate() may not return as soon as expected. If your strategy is dependent on tracking the order object from the very first update, you should try to match your order objects by the order.Name (signal name) from during the OnOrderUpdate() as the order is first updated."
                              https://ninjatrader.com/support/help...rderupdate.htm
                              Chelsea B.NinjaTrader Customer Service

                              Comment


                                #30
                                Perfect, Thanks Chelsea. Now seems to be working fine on live !

                                Comment

                                Latest Posts

                                Collapse

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