Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How do I color a plot when rising/falling a different color?

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

    How do I color a plot when rising/falling a different color?

    Hi, I used to use color in NT7, now I can't figure out how to use the brush/brushes....

    All I am trying to do is to color the FisherTransform a different color if it is going up or down.

    Here is my attempt, I don't know why this doesn't work. Please help.

    Here is the error I am getting:
    Indicator 'MFFTCCI': Error on calling 'OnCalculateMinMax' method on bar -1: Value cannot be null.
    Parameter name: brush


    // This namespace holds indicators in this folder and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.Indicators
    {
    /// <summary>
    /// The Commodity Channel Index (MFFTCCI) 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>
    public class MFFTCCI : Indicator
    {
    private Brush downColor;
    private Brush upColor;

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = NinjaTrader.Custom.Resource.NinjaScriptIndicatorDe scriptionCCI;
    Name = "MFFTCCI";
    IsSuspendedWhileInactive = true;
    Calculate = Calculate.OnEachTick;

    // BarsRequiredToPlot = 50; // Do not plot until this bar on the chart
    CCIPeriod = 6;
    FTPeriod = 10;

    // downColor = Brushes.Red;
    // upColor = Brushes.Green;

    AddPlot(Brushes.Blue, "CCI");
    AddPlot(Brushes.Magenta, "FT");
    AddLine(Brushes.DarkGray, 200, "Level 2");
    AddLine(Brushes.DarkGray, 100, "Level 1");
    AddLine(Brushes.Black, 0, "Zero line");
    AddLine(Brushes.DarkGray, -100, "Level -1");
    AddLine(Brushes.DarkGray, -200, "Level -2");
    }
    else if (State == State.Configure)
    {
    Plots[0].Width = 1; // CCI
    Plots[1].Width = 2; // FT

    // setup for color coding the plot if it is going up or down
    Brush downColor = Plots[1].Brush.Clone();
    Brush upColor = Plots[1].Brush.Clone();
    downColor = Brushes.Red;
    upColor = Brushes.Green;
    }
    }

    protected override void OnBarUpdate()
    {
    // if (CurrentBar <= BarsRequiredToPlot)
    // {
    // Value[0] = 0;
    // FT[0] = 0;
    // }
    // else
    {
    // calc CCI
    Value[0] = CCI(CCIPeriod)[0];

    // calc FT
    FT[0] = FisherTransform (FTPeriod)[0] * 20;
    if (IsRising (FT))
    {
    Plots[1].Brush = upColor;
    } else {
    // Plots[1].Brush = downColor;
    }
    }
    }

    #region Properties
    [Browsable(false)]
    [XmlIgnore]
    public Series<double> Default
    {
    get { return Values[0]; }
    }

    [Browsable(false)]
    [XmlIgnore]
    public Series<double> FT
    {
    get { return Values[1]; }
    }

    [Range(1, int.MaxValue), NinjaScriptProperty]
    [Display(ResourceType = typeof(Custom.Resource), Name = "CCIPeriod", Description="CCI Period", GroupName = "NinjaScriptParameters", Order = 0)]
    public int CCIPeriod
    { get; set; }

    [Range(1, int.MaxValue), NinjaScriptProperty]
    [Display(ResourceType = typeof(Custom.Resource), Name = "FTPeriod", Description="Fisher Transform Period", GroupName = "NinjaScriptParameters", Order = 1)]
    public int FTPeriod
    { get; set; }

    // [Range(-200, 0), NinjaScriptProperty]
    // [Display(ResourceType = typeof(Custom.Resource), Name = "CCIBuyLevel", Description="CCI Buy Level", GroupName = "NinjaScriptParameters", Order = 2)]
    // public int CCIBuyLevel
    // { get; set; }
    //
    // [Range(1, 200), NinjaScriptProperty]
    // [Display(ResourceType = typeof(Custom.Resource), Name = "CCISellLevel", Description="CCI Sell Level", GroupName = "NinjaScriptParameters", Order = 3)]
    // public int CCISellLevel
    // { get; set; }
    #endregion
    }
    }
    Attached Files

    #2
    OK I tried out your script and played with it.

    First, you don't need to clone the Brushes from the plots in the Configure State, so you can take that out.

    Second, you will want to use PlotBrushes instead of Plots, reason being is that PlotsBrushes will change that specific time the condition was true to that color. Plots will change the entire plot to that new color.

    I also assume you wanted these colors changeable from the indicator properties so I added those in so you can see how those work as well.

    I have uploaded the new script for you
    Attached Files

    Comment


      #3
      Hi Calonious, thank you SO MUCH for the very quick response, and for fixing this for me - and teaching me about the code change. I spent hours on this.... works like a charm !

      Thanks again

      Comment


        #4
        Originally posted by DaFish View Post
        Hi Calonious, thank you SO MUCH for the very quick response, and for fixing this for me - and teaching me about the code change. I spent hours on this.... works like a charm !
        You might also want to take a look at the NT8 reference samples..



        Multi-Colored Plot example based on Rising and Falling Conditions..




        -=Edge=-
        NinjaTrader Ecosystem Vendor - High Tech Trading Analysis

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by fx.practic, 10-15-2013, 12:53 AM
        5 responses
        5,404 views
        0 likes
        Last Post Bidder
        by Bidder
         
        Started by Shai Samuel, 07-02-2022, 02:46 PM
        4 responses
        95 views
        0 likes
        Last Post Bidder
        by Bidder
         
        Started by DJ888, Yesterday, 10:57 PM
        0 responses
        8 views
        0 likes
        Last Post DJ888
        by DJ888
         
        Started by MacDad, 02-25-2024, 11:48 PM
        7 responses
        159 views
        0 likes
        Last Post loganjarosz123  
        Started by Belfortbucks, Yesterday, 09:29 PM
        0 responses
        8 views
        0 likes
        Last Post Belfortbucks  
        Working...
        X