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 timeframe ema

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

    #16
    Originally posted by NinjaTrader_Kate View Post
    Hello chartish,

    Thank you for your post.

    You could do this in one of two ways. One would be to use a secondary data series on the chart and simply have the EMA calculate on that secondary series, but apply it to plot over your primary data series.

    To add an indicator based off of a 5 minute chart, plotted over a 2 minute chart, please follow the instructions below:

    * First create a 2 minute chart as usual (File>New>Chart...)
    * Right click within your chart window and select Data Series...
    * Add a second data series, the same instrument as your 2 minute chart, but set the parameters to be a 5 minute chart
    * You should now see two-paneled chart with 2 minute on top and 5 minute below
    * Next, right click and select Indicators... Find your indicator in the top left hand corner, and click New to add it.
    * In the parameters to the right, set your Input Series to your 5 minute data series (Close)
    * Set your Panel to 1,and click OK
    * Next, move your pointer to the right of your price axis (y-axis) on the top panel of your chart window, and right click>Maximize.

    You now should see a 2 minute chart, with an indicator plotted over it based off of a 5 minute Data series.

    Your other option would be to create an indicator as you've started doing.

    You are basically assigning the ema data series to current bar of the plot, so all you theoretically need is to change that line to this:

    Value[0] = EMA(Closes[1], 20)[0];

    However, working with multiple data series is a bit more complicated than using a single data series in an indicator.

    Here's a simple example using some better practices, including checking on the current bar, using BarsInProgress to plot on the 2 minute data points, and using a private EMA variable to hold our EMA values from the secondary series. (I've attached the full code for this below):

    private EMA EMA1; // we create a new array of EMA values

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Indicator here.";
    Name = "EMAon5minData";
    Calculate = Calculate.OnBarClose;
    IsOverlay = true;
    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;

    AddPlot(Brushes.LightCoral, "EMAPlot");
    }
    else if (State == State.Configure)
    {
    AddDataSeries(Data.BarsPeriodType.Minute, 5); //add our secondary 5 minute data series for calculating the EMA
    }
    else if (State == State.DataLoaded)
    {
    EMA1 = EMA(BarsArray[1], 20); //set EMA here so we make sure it's calculated on the secondary data series
    }
    }

    protected override void OnBarUpdate()
    {
    if (CurrentBars[0] < 1 || CurrentBars[1] < 20) // make sure there's at least one bar for primary series and at least 20 of the secondary series prior to processing
    return;

    if(BarsInProgress == 0) // if OnBarUpdate was called from the primary bar series, then set the current value to the latest EMA1 value
    Value[0] = EMA1[0];
    }

    If I may be of further assistance, please let me know.
    Thank you Kate, I was searching this exactly!

    Comment


      #17
      Hi,

      How do i get access to a secondary dataseries, ie Values[2] from the SMA object

      //class level variable
      private SMA mySMA;

      //State.Configure
      AddDataSeries(BarsPeriodType.Minute, 15);

      //State.DataLoaded
      mySMA = SMA.SecondarySeries(BarsArray[1], 20);
      // how do get Values[2][0] from the SMA object?

      Edit : I worked that out with the following;
      Code:
      // Global Objects
      private Indicator _Ex5min
      private Series<double> Bar_info_5m;
      ​
      else if (State == State.DataLoaded)
      {
      var Ex5min = ExIndicator();// 1. add the indicator to a local varable : its calculations are all based of 5 minute data series
      Bar_info_5m = Ex5min.Series; // 2. access the series from the ind. & send it to a global series
      _Ex5min = Ex5min; // 3. Now store the indicator in a global indicator object
      ​}
      But, now the data i receive from the indicator is not correct;

      The indicator or 'class' that i built (that i'm trying to import) has all of its calculations based on the 5m dataseries.
      > if i load it directly of a 5m chart to test, it works well on a 5minute chart.
      But... when i change it to primary bars array to a 1 minute , then the indicator's values distort.

      That same problem is occurring, when, when i import it into the new script & display it on a 1 minute chart.

      * since all of the calculations are based on the 5 minute data series, i was hoping the timeframe i display it on would be irrelevant & not distort the calculations.

      Is there a known solution for this?
      Thanks.​
      Last edited by yertle; 07-11-2024, 11:55 AM.

      Comment


        #18
        Hi,
        You can check again the post #2, the second method could be useful for you. Values[2] is not used in this method but you have the same output parameters in OnBarUpdate() code.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by angelgrace11, Today, 08:10 PM
        0 responses
        4 views
        0 likes
        Last Post angelgrace11  
        Started by cryfgg, Today, 05:25 PM
        0 responses
        9 views
        0 likes
        Last Post cryfgg
        by cryfgg
         
        Started by xiinteractive, 02-02-2023, 04:25 PM
        6 responses
        242 views
        0 likes
        Last Post xiinteractive  
        Started by cryfgg, 12-01-2023, 10:12 AM
        7 responses
        123 views
        0 likes
        Last Post Rxxar
        by Rxxar
         
        Started by RayHouston, Today, 04:45 PM
        0 responses
        13 views
        0 likes
        Last Post RayHouston  
        Working...
        X