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

Multi instrument entry/exit delay

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

    Multi instrument entry/exit delay

    Hi,

    I am trying to create a multi instrument strategy. I have managed to get it working somehow, the problem is that the entry and exit signals is delayed by 1 bar, as opposed to running the strategy with only 1 instrument.
    I tested it on the ES instrument by running a multi instrument strategy agains ES itself. This resulted in the entry and exit signals being off by 1 bar. When I tried running the same strategy without multi instrument the entry and exit signals was hit on the right bars.

    Does anyone know what the problem might be?

    This is what I use to execute the multi instrument strategy:

    protected override void Initialize(){
    CalculateOnBarClose = false;
    TraceOrders = true;
    Add("ES 06-14", PeriodType.Day, 1);
    }

    protected override void OnBarUpdate(){
    if (BarsInProgress == 1){
    Entry/exit signals come from this.
    }

    if (BarsInProgress == 0){
    Entry/exit signals executed here.
    }

    }

    #2
    Hello Stoop,

    Thank you for your post

    Was this done with a Backtest from the Strategy Analyzer or loaded onto a chart for real-time data?
    Cal H.NinjaTrader Customer Service

    Comment


      #3
      Hi Cal,

      Sorry for not specifying that in my OP, it was done with a backtest from the strategy analyzer.

      Comment


        #4
        Hello Stoop,

        Thank you for your reply.

        This is expected as the CalculateOnBarClose runs true.

        What will happen is the at the end of a bar, a calculation is ran through the OnBarUpdate which will submit any orders. Then the FillType is called which checks the next bar for it to get filled. Thus why you see the executions plotted on the bar after the condition is true.

        Let me know if I can be of further assistance.
        Cal H.NinjaTrader Customer Service

        Comment


          #5
          I understand that the strategy won't execute an order until the condition has been met by a complete bar. The problem I have is that the multi instrument strategy currently looks like this:
          Bar 1: Condition for entry fulfilled.
          Bar 2: Waiting.
          Bar 3: Entry.

          While it should look like:
          Bar 1: Condition for entry fulfilled.
          Bar 2: Entry.

          That second example is how the strategy runs when I only use 1 instrument. When I add the same instrument (2x of same instrument) in the multi instrument strategy it looks like the first example.

          Is it supposed to be like that when running multi instruments, or is there a way to avoid the waiting in the first example? I just don't understand why it is waiting a whole bar before entering a new position.

          Thanks,
          Alex

          Comment


            #6
            Alex,

            Would you be willing to attach your strategy so that I can test this on my end and further investigate?

            What primary Timeframe do you run this on?
            Cal H.NinjaTrader Customer Service

            Comment


              #7
              Hi,

              I created an example strategy that has the same problem as my strategy.
              I'm running on daily bars as my primary, and only bars.

              Example1 with the waiting problem and multi instrument:
              Code:
              #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.Gui.Chart;
              using NinjaTrader.Strategy;
              #endregion
              
              // This namespace holds all strategies and is required. Do not change it.
              namespace NinjaTrader.Strategy
              {
                 /// <summary>
                 /// </summary>
                 [Description("")]
                 public class example : Strategy
                 {
                     #region Variables
              
              public bool ESEntry = false;
              public bool ESExit = false;
              
              // User defined variables (add any user defined variables below)
                     #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()
              {	
              
              	CalculateOnBarClose = false;
              
              	TraceOrders      = true;
              	Add("ES 06-14", PeriodType.Day, 1);
              }
              
              
                     /// <summary>
                     /// Called on each bar update event (incoming tick)
                     /// </summary>
                     protected override void OnBarUpdate()
                     {
              if (BarsInProgress == 1){
              #region Long Entry
                         if (Close[0] > Open[0]){ESEntry = true;}
              #endregion
              }
              
              if (BarsInProgress == 0){
              if (ESEntry){
              	EnterLong(1,"");
              	ESEntry = false;
              }
              }
              
              if (BarsInProgress == 1){
              #region Long Exit
              if (High[0] > Open[0]){
                        if (Close[0] < Open[0]){ESExit = true;}
              }
              #endregion
              }
              
              if (BarsInProgress == 0){
              if (ESExit){
              	ExitLong("","");
              	ESExit = false;
              }
              }
              
              
                     } // OnBarUpdate
              
                 }
              }
              Example 2 with single instrument and no problem:
              Code:
              #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.Gui.Chart;
              using NinjaTrader.Strategy;
              #endregion
              
              // This namespace holds all strategies and is required. Do not change it.
              namespace NinjaTrader.Strategy
              {
                 /// <summary>
                 /// </summary>
                 [Description("")]
                 public class example2 : Strategy
                 {
                     #region Variables
              // User defined variables (add any user defined variables below)
                     #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()
              {	
              
              	CalculateOnBarClose = false;
              
              	TraceOrders      = true;
              }
              
              
                     /// <summary>
                     /// Called on each bar update event (incoming tick)
                     /// </summary>
                     protected override void OnBarUpdate()
                     {
              
              #region Long Entry
                         if (Close[0] > Open[0]){EnterLong(1,"");}
              #endregion
              
              
              
              #region Long Exit
              if (High[0] > Open[0]){
                        if (Close[0] < Open[0]){ExitLong("","");}
              }
              #endregion
              
              
                     } // OnBarUpdate
              
                 }
              }
              Thanks,
              Alex

              Comment


                #8
                Hello Alex,

                Thank you for your response.

                So you are running the multi-instrument strategy on a ES Daily chart and using ADD() to add another ES Daily bar series? If not, what is the primary series for the strategy/what is the instrument you apply the strategy to?

                Comment


                  #9
                  Hi PatrickH,

                  Sorry for not being clearer on that point. You're right, currently I'm running it on an ES Daily chart using ADD() to add another ES Daily bar series. I did this as a test to see if the multi instrument strategy worked exactly like the single instrument strategy would. The idea was to get entry/exit signals from the ES Daily bar series and have entries and exits executed on various S&P 500 stocks.

                  Thanks for your response.

                  Comment


                    #10
                    Anyone have a clue of what might work to solve this problem?

                    Comment


                      #11
                      Stoop,

                      You are checking the entry condition for BarsInProgress == 1.

                      When the second call for BIP 1 comes through it sets the bool, ESEntry, to true. Then the next Bar Update for BIP 0, which is where you place the order, is called on the next day after the entry signal has been changed. Thus this is why you are seeing the extra delay in the fill.

                      Let me know if I can be of further assistance.
                      Cal H.NinjaTrader Customer Service

                      Comment


                        #12
                        Is there a way to write the strategy so that I avoid the delay?

                        Comment


                          #13
                          Hi Stoop, to a large degree this would be expected from the framework, since you could not force a different bars updating order. One way to look into minimizing the discrepancy could be working the conditions from BIP 0 without passing the bool -

                          if (BarsInProgress == 0)
                          {
                          if (Closes[1][0] > Opens[1][0])
                          {
                          EnterLong(1, "");
                          }
                          }
                          BertrandNinjaTrader Customer Service

                          Comment


                            #14
                            I got it working now thanks to your example, Bertrand. Thank you very much for the help!

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by Barry Milan, Yesterday, 10:35 PM
                            5 responses
                            16 views
                            0 likes
                            Last Post NinjaTrader_Manfred  
                            Started by DanielSanMartin, Yesterday, 02:37 PM
                            2 responses
                            13 views
                            0 likes
                            Last Post DanielSanMartin  
                            Started by DJ888, 04-16-2024, 06:09 PM
                            4 responses
                            13 views
                            0 likes
                            Last Post DJ888
                            by DJ888
                             
                            Started by terofs, Today, 04:18 PM
                            0 responses
                            12 views
                            0 likes
                            Last Post terofs
                            by terofs
                             
                            Started by nandhumca, Today, 03:41 PM
                            0 responses
                            8 views
                            0 likes
                            Last Post nandhumca  
                            Working...
                            X