Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

DrawRegion nightmare

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

    DrawRegion nightmare

    I am using NT 7 build 21 and I cannot get DrawRegion to work correctly. I am getting some weird results, including the opacity of the region changing by itself and some really inconsistent results.
    Basically I have two dataseries, each dataseries is defined with maximumbarslookback set to infinite. When Series1 crosses over Series 2 I would like for the area to be blue, and when Series 1 crosses below Series 2 I would like for the area to be red. A new cross over should only change the color of the area looking forward, not the color of the previous regions that were drawn. Can you guys help??
    Thanks!

    #2
    trader2be, can you please share the code you use so we can look into here?

    Thanks

    Comment


      #3
      Here is the code below

      #region Using declarations
      using System;
      using System.ComponentModel;
      using System.Diagnostics;
      using System.Drawing;
      using System.Drawing.Drawing2D;
      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>
      /// Enter the description of your new custom indicator here
      /// </summary>
      [Description("Enter the description of your new custom indicator here")]
      public class MyCustomIndicator : Indicator
      {
      #region Variables
      // Wizard generated variables
      DataSeries Serie1, Serie2;

      // 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.FromKnownColor(KnownColor.WindowText), PlotStyle.Line, "Plot0"));
      Add(new Plot(Color.FromKnownColor(KnownColor.WindowText), PlotStyle.Line, "Plot1"));
      Overlay = false;
      Serie1 = new DataSeries(this);
      Serie2 = new DataSeries(this);
      }

      /// <summary>
      /// Called on each bar update event (incoming tick)
      /// </summary>
      protected override void OnBarUpdate()
      {
      // Use this method for calculating your indicator values. Assign a value to each
      // plot below by replacing 'Close[0]' with your own formula.
      Serie1.Set(TSF(Input,0,20)[0]);
      Serie2.Set(EMA(TSF(Input,0,20),5)[0]);
      Plot0.Set(Serie1[0]);
      Plot1.Set(Serie2[0]);
      if (Serie1[0] >= Serie2[0]) DrawRegion("CrossBelow",CurrentBar,0,Values[0],Values[1],Color.Transparent,Color.Black,4);
      if (Serie1[0] < Serie2[0]) DrawRegion("CrossAbove",CurrentBar,0,Values[0],Values[1],Color.Transparent,Color.Yellow,4);

      }

      #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 Plot0
      {
      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 Plot1
      {
      get { return Values[1]; }
      }
      #endregion
      }
      }

      #region NinjaScript generated code. Neither change nor remove.
      // This namespace holds all indicators and is required. Do not change it.
      namespace NinjaTrader.Indicator
      {
      public partial class Indicator : IndicatorBase
      {
      private MyCustomIndicator[] cacheMyCustomIndicator = null;

      private static MyCustomIndicator checkMyCustomIndicator = new MyCustomIndicator();

      /// <summary>
      /// Enter the description of your new custom indicator here
      /// </summary>
      /// <returns></returns>
      public MyCustomIndicator MyCustomIndicator()
      {
      return MyCustomIndicator(Input);
      }

      /// <summary>
      /// Enter the description of your new custom indicator here
      /// </summary>
      /// <returns></returns>
      public MyCustomIndicator MyCustomIndicator(Data.IDataSeries input)
      {
      if (cacheMyCustomIndicator != null)
      for (int idx = 0; idx < cacheMyCustomIndicator.Length; idx++)
      if (cacheMyCustomIndicator[idx].EqualsInput(input))
      return cacheMyCustomIndicator[idx];

      lock (checkMyCustomIndicator)
      {
      if (cacheMyCustomIndicator != null)
      for (int idx = 0; idx < cacheMyCustomIndicator.Length; idx++)
      if (cacheMyCustomIndicator[idx].EqualsInput(input))
      return cacheMyCustomIndicator[idx];

      MyCustomIndicator indicator = new MyCustomIndicator();
      indicator.BarsRequired = BarsRequired;
      indicator.CalculateOnBarClose = CalculateOnBarClose;
      #if NT7
      indicator.ForceMaximumBarsLookBack256 = ForceMaximumBarsLookBack256;
      indicator.MaximumBarsLookBack = MaximumBarsLookBack;
      #endif
      indicator.Input = input;
      Indicators.Add(indicator);
      indicator.SetUp();

      MyCustomIndicator[] tmp = new MyCustomIndicator[cacheMyCustomIndicator == null ? 1 : cacheMyCustomIndicator.Length + 1];
      if (cacheMyCustomIndicator != null)
      cacheMyCustomIndicator.CopyTo(tmp, 0);
      tmp[tmp.Length - 1] = indicator;
      cacheMyCustomIndicator = tmp;
      return indicator;
      }
      }
      }
      }

      // This namespace holds all market analyzer column definitions and is required. Do not change it.
      namespace NinjaTrader.MarketAnalyzer
      {
      public partial class Column : ColumnBase
      {
      /// <summary>
      /// Enter the description of your new custom indicator here
      /// </summary>
      /// <returns></returns>
      [Gui.Design.WizardCondition("Indicator")]
      public Indicator.MyCustomIndicator MyCustomIndicator()
      {
      return _indicator.MyCustomIndicator(Input);
      }

      /// <summary>
      /// Enter the description of your new custom indicator here
      /// </summary>
      /// <returns></returns>
      public Indicator.MyCustomIndicator MyCustomIndicator(Data.IDataSeries input)
      {
      return _indicator.MyCustomIndicator(input);
      }
      }
      }

      // This namespace holds all strategies and is required. Do not change it.
      namespace NinjaTrader.Strategy
      {
      public partial class Strategy : StrategyBase
      {
      /// <summary>
      /// Enter the description of your new custom indicator here
      /// </summary>
      /// <returns></returns>
      [Gui.Design.WizardCondition("Indicator")]
      public Indicator.MyCustomIndicator MyCustomIndicator()
      {
      return _indicator.MyCustomIndicator(Input);
      }

      /// <summary>
      /// Enter the description of your new custom indicator here
      /// </summary>
      /// <returns></returns>
      public Indicator.MyCustomIndicator MyCustomIndicator(Data.IDataSeries input)
      {
      if (InInitialize && input == null)
      throw new ArgumentException("You only can access an indicator with the default input/bar series from within the 'Initialize()' method");

      return _indicator.MyCustomIndicator(input);
      }
      }
      }
      #endregion

      Comment


        #4
        This is unfortunately expected : you can't turn a drawn region on / off, you essentially color and recolor the same region then based on your condition for all the bars on the chart (0 to CurrentBar as per your code).

        Comment


          #5
          So, is there a way to change the color of the region based on the conditions that I have specified?

          Comment


            #6
            Please try this code change below -

            Code:
            if (Serie1[0] > Serie2[0]) DrawRegion("DR1" + CurrentBar,1,0,Values[0],Values[1],Color.Transparent,Color.Black,5);
            	else DrawRegion("DR2" + CurrentBar,1,0,Values[0],Values[1],Color.Transparent,Color.Yellow,5);

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            569 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            330 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            101 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            548 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            548 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X