• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Script drawing lines on the wrong bar

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

    Script drawing lines on the wrong bar

    Hello

    I am trying to get my script to draw lines on the chart panel but, while it prints the right start and end bars in the drawing objects window, the lines are drawn towards the beginning of the chart.

    For instance, a bar with a start bar index of 930 is drawn at bar 13, but not only that: the end time is before the start time, so the bars are sloped the opposite way to what they should be, as in the second screenshot.

    C, S, E are the CurrentBar, StartBar, EndBar

    Please see my code below

    Code:
    public class Divergence5min : Strategy
    {
        private DashStyleHelper divDashStyle         = DashStyleHelper.Solid;
        private Brush divColour                       = Brushes.Magenta;
        private Brush hiddenDivColour                 = Brushes.DodgerBlue;
    ...
        protected override void OnBarUpdate()
        {
    ...........
            NegativeDivergence();
    ...........
        }
        private void NegativeDivergence()        
        {
            if (HSwDMI.Count != lastListCountH)
            {
                 if (!hidNegDiv && ((LastHSwDMI[j - 1] - LastHSwDMI[j - 2] > 0.01 && LastHSwDMIprice[j - 2] > LastHSwDMIprice[j - 1] && LastHSwDMIprice[j - 2] - LastHSwDMIprice[j - 1] <= (int)HidDivOffset * TickSize)
                       || (HSwDMI[b - 1] - LastHSwDMI[j - 1] > 0.01 && LastHSwDMIprice[j - 1] > HSwDMIprice[b - 1] && LastHSwDMIprice[j - 1] - HSwDMIprice[b - 1] <= (int)HidDivOffset * TickSize)))
                 {
                     if((HSwDMI[b - 1] - LastHSwDMI[j - 1] > 0.01 && LastHSwDMIprice[j - 1] > HSwDMIprice[b - 1] && LastHSwDMIprice[j - 1] - HSwDMIprice[b - 1] <= (int)HidDivOffset * TickSize))
                           Draw.Line(this, "hidNegDiv1" + " / C " + CurrentBar.ToString() + " / S " + LastHSwDMIpriceBar[j - 1].ToString() + " / E " + PriceHighBar[b - 1], false, LastHSwDMIpriceBar[j - 1], LastHSwDMIprice[j - 1] + TickSize, PriceHighBar[b - 1], HSwDMIprice[b - 1] + TickSize, hiddenDivColour, divDashStyle, 2, true);
           ...........
                  }
              }
          }
    Attached Files

    #2
    Oops! Never mind... got it: mistook startBarsAgo for BarIndex

    Comment


      #3
      Hi itrader46, thanks for your post.

      It cant tell what is going wrong from the snippet. Have you printed the values you are using in the IF statement as well as Draw.Line() to confirm where the lines are being drawn at? I solve 99% of the bugs I get by Printing the values that I use to the output window. I would recommend reducing this part of the code until you see the issue.

      Kind regards,

      -ChrisL
      Chris L.NinjaTrader Customer Service

      Comment


        #4
        OK, so I am drawing the lines with this code:

        Code:
        if((LastHSwDMI[j - 2] - LastHSwDMI[j - 1] > 0.01 && LastHSwDMIprice[j - 1] - LastHSwDMIprice[j - 2] > TickSize))
             Draw.Line(this, "negDiv" + " / C " + CurrentBar.ToString() + " / S " + LastHSwDMIpriceBar[j - 2].ToString() + " / E " + LastHSwDMIpriceBar[j - 1].ToString(), false, CurrentBar - LastHSwDMIpriceBar[j - 2], LastHSwDMIprice[j-2] + TickSize, CurrentBar - LastHSwDMIpriceBar[j - 1], LastHSwDMIprice[j-1] + TickSize, negDivColour, divDashStyle, 1, true);
        ... but when I try to remove them with this code:

        Code:
        if (DrawObjects["negDiv" + " / C " + CurrentBar.ToString() + " / S " + LastHSwDMIpriceBar[j - 2].ToString() + " / E " + LastHSwDMIpriceBar[j - 1].ToString()] != null)
            RemoveDrawObject("negDiv" + " / C " + CurrentBar.ToString() + " / S " + LastHSwDMIpriceBar[j - 2].ToString() + " / E " + LastHSwDMIpriceBar[j - 1].ToString());    
        Print("negDiv = " + (DrawObjects["negDiv" + " / C " + CurrentBar.ToString() + " / S " + LastHSwDMIpriceBar[j - 2].ToString() + " / E " + LastHSwDMIpriceBar[j - 1].ToString()] != null).ToString());
        ...it doesn't work and the Print shows the condition to remove as False, in other words the DrawObjects[tag] == null, while I am looking at the line on the chart.

        And this code doesn't do much better and to add insult to injury, it shows the RemoveDrawObject if() condition as TRUE, but the line is still not removed.

        Code:
        if((LastHSwDMI[j - 1] - HSwDMI[b - 1] > 0.01 && HSwDMIprice[b - 1] - LastHSwDMIprice[j - 1] > TickSize))
            Draw.Line(this, "negDiv1" + " / C " + CurrentBar.ToString() + " / S " + LastHSwDMIpriceBar[j - 1].ToString() + " / E " + PriceHighBar[b - 1], false, CurrentBar - LastHSwDMIpriceBar[j - 1], LastHSwDMIprice[j - 1] + TickSize, CurrentBar - PriceHighBar[b - 1], HSwDMIprice[b - 1] + TickSize, negDivColour, divDashStyle, 1, true);
        
        
        if(LastHSwDMI[j - 1] - HSwDMI[b - 1] > 0.01 && HSwDMIprice[b - 1] - LastHSwDMIprice[j - 1] > TickSize)
            RemoveDrawObject("negDiv1" + " / C " + CurrentBar.ToString() + " / S " + LastHSwDMIpriceBar[j - 1].ToString() + " / E " + PriceHighBar[b - 1]);
        Print("negDiv1 = " + (LastHSwDMI[j - 1] - HSwDMI[b - 1] > 0.01 && HSwDMIprice[b - 1] - LastHSwDMIprice[j - 1] > TickSize))
        ... And that's how I've spent the last four hours.

        What am I not getting?
        Last edited by itrader46; 01-24-2020, 12:28 PM.

        Comment


          #5
          I just realised my tag included the CurrentBar, which would have been different by the time I wanted to remove the last line, hence the null print

          Comment


            #6
            Hi itrader46, thanks for your reply.

            Great job on finding the issue with prints, they are a useful tool indeed. Did you get everything sorted out with your indicator now?
            Chris L.NinjaTrader Customer Service

            Comment


              #7
              Hi Chris. Not just now. I've made the script see the values I see, now I'm trying to make it interpret things the way I would.
              Thanks for your help

              Comment


                #8
                I've just learned that it is better to draw the lines in OnRender, rather than use the Draw method, for performance issues. Please confirm if that is the case.

                The other reason is that, due to some NT8 bug, the DrawOnPricePanel = false parameter is not respected when called on objects drawn on an indicator applied by the script.

                Problem is, when I'm trying to implement the method, my lines are not looking as in the Draw method, as you can see in the screenshot.

                Am I doing something wrong in my code here?

                I find this hard to understand, since I already know my values are correct, as the lines were drawn fine in the Draw method, so I'm not even sure in this case what Prints to look at.
                The line I was expecting to be drawn is the bottom if() statement, as I can see the conditions are printed as true

                Finally, I can see the lines drawn by this method don't have a tag (nothing shows in Drawing Objects window), which I found really handy for checking and tweaking the way the script works, so is there anything I can do to put a tag on those lines?

                Thank you

                Code:
                protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
                {
                    if (j > 1 && b > 1)
                          {
                                if ((LastHSwDMI[j - 2] - LastHSwDMI[j - 1] > 0.01 && LastHSwDMIprice[j - 1] - LastHSwDMIprice[j - 2] > TickSize))                            
                                {
                                    SharpDX.Vector2 start = new System.Windows.Point((LastHSwDMIpriceBar[j - 2]), (LastHSwDMIprice[j - 2] + TickSize)).ToVector2();                    
                                    SharpDX.Vector2 end = new System.Windows.Point((LastHSwDMIpriceBar[j - 1]), (LastHSwDMIprice[j - 1] + TickSize)).ToVector2();
                
                                    using (SharpDX.Direct2D1.SolidColorBrush brush = new SharpDX.Direct2D1.SolidColorBrush(RenderTarget, SharpDX.Color.Cyan))
                                    {
                                        RenderTarget.DrawLine(start, end, brush);                        
                                    }
                                }
                
                                if ((LastHSwDMI[j - 1] - HSwDMI[b - 1] > 0.01 && HSwDMIprice[b - 1] - LastHSwDMIprice[j - 1] > TickSize))
                                {
                                    SharpDX.Vector2 start1 = new System.Windows.Point((LastHSwDMIpriceBar[j - 1]), (LastHSwDMIprice[j - 1] + TickSize)).ToVector2();
                                    SharpDX.Vector2 end1 = new System.Windows.Point((PriceHighBar[b - 1]), (HSwDMIprice[b - 1] + TickSize)).ToVector2();
                
                                    using (SharpDX.Direct2D1.SolidColorBrush brush1 = new SharpDX.Direct2D1.SolidColorBrush(RenderTarget, SharpDX.Color.Cyan))
                                    {
                                        RenderTarget.DrawLine(start1, end1, brush1);
                                    }
                                }
                            }
                       }
                }
                Attached Files
                Last edited by itrader46; 01-25-2020, 11:09 PM.

                Comment


                  #9
                  Hi itrader46, thanks for your reply.

                  One thing I am seeing is you're converting some price value to a Point LastHSwDMIpriceBar[j - 2], looks like its a price, if you need the X or Y coordinate of a price you need to use the various methods within the ChartControl object. See GetXByBarIndex() and GetXByTime() on the ChartControl page:
                  https://ninjatrader.com/support/help...artcontrol.htm

                  And also see GetValueByY() GetValueByYWpf() GetYByValue() GetYByValueWpf() in the ChartScale object:

                  https://ninjatrader.com/support/help...chartscale.htm

                  Please let me know if I can assist any further.

                  Chris L.NinjaTrader Customer Service

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by dtl-saw, Yesterday, 08:44 PM
                  0 responses
                  12 views
                  0 likes
                  Last Post dtl-saw
                  by dtl-saw
                   
                  Started by ballboy11, Yesterday, 04:29 PM
                  0 responses
                  10 views
                  0 likes
                  Last Post ballboy11  
                  Started by zacharydw00, Yesterday, 02:29 PM
                  2 responses
                  30 views
                  0 likes
                  Last Post zacharydw00  
                  Started by timcjpfx, Yesterday, 06:22 AM
                  4 responses
                  27 views
                  0 likes
                  Last Post timcjpfx  
                  Started by welson, Yesterday, 08:26 AM
                  2 responses
                  18 views
                  0 likes
                  Last Post NinjaTrader_PatrickG  
                  Working...
                  X