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-Time Frame code issue.

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

    Multi-Time Frame code issue.

    Hello,
    I am trying to add a second timeframe to a strategy. I have looked at a sample code for such a strategy on the forum but can't get it to work.


    This is what I have:

    protectedoverridevoid Initialize()
    {
    Add(PeriodType.Minute, 60);

    ]
    // Condition set 1
    if (Close(BarsArray[1],60)[0] > SMA(BarsArray[1], 100)[0]);

    {
    EnterLongLimit(1000, Close[0], "");

    }


    Thank you

    #2
    Hi strategy1, you miss one curly bracket to finish your Initialize() and you also you want to put the Condition1 and order entry in the OnBarUpdate() section of your strategy.

    Here's a good overview over MultiTime and MultiInstrument strategies - http://www.ninjatrader-support.com/H...struments.html
    BertrandNinjaTrader Customer Service

    Comment


      #3
      Curly bracket is not the problem.

      Thanks, but the curly bracket is in my code. I just forgot to copy it when posting.
      Also, both condition 1 and order entry are within the OnBarUpdate Section of the strategy.

      I believe it is my use of the Close parameter that is causing it not to compile.

      This is the error message I get:

      'NinjaTrader.Strategy StrategyBase Close' is a 'property' but is used like a 'method'

      Comment


        #4
        Hi strategy1, you want to use Closes then: here is the syntax Closes[int barSeriesIndex][int barsAgo]
        BertrandNinjaTrader Customer Service

        Comment


          #5
          How do I add SMA for the 2nd BarsArray to Chart.

          Bertrand, Thanks. Using closes compiled my code. I am now trying to see if the results I get are correct.

          One thing I noticed is: under instrument it now displays the symbol twice on the same line (i.e. GLD/GLD), whereas before it used to display it just once (GLD). Is this correct?

          Also, to better analyze the results I am trying to add the SMA of the second BarsArray to the chart and caanot seem to do it.

          This is my code:
          protectedoverridevoid Initialize()
          {
          Add(PeriodType.Minute, 100);
          Add(SMA(BarsArray[1], 100)[0]);
          SMA(BarsArray[1], 100)[0].Plots[0].Pen.Color = Color.Orange;
          }


          I have added a few other SMA's in this way before. What am I missing?


          It would of course be better to add this to a subchart and not the main chart along with the prices (for the second BarsArray) is this possible?


          Thanks

          Comment


            #6
            GLD/GLD is correct. You have two instruments listed because you are using multi-time frames.

            You cannot add indicators of a different time series for charting.
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              Will this be added to the next version?

              Josh, thanks.

              Is this on the list of things to be added in the next version?

              It appears to be essential for analysis, not to have to pull up another chart, set it for the same date range, position it over the main chart, etc.

              Comment


                #8
                You cannot plot a different time frames indicator on the current time frame. It doesn't make sense.

                Consider 1min and 100tick. How does the 100 tick fit onto the 1min? Which bars get plotted and where?

                Charting is different than accessing values. You can run multi-instrument/time frame strategies no problem for automated trading, charting wise it only really makes sense when you chart the indicator on the appropriate time frame. If you still wish for this functionality please see this reference sample. It is provided as-is: http://www.ninjatrader-support2.com/...ead.php?t=6651
                Josh P.NinjaTrader Customer Service

                Comment


                  #9
                  It is absolutely needed.

                  "You cannot plot a different time frames indicator on the current time frame. It doesn't make sense."

                  I am not sure you understood what I requested.

                  I am not trying to plot an SMA ( or just price bars) of a different time frame on the current time frame - but do so in a subchart. It is very easily done in TradeStation, as you can have a main chart with a number of subcharts, where each one can have a different time frame.

                  Not only does it make sense to do this, but if your strategy is based on more than one time frame - it is the only efficient way to check your results.

                  Comment


                    #10
                    There is no "subchart" in NT6.5. Multi-series charting is on our list. In the meantime, please consider this customer made indicator: http://www.ninjatrader-support2.com/...&highlight=mtf

                    I do not know if it is still compatible or not and cannot offer you any support on it.
                    Josh P.NinjaTrader Customer Service

                    Comment


                      #11
                      Showing more than 1 trade per direction

                      Going to my original post, now that I have added the second time frame to my strategy, I seem to be getting more than one trade per direction.

                      Here is my code:
                      Variables

                      bool HasTradedLong = false;


                      protectedoverridevoid OnBarUpdate()

                      {
                      if (Bars.FirstBarOfSession)
                      {
                      HasTradedLong = false;

                      }

                      if (Closes[1][0] > SMA(BarsArray[1], 100)[0]
                      && !HasTradedLong);
                      {
                      HasTradedLong = true;
                      EnterLongLimit(1000, Close[0], "");

                      }

                      This code used to work prior to me adding the second time frame.
                      The only other thing I changed was commenting out the short side processing.

                      Thanks

                      Comment


                        #12
                        strategy1,

                        When you add a second time frame it fundamentally changes every aspect of your code. Please review this article: http://www.ninjatrader-support.com/H...struments.html
                        Josh P.NinjaTrader Customer Service

                        Comment


                          #13
                          I've read it and added code, but still can't get it to work.

                          I've read it and they way I understood it is that I should check for the BarsInProgress, so that my condition is only executed when OnBarUpdate is called against the primary Bars Object.

                          I've added the following code in the OnBarUpdate() method, but still get more than 1 trade per direction.

                          if (BarsInProgress != 0)
                          return;

                          There must be something else that I am missing.

                          Thanks

                          Comment


                            #14
                            I still can't get it to work.

                            I tried putting

                            if (BarsInProgress != 0)
                            return;

                            in the beginning of OnBarUpdate() as well as right before OrderEntry and still can't get it to work.

                            I get more than 1 trade per direction.

                            I hope someone can help.

                            Thanks.

                            Comment


                              #15
                              Hi strategy1, please post the code you use and also check your EntriesPerDirection setting in the Entryhandling options when starting your strategy. Thanks!
                              BertrandNinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by mattbsea, Today, 05:44 PM
                              0 responses
                              5 views
                              0 likes
                              Last Post mattbsea  
                              Started by RideMe, 04-07-2024, 04:54 PM
                              6 responses
                              31 views
                              0 likes
                              Last Post RideMe
                              by RideMe
                               
                              Started by tkaboris, Today, 05:13 PM
                              0 responses
                              2 views
                              0 likes
                              Last Post tkaboris  
                              Started by GussJ, 03-04-2020, 03:11 PM
                              16 responses
                              3,282 views
                              0 likes
                              Last Post Leafcutter  
                              Started by WHICKED, Today, 12:45 PM
                              2 responses
                              20 views
                              0 likes
                              Last Post WHICKED
                              by WHICKED
                               
                              Working...
                              X