Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

color region between moving averages.

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

    color region between moving averages.



    people with nt,



    i have been working on an indicator which includes several pairs of moving averages.



    if ((Ema01[0]) > (Ema02[0]))
    {
    PlotBrushes[0][0] = Brushes.Purple;
    }
    else if ((Ema01[0]) < (Ema02[0]))
    {
    PlotBrushes[0][0] = Brushes.Orange;
    }
    else
    {
    PlotBrushes[0][0] = Brushes.Yellow;
    }​


    i can manage to have the lines change color depending on some conditions, however, i am also interested in having the indicator color the background - region between the lines. ¿how can that be achieved?


    i have taken a look at the macross indicator that nt made available years ago and it is not useful for me as it uses a crossbelow-above structure that will not work for me. it is also full of options and parameters that make it very difficult to follow the logic for the different actions.



    very well, regards.




    #2
    Hello rtwave,

    You can use Draw.Region() to color a region between two plot series.


    You'll want to use a new object (with a new unique tag name) for each segment that is a different color.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3


      people with nt,



      i have created this indicator. it does color the region between two averages as intended, but i do not understand why does it only apply the conditional logic to the latest region where the conditions are observed and how to manage for the indicator to color all the observations in the chart.




      Code:
      
      {
          public class sampledrawregion : Indicator
          {
              
              private    SMA FastCalculation;
              private    SMA SlowCalculation;        
              private int Down = 0;
              private int Up = 0;
              private int BarDown = 0;
              private int BarUp = 0;
              
              
              protected override void OnStateChange()
              {
                  if (State == State.SetDefaults)
                  {
                      Description                                    = @"sampledrawregion";
                      Name                                        = "sampledrawregion";
                      Calculate                                    = Calculate.OnPriceChange;
                      IsOverlay                                    = true;
                      DisplayInDataBox                            = true;
                      DrawOnPricePanel                            = true;
                      DrawHorizontalGridLines                        = true;
                      DrawVerticalGridLines                        = true;
                      PaintPriceMarkers                            = true;
                      ScaleJustification                            = NinjaTrader.Gui.Chart.ScaleJustification.Right;
                      //Disable this property if your indicator requires custom values that cumulate with each new market data event.
                      //See Help Guide for additional information.
                      IsSuspendedWhileInactive                    = true;
                      InputFast                    = 10;
                      InputSlow                    = 20;
                      AddPlot(new Stroke(Brushes.DarkGray, DashStyleHelper.Solid, 1), PlotStyle.Line, "Fast");
                      AddPlot(new Stroke(Brushes.DarkGray, DashStyleHelper.Solid, 1), PlotStyle.Line, "Slow");
                      
                  }
                  else if (State == State.Configure)
                  {
                  }
                  else if (State == State.DataLoaded)
                  {
                      
                      FastCalculation        = SMA(Close, InputFast);
                      SlowCalculation        = SMA(Close, InputSlow);
                      
                      
                  }
              }
      
              protected override void OnBarUpdate()
              {
                  
                  if (CurrentBar < 2) return;
                  
                  
                  Fast[0]        = FastCalculation[0];
                  Slow[0]        = SlowCalculation[0];
                  
                  
                  
                  Print("t0 " + Times[0][0] + " " + CurrentBar + " " + Down + " " + BarDown + " " + Up + " " + BarUp );
                  
                  
                  
                  
                  if ((Fast[0]) < (Slow[0]))
                  {
                  PlotBrushes[1][0] = Brushes.Orange;
      //            ++ bap01dti;
                  if (Down == 1 && CurrentBar != BarDown)
                  {
      //            Draw.Region(this, "tag1", CurrentBar, 0, Bollinger(2, 14).Upper, Bollinger(2, 14).Lower, null, Brushes.Blue, 50);
                  Draw.Region(this, "Down", CurrentBar - BarDown + 1, 0, Fast, Slow, null, Brushes.Orange, 50);
                  }
                  }
                  else if ((Fast[0]) > (Slow[0]))
                  {
                  PlotBrushes[1][0] = Brushes.Purple;
      //            ++ bap01uti;
                  if (Up == 1 && CurrentBar != BarUp)
                  {
                  Draw.Region(this, "Up", CurrentBar - BarUp + 1, 0, Fast, Slow, null, Brushes.Purple, 50);
                  }
                  }
                  else
                  {
                  PlotBrushes[1][0] = Brushes.Yellow;            
                  }
                  
                  if ((Fast[0]) < (Slow[0]) && (Fast[1]) > (Slow[1])
                      && CurrentBar != BarDown
                      )
                  {
                  Down = 1;
                  BarDown = CurrentBar;
                  }
                  
                  if ((Fast[0]) > (Slow[0]) && (Fast[1]) < (Slow[1])
                      && CurrentBar != BarUp
                      )
                  {
                  Up = 1;
                  BarUp = CurrentBar;
                  }
                  
                  
                  
              }
      
              #region Properties
              
              [NinjaScriptProperty]
              [Range(1, int.MaxValue)]
              [Display(Name="InputFast", Description="InputFast.", Order=1, GroupName="parameters.")]
              public int InputFast
              { get; set; }
      
              [NinjaScriptProperty]
              [Range(1, int.MaxValue)]
              [Display(Name="InputSlow", Description="InputSlow.", Order=4, GroupName="parameters.")]
              public int InputSlow
              { get; set; }
              
              [Browsable(false)]
              [XmlIgnore]
              public Series<double> Fast
              {
                  get { return Values[0]; }
              }
              
              [Browsable(false)]
              [XmlIgnore]
              public Series<double> Slow
              {
                  get { return Values[1]; }
              }
              
              #endregion​

      Comment


        #4
        Hello rtwave,

        I'm not quite sure what you are asking.

        Are you asking why the region is updating to the latest bar instead of being drawn multiple times?

        If so, this would be because the tag name is not unique.
        When the tag name is re-used this updates the existing object.
        When the tag name is unique, a new object is drawn.

        Below is a link to a forum post that discusses.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5


          people with nt,



          i made some minor adjustments to the indicator i shared previously.


          if ((Fast[0]) < (Slow[0]))
          {
          PlotBrushes[1][0] = Brushes.Orange;
          if (Down == 1
          // && CurrentBar != BarDown
          )
          {
          Draw.Region(this, "Down " + BarDown, CurrentBar - BarDown + 1, 0, Fast, Slow, null, Brushes.Orange, 50);
          }​



          this version above will indeed color all the areas between the moving averages. but even when it is clearly meant to color only those areas where (Fast[0]) < (Slow[0]), for reasons i cannot understand it will color all areas in the chart. and if i happen to also enable a color for the cases when (Fast[0]) > (Slow[0]) then both colors will be applied everywhere, which obviously defeats the purpose of the entire exercise even more.


          the plots for the moving averages will work precisely as intended but i cannot understand why the nt platform has to be so complicated and convoluted, and in this case have two different structures for very similar objectives like applying colors conditionally.


          as i see, my code is almost identical to this indicator nt makes available, however, the builder works well but trying to use a different structure will generate nonsensical results.

          Rewrite of the MACrossBuilder 1) Various Moving averages: DEMA, EMA, LinReg, HMA, SMA, TEMA, TMA, VWMA, WMA, ZLEMA 2) Various indication options 3) Can output to alerts window 4) Can output to Market Analyzer (+1, 0, &#8208;1 = Cross up, No cross, cross down) 5) Colors region between Moving averages V1.02 Update 10&#8208;31&#8208;17, minor code [&#8230;]


          Comment


            #6
            Hello rtwave,

            The Draw.Region() has parameters for the startBarsAgo and endBarsAgo. For each segment of color, you would be supplying the number of bars you want the region displayed over. For each new segment with a different color a new tag name would be used to create a new object for that segment.

            "for reasons i cannot understand it will color all areas in the chart. "

            This is likely because you are providing the startBarsAgo over the entire chart and not from where you want this segment to begin.

            Chelsea B.NinjaTrader Customer Service

            Comment


              #7



              people with nt,



              i had not been able to spend time coding on nt and i had completely abandoned this indicator.


              however, i used the structure that this sample indicator uses, and i also tried several variations and nothing works:


              Rewrite of the MACrossBuilder 1) Various Moving averages: DEMA, EMA, LinReg, HMA, SMA, TEMA, TMA, VWMA, WMA, ZLEMA 2) Various indication options 3) Can output to alerts window 4) Can output to Market Analyzer (+1, 0, &#8208;1 = Cross up, No cross, cross down) 5) Colors region between Moving averages V1.02 Update 10&#8208;31&#8208;17, minor code [&#8230;]





              if (ColorRegions)
              {
              if (CrossDetect[0] == 1 || (savedUBar > savedDBar))
              {
              Draw.Region(this, "Up" + savedUBar, CurrentBar - savedUBar + 1, 0, FstMA, SlwMA, Brushes.Transparent, ColorRegionAbove, RegionOpacity, 0);
              }

              if (CrossDetect[0] == -1 || (savedDBar > savedUBar))
              {
              Draw.Region(this, "Dwn" + savedDBar, CurrentBar - savedDBar + 1, 0, FstMA, SlwMA, Brushes.Transparent, ColorRegionBelow, RegionOpacity, 0);
              }
              }​


              it makes no sense that these coordinates CurrentBar - savedDBar + 1, 0 do work on the sample provided by nt but then result in a mess when i use them.


              and if it is necessary to have individual tag names for each event, ¿are there any samples of nt code where this is applied? ¿are there any links where anyone could read on something like this?

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
              0 responses
              568 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