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

Using DrawLine method to connect datapoints

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

    Using DrawLine method to connect datapoints

    >>PART 1.<<
    >>Post is broken down into three messages as it is exceeding maximum character limit<<


    I have been struggling with a custom indicator for some time now, and hope that someone might be able to direct me to a solution.

    I initially started a thread on this topic at http://www.ninjatrader.com/support/forum/showthread.php?t=46422 For the sake of example, I compared it to the ZigZag indicator. I am afraid that this comparison was overly complex and derailed the simple intent of the post.

    Basically, what I want to do is draw a line from the close of one inside day to the close of the next inside day (see attached .png).
    Attached Files
    Last edited by h1000; 12-30-2011, 01:19 PM. Reason: clarity

    #2
    Part 2

    >>Part 2.<<
    >>Post is broken down into three messages as it is exceeding maximum character limit<<

    The only solution that I could achieve with my limited programming experience would be to manually code all possible inside-day combinations, with code similar to this:
    Code:
     
    [COLOR=#008000][FONT=Courier New][SIZE=2]//If the current day [0] and the previous day [1] are inside days[/SIZE][/FONT][/COLOR]
    [COLOR=#0000ff][FONT=Courier New][SIZE=2]if[/SIZE][/FONT][/COLOR][COLOR=#000000][FONT=Courier New][SIZE=2]([/SIZE][/FONT][/COLOR]
    [FONT=Courier New][SIZE=2]Low[[/SIZE][/FONT][COLOR=#800080][FONT=Courier New][SIZE=2]0[/SIZE][/FONT][/COLOR][FONT=Courier New][SIZE=2]]>Low[[/SIZE][/FONT][COLOR=#800080][FONT=Courier New][SIZE=2]1[/SIZE][/FONT][/COLOR][FONT=Courier New][SIZE=2]] && High[[/SIZE][/FONT][COLOR=#800080][FONT=Courier New][SIZE=2]0[/SIZE][/FONT][/COLOR][FONT=Courier New][SIZE=2]]<High[[/SIZE][/FONT][COLOR=#800080][FONT=Courier New][SIZE=2]1[/SIZE][/FONT][/COLOR][FONT=Courier New][SIZE=2]] [/SIZE][/FONT][COLOR=#008000][FONT=Courier New][SIZE=2]//Check for inside day on current day [0][/SIZE][/FONT][/COLOR]
    [COLOR=#000000][FONT=Courier New][SIZE=2]&&[/SIZE][/FONT][/COLOR]
    [FONT=Courier New][SIZE=2]Low[[/SIZE][/FONT][COLOR=#800080][FONT=Courier New][SIZE=2]1[/SIZE][/FONT][/COLOR][FONT=Courier New][SIZE=2]]>Low[[/SIZE][/FONT][COLOR=#800080][FONT=Courier New][SIZE=2]2[/SIZE][/FONT][/COLOR][FONT=Courier New][SIZE=2]] && High[[/SIZE][/FONT][COLOR=#800080][FONT=Courier New][SIZE=2]1[/SIZE][/FONT][/COLOR][FONT=Courier New][SIZE=2]]<High[[/SIZE][/FONT][COLOR=#800080][FONT=Courier New][SIZE=2]2[/SIZE][/FONT][/COLOR][FONT=Courier New][SIZE=2]] [/SIZE][/FONT][COLOR=#008000][FONT=Courier New][SIZE=2]//Check for inside day on previous day [1][/SIZE][/FONT][/COLOR]
    [COLOR=#000000][FONT=Courier New][SIZE=2])[/SIZE][/FONT][/COLOR]
    [FONT=Courier New][SIZE=2]{ DrawLine(CurrentBar.ToString() + [/SIZE][/FONT][COLOR=#800000][FONT=Courier New][SIZE=2]"Line"[/SIZE][/FONT][/COLOR][FONT=Courier New][SIZE=2], [/SIZE][/FONT][COLOR=#0000ff][FONT=Courier New][SIZE=2]false[/SIZE][/FONT][/COLOR][FONT=Courier New][SIZE=2], [/SIZE][/FONT][COLOR=#800080][FONT=Courier New][SIZE=2]1[/SIZE][/FONT][/COLOR][FONT=Courier New][SIZE=2], Close[[/SIZE][/FONT][COLOR=#800080][FONT=Courier New][SIZE=2]1[/SIZE][/FONT][/COLOR][FONT=Courier New][SIZE=2]], [/SIZE][/FONT][COLOR=#800080][FONT=Courier New][SIZE=2]0[/SIZE][/FONT][/COLOR][FONT=Courier New][SIZE=2], Close[[/SIZE][/FONT][COLOR=#800080][FONT=Courier New][SIZE=2]0[/SIZE][/FONT][/COLOR][FONT=Courier New][SIZE=2]], Color.Blue, DashStyle.Dash, [/SIZE][/FONT][COLOR=#800080][FONT=Courier New][SIZE=2]2[/SIZE][/FONT][/COLOR][FONT=Courier New][SIZE=2]); }[/SIZE][/FONT]
    [COLOR=#008000][FONT=Courier New][SIZE=2][/SIZE][/FONT][/COLOR] 
    [COLOR=#008000][FONT=Courier New][SIZE=2]//If the current day [0] and day [2] are inside days[/SIZE][/FONT][/COLOR]
    [COLOR=#0000ff][FONT=Courier New][SIZE=2]if[/SIZE][/FONT][/COLOR][COLOR=#000000][FONT=Courier New][SIZE=2]([/SIZE][/FONT][/COLOR]
    [FONT=Courier New][SIZE=2]Low[[/SIZE][/FONT][COLOR=#800080][FONT=Courier New][SIZE=2]0[/SIZE][/FONT][/COLOR][FONT=Courier New][SIZE=2]]>Low[[/SIZE][/FONT][COLOR=#800080][FONT=Courier New][SIZE=2]1[/SIZE][/FONT][/COLOR][FONT=Courier New][SIZE=2]] && High[[/SIZE][/FONT][COLOR=#800080][FONT=Courier New][SIZE=2]0[/SIZE][/FONT][/COLOR][FONT=Courier New][SIZE=2]]<High[[/SIZE][/FONT][COLOR=#800080][FONT=Courier New][SIZE=2]1[/SIZE][/FONT][/COLOR][FONT=Courier New][SIZE=2]] [/SIZE][/FONT][COLOR=#008000][FONT=Courier New][SIZE=2]//Check for inside day on current day [0][/SIZE][/FONT][/COLOR]
    [COLOR=#000000][FONT=Courier New][SIZE=2]&&[/SIZE][/FONT][/COLOR]
    [FONT=Courier New][SIZE=2]Low[[/SIZE][/FONT][COLOR=#800080][FONT=Courier New][SIZE=2]2[/SIZE][/FONT][/COLOR][FONT=Courier New][SIZE=2]]>Low[[/SIZE][/FONT][COLOR=#800080][FONT=Courier New][SIZE=2]3[/SIZE][/FONT][/COLOR][FONT=Courier New][SIZE=2]] && High[[/SIZE][/FONT][COLOR=#800080][FONT=Courier New][SIZE=2]2[/SIZE][/FONT][/COLOR][FONT=Courier New][SIZE=2]]<High[[/SIZE][/FONT][COLOR=#800080][FONT=Courier New][SIZE=2]3[/SIZE][/FONT][/COLOR][FONT=Courier New][SIZE=2]] [/SIZE][/FONT][COLOR=#008000][FONT=Courier New][SIZE=2]//Check for inside day on previous day [2][/SIZE][/FONT][/COLOR]
    [COLOR=#000000][FONT=Courier New][SIZE=2])[/SIZE][/FONT][/COLOR]
    [FONT=Courier New][SIZE=2]{ DrawLine(CurrentBar.ToString() + [/SIZE][/FONT][COLOR=#800000][FONT=Courier New][SIZE=2]"Line"[/SIZE][/FONT][/COLOR][FONT=Courier New][SIZE=2], [/SIZE][/FONT][COLOR=#0000ff][FONT=Courier New][SIZE=2]false[/SIZE][/FONT][/COLOR][FONT=Courier New][SIZE=2], [/SIZE][/FONT][COLOR=#800080][FONT=Courier New][SIZE=2]2[/SIZE][/FONT][/COLOR][FONT=Courier New][SIZE=2], Close[[/SIZE][/FONT][COLOR=#800080][FONT=Courier New][SIZE=2]2[/SIZE][/FONT][/COLOR][FONT=Courier New][SIZE=2]], [/SIZE][/FONT][COLOR=#800080][FONT=Courier New][SIZE=2]0[/SIZE][/FONT][/COLOR][FONT=Courier New][SIZE=2], Close[[/SIZE][/FONT][COLOR=#800080][FONT=Courier New][SIZE=2]0[/SIZE][/FONT][/COLOR][FONT=Courier New][SIZE=2]], Color.Blue, DashStyle.Dash, [/SIZE][/FONT][COLOR=#800080][FONT=Courier New][SIZE=2]2[/SIZE][/FONT][/COLOR][FONT=Courier New][SIZE=2]); } [/SIZE][/FONT]
    ...but of course all possible iterations/combinations of inside days would have to be manually coded to make this work. This is clearly impractical.
    Last edited by h1000; 12-30-2011, 01:26 PM. Reason: clarity

    Comment


      #3
      Part 3.

      >>Part 3.<<
      >>Post is broken down into three messages as it is exceeding maximum character limit<<

      I have just stumbled on a thread that draws similar lines and offers a potential pathway to a solution (see http://www.ninjatrader.com/support/forum/showthread.php?t=46426). However, as only some of the code is posted, I have been unable to use it to create a working solution.

      I am hoping that perhaps some forum members with programming experience can see how the info at http://www.ninjatrader.com/support/forum/showthread.php?t=46426 could develop into a solution.

      Any insights, however small, would be helpful.
      Last edited by h1000; 12-30-2011, 01:21 PM. Reason: clarity

      Comment


        #4
        h1000,

        Why don't you keep track of the location of the previous "inside bar" then when a new one is found draw a new line?

        Notice :

        DrawLine(string tag, bool autoScale, DateTime startTime, double startY, DateTime endTime, double endY, Color color, DashStyle dashStyle, int width)

        The startTime can be found using Time[0] and startY can be found using Close[0] obviously stored for the previous "inside bar". When you find a new "inside bar" then you draw a line between the previous one and the current one, then set the previous startTime and prebious startY again to Time[0], and Close[0] respectively.

        Code:
        if(Low[0]>Low[1] && High[0]<High[1])
        {
             DrawLine(CurrentBar.ToString(), true, previousStart, previousY, Time[0], Close[0], Color.Blue, DashStyle.Dash, 3);
         
            previousStart = Time[0];
            previousY = Close[0];
        
        }
        Please let me know if I may assist further.
        Last edited by NinjaTrader_AdamP; 12-30-2011, 02:24 PM.
        Adam P.NinjaTrader Customer Service

        Comment


          #5
          Untested code that should do the trick. Let me know if you have any errors. I would usually test the code, but the market today frustrated me so much that by the time my opportunity came, I was too jaded to take it, and I am too annoyed at myself for allowing the frustration to break my discipline, after all these years, when I thought that I was over such problems.

          DrawLine() parameters have been hard-coded. You may want to turn them into user parameters.

          Code:
          private bool IsInit = false;
          private bool IsInsideDay = false;
          private int LastInsideDayBarNum = 0;
          private int CurrentInsideDayBarNum = 0;
          Code:
                   protected override void Initialize()
                  {
                      Overlay                = false;
                      this.ZOrder            = 100; //ensures that plot stays on top of candles
                  }
          Code:
           protected override void OnBarUpdate()
                  {
                      if (CurrentBar < 1) return; //escape first bar, as we need to compare with the bar before it.
                      
                      this.IsInsideDay = false;
                      if (High[0] < High[1] && Low[0] > Low[1]) IsInsideDay = true;
          
                      // initialize the points for drawing lines. We need 2 points to start, so do nothing until we have those 2 points.
          
                      if (!this.IsInit)
                      {
                          if (this.IsInsideDay && this.LastInsideDayBarNum == 0 && this.CurrentInsideDayBarNum != 0)
                          {
                              this.IsInit = true;
                          }
                          else if (this.IsInsideDay && this.LastInsideDayBarNum == 0 && this.CurrentInsideDayBarNum == 0)
                          {
                              this.CurrentInsideDayBarNum = CurrentBar;
                              return;                
                          }
                      }
                      
                      if (this.IsInsideDay)
                      {
                          this.LastInsideDayBarNum = this.CurrentInsideDayBarNum;
                          this.CurrentInsideDayBarNum = CurrentBar;
                          int StartBarsAgo = CurrentBar - this.LastInsideDayBarNum;
                          DrawLine("Line" + CurrentBar.ToString(), true, 
                                  StartBarsAgo, Close[StartBarsAgo], 
                                  0, Close[0], 
                                  Color.Blue, DashStyle.Dash, 2);
                      }

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by samish18, 04-17-2024, 08:57 AM
          16 responses
          55 views
          0 likes
          Last Post samish18  
          Started by arvidvanstaey, Today, 02:19 PM
          3 responses
          9 views
          0 likes
          Last Post NinjaTrader_Zachary  
          Started by jordanq2, Today, 03:10 PM
          2 responses
          8 views
          0 likes
          Last Post jordanq2  
          Started by traderqz, Today, 12:06 AM
          10 responses
          18 views
          0 likes
          Last Post traderqz  
          Started by algospoke, 04-17-2024, 06:40 PM
          5 responses
          47 views
          0 likes
          Last Post NinjaTrader_Jesse  
          Working...
          X