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

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.



    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].


    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.
    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, 01: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 DJ888, 04-16-2024, 06:09 PM
            6 responses
            18 views
            0 likes
            Last Post DJ888
            by DJ888
             
            Started by Jon17, Today, 04:33 PM
            0 responses
            1 view
            0 likes
            Last Post Jon17
            by Jon17
             
            Started by Javierw.ok, Today, 04:12 PM
            0 responses
            6 views
            0 likes
            Last Post Javierw.ok  
            Started by timmbbo, Today, 08:59 AM
            2 responses
            10 views
            0 likes
            Last Post bltdavid  
            Started by alifarahani, Today, 09:40 AM
            6 responses
            41 views
            0 likes
            Last Post alifarahani  
            Working...
            X