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

Looking for simple example of plotting two EMA's from a higher time frame on chart.

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

    Looking for simple example of plotting two EMA's from a higher time frame on chart.

    HI,

    I've seen several posts relating to this topic and not being a professional developer I've spent the last few days trying to figure this out and finally gave up.


    I'm using a shorter time frame 1 to 5 minutes and also experimenting with non time based chart types as I like the natural smoothing that occurs with price data during inactive market periods.

    I just like to be able for example to create a few has plots on the short term chart I do my trading from with a EMA from a 15 and 30 minute chart.

    I tried using the strategy builder specifying two additional data series for the 15 and 30 but doesn't appear that I can change the plot/brush characteristics.

    I thought this could be done by adding additional data series to a base chart but I need to be able to run it through the strategy analyzer, back test and eventually automate.

    Any help would be appreciated the simpler the example the better, I realize how busy you all are and don't want anyone to spend a lot of time on my request.

    As always thank you all so much for you help, people with less years in the business than I have don't know how fortunate they are to have the world class support you all provide.

    If there are doubts, try call ANY other vendor for assistance.

    thank you,

    glen

    #2
    Hello Glen,

    Thanks for your post.

    In the strategy builder that is correct that you can add a higher/ different time frame and can get the values of the moving averages based on those higher/different times but you cannot plot those indicators when they do not use the timeframe of the chart. This would be a strategy builder limitation.

    In a nutshell, you would end up creating a multi time frame indicator to accomplish your goals where you would hard code the added data series, like this:

    else if (State == State.Configure)
    {
    AddDataSeries("", BarsPeriodType.Minute, 15);
    }


    In the OnBarUpdate() you would do something like:

    if (CurrentBars[1] < 10) return; // make sure enough higher time frame bars

    if (BarsInProgress != 0) return; // only plot when chart bars plot

    myPlot[0] = EMA(Closes[1], Period)[0]; // plot the current bar value of the 15 minute EMA (Period is a variable you would create and set as needed). Closes[1] points to the 15 minute series

    // myPlot would be a plot that you have created/added.

    References:

    This link explains everything for Multi time frame: https://ninjatrader.com/support/help...nstruments.htm


    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Paul H,

      Thank for the response and I apologize for the dumb questions but I'm new to C# and getting up to speed closly.

      I have two higher level time frames what I need to define.

      Working with the example I'm getting errors on myplot, can you please tell me how that is to be defined and does that have to be added to the chart with the plotting also setup explicitly.

      thank you,

      glen

      Comment


        #4
        Hello Glen,

        Thanks for your reply.

        While we want to help we have to thread the needle between writing the complete code for you and writing just enough to give you an idea to then learn from. Please make sure you are reviewing the help guide links provided to gain a better understanding.

        To add the second data series would be the same as I showed for the first, except that you would set it to 30 minute.

        You would need to add current bars check to ensure that you have some bars loaded on the 2nd added data series. Please see the example here: https://ninjatrader.com/support/help...urrentbars.htm

        You would need to use AddPlot() twice, one for each EMA and you can name it anything you wish. See the help guide link in post #2 In my example I used myPlot but again you can call it what you wish.

        The plots you create in the indicator will be shown on the chart when you add the indicator.

        When you assign the values for the 2nd plot it would be the same except it would point to the 2nd added series. Here is what both could look like:

        myPlot[0] = EMA(Closes[1], Period)[0]; // plot the current bar value of the 15 minute EMA (Period is a variable you would create and set as needed). Closes[1] points to the 15 minute series

        my2ndPlot[0] = EMA(Closes[2], Period2)[0]; // plot the current bar value of the 30 minute EMA (Period2 is a variable you would create and set as needed). Closes[2] points to the 30 minute series

        I'm not sure how you are creating your indicator but I would recommend using the indicator wizard as it will create much of the structure you need. Here is a short video I made relative to your indicator: https://Paul-ninjaTrader.tinytake.co...N18xODc1ODc3Ng

        Here is a link to the Ninjascript wizard: https://ninjatrader.com/support/help...?ns_wizard.htm

        Note: Calling indicators, like EMA, in OnBarUpdate() will work fine however it is not considered as our best practice. I am showing it in OnBarUpdate() to make this an easier learning process.
        Paul H.NinjaTrader Customer Service

        Comment


          #5
          Paul,

          I very much realize the "threading the needle" issue you mentioned and appreciate the help provided.

          I do apologize and struggled for two days and had given up so than you again.

          Thank you so much for the video on the indicator wizard. I was aware of the strategy builder but not aware of the indicator wizard.

          That video was extremely helpful thank you so much for taking the time and effort from your busy day to provide that for me.

          You guys are truly the best in the business.

          glen

          Comment


            #6
            Hi Paulh,

            Because I eventually want to make this part of a strategy I used the strategy wizard but received a compiler on one of the using declarations?

            Click image for larger version

Name:	compilererror.PNG
Views:	615
Size:	803.4 KB
ID:	1184800

            Thank you.

            Comment


              #7
              Hello Glen,

              Thanks for your reply.

              You can use the indicator in a strategy but since the indicator calls two added dataseries, the strategy will have to also call those two data series so the indicator can work because the strategy becomes the host and data provider to indicators used in the strategy.

              The error message is not related. Any error will prevent the compiler from completing successfully which also prevents using the Strategy builder until the errors have been cleared through a successful compilation. The error is caused by using Visual studio inappropriately. The cause is related to launching visual studio from NS Editor, and at some point having clicked Start in VS, in both release and debug mode, which creates an Assembly Attributes CS, that causes the error.

              This can be resolved by deleting the release folder in Documents\NinjaTrader 8\bin\Custom\obj\release.

              Paul H.NinjaTrader Customer Service

              Comment


                #8
                PaulH,

                Thank you for the response. I did install Visual Studio but never invoked VS from the Ninjascript Editor. I realize VS should not be used for compiling any NT components.
                I did delete the file you mentioned and still getting the error. I may just delete the strategy and rebuild it as an indicator.

                Thank you again for your help.

                glen

                Comment


                  #9
                  This is the old topic but I thought I'd provide an example for someone who is still looking for it like I was ...

                  in my code I define EMAs first, then assign them values OnStateChange (which could be dynamically set from variable, I've just hardcoded them while working on a strategy). and finally just assign values from EMAs to the plot.

                  Code:
                  namespace NinjaTrader.Custom.Strategies
                  {
                      public class ScalpingWithTheTrend : Strategy
                      {
                          private EMA EMA1;
                          private EMA EMA2;
                  
                  
                          protected override void OnStateChange()
                          {
                              if (State == State.SetDefaults)
                              {
                                  Description = @"Trend following along the fast MA";
                                  Name = "Scalping With The Trend";
                  
                   // Add two plots and associated Series<double> objects
                                  AddPlot(Brushes.LightYellow, "PlotA");     // Defines the plot for Values[0]
                                  AddPlot(Brushes.DeepSkyBlue, "PlotB");     // Defines the plot for Values[1]
                  
                              }
                              else if (State == State.Configure)
                              {
                              }
                              else if (State == State.DataLoaded)
                              {
                                  EMA1 = EMA(Close, 20);
                                  EMA2 = EMA(Close, 50);
                                  SetStopLoss(@"Long", CalculationMode.Ticks, InitialStopLoss, false);
                                  SetStopLoss(@"Short", CalculationMode.Ticks, InitialStopLoss, false);
                              }
                          }
                  
                          protected override void OnBarUpdate()
                          {
                              if (BarsInProgress != 0)
                                  return;
                  
                              if (CurrentBars[0] < 10)
                                  return;
                  
                              Values[0][0] = EMA1[0];   // "Plot A"
                              Values[1][0] = EMA2[0];   // "Plot B"​
                  result:

                  Click image for larger version  Name:	Screenshot 2023-10-29 212437.png Views:	0 Size:	120.4 KB ID:	1275367

                  xcoder
                  NinjaTrader Ecosystem Vendor - EMS

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by lightsun47, Today, 03:51 PM
                  0 responses
                  5 views
                  0 likes
                  Last Post lightsun47  
                  Started by 00nevest, Today, 02:27 PM
                  1 response
                  9 views
                  0 likes
                  Last Post 00nevest  
                  Started by futtrader, 04-21-2024, 01:50 AM
                  4 responses
                  45 views
                  0 likes
                  Last Post futtrader  
                  Started by Option Whisperer, Today, 09:55 AM
                  1 response
                  14 views
                  0 likes
                  Last Post bltdavid  
                  Started by port119, Today, 02:43 PM
                  0 responses
                  9 views
                  0 likes
                  Last Post port119
                  by port119
                   
                  Working...
                  X