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

Color Plot Problem on Modified CCI Indicator

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

    Color Plot Problem on Modified CCI Indicator

    I modified the CCI indicator, creating an indicator I call CCIcolored1, in an attempt to make it display in a different color when overbought or over sold.

    It almost works, but there are gaps between the green and orange colored plots (see attachment.)

    The modified indicator code looks like this:

    #region Using declarations
    using System;
    using System.Diagnostics;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.ComponentModel;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Data;
    using NinjaTrader.Gui.Chart;
    #endregion

    // This namespace holds all indicators and is required. Do not change it.
    namespace NinjaTrader.Indicator
    {
    /// <summary>
    /// The Commodity Channel Index (CCI) measures the variation of a security's price from its statistical mean. High values show that prices are unusually high compared to average prices whereas low values indicate that prices are unusually low.
    /// </summary>
    [Description("The Commodity Channel Index (CCI) measures the variation of a security's price from its statistical mean. High values show that prices are unusually high compared to average prices whereas low values indicate that prices are unusually low.")]
    [Gui.Design.DisplayName("CCIcolored1 (Commodity Channel Index)")]
    public class CCIcolored1 : Indicator
    {
    #region Variables
    // Wizard generated variables
    private int period = 14; // Default setting for Period
    // User defined variables (add any user defined variables below)
    #endregion

    /// <summary>
    /// This method is used to configure the indicator and is called once before any bar data is loaded.
    /// </summary>
    protected override void Initialize()
    {
    Add(new Plot(Color.Orange, PlotStyle.Line, "CCIorange"));
    Add(new Plot(Color.Green, PlotStyle.Line, "CCIgreen"));
    Add(new Line(Color.DarkGray, 200, "Level2"));
    Add(new Line(Color.DarkGray, 100, "Level1"));
    Add(new Line(Color.DarkGray, 0, "ZeroLine"));
    Add(new Line(Color.DarkGray, -100, "LevelM1"));
    Add(new Line(Color.DarkGray, -200, "LevelM2"));
    Period = 14;
    CalculateOnBarClose = true;
    Overlay = false;
    PriceTypeSupported = true;
    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    if (CurrentBar == 0)
    Value.Set(0);
    else
    {
    double mean = 0;
    for (int idx = Math.Min(CurrentBar, Period - 1); idx >= 0; idx--)
    mean += Math.Abs(Typical[idx] - SMA(Typical, Period)[0]);
    double plotval = (Typical[0] - SMA(Typical, Period)[0])
    / (mean == 0 ? 1 : (0.015 * (mean / Math.Min(Period, CurrentBar + 1))));
    int val_idx = (Math.Abs(plotval) >= 100) ? 1 : 0; // Select Green plot color for oversold (<-100)
    // or overbought (>+100).
    Values[ val_idx].Set( plotval ); // Plot a point on the chart, using Orange (Values[0]) or Green (Values[1]).
    }
    }

    #region Properties
    [Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
    [XmlIgnore()] // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
    public DataSeries CCIorange
    {
    get { return Values[0]; }
    }

    [Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
    [XmlIgnore()] // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
    public DataSeries CCIgreen
    {
    get { return Values[1]; }
    }

    [Description("Numbers of bars used for calculations")]
    [Category("Parameters")]
    public int Period
    {
    get { return period; }
    set { period = Math.Max(1, value); }
    }
    #endregion
    }
    }
    If I change the Values array index (val_idx) to either zero or one, it displays a solid line of one color, but when its set to toggle back and forth (as above), it displays with gaps.

    Any ideas?

    KBJ
    Attached Files

    #2
    imported post

    Right, this is expected behaviour.

    Check the docs for a sample on how to achieve what you are looking for: NinjaScript->Developing custom indicators->Tutorial (Level5)

    Comment


      #3
      imported post

      I've changed the OnBarUpdate to follow the example, and it looks like this:


      protected override void OnBarUpdate()
      {
      double mean = 0;
      for (int idx = Math.Min(CurrentBar, Period - 1); idx >= 0; idx--)
      mean += Math.Abs(Typical[idx] - SMA(Typical, Period)[0]);

      double plotval = (Typical[0] - SMA(Typical, Period)[0])
      / (mean == 0 ? 1 : (0.015 * (mean / Math.Min(Period, CurrentBar + 1))));

      if (Math.Abs(plotval) >= 100) // Select Green plot color for oversold (<-100) or overbought (>+100).
      CCIgreen.Set( plotval ); // Green for oversold or overbought.
      else
      CCIorange.Set( plotval ); // Orange for NOT oversold or overbought.
      }
      However, it still displays the same.

      I'm still a little green with the C# syntax, so I hope I'm not making some incredibly stupid blunder here, but aren't these two implementations equivalent?

      KBJ

      Comment


        #4
        imported post

        Please follow the tutorial below closely and make sure you don't skip important parts like setting the Min/Max values in the Initialize method.

        Comment


          #5
          imported post

          I did what you said, and it worked after I added the missing statements.

          It was late when I was working on this and just didn't read far enough in the help. I saw the "Set Up" page which ended with the code that's generated for the Initialize() and OnBarUpdate() methods and didn't look at the next page ("Entering Calculation Logic") which explains about adding values for Plots[0].Min and Plots[0].Max, etc. I'm embarassed because this is one of the better sections of the help and I completely missed it.

          Oh, and if I'd looked a couple of pages further, the next example (#6) implements what I was trying to do, plus a little.

          Thanks for not singeing the egg on my face.

          KBJ

          Comment


            #6
            imported post

            No worries KBJ, that's what we are here for...pointing you in the right direction.

            Ray
            RayNinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by cre8able, Today, 03:20 PM
            0 responses
            5 views
            0 likes
            Last Post cre8able  
            Started by Fran888, 02-16-2024, 10:48 AM
            3 responses
            47 views
            0 likes
            Last Post Sam2515
            by Sam2515
             
            Started by martin70, 03-24-2023, 04:58 AM
            15 responses
            114 views
            0 likes
            Last Post NinjaTrader_Jesse  
            Started by The_Sec, Today, 02:29 PM
            1 response
            7 views
            0 likes
            Last Post NinjaTrader_Jesse  
            Started by jeronymite, 04-12-2024, 04:26 PM
            2 responses
            31 views
            0 likes
            Last Post NinjaTrader_BrandonH  
            Working...
            X