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

Chart Draw not correct in real time

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

    Chart Draw not correct in real time

    I created a chart drawing for whenever RSI is above 70 or below 30 to draw a triangle above or below the price bar. If functions correctly on past data but it marks the bar if the RSI dips below 30 or above 70 intrabar in real time. Is there any way to get it to mark the bar only if the bar closes while RSI is in the extreme area?

    Here is the code.
    ------------------------------------
    namespace NinjaTrader.Indicator
    {
    /// <summary>
    /// The RSI_mod (Relative Strength Index) is a price-following oscillator that ranges between 0 and 100.
    /// </summary>
    [Description("The RSI_mod (Relative Strength Index) is a price-following oscillator that ranges between 0 and 100.")]
    public class RSI_mod : Indicator
    {
    #region Variables
    private DataSeries avgUp;
    private DataSeries avgDown;
    private DataSeries down;
    private int period = 14;
    private int smooth = 3;
    private DataSeries up;
    #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.Green, "RSI_mod"));
    Add(new Plot(Color.Orange, "Avg"));

    Add(new Line(System.Drawing.Color.DarkViolet, 30, "Lower"));
    Add(new Line(System.Drawing.Color.DarkViolet, 70, "Upper"));

    avgUp = new DataSeries(this);
    avgDown = new DataSeries(this);
    down = new DataSeries(this);
    up = new DataSeries(this);

    PriceTypeSupported = true;
    }

    /// <summary>
    /// Calculates the indicator value(s) at the current index.
    /// </summary>
    protected override void OnBarUpdate()
    {
    if (CurrentBar == 0)
    {
    down.Set(0);
    up.Set(0);
    return;
    }

    down.Set(Math.Max(Input[1] - Input[0], 0));
    up.Set(Math.Max(Input[0] - Input[1], 0));

    if ((CurrentBar + 1) < Period)
    {
    if ((CurrentBar + 1) == (Period - 1))
    Avg.Set(50);
    return;
    }

    if ((CurrentBar + 1) == Period)
    {
    // First averages
    avgDown.Set(SMA(down, Period)[0]);
    avgUp.Set(SMA(up, Period)[0]);
    }
    else
    {
    // Rest of averages are smoothed
    avgDown.Set((avgDown[1] * (Period - 1) + down[0]) / Period);
    avgUp.Set((avgUp[1] * (Period - 1) + up[0]) / Period);
    }

    double rs = avgUp[0] / (avgDown[0] == 0 ? 1 : avgDown[0]);
    double rsi = 100 - (100 / (1 + rs));
    double rsiAvg = (2.0 / (1 + Smooth)) * rsi + (1 - (2.0 / (1 + Smooth))) * Avg[1];

    Avg.Set(rsiAvg);
    Value.Set(rsi);
    //**********************

    if (rsi > 70)
    {DrawTriangleUp("rsi up" + CurrentBar, false, 0, Low[0]-.50, Color.Green);}
    if (rsi < 30)
    {DrawTriangleDown("rsi down" + CurrentBar, false, 0, High[0]+0.40, Color.Red);}
    }

    #2
    Jason, please add this line of code to your Initialize() section in order for it to just act when the bar closes:
    Code:
    CalculateOnBarClose = true;
    AustinNinjaTrader Customer Service

    Comment


      #3
      Austin,

      Thanks for the reply but it still marks the bar when RSI moves out of the extreme areas in real time. I want the RSI to display in real time not on bar close.
      Is there a way to make the mark disappear when RSI moves out of the extreme area?


      Originally posted by NinjaTrader_Austin View Post
      Jason, please add this line of code to your Initialize() section in order for it to just act when the bar closes:
      Code:
      CalculateOnBarClose = true;

      Comment


        #4
        Originally posted by Jason11 View Post
        Is there a way to make the mark disappear when RSI moves out of the extreme area?
        Yes, you can use RemoveDrawObject() to delete the mark when the RSI moves out of the extreme area.
        AustinNinjaTrader Customer Service

        Comment


          #5
          OK. I put it into the code and its still not removing the object when RSI moves out of the extreme. What am I doing wrong?



          if (rsi > 70)
          {DrawTriangleUp("rsi up" + CurrentBar, false, 0, Low[0]-.50, Color.Green);}
          if (rsi < 70)
          {RemoveDrawObject("rsi up");}
          if (rsi < 30)
          {DrawTriangleDown("rsi down" + CurrentBar, false, 0, High[0]+0.40, Color.Red);}
          if (rsi > 30)
          {RemoveDrawObject("rsi down");}


          Originally posted by NinjaTrader_Austin View Post
          Yes, you can use RemoveDrawObject() to delete the mark when the RSI moves out of the extreme area.

          Comment


            #6
            Jason, you need to specify the exact tag, which in your case also includes the current bar number ("rsi up3056" for example). You'll probably need a collection of sorts to hold all the draw objects, or you could just use the same markers over and over again, which would make it much easier to delete the objects.
            AustinNinjaTrader Customer Service

            Comment


              #7
              OK. Is there an easier way to rewrite my code to achieve this?

              All I'm trying to do is mark the bar when RSI goes above 70 and below 30. When RSI is in between 30 and 70, I don't want a market to show up.

              Can you paste the code in here that does that?

              Comment


                #8
                Your original code, with CalculateOnBarClose set to true, will do exactly what you want.

                The following code will draw two triangles, one for the most recent time RSI went over 70 and one for the most recent time RSI went below 30:
                Code:
                if (RSI(14, 1)[0] > 70)
                     DrawTriangleDown("down triangle", false, 0, High[0] + TickSize, Color.Red);
                else if (RSI(14, 1)[0] < 30)
                     DrawTriangleUp("up triangle", false, 0, Low[0] - TickSize, Color.Green);
                AustinNinjaTrader Customer Service

                Comment


                  #9
                  Austin,

                  Thanks for the code. I just tried it and it doesn't remove the triangle when RSI goes back out of the extreme area. I'm looking for it to disappear when RSI is not in extreme. Is there a way to do this?
                  Also is it possible to show the triangles on all previous bars where RSI closed in the extreme area?

                  I don't want to use the indicator set to 'calculate on bar close' because I use actual RSI indicator and need it see it real time.

                  Comment


                    #10
                    It will not remove correct. If you actually want it to remove you have to use RemoveDrawObject().
                    Josh P.NinjaTrader Customer Service

                    Comment


                      #11
                      OK I added the removedrawobject() and now get an error.

                      here is the code:

                      /// <summary>
                      /// The RSI_mod (Relative Strength Index) is a price-following oscillator that ranges between 0 and 100.
                      /// </summary>
                      [Description("The RSI_mod (Relative Strength Index) is a price-following oscillator that ranges between 0 and 100.")]
                      public class RSI_mod : Indicator
                      {
                      #region Variables
                      private DataSeries avgUp;
                      private DataSeries avgDown;
                      private DataSeries down;
                      private int period = 14;
                      private int smooth = 3;
                      private DataSeries up;
                      #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.Green, "RSI_mod"));
                      Add(new Plot(Color.Orange, "Avg"));

                      Add(new Line(System.Drawing.Color.DarkViolet, 30, "Lower"));
                      Add(new Line(System.Drawing.Color.DarkViolet, 70, "Upper"));

                      avgUp = new DataSeries(this);
                      avgDown = new DataSeries(this);
                      down = new DataSeries(this);
                      up = new DataSeries(this);

                      PriceTypeSupported = true;
                      CalculateOnBarClose = true;
                      }

                      /// <summary>
                      /// Calculates the indicator value(s) at the current index.
                      /// </summary>
                      protected override void OnBarUpdate()
                      {
                      if (CurrentBar == 0)
                      {
                      down.Set(0);
                      up.Set(0);
                      return;
                      }

                      down.Set(Math.Max(Input[1] - Input[0], 0));
                      up.Set(Math.Max(Input[0] - Input[1], 0));

                      if ((CurrentBar + 1) < Period)
                      {
                      if ((CurrentBar + 1) == (Period - 1))
                      Avg.Set(50);
                      return;
                      }

                      if ((CurrentBar + 1) == Period)
                      {
                      // First averages
                      avgDown.Set(SMA(down, Period)[0]);
                      avgUp.Set(SMA(up, Period)[0]);
                      }
                      else
                      {
                      // Rest of averages are smoothed
                      avgDown.Set((avgDown[1] * (Period - 1) + down[0]) / Period);
                      avgUp.Set((avgUp[1] * (Period - 1) + up[0]) / Period);
                      }

                      double rs = avgUp[0] / (avgDown[0] == 0 ? 1 : avgDown[0]);
                      double rsi = 100 - (100 / (1 + rs));
                      double rsiAvg = (2.0 / (1 + Smooth)) * rsi + (1 - (2.0 / (1 + Smooth))) * Avg[1];

                      Avg.Set(rsiAvg);
                      Value.Set(rsi);
                      //**********************

                      if (rsi > 70)
                      {DrawTriangleUp("rsi up" + CurrentBar, false, 0, Low[0]- TickSize, Color.Green);}
                      if (rsi < 70)
                      {RemoveDrawObject("rsi up" + CurrentBar, false, 0, Low[0]- TickSize, Color.Green);}
                      if (rsi < 30)
                      {DrawTriangleDown("rsi down" + CurrentBar, false, 0, High[0]+ TickSize, Color.Red);}
                      if (rsi > 30)
                      {RemoveDrawObject("rsi down" + CurrentBar, false, 0, High[0]+ TickSize, Color.Red);}

                      }






                      Can someone please help. All I'm trying to do is have a triangle (or any shape) draw below the price bar if RSI is above 70 and disappear if RSI goes back below 70. I'd like this to happen in realtime and if the price bar closes below 70 then there should be no triangle. If the price bar closes above 70 then the triangle should remain.
                      The opposite should also be the case for RSI below 30.

                      If someone could please look at my code and either point out what I'm doing wrong or correct my code it would be greatly appreciated.

                      Comment


                        #12
                        Jason, please try the following code:
                        Code:
                        if (RSI(RSI_Period, 1)[0] > 70)
                        {
                            DrawTriangleDown("down triangle" + CurrentBar, false, 0, High[0] + TickSize, Color.Red);
                        }
                        else if (RSI(RSI_Period, 1)[0] < 30)
                        {
                            DrawTriangleUp("up triangle" + CurrentBar, false, 0, Low[0] - TickSize, Color.Green);
                        }
                        else if (RSI(RSI_Period, 1)[0] > 30 && RSI(RSI_Period, 1)[0] < 70)
                        {
                            RemoveDrawObject("down triangle" + CurrentBar);
                            RemoveDrawObject("up triangle" + CurrentBar);
                        }
                        AustinNinjaTrader Customer Service

                        Comment


                          #13
                          Absolutely fantastic.

                          Works perfectly Austin.

                          Thank you.

                          Comment


                            #14
                            Not a problem Jason. Sorry I didn't get you what you were looking for the first time around. I didn't fully understand your requirements.
                            AustinNinjaTrader Customer Service

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by GussJ, 03-04-2020, 03:11 PM
                            15 responses
                            3,270 views
                            0 likes
                            Last Post xiinteractive  
                            Started by Tim-c, Today, 02:10 PM
                            1 response
                            8 views
                            0 likes
                            Last Post NinjaTrader_ChelseaB  
                            Started by Taddypole, Today, 02:47 PM
                            0 responses
                            2 views
                            0 likes
                            Last Post Taddypole  
                            Started by chbruno, 04-24-2024, 04:10 PM
                            4 responses
                            51 views
                            0 likes
                            Last Post chbruno
                            by chbruno
                             
                            Started by TraderG23, 12-08-2023, 07:56 AM
                            10 responses
                            403 views
                            1 like
                            Last Post beobast
                            by beobast
                             
                            Working...
                            X