• 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

Adding histogram to an indicator to show differences between two lines

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

    Adding histogram to an indicator to show differences between two lines

    I'd like to add a histogram plot to the PPO indicator that plots the differences between the fast EMA and slow EMA. I'm not familiar with Ninjascript and the MACD indicator code is set up differently than the PPO indicator code, so it isn't as simple as copying over a few lines of code. Any help is appreciated!

    This is the PPO indicator code:
    Code:
        public class PPO : Indicator
        {
            private EMA emaFast;
            private EMA emaSlow;
    
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                    = NinjaTrader.Custom.Resource.NinjaScriptIndicatorDescriptionPPO;
                    Name                        = NinjaTrader.Custom.Resource.NinjaScriptIndicatorNamePPO;
                    IsSuspendedWhileInactive    = true;
                    Fast                        = 12;
                    Slow                        = 26;
                    Smooth                        = 9;
    
                    AddPlot(Brushes.DimGray,        NinjaTrader.Custom.Resource.NinjaScriptIndicatorDefault);
                    AddPlot(Brushes.Crimson,        NinjaTrader.Custom.Resource.PPOSmoothed);
                    AddLine(Brushes.DarkGray,    0,    NinjaTrader.Custom.Resource.NinjaScriptIndicatorZeroLine);
                }
                else if (State == State.DataLoaded)
                {
                    emaFast = EMA(Fast);
                    emaSlow = EMA(Slow);
                }
            }
    
            protected override void OnBarUpdate()
            {
                double emaSlow0        = emaSlow[0];
                Default[0]             = 100 * ((emaFast[0] - emaSlow0) / emaSlow0);
                Smoothed[0]            = EMA(Values[0], Smooth)[0];
            }
    I need a plot for the histogram, so I copied this from the MACD code:
    Code:
    AddPlot(new Stroke(Brushes.DodgerBlue, 2),    PlotStyle.Bar,    NinjaTrader.Custom.Resource.NinjaScriptIndicatorDiff);
    Not sure what additions need to be made to the OnBarUpdate section.

    #2
    Hello dolomite,

    You will first need to make a copy of the PPO indicator with a new unique name. Open the script -> right-click in the code -> select Save As -> type a new unique name -> click OK.

    AddPlot() does add a plot and the PlotStyle.Bar is what makes this a histogram. This code from the MACD is useful and should be added in your new script in OnStateChange() in State.SetDefaults.
    https://ninjatrader.com/support/help...t8/addplot.htm


    Each plot adds a series to the Values collection. This uses two indexes the series index and the barsAgo index. Values[plotSeries index][barsAgo index].
    https://ninjatrader.com/support/help...nt8/values.htm

    Values[3] would be the series for the 4th call to AddPlot() which would be your series.
    Values[3][0] would be the most recent bar value of that series.

    The emaFast current bar's value is emaFast[0].

    The code would appear as:
    Values[3][0] = emaFast[0] - emaSlow[0];

    And would be placed in OnBarUpdate().

    Below is a link to a forum post with helpful information about getting started with NinjaScript.
    https://ninjatrader.com/support/foru...040#post786040
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thanks Chelsea. Got it working.
      Originally posted by NinjaTrader_ChelseaB View Post
      Each plot adds a series to the Values collection. This uses two indexes the series index and the barsAgo index. Values[plotSeries index][barsAgo index].
      https://ninjatrader.com/support/help...nt8/values.htm

      Values[3] would be the series for the 4th call to AddPlot() which would be your series.
      Values[3][0] would be the most recent bar value of that series.

      The emaFast current bar's value is emaFast[0].

      The code would appear as:
      Values[3][0] = emaFast[0] - emaSlow[0];
      Thanks for the explanation on how plots are connected to the Values collection.

      In this case, there are only 3 calls to AddPlot() (one of the calls is to AddLine()). Also, I want the histogram to show the difference between the PPO line (which is the difference in EMAs) and the smoothed PPO line. So the code I needed to add to OnBarUpdate() is:
      Code:
      Values[2][0] = Default[0] - Smoothed[0];

      Originally posted by NinjaTrader_ChelseaB View Post
      I noticed the links to topics in the help guide, such as the one you posted, and other similar links I see NT support posting to the forum, don't land on the correct page, but instead, all go to https://ninjatrader.com/support/helpGuides/nt8/

      Not sure if this is just on my end, or if everyone outside of NT's network is running into this issue.

      Thanks again!
      Last edited by dolomite; 09-29-2019, 12:41 PM.

      Comment


        #4
        Hello dolomite,

        I overlooked one of was AddLine().

        So Values[2] would be the 3rd added plot series.

        I've clicked the link I've posted and this is taking me to the correct help guide page.
        I recommend you try the link in another browser.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_ChelseaB View Post
          I've clicked the link I've posted and this is taking me to the correct help guide page.
          I recommend you try the link in another browser.
          I tried it in both Chrome and Internet Explorer, with the same negative result.

          If no one else in the forum is experiencing this (and since NT Support staff frequently puts links to the Help Guide in their posts, you'd probably be hearing about it if it was a widespread issue), it must be an issue on my end.

          Comment


            #6
            Hello dolomite,

            Are you actually clicking the link?

            Or are you trying to copy the text and paste it?
            Chelsea B.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Dagamos, Today, 11:48 AM
            1 response
            9 views
            0 likes
            Last Post NinjaTrader_RyanS  
            Started by kantkant2, Yesterday, 03:20 PM
            4 responses
            24 views
            0 likes
            Last Post eDanny
            by eDanny
             
            Started by EnveousColnago, Today, 11:50 AM
            1 response
            6 views
            0 likes
            Last Post NinjaTrader_ShawnB  
            Started by Skyfly1715, 01-20-2020, 03:36 PM
            4 responses
            33 views
            1 like
            Last Post eDanny
            by eDanny
             
            Started by Cyanez, Yesterday, 05:05 PM
            1 response
            10 views
            0 likes
            Last Post NinjaTrader_PaulH  
            Working...
            X