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

DrawRegion

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

    DrawRegion

    NT 7.0.1000.4
    WinXP SP3
    CalculateOnBarClose = True

    I am trying to shade the background color of the price chart based on a fast moving average being above (green) or below (red) a slower moving average using "DrawRegion". What happens is that it shades the current bar correctly when the bar closes but then "clears" the shading of the prior bar if it has the same tag. It appears that the only bar "shaded" is the last bar of the "region".

    I guess I do not understand how a DrawRegion actually works. What happens to the prior region if the same tag is used several times in a row?? I have tried using 1 or 0 in the start/end bars ago but end up with no shading or the same issue.

    In the attachment you can see the blank space between the last "red" shade and the last "green" shade. When the green shading first appeared it was next to the red but as each new bar appeared it cleared out the prior "green" shade and printed a new green shade.

    This is the code section with the problem.

    RegHigh.Set(High[0] + 100); //Sets the top of price chart
    RegLow.Set(Low[
    0] - 100); //Sets the bottom of price chart
    if (dsFast[0] > dsSlow[0]) //The moving averages 20 & 100
    {
    DrawRegion(
    "Green", 1, 0, RegHigh, RegLow, Color.Empty, Color.Green, 5);
    }
    elseif (dsFast[0] < dsSlow[0])
    {
    DrawRegion(
    "Red", 1, 0, RegHigh, RegLow, Color.Empty, Color.Red, 5);
    }
    else
    {
    DrawRegion(
    "Yellow", 1, 0, RegHigh, RegLow, Color.Empty, Color.Yellow, 5);
    }
    Attached Files

    #2
    Originally posted by Romulan View Post
    NT 7.0.1000.4
    WinXP SP3
    CalculateOnBarClose = True

    I am trying to shade the background color of the price chart based on a fast moving average being above (green) or below (red) a slower moving average using "DrawRegion". What happens is that it shades the current bar correctly when the bar closes but then "clears" the shading of the prior bar if it has the same tag. It appears that the only bar "shaded" is the last bar of the "region".

    I guess I do not understand how a DrawRegion actually works. What happens to the prior region if the same tag is used several times in a row?? I have tried using 1 or 0 in the start/end bars ago but end up with no shading or the same issue.

    In the attachment you can see the blank space between the last "red" shade and the last "green" shade. When the green shading first appeared it was next to the red but as each new bar appeared it cleared out the prior "green" shade and printed a new green shade.

    This is the code section with the problem.

    RegHigh.Set(High[0] + 100); //Sets the top of price chart
    RegLow.Set(Low[
    0] - 100); //Sets the bottom of price chart
    if (dsFast[0] > dsSlow[0]) //The moving averages 20 & 100
    {
    DrawRegion(
    "Green", 1, 0, RegHigh, RegLow, Color.Empty, Color.Green, 5);
    }
    elseif (dsFast[0] < dsSlow[0])
    {
    DrawRegion(
    "Red", 1, 0, RegHigh, RegLow, Color.Empty, Color.Red, 5);
    }
    else
    {
    DrawRegion(
    "Yellow", 1, 0, RegHigh, RegLow, Color.Empty, Color.Yellow, 5);
    }
    That is the expected behavior if you are not using unique tags. For continuous drawing, you need unique tags. Most folks create a unique tag by appending the CurrentBar to some prefix or suffix, as in this example:

    Code:
    DrawRegion("Green" + CurrentBar.ToString(), 1, 0, RegHigh, RegLow, Color.Empty, Color.Green, 5);

    Comment


      #3
      Thank you, koganam for the assistance.

      As said, if you use the same tag repeatedly, it will update the existing draw object with the new parameters you passed to DrawRegion. So that is why using the CurrentBar.ToString() in the tag is used to create a unique tag for each region you are drawing.

      For more information on using DrawRegion, see the following helpguide article: http://www.ninjatrader.com/support/h...drawregion.htm
      DexterNinjaTrader Customer Service

      Comment


        #4
        For some reason using the CurrentBar.ToString() in the Tag seemed to really suckup CPUs.

        I did basically the same idea but only created a new tag if the region color changed. For example, if the region was "Red" and the new bar was the same ("Red") I used the same tag. If the region was "Red" and the new bar was different ("Yellow" or "Green") then I created a new tag. This way I created far fewer "Regions" and the CPUs were not impacted as much.

        Thanks for helping me understand what DrawRegion was doing.

        Comment


          #5
          Hello, whats the proper way to call the DrawRegion method?

          i did this and experimented adding and changing things , looked how was done in other indicators and i still cant understand why this is making the whole indicator disappear

          DrawOnPricePanel= false;
          if (CurrentBar > 50) return;

          if (MultiHigh[0] >= 0)
          {
          DrawRegion("Up"+CurrentBar ,1,0, MultiHigh , 0 ,Color.Black , highUp, opacityHighUp );
          }
          else
          {
          DrawRegion("Up"+CurrentBar ,1,0, MultiHigh , 0 ,Color.Black , highDown, opacityHighDown );
          }

          Comment


            #6
            Code:
            if (CurrentBar > 50) return;
            says that after 50 bars, the indicator should stop calculating. I would expect, therefore, to see nothing after the first 50 bars.

            Comment


              #7
              I toked that out and recompiled, the panel is still empty, if i remove the portion of code i posted before everything is ok and the indicator reappears, so i must be doing something wrong in that portion of code,
              (and i thoungh that Currentbar > 50 was for checking you have enough bars to calculate ) anyways, i removed that and we r still in the same situation :P

              Comment


                #8
                Originally posted by kabott View Post
                I toked that out and recompiled, the panel is still empty, if i remove the portion of code i posted before everything is ok and the indicator reappears, so i must be doing something wrong in that portion of code,
                (and i thoungh that Currentbar > 50 was for checking you have enough bars to calculate ) anyways, i removed that and we r still in the same situation :P
                Without knowing what your variables mean, or how they were declared, or what you are trying to achieve, it is hard to say from just that snippet of code, what is wrong.

                Comment


                  #9
                  o sorry, here it goes, its just a simple indi called Rubber bands


                  public class RubberBand2 : Indicator
                  {
                  #region Variables
                  // Wizard generated variables
                  private int longLeg = 35;
                  private int shortLeg = 1;
                  private int multiLeg = 10;
                  private int multiW = 1;


                  private Color highUp = Color.RoyalBlue;
                  private Color highDown = Color.Pink;
                  private Color lowUp = Color.RoyalBlue;
                  private Color lowDown = Color.Pink;

                  private int opacityHighUp = 2;
                  private int opacityHighDown = 2;

                  private int opacityLowUp = 2;
                  private int opacityLowDown = 2;



                  #endregion

                  double SyncShort, SyncLong, SyncMulti;
                  double ShortWeight, LongWeight, MultiWeight;


                  protected override void Initialize ()
                  {
                  Add (new Line (Color.DarkGray, 0, "ZeroLine"));

                  Add (new Plot (Color.FromKnownColor (KnownColor.Red), PlotStyle.Line, "MultiOsc"));
                  Add (new Plot (Color.FromKnownColor (KnownColor.Purple), PlotStyle.Line, "MultiHigh"));
                  Add (new Plot (Color.FromKnownColor (KnownColor.Purple), PlotStyle.Line, "MultiLow"));

                  CalculateOnBarClose = true;
                  Overlay = false;
                  PriceTypeSupported = true;
                  PriceType = PriceType.Close;

                  ShortWeight = 2.0 / (ShortLeg + 1);
                  LongWeight = 2.0 / (LongLeg + 1);
                  MultiWeight = 2.0 / (MultiLeg + 1);
                  }



                  protected override void OnBarUpdate ()
                  {
                  if (SyncShort == 0)
                  {
                  SyncShort = Close [0]*10;
                  SyncLong = Close [0]*10;
                  }
                  else
                  {
                  SyncShort = SyncShort * (1 - ShortWeight) + (ShortWeight * Close [0]*10);
                  SyncLong = SyncLong * (1 - LongWeight) + (LongWeight * Close [0]*10);
                  }

                  double multiOsc = 100 * ((SyncShort / SyncLong) - 1);
                  if (SyncMulti == 0)
                  SyncMulti = multiOsc;
                  else
                  SyncMulti = Math.Abs (SyncMulti) * (1 - MultiWeight) + (MultiWeight * multiOsc);

                  MultiHigh.Set (SyncMulti * MultiW);
                  MultiLow.Set (-SyncMulti * MultiW);
                  MultiOsc.Set (multiOsc);





                  DrawOnPricePanel= false;

                  {
                  if (MultiHigh[0] >0)
                  {
                  DrawRegion("Up"+CurrentBar ,1,0, MultiHigh , 0 ,Color.Black , highUp, opacityHighUp );
                  }
                  else
                  {
                  DrawRegion("Up"+CurrentBar ,1,0, MultiHigh , 0 ,Color.Black , highDown, opacityHighDown );
                  }

                  Comment


                    #10
                    Looks like you are missing your barEscape at the start of the indicator. You need the line in red at the place indicated.


                    Code:
                    protected override void OnBarUpdate ()
                            {
                               [B][COLOR=Red] if (CurrentBar < 1) return;[/COLOR][/B]
                                if (SyncShort == 0)
                                {
                                    SyncShort = Close [0]*10;
                    
                    etc.
                    It is needed because you are drawing from one bar back. That bar does not exist on bar0, so we simply escape it.

                    You should see an error message in your log.

                    Comment


                      #11
                      Thank you very much Koganam!!

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by mangel2000, Today, 01:30 AM
                      0 responses
                      7 views
                      0 likes
                      Last Post mangel2000  
                      Started by Doxxxx, Today, 01:24 AM
                      0 responses
                      3 views
                      0 likes
                      Last Post Doxxxx
                      by Doxxxx
                       
                      Started by ezekilany, Today, 01:10 AM
                      0 responses
                      4 views
                      0 likes
                      Last Post ezekilany  
                      Started by usjavaburn, Today, 12:59 AM
                      0 responses
                      3 views
                      0 likes
                      Last Post usjavaburn  
                      Started by heatherjmarshalls, Today, 12:56 AM
                      0 responses
                      2 views
                      0 likes
                      Last Post heatherjmarshalls  
                      Working...
                      X