NinjaTrader Support Forum  
X

Attention!

This website will be down for maintenance from Friday May 24th at 6PM MDT until Saturday May 25th at 11AM MDT. We apologize for the inconvenience. If you need assistance during this time, please email sales@ninjatrader.com


Go Back   NinjaTrader Support Forum > NinjaScript Development Support > General Programming

General Programming General NinjaScript programming questions.

Reply
 
Thread Tools Display Modes
Old 12-30-2011, 12:14 PM   #1
h1000
Junior Member
 
Join Date: Dec 2010
Posts: 29
Thanks: 5
Thanked 1 time in 1 post
Default 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 Images
File Type: png Chart 01.png (56.1 KB, 17 views)
Last edited by h1000; 12-30-2011 at 12:19 PM. Reason: clarity
h1000 is offline  
Reply With Quote
Old 12-30-2011, 12:16 PM   #2
h1000
Junior Member
 
Join Date: Dec 2010
Posts: 29
Thanks: 5
Thanked 1 time in 1 post
Default 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:
 
//If the current day [0] and the previous day [1] are inside days
if(
Low[0]>Low[1] && High[0]<High[1] //Check for inside day on current day [0]
&&
Low[1]>Low[2] && High[1]<High[2] //Check for inside day on previous day [1]
)
{ DrawLine(CurrentBar.ToString() + "Line", false, 1, Close[1], 0, Close[0], Color.Blue, DashStyle.Dash, 2); }
 
//If the current day [0] and day [2] are inside days
if(
Low[0]>Low[1] && High[0]<High[1] //Check for inside day on current day [0]
&&
Low[2]>Low[3] && High[2]<High[3] //Check for inside day on previous day [2]
)
{ DrawLine(CurrentBar.ToString() + "Line", false, 2, Close[2], 0, Close[0], Color.Blue, DashStyle.Dash, 2); } 
...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 at 12:26 PM. Reason: clarity
h1000 is offline  
Reply With Quote
Old 12-30-2011, 12:17 PM   #3
h1000
Junior Member
 
Join Date: Dec 2010
Posts: 29
Thanks: 5
Thanked 1 time in 1 post
Default 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 at 12:21 PM. Reason: clarity
h1000 is offline  
Reply With Quote
Old 12-30-2011, 01:18 PM   #4
NinjaTrader_AdamP
NinjaTrader Customer Service
 
NinjaTrader_AdamP's Avatar
 
Join Date: Aug 2011
Location: Denver, CO, USA
Posts: 2,895
Thanks: 241
Thanked 375 times in 365 posts
Default

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 at 01:24 PM.
NinjaTrader_AdamP is offline  
Reply With Quote
Old 12-30-2011, 03:04 PM   #5
koganam
Senior Member
 
Join Date: Feb 2008
Location: Durham, North Carolina, USA
Posts: 3,201
Thanks: 24
Thanked 1,227 times in 998 posts
Send a message via Skype™ to koganam
Default

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);
            }
koganam is offline  
Reply With Quote
The following 2 users say thank you to koganam for this post:
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Auto Reconnect to CQG At End of Weekend & Replay Connect Errors in Custom Plot Method KBJ Connecting 2 10-14-2011 09:27 PM
DrawLine() dowhk Indicator Development 4 02-10-2010 09:48 AM
drawline to nowhere gg80108 Indicator Development 9 05-01-2009 12:43 PM
Drawline puppeye Indicator Development 2 12-22-2008 06:49 AM
DrawLine Help PrTester Charting 1 02-09-2008 08:41 PM


All times are GMT -6. The time now is 10:22 AM.