• 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

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)
    DierkNinjaTrader Customer Service

    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.
        DierkNinjaTrader Customer Service

        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 Teebone21, Today, 10:38 AM
            0 responses
            6 views
            0 likes
            Last Post Teebone21  
            Started by J_o_s, 03-30-2020, 12:29 AM
            2 responses
            15 views
            0 likes
            Last Post J_o_s
            by J_o_s
             
            Started by ptpatrader, Today, 10:27 AM
            0 responses
            4 views
            0 likes
            Last Post ptpatrader  
            Started by albertokeira, Today, 09:30 AM
            0 responses
            8 views
            0 likes
            Last Post albertokeira  
            Started by U0000999, Today, 05:44 AM
            0 responses
            10 views
            0 likes
            Last Post U0000999  
            Working...
            X