• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

MTF (MultiTimeframe) Indicator example?

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

    MTF (MultiTimeframe) Indicator example?

    I've seen several topic (searching solution for "Error on calling 'OnStateChange' method: 'AddChartIndicator' cannot be called from this state.).

    In all topics (like this: http://ninjatrader.com/support/forum...ad.php?t=83380 ) NT Support describes how to do that. However, that's theorical explanation, and can anyone give show the real example of code, how to do that?

    #2
    Hello selnomeria,

    Below I am including some publicly available links to the help guide.

    An example would be:
    Code:
    protected override void OnStateChange()
    {
        if (State == State.DataLoaded)
        {
            AddChartIndicator(SMA(20));
        }
    }
    http://ninjatrader.com/support/helpG...tindicator.htm

    If you are trying to add additional series, these cannot reference the primary series.

    Code:
    AddDataSeries(BarsPeriodType.Minute, 5);
    http://ninjatrader.com/support/helpG...dataseries.htm

    From the help guide:
    "Arguments supplied to AddDataSeries() should be hardcoded and NOT dependent on run-time variables which cannot be reliably obtained during State.Configure (e.g., Instrument, Bars, or user input). Attempting to add a data series dynamically is NOT guaranteed and therefore should be avoided. Trying to load bars dynamically may result in an error similar to: Unable to load bars series. Your NinjaScript may be trying to use an additional data series dynamically in an unsupported manner."
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Multi-Timeframe Instances of Same Indicator

      Originally posted by NinjaTrader_ChelseaB View Post
      Hello selnomeria,

      Below I am including some publicly available links to the help guide.

      An example would be:
      Code:
      protected override void OnStateChange()
      {
          if (State == State.DataLoaded)
          {
              AddChartIndicator(SMA(20));
          }
      }
      http://ninjatrader.com/support/helpG...tindicator.htm

      If you are trying to add additional series, these cannot reference the primary series.

      Code:
      AddDataSeries(BarsPeriodType.Minute, 5);
      http://ninjatrader.com/support/helpG...dataseries.htm

      From the help guide:
      "Arguments supplied to AddDataSeries() should be hardcoded and NOT dependent on run-time variables which cannot be reliably obtained during State.Configure (e.g., Instrument, Bars, or user input). Attempting to add a data series dynamically is NOT guaranteed and therefore should be avoided. Trying to load bars dynamically may result in an error similar to: Unable to load bars series. Your NinjaScript may be trying to use an additional data series dynamically in an unsupported manner."
      Hi ChlseaB,

      Can one use the AddDataSeries to plot a second instance of an indicator for a higher time frame on the same lower time frame chart?

      For example, I have a simple 1M chart with ATR(14) plotted in panel 2. How can one plot a 5-minute ATR(14) superimposed on panel 2? Can the AddDataSeries be used here? Is there a sample indicator?

      I know this is possible by plotting a supper imposed instance of a transparent 5M chart, then plot the ATR for the 5M chart. But, that is not what I am asking.

      Many thanks.

      Comment


        #4
        Hello aligator,

        Yes, you can add a series using a different time frame and use this for the input series of an indciator call and then add this to a chart with AddChartIndicator().

        Below is a public link to an example of using an added series to an indicator.
        https://ninjatrader.com/support/foru...ead.php?t=3572
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_ChelseaB View Post
          Hello aligator,

          Yes, you can add a series using a different time frame and use this for the input series of an indciator call and then add this to a chart with AddChartIndicator().

          Below is a public link to an example of using an added series to an indicator.
          https://ninjatrader.com/support/foru...ead.php?t=3572
          Thank you so much ChelseaB,

          The script is for a strategy and I only want indicators, but it got me started with this Multi_SMA indicator. It compiles but it will draw the two SMAs at zero. I am new to AddDatSeries, what is missing here please?

          Code:
          namespace NinjaTrader.NinjaScript.Indicators
          {
          	public class SMAMulti : Indicator
          	{
          		private Series<double> sma1;
          		private Series<double> sma2;
          
          		protected override void OnStateChange()
          		{
          			if (State == State.SetDefaults)
          			{
          				Description					= @"Indicator will draw a secondarary sma for a different (i.e.5-min) timeframe";
          				Name						= "SMAMulti";
          				IsOverlay					= true;
          				IsSuspendedWhileInactive	= true;
          				Period						= 14;
          				AddPlot(new Stroke(Brushes.Blue, 2), PlotStyle.Line, "sma1");					
          				AddPlot(new Stroke(Brushes.Red, 4), PlotStyle.Line, "sma2");					
          			}
          			else if (State == State.Configure)
          			{
          				AddDataSeries(BarsPeriodType.Minute, 5);
          			}
          			
          			else if (State == State.DataLoaded)
          			{
          				// Syncs a DataSeries object to the primary bar object
          				sma1 = new Series<double>(SMA(BarsArray[0], Period));
          				sma2 = new Series<double>(SMA(BarsArray[1], Period));
          			}			
          		}
          
          		protected override void OnBarUpdate()
          		{
          			if (CurrentBar < Period)
          				return;
          			
          				Value[0] = sma1[0];	
          				Value[1] = sma2[0];
          		}
          
          		#region Properties
          		[Range(1, int.MaxValue), NinjaScriptProperty]
          		[Display(ResourceType = typeof(Custom.Resource), Name = "Period", GroupName = "Parameters", Order = 0)]
          		public int Period
          		{ get; set; }
          		#endregion
          	}
          }
          Many thanks.

          Comment


            #6
            Hello aligator,

            I can see where this example may have confused you as it creates its own series.

            This example should be better.
            https://ninjatrader.com/support/foru...d.php?t=109177

            You would need to call the SMA and pass the secondary series as the input series and assign the return indicator to a variable. Then call AddChartIndicator using that variable.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Originally posted by NinjaTrader_ChelseaB View Post
              Hello aligator,

              I can see where this example may have confused you as it creates its own series.

              This example should be better.
              https://ninjatrader.com/support/foru...d.php?t=109177

              You would need to call the SMA and pass the secondary series as the input series and assign the return indicator to a variable. Then call AddChartIndicator using that variable.
              ChelseaB,

              Fantastic, as always great support!

              Many thanks.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Atilla, Today, 04:05 PM
              0 responses
              4 views
              0 likes
              Last Post Atilla
              by Atilla
               
              Started by Displacer, Today, 02:57 PM
              2 responses
              13 views
              0 likes
              Last Post Displacer  
              Started by SAILIKITY, 05-22-2019, 05:41 PM
              1 response
              8 views
              0 likes
              Last Post NinjaTrader_PaulH  
              Started by rmillar, 01-09-2019, 06:02 PM
              6 responses
              70 views
              1 like
              Last Post eDanny
              by eDanny
               
              Started by mlarocco, Today, 11:39 AM
              4 responses
              22 views
              0 likes
              Last Post mlarocco  
              Working...
              X