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

PeriodType.Day

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

    PeriodType.Day

    I'm in a lower time frame and would like to have the daily ATR as a condition in the strategy with the following code, but when executing the strategy, the whole strategy stops working. Can I call a PeriodType.Day in a strategy or is the syntax wrong?

    Code:
    protected override void Initialize()
            {            
                Add(PeriodType.Day,1);
            }
    protected override void OnBarUpdate()
            {
                double ATRDaily       =   (ATR(BarsArray[1],14)[0]);
    
                 if (Position.MarketPosition == MarketPosition.Flat)
                    {                
                        if (ConditionA && ATRDaily )
                        {                        
                            EnterLong();
                        }                
                    }
          }
    Thanks in advance.

    #2
    Hello 2Look4me,

    Thanks for your post.

    If the strategy stops working you may want to check the "log" tab of the control center for any error messages related to the strategy.

    I suspect you will need to check for the correct number of bars to be loaded before processing your code. If you are looking at a 14 day ATR, then to have the correct value for the ATR you would need a minimum of 14 days loaded before trying to execute the code starting at the line double ATRDaily = (ATR(BarsArray[1],14)[0]); This would be done by using a CurrentBars[1] check. For example, as the first statement in the OnBarUpdate() method you would have: if (CurrentBars[1] < 14) return; // do not process further until 14 daily bars loaded. Reference: http://ninjatrader.com/support/helpG...urrentbars.htm

    Also in the conditions statement: if (ConditionA && ATRDaily ) you would need to have the ATRDaily resolve to a logical condition as currently it is a numeric value. An example would be if (ConditionA && ATRDaily > 30.2)

    When using add dataseries please note that when the daily bar closes it will also call OnBarUpdate() and you may experience unintended actions as all of the references would change from your lower time frame to the higher timeframe. In this case, you may want to add a statement at the beginning, after the CurrentBars check, to check for the "BarsInProgress". A BarsInProgress value of 0 (zero) will be the chart bars BarsInProgress and 1 would be in this case the daily bars, something like: if (BarsInProgress != 0) return; // only execute when lower time frame calls OBU. Reference: http://ninjatrader.com/support/helpG...inprogress.htm

    To tie all these together we recommend reviewing this section of the helpguide: http://ninjatrader.com/support/helpG...nstruments.htm
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Paul, thanks for the detailed explanation and for the links. I'm still processing the info and made the following changes with the same results as before and the log tab shows no errors either.

      Code:
      protected override void Initialize()
              {            
                  Add(PeriodType.Day,1);
               }
              protected override void OnBarUpdate()
              {
                  if(CurrentBars[0] <= BarsRequired || CurrentBars[1] <= BarsRequired)
                      return;
                  bool ATRDaily       =   (ATR(BarsArray[1],14)[0])>=30;
                              
                  if (BarsInProgress != 0) 
                      return;
                  if (Position.MarketPosition == MarketPosition.Flat)
                      {                
                          if (ConditionA && ATRDaily)
                          {                        
                              EnterLong();
                          }                
                      }
              }

      Comment


        #4
        Hello 2Look4me,

        Thanks for your post.

        At this point you would need to use print statements in your code to help you assess the actual values of ATR(BarsArray[1],14)[0] and of the state of the bool ATRDaily.

        I copied your code and removed the section:

        if (ConditionA && ATRDaily)
        {
        EnterLong();
        }

        and in its placed added this print line:

        Print ("Daily ATR value: "+ATR(BarsArray[1],14)[0]+" ATRDaily state: "+ATRDaily);

        I used the ES 12-16 with 60 minute bars and added the strategy to it, which resulted in an output like:

        Daily ATR value: 22.5002226248533 ATRDaily state: False
        Daily ATR value: 22.5002226248533 ATRDaily state: False
        Daily ATR value: 22.5002226248533 ATRDaily state: False
        Daily ATR value: 22.5002226248533 ATRDaily state: False
        Daily ATR value: 30.7680638659352 ATRDaily state: True
        Daily ATR value: 30.7680638659352 ATRDaily state: True
        Daily ATR value: 30.7680638659352 ATRDaily state: True
        Daily ATR value: 30.7680638659352 ATRDaily state: True

        Note: I suspect you may not have enough bars loaded. In my test it required 40 days of data to produce results
        Paul H.NinjaTrader Customer Service

        Comment


          #5
          As soon as
          Code:
          Add(PeriodType.Day,1)
          is added to the script, the strategy becomes disabled.

          Then adding the bars check:
          Code:
          if(CurrentBars[0] <= BarsRequired || CurrentBars[1] <= BarsRequired ) 
               return;
          will not get the strategy enabled again.

          Therefore as I followed what you suggested, there is no Print in the Output Window and there aren't any errors in the log tab.

          Thanks for the continuous help.

          Comment


            #6
            Hello 2Look4me,

            Thanks for your reply.

            Please download the attached sample strategy to your Ninjatrader.

            Open a chart of ES 12-16, 60 minute bars with 50 days of data.

            Open the output window (Tools>output window)

            Apply the strategy to the 60 minute chart and then observe the output window.

            You might then review the differences between your code and the sample code as the sample code is based on what you have advised in this thread.
            Attached Files
            Paul H.NinjaTrader Customer Service

            Comment


              #7
              Paul, thanks for the sample code. I followed your instructions:
              HTML Code:
              Please download the attached sample strategy to your Ninjatrader.
              
              Open a chart of ES 12-16, 60 minute bars with 50 days of data.
              
              Open the output window (Tools>output window)
              
              Apply the strategy to the 60 minute chart and then observe the output window.
              Nothing was printed in the output window. I also observed a daily ES 12-16 chart with a ATR(14) and even changed the value of the ATRDaily from 30 to 15; yet still no print in the output window.

              Comment


                #8
                Hello 2Look4me,

                Thanks for your reply.

                Please confirm that you are "enabling" the strategy when you apply it to live data.

                What data feed are you connected to?
                Paul H.NinjaTrader Customer Service

                Comment


                  #9
                  With your sample code I isolated the problem, but I'm getting an error from the following code:
                  Code:
                  if(Position.MarketPosition == MarketPosition.Flat)
                                  {
                                      if(BarsSinceExit()!=0 && ConditionA && ConditionB)
                                      {
                                          EnterLong();    
                                      }
                                  }
                  HTML Code:
                  You must use the overload that has a BarsInProgress parameter when calling the BarsSinceExit method in the context of a multi time frame and instrument strategy

                  Comment


                    #10
                    Hello 2Look4me,

                    Thanks for your reply.

                    The error message is advising that because you have a multitimeframe strategy that you will need to use the overload of the method that includes the BarsInProgress index so that it then would know which Bars object you want Ninjascript to look for the last exit on.
                    Please see the overload syntax of the method in the helpguide: http://ninjatrader.com/support/helpG...ssinceexit.htm

                    In addition, note that the example in the helpguide shows two checks of the BarsSinceExit(), one is to see if the specified number of bars have passed since exit and the other is to see if there has been an exit (-1). You will need to add that check for -1 or your condition may not become true.
                    Paul H.NinjaTrader Customer Service

                    Comment


                      #11
                      Paul, thanks for the link and the explanation. Upon adding:
                      Code:
                      if((BarsSinceExit()!=0 || BarsSinceExit()==-1) && ConditionA && ConditionB)
                                          {
                                              EnterLong();    
                                          }
                      The error in the log tab is no longer there. But the strategy cannot be enabled. I have tried with Enabled=true; or enabling it from the Strategies tab, the box cannot be check marked.

                      Comment


                        #12
                        I restarted NT. Still the strategy cannot be enabled as by the previous post. After failing to enable the strategy, there are no errors in the log tab, but now I noticed that the output window gives the same error:
                        HTML Code:
                        You must use the overload that has a BarsInProgress parameter when calling......

                        Comment


                          #13
                          Hello 2Look4me,

                          Thanks for your reply.

                          The issue here is that you did not use the overload method as shown below from the helpguide:

                          Syntax: BarsSinceExit(int barsInProgressIndex, string signalName, int exitsAgo)

                          where:

                          barsInProgressIndex - The index of the Bars object the entry order was submitted against.

                          signalName - The signal name of an entry order specified in an order entry method. Pass in empty string "" for default signal.

                          exitsAgo - Number of exits ago. Pass in 0 for the number of bars since the last exit.

                          Taking a guess on what you have shared, , I suspect you want to use 0 for the BarsInProgress. If you haven't given your orders a name then you would use "" for the signal name and I would assume you want the last exit so a 0 would work there. Putting it together:
                          BarsSinceExit(0,"",0)!=0 || BarsSinceExit(0,"",0)==-1)
                          Paul H.NinjaTrader Customer Service

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by jclose, Today, 09:37 PM
                          0 responses
                          5 views
                          0 likes
                          Last Post jclose
                          by jclose
                           
                          Started by WeyldFalcon, 08-07-2020, 06:13 AM
                          10 responses
                          1,413 views
                          0 likes
                          Last Post Traderontheroad  
                          Started by firefoxforum12, Today, 08:53 PM
                          0 responses
                          11 views
                          0 likes
                          Last Post firefoxforum12  
                          Started by stafe, Today, 08:34 PM
                          0 responses
                          11 views
                          0 likes
                          Last Post stafe
                          by stafe
                           
                          Started by sastrades, 01-31-2024, 10:19 PM
                          11 responses
                          169 views
                          0 likes
                          Last Post NinjaTrader_Manfred  
                          Working...
                          X