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

Indicator data from additional series

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

    Indicator data from additional series

    My main data series is 900 ticks.

    However, I want to add daily data series and calculate 10 day ATR from additional (daily) data series.

    Can anybody provide a framework/example of it?

    Also, I want to count current day volume (as I am not sure live daily bar's volume would be displayed) and compare current day volume with 10-day volume SMA.

    #2
    Hello UltraNIX,

    Thank you for your post.

    I've created an example indicator that can be used with whatever primary series you like, that calculates an ATR of the secondary daily series. The main issue I can see with this is that you'd have to load at least 10 days of data so you have enough daily bars to calculate the ATR.

    In the example I've also put in (commented out) how you can also calculate an SMA of the volumes of the daily bars - I wouldn't plot them both at the same time since the values are so far off from each other, but you could then use that calculation however you want.

    Please let us know if we may be of further assistance to you.
    Attached Files
    Kate W.NinjaTrader Customer Service

    Comment


      #3
      Thanks, Kate, but I want to have the ability to change day variable, be it 10, 5 or 20, so I'd prefer to have it in a strategy.

      What I did was tried to create Custom Strategy in Strategy Builder.

      Added Data series - 1 day. And then in Conditions and Actions tab tried to create ATR of 1 day series. And then hit "View Code", here it is:

      Code:
      namespace NinjaTrader.NinjaScript.Strategies
      {
      public class MyCustomStrategy : Strategy
      {
      private ATR ATR1;
      
      protected override void OnStateChange()
      {
      if (State == State.SetDefaults)
      {
      Description = @"Enter the description for your new custom Strategy here.";
      Name = "MyCustomStrategy";
      Calculate = Calculate.OnBarClose;
      EntriesPerDirection = 1;
      EntryHandling = EntryHandling.AllEntries;
      IsExitOnSessionCloseStrategy = true;
      ExitOnSessionCloseSeconds = 30;
      IsFillLimitOnTouch = false;
      MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
      OrderFillResolution = OrderFillResolution.Standard;
      Slippage = 0;
      StartBehavior = StartBehavior.ImmediatelySubmitSynchronizeAccount;
      TimeInForce = TimeInForce.Gtc;
      TraceOrders = false;
      RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
      StopTargetHandling = StopTargetHandling.PerEntryExecution;
      BarsRequiredToTrade = 20;
      // Disable this property for performance gains in Strategy Analyzer optimizations
      // See the Help Guide for additional information
      IsInstantiatedOnEachOptimizationIteration = true;
      }
      else if (State == State.Configure)
      {
      AddDataSeries(Data.BarsPeriodType.Day, 1);
      }
      else if (State == State.DataLoaded)
      {
      ATR1 = ATR(Closes[1], 14);
      }
      }
      
      protected override void OnBarUpdate()
      {
      if (BarsInProgress != 0)
      return;
      
      if (CurrentBars[0] < 1
      || CurrentBars[1] < 0)
      return;
      
      // Set 1
      if ((ATR1[0] == High[0] - Low[0]))
      {
      EnterLongStopMarket(Convert.ToInt32(DefaultQuantit y), (High[0] + (1 * TickSize)) , "");
      }
      
      }
      }
      }
      I tried to copy required parts into my main strategy, that included:
      1) private ATR ATR1; (After public class MyCustomStrategy : Strategy)
      2) AddDataSeries(Data.BarsPeriodType.Day, 1); (In State.Configure)
      3) ATR1 = ATR(Closes[1], 14); (in State.DataLoaded)
      4) if (BarsInProgress != 0)
      return;

      if (CurrentBars[0] < 1
      || CurrentBars[1] < 0)
      return; (In OnBarUpdate)

      and then I wanted to Print ATR1[0] using this code:
      Print(Time[0] + " | ATR1[0]: " + ATR1[0]);

      However, I got this error: Strategy 'AI_ADX_200807_O': Error on calling 'OnBarUpdate' method on bar 86: Strategy 'AI_ADX_200807_O/-1': You must use the overload that has a 'BarsInProgress' parameter when calling the BarsSinceEntryExecution() method in the context of a multi-time frame and instrument strategy.

      And it stopped printing values.

      Comment


        #4
        Originally posted by NinjaTrader_Kate View Post
        Hello UltraNIX,

        Thank you for your post.

        I've created an example indicator that can be used with whatever primary series you like, that calculates an ATR of the secondary daily series. The main issue I can see with this is that you'd have to load at least 10 days of data so you have enough daily bars to calculate the ATR.

        In the example I've also put in (commented out) how you can also calculate an SMA of the volumes of the daily bars - I wouldn't plot them both at the same time since the values are so far off from each other, but you could then use that calculation however you want.

        Please let us know if we may be of further assistance to you.
        However, if ATR would allow for dynamic input, that could be used and called in a strategy, like SMA(Close, Period), it would acceptable to have it as an indicator.

        Comment


          #5
          NinjaTrader_Kate I removed VOLSMA part from indicator and renamed it to ATRDay.

          Here's an updated code:
          Code:
          public class ATRDay : Indicator
          {
          protected override void OnStateChange()
          {
          if (State == State.SetDefaults)
          {
          Description = @"Enter the description for your new custom Indicator here.";
          Name = "ATRDay";
          Calculate = Calculate.OnEachTick;
          IsOverlay = false;
          DisplayInDataBox = true;
          DrawOnPricePanel = true;
          DrawHorizontalGridLines = true;
          DrawVerticalGridLines = true;
          PaintPriceMarkers = true;
          ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
          //Disable this property if your indicator requires custom values that cumulate with each new market data event.
          //See Help Guide for additional information.
          IsSuspendedWhileInactive = true;
          ATRPeriod = 10;
          AddPlot(Brushes.Orange, "ATRDay");
          }
          else if (State == State.Configure)
          {
          AddDataSeries(Data.BarsPeriodType.Day, 1);
          }
          }
          
          protected override void OnBarUpdate()
          {
          if(CurrentBars[1] < ATRPeriod)
          return;
          
          if(BarsInProgress == 1)
          {
          // when we process a new daily bar update to the new ATR value
          ATRPlot[0] = ATR(BarsArray[1], ATRPeriod)[0];
          }
          else if (BarsInProgress == 0)
          {
          // when our primary series updates assign the previous value so we geta a continuous plot
          ATRPlot[0] = ATRPlot[1];
          }
          }
          And I tried calling it in a strategy:

          if(ATRDay(10)[0] != null)
          {
          Print(Time[0] + " | ATRDay(10)[0]: " + ATRDay(10)[0]);
          }

          I added if statement to ensure that it's not null. Yet, I get this error:
          Code:
          'ATRDay' tried to load additional data. All data must first be loaded by the hosting NinjaScript in its configure state. Attempted to load MES 12-20 Globex: Daily
          What is wrong and how to correct it?

          Comment


            #6
            Hello UltraNIX,

            Thank you for your replies.

            My original script does allow you to specify the ATRPeriod as a user input. I don't see that in your modified script but you may have just left out the parameter that allows the user input:

            [NinjaScriptProperty]
            [Range(1, int.MaxValue)]
            [Display(Name="ATRPeriod", Order=1, GroupName="Parameters")]
            public int ATRPeriod
            { get; set; }

            Since ATRDay adds an additional 1 day data series, you'd also need to add the same series to your strategy as well. So this needs to be added to the strategy to avoid that error:


            else if (State == State.Configure)
            {
            AddDataSeries(Data.BarsPeriodType.Day, 1);
            }

            Please let us know if we may be of further assistance to you.
            Kate W.NinjaTrader Customer Service

            Comment


              #7
              I am getting the same error, when added additional series (Daily):

              Code:
              Strategy 'AI_ADX_200807_O': Error on calling 'OnBarUpdate' method on bar 86: Strategy 'AI_ADX_200807_O/-1': You must use the overload that has a 'BarsInProgress' parameter when calling the BarsSinceEntryExecution() method in the context of a multi-time frame and instrument strategy.
              Can you help me solve this error?

              Comment


                #8
                Hello UltraNIX,
                Can you clarify your exact requirement, it seems a simple thing but stuck somewhere. If I am understanding correctly you want ATR for Daily Series added in State.Configure & what you want is to change this series from 1 day to may be 10 days & get the ATR?

                Comment


                  #9
                  Hello UltraNIX,

                  Thank you for your reply.

                  The error you're getting now means that the strategy you're using uses BarsSinceEntryExecution() somewhere within it. Since you've added the 1 Day data series, that makes it a multi-series strategy, and you'd have to use the overload that includes a BarsInProgress parameter for that method since we now have more than 1 set of bars. Please see our help guide on BarsSinceEntryExecution here:



                  Please let us know if we may be of further assistance to you.
                  Kate W.NinjaTrader Customer Service

                  Comment


                    #10
                    Originally posted by s.kinra View Post
                    Hello UltraNIX,
                    Can you clarify your exact requirement, it seems a simple thing but stuck somewhere. If I am understanding correctly you want ATR for Daily Series added in State.Configure & what you want is to change this series from 1 day to may be 10 days & get the ATR?
                    Exactly. those 10 days need to be controlled with variable.

                    Comment


                      #11
                      Hello UltraNIX,
                      Let me share a sample code with you so you can test it with your requirements. Here you get it.
                      SampleATR.zip

                      Comment


                        #12
                        thank you s.kinra and NinjaTrader_Kate, but the problem seems to be that BarsSinceEntryExecution() function. And, if I add additional data series, I would have to adjust the code (like Time[0] -> Times[1][0]) and all that. And it seems too much of a work for a questionable result. So I discarded this feature and will stay on single data series.

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by DanielTynera, Today, 01:14 AM
                        0 responses
                        2 views
                        0 likes
                        Last Post DanielTynera  
                        Started by yertle, 04-18-2024, 08:38 AM
                        9 responses
                        40 views
                        0 likes
                        Last Post yertle
                        by yertle
                         
                        Started by techgetgame, Yesterday, 11:42 PM
                        0 responses
                        11 views
                        0 likes
                        Last Post techgetgame  
                        Started by sephichapdson, Yesterday, 11:36 PM
                        0 responses
                        2 views
                        0 likes
                        Last Post sephichapdson  
                        Started by bortz, 11-06-2023, 08:04 AM
                        47 responses
                        1,615 views
                        0 likes
                        Last Post aligator  
                        Working...
                        X