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

Strategy based on multi-time frames

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

    Strategy based on multi-time frames

    I am new to Ninjatrader. How can I define following: IF On daily time-frame SMA(5) crosses above SMA(8) and SMA(13) and ParabolicSAR(0.02, 0.2, 0.02) is under the price AND on weekly time-frame (the previous week) SMA(5) is under SMA(8) and SMA(13) and ParabolicSAR(0.02, 0.2, 0.02) is under the price THEN enter long. Thanks.

    #2
    Thank you for contacting us regarding your inquiry.

    First initialize the 2 times frames as follows in Initialize():

    Add(PeriodType.Day, 1); // =BarsArray[0]
    Add(PeriodType.Week, 1); // = BarsArray[1]


    This can be done by inserting this code into your OnBarUpdate() :

    if (BarsInProgress != 0)
    return;
    if(CrossAbove(SMA(Closes[1],5) ,SMA(Closes[1],8), 1)
    && CrossAbove(SMA(Closes[1],5), SMA(Closes[1],13), 1)
    && SMA(Closes[2],5)[0]< SMA(Closes[2],8)[0]
    && SMA(Closes[2],5)[0]< SMA(Closes[2],13)[0]
    && ParabolicSAR(Closes[1],0.02,0.2,0.02)[0] < Close[0]
    && ParabolicSAR(Closes[2],0.02,0.2,0.02)[0] < Close[0])

    EnterLong();


    (***Please note I had to use a look back value (the final 1 in the code below) with the cross over check. In this case, I used 1 day lookback for the cross over to occur. This can be changed to if you want to incorporate recent days for the cross to occur.

    SMA(Closes[1],8), 1) )
    Here is a link for your reviewhttp://ninjatrader.com/support/helpGuides/nt7/crossabove.htm
    Matt L.NinjaTrader Customer Service

    Comment


      #3
      Thank you very much for help, it seems to work but still need small improvement, i put my comments in italics.
      First initialize the 2 times frames as follows in Initialize():

      Add(PeriodType.Day, 1); // =BarsArray[0]

      Add(PeriodType.Week, 1); // = BarsArray[1]


      This can be done by inserting this code into your OnBarUpdate() :

      if (BarsInProgress != 0)
      return;
      if(CrossAbove(SMA(Closes[1],5) ,SMA(Closes[1],8), 1)
      && CrossAbove(SMA(Closes[1],5), SMA(Closes[1],13), 1)
      //are these crosses on daily frames?
      && SMA(Closes[2],5)[0]< SMA(Closes[2],8)[0]
      && SMA(Closes[2],5)[0]< SMA(Closes[2],13)[0]
      //are these on conditions on weekly?
      && ParabolicSAR(Closes[1],0.02,0.2,0.02)[0] < Close[0]
      //is this on daily?
      && ParabolicSAR(Closes[2],0.02,0.2,0.02)[0] < Close[0])
      //is this weekly on previous week?

      //according to results it seems it is looking at the week of the day, not on the previous week (preceding the day on which crosses happened)


      EnterLong();

      Comment


        #4
        Jirkos,

        The [1] after Close represents which data series is being reference. So in this case:

        if(CrossAbove(SMA(Closes[1],5) ,SMA(Closes[1],8), 1)
        && CrossAbove(SMA(Closes[1],5), SMA(Closes[1],13), 1)
        && ParabolicSAR(Closes[1],0.02,0.2,0.02)[0] < Close[0]

        All of the above deal with daily data [1]. anything with a [2] represents the second data series which we declared up top.
        To answer your second question, referencing the previous week would change the code to:
        && SMA(Closes[2],5)[1]< SMA(Closes[2],8)[1]
        && SMA(Closes[2],5)[1]< SMA(Closes[2],13)[1]

        && ParabolicSAR(Closes[2],0.02,0.2,0.02)[1] < Close[0])


        Also, please replace "if (BarsInProgress != 0)"


        to if(CurrentBars[1] >0) . This is a better check than the original one I provided.
        Matt L.NinjaTrader Customer Service

        Comment


          #5
          [QUOTE=NinjaTrader_MattL;426696]Thank you for contacting us regarding your inquiry.

          First initialize the 2 times frames as follows in Initialize():

          Add(PeriodType.Day, 1); // =BarsArray[0]
          Add(PeriodType.Week, 1); // = BarsArray[1]

          When setting up a chart, one can select minutes and even seconds. Can these periods be used here and if using seconds, how much processor load will that cause? Thanks in advance.

          Comment


            #6
            Dolfan,

            Thank you for your inquiry. Yes, both minutes and seconds can be added, in the similar syntax as the others. Here is a link that describes this, and also how bars are formed.


            Using additional data series and timeframes can put an additional load on the processor, but it is hard to determine as that can be strategy and computer specific.

            Let us know if we can help any further.
            Matt L.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by cmtjoancolmenero, Yesterday, 03:58 PM
            11 responses
            39 views
            0 likes
            Last Post cmtjoancolmenero  
            Started by FrazMann, Today, 11:21 AM
            0 responses
            5 views
            0 likes
            Last Post FrazMann  
            Started by geddyisodin, Yesterday, 05:20 AM
            8 responses
            52 views
            0 likes
            Last Post NinjaTrader_Gaby  
            Started by DayTradingDEMON, Today, 09:28 AM
            4 responses
            26 views
            0 likes
            Last Post DayTradingDEMON  
            Started by George21, Today, 10:07 AM
            1 response
            22 views
            0 likes
            Last Post NinjaTrader_ChristopherJ  
            Working...
            X