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 and WMA

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

    Multi time frame and WMA

    I am working with a 10 minute chart and trying to add a second time frame of 30 minutes.

    In my primary time frame, one condition I am checking for is

    Close[0] > WMA(High, LookBackPeriods)[0]

    I am trying to check for something similar in the secondary time frame but have not been able to figure out the correct syntax for the WMA. My last attempt was this:

    Closes[1][0] > WMA(High, LookBackPeriods)BarsArray[1][0]

    I think the portion before the greater than is OK, but regardless, I can't get seem figure this out.

    Thanks,

    David

    #2
    Hello,

    Remember the different time frames will be called at different times:


    Try using BarsInProgress and Print("30 min") statements to better understand when the bars are being called and in what order. You also may want to print the various bar indicator values in all BarsInProgress blocks to see how those change too. It can be confusing, but once you sort it out its pretty easy.
    DenNinjaTrader Customer Service

    Comment


      #3
      That's all well and good. If I could get the code to compile I could try some of those things. In the meantime I could use some help figuring out the proper syntax to reference the WMA high in my secondary time frame. As far as I can tell "Closes[1][0] is fine with the system. I cannot figure out the correct syntax for the second half of my statement to confirm that allows me to confirm that the close is above the WMA high in the secondary time frame.
      Different things I have tried all result in errors. Sometimes a CS0021 error or a bunch of "statement expected" and "expected;" errors.

      Comment


        #4
        Hello ddurocher,

        The simplest way to pass the high of the secondary series into WMA is through the use of Highs[1]

        WMA(Highs[1], 14) will capture the 14 period WMA of the secondary series high.

        An alternative is creating your own custom data series. This tutorial can help with creating a data series.

        Data Series are:
        1) Declared in Variables region
        private DataSeries secondSeriesHigh;

        2) Instantiated in Initialize() method
        secondSeriesHigh = new DataSeries (this);

        3) Set in OnbarUpdate()
        secondSeriesHigh.Set(Highs[1][0]);

        The set statement above will use Highs to access the secondary series high.

        You can then pass this value into the series expected for the WMA indicator.
        if (Closes[1][0] > WMA(secondSeriesHigh, LookBackPeriods)[0])


        Last edited by NinjaTrader_RyanM1; 06-01-2010, 11:46 AM.
        Ryan M.NinjaTrader Customer Service

        Comment


          #5
          Thanks Ryan. That works great. I even played around with it trying "Lows" and "Mediums" as well.

          David

          Comment


            #6
            Glad to hear you're able to get it working, David.
            Ryan M.NinjaTrader Customer Service

            Comment


              #7
              Apparently I have not got this working yet. After testing my strategy for a few days on a simulated account with real data I noticed some unexpected behaviors. I've corrected some of them but still seeing entries when my 60 minute bars are not closing above the WMA in the 60 minute time frame.

              I've been testing today with Market Replay data and also added some Print statements to my code. All the numbers are correct (10 min close, 10 min WMA high & 60 minute close) except for the 60 minute WMA high (or median, I've tested with both). In fact I can't even guess what the number might be referring to that I am seeing printed out as the supposed WMA High or Median.

              I've looked through my code and I believe I've coded this properly.

              Thanks,

              David

              Comment


                #8
                David, did you post your latest code version somewhere for review? How many days back are you loading and what's your min bars required setting? Please note the min bars have to be fullfilled for all series first and then straregy would move forward, so this might explain 'out of range' higher timeframe values perhaps.
                BertrandNinjaTrader Customer Service

                Comment


                  #9
                  Is there a setting for a strategy to indicate how many days to go back? The chart I am using to watch the strategy is going back 15 days. Min bars is set to 20. I am using 14 for my WMA periods. 10 min main time frame, 60 minute for the secondary time frame. I suppose I could post part of my code due to the character limit on posts, but i'm not sure how useful that would be.

                  Comment


                    #10
                    David, the strategy would take whatever is on the chart as lookback range, so for all timeframes you have the min bars of 20 has to be fulfilled and then the strategy would start processing.

                    If you startup your strategy from the strategies tab, please be aware that the lookback taken in NT 6.5 is determined by the chart defaults setup under Tools > Options > Data.
                    BertrandNinjaTrader Customer Service

                    Comment


                      #11
                      OK, on the data tab I have 15 days for minute bars. Since I'm using 10 and 60 minute timeframes and a 14 period WMA I should think that would be sufficient. I suspect my issue has to do with the use of BarsInProgress.

                      I've added a print statement now to see what sevaral of the values are after each 10 minute bar closes. Interestingly the WMA High, Low and Median on the 60 min timeframe are changing after each 10 minute bar closes. Aside from the fact that all these values are way off, should they be changing in 60 minute time frame after each 10 minute bar closes?

                      David

                      Comment


                        #12
                        Hello David,

                        The help guide article posted by Ben is good for figuring out how different bar objects are updated.



                        If you're running into unexpected results here, share the code you're working with and what instruments it's applied to.
                        Ryan M.NinjaTrader Customer Service

                        Comment


                          #13
                          Here's the code. I've been using Markey replay data the last couple of days for testing on HNU (cdn etf) I've also tested it wiuth live data on several other ETFs such as BGZ and SMN.



                          #region Variables
                          // Wizard generated variables
                          private int lookBackPeriods = 14; // Default setting for LookBackPeriods
                          private int longEntry = 150; // Default setting for LongEntry
                          // User defined variables (add any user defined variables below)
                          private DataSeries secondSeriesMed; //declaring dataseries created for passing Median of secondary time frame
                          private DataSeries secondSeriesHigh; //declaring dataseries created for passing High of secondary time frame
                          private DataSeries secondSeriesLow; //declaring dataseries created for passing Low of secondary time frame


                          #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()
                          {
                          // use multi time frame of 10 minutes with 60 minutes for confirmation of entry
                          Add(PeriodType.Minute, 60); // this is the timeframe for confirmation of entry
                          secondSeriesMed = new DataSeries (this); // instantiating dataseries to reference Median of secondary time frame
                          secondSeriesHigh = new DataSeries (this); // instantiating dataseries to reference High of secondary time frame
                          secondSeriesLow = new DataSeries (this); // instantiating dataseries to reference Low of secondary time frame
                          CalculateOnBarClose = true;
                          Add(WMA(High, LookBackPeriods));
                          Add(WMA(Low, LookBackPeriods));
                          ExitOnClose = false;
                          ExcludeWeekend = true;
                          SessionBegin = DateTime.Parse("09:30 AM");
                          SessionEnd = DateTime.Parse("04:00 PM");
                          }
                          /// <summary>
                          /// Called on each bar update event (incoming tick)
                          /// </summary>
                          protected override void OnBarUpdate()
                          {
                          secondSeriesMed.Set(Medians[1][0]); // use Medians to access the secondary series median
                          secondSeriesHigh.Set(Highs[1][0]); // use Highs to access the secondary series high
                          secondSeriesLow.Set(Lows[1][0]); // use Lows to access the secondary series high
                          // Only run on real-time data
                          if (Historical)
                          return;
                          // We only want to process events on our primary Bars object (index = 0) which is set when adding
                          // the strategy to a chart
                          // if (BarsInProgress != 0)
                          // return;
                          // if (BarsInProgress == 0)

                          // Condition set 1
                          if (BarsInProgress == 1
                          && Position.MarketPosition == MarketPosition.Flat // if we are flat
                          && Closes[0][0] > WMA(High, LookBackPeriods)[0] // Last 10 min bar closed above High WMA
                          && Closes[0][0] >= Opens[0][0] // Last 10 min bar was an UP bar
                          && Closes[0][1] < WMA(High, LookBackPeriods)[1] // Close of previous 10 min bar was below High WMA
                          && Close[0] > WMA(secondSeriesMed, LookBackPeriods)[0]) // Last 60 min bar closed above Median WMA
                          // && Close[0] > WMA(secondSeriesHigh, LookBackPeriods)[0]) // Last 60 min bar closed above High WMA
                          {
                          Print("Enter Long. 10 min Close: "+ Closes[0][0] + " 10 min WMA High: " + WMA(High, LookBackPeriods)[0] + " 60 Min Close: " + Close[0] + " 60 Min WMA High: " + WMA(secondSeriesHigh, LookBackPeriods)[0] + " 60 Min WMA Med: " + WMA(secondSeriesMed, LookBackPeriods)[0] + " 60 Min WMA Low: " + WMA(secondSeriesLow, LookBackPeriods)[0]);
                          EnterLong(LongEntry, "EnterCloseHi");
                          }

                          // Condition set 2
                          if (BarsInProgress == 0 // added this to try to resolve anomolous exit
                          && Position.MarketPosition == MarketPosition.Long // if we are flat
                          && Close[0] < WMA(Low, LookBackPeriods)[0]) // Last bar closed below Low WMA
                          {
                          // ExitLong("Exit", "EnterCloseHi");
                          Print("Exit Long. 10 min Close: "+ Close[0] + " 10 min WMA Low: " + WMA(Low, LookBackPeriods)[0]);
                          ExitLong("", "");
                          }

                          // Condition set 2
                          if (BarsInProgress == 0) // added this to print values at each bar

                          {
                          Print("Print Bar. 10 min Close: "+ Close[0] + " 10 min WMA High: " + WMA(High, LookBackPeriods)[0] + " 60 Min Close: " + Closes[1][0] + " 60 Min WMA High: " + WMA(secondSeriesHigh, LookBackPeriods)[0] + " 60 Min WMA Med: " + WMA(secondSeriesMed, LookBackPeriods)[0] + " 60 Min WMA Low: " + WMA(secondSeriesLow, LookBackPeriods)[0]);
                          }

                          Comment


                            #14
                            Hello ddurocher,

                            Thanks for the code.

                            Unfortunately we got a little off track and I apologize for any of your time used debugging. Resolving this can be done one of two ways.

                            1) Using the convention with DataSeries would require syncing per this reference sample:
                            Note: In NinjaTrader 8 It is no longer needed to use an indicator to sync a secondary series. This can be done directly from the Series&lt;T&gt; (https://ninjatrader.com/support/helpGuides/nt8/NT%20HelpGuide%20English.html?seriest.htm) constructor. This post is left for historical purposes. Series objects are useful for


                            2) Your usage does not require creating custom series. A simpler solution may be to use the built in methods. You can pass Highs[1] directly into WMA to access the secondary series high. Same is true for Lows[1] or Medians[1].

                            WMA(Highs[1], 14);
                            Ryan M.NinjaTrader Customer Service

                            Comment


                              #15
                              Haven't tried approach 1 yet, trying the second.

                              When I use Closes[1][0] I get the close not from the previous completed bar but from the bar before that. Same if I use WMA(Highs[1], 14)[0] - I get the WMA high not from the last completed bar but from the bar before that. Am I missing something obvious?

                              Thanks,

                              David

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Vitamite, Yesterday, 12:48 PM
                              3 responses
                              17 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by aligator, Today, 02:17 PM
                              0 responses
                              8 views
                              0 likes
                              Last Post aligator  
                              Started by lorem, 04-25-2024, 09:18 AM
                              22 responses
                              94 views
                              0 likes
                              Last Post lorem
                              by lorem
                               
                              Started by sofortune, Today, 10:05 AM
                              3 responses
                              16 views
                              0 likes
                              Last Post NinjaTrader_ChristopherJ  
                              Started by ETFVoyageur, 05-07-2024, 07:05 PM
                              23 responses
                              185 views
                              0 likes
                              Last Post ETFVoyageur  
                              Working...
                              X