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

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 == [B]State.DataLoaded[/B])
        {
            AddChartIndicator(SMA(20));
        }
    }


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

    Code:
    AddDataSeries(BarsPeriodType.Minute, 5);


    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 == [B]State.DataLoaded[/B])
          {
              AddChartIndicator(SMA(20));
          }
      }


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

      Code:
      AddDataSeries(BarsPeriodType.Minute, 5);


      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.
        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<T> (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
        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.


            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.


              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 timmbbo, Today, 08:59 AM
              1 response
              2 views
              0 likes
              Last Post NinjaTrader_ChelseaB  
              Started by KennyK, 05-29-2017, 02:02 AM
              2 responses
              1,281 views
              0 likes
              Last Post marcus2300  
              Started by fernandobr, Today, 09:11 AM
              0 responses
              2 views
              0 likes
              Last Post fernandobr  
              Started by itrader46, Today, 09:04 AM
              1 response
              6 views
              0 likes
              Last Post NinjaTrader_Clayton  
              Started by bmartz, 03-12-2024, 06:12 AM
              5 responses
              33 views
              0 likes
              Last Post NinjaTrader_Zachary  
              Working...
              X