namespace NinjaTrader.NinjaScript.Indicators { public class aCopyClickTrendline : Indicator { private ChartScale chartScale; private Point clickPoint = new Point(); private double convertedPrice; private DateTime convertedTime; private int clickCount = 0; double[] PriceValues = new double[2]; DateTime[] TimeValues = new DateTime[2]; private int barsAgo1; private int barsAgo2; private double firstPrice; private double secondPrice; private DateTime firstTime; private DateTime secondTime; protected override void OnStateChange() { if (State == State.SetDefaults) { Description = @"Demonstrates how to capture a mouse click and adjust for DPI settings properly"; Name = "aCopyClickTrendline"; Calculate = Calculate.OnBarClose; IsOverlay = true; DisplayInDataBox = false; DrawOnPricePanel = true; ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right; } else if (State == State.Historical) { if (ChartControl != null) { foreach (ChartScale scale in ChartPanel.Scales) if (scale.ScaleJustification == ScaleJustification) chartScale = scale; ChartControl.MouseLeftButtonDown += MouseClicked; } } else if (State == State.Terminated) { if (ChartControl != null) ChartControl.MouseLeftButtonDown -= MouseClicked; } } protected override void OnBarUpdate() { } protected void MouseClicked(object sender, MouseButtonEventArgs e) { clickCount++; // convert e.GetPosition for different dpi settings clickPoint.X = ChartingExtensions.ConvertToHorizontalPixels(e.GetPosition(ChartControl as IInputElement).X, ChartControl.PresentationSource); clickPoint.Y = ChartingExtensions.ConvertToVerticalPixels(e.GetPosition(ChartControl as IInputElement).Y, ChartControl.PresentationSource); convertedPrice = Instrument.MasterInstrument.RoundToTickSize(chartScale.GetValueByY((float)clickPoint.Y)); convertedTime = ChartControl.GetTimeBySlotIndex((int)ChartControl.GetSlotIndexByX((int)clickPoint.X)); Draw.TextFixed(this, "priceTime", string.Format("Price: {0}, Time: {1}", convertedPrice, convertedTime), TextPosition.BottomLeft); ForceRefresh(); if (clickCount > 1) { ChartControl.MouseLeftButtonDown -= MouseClicked; } Print(clickCount); PriceValues[clickCount-1] = convertedPrice; TimeValues[clickCount-1] = convertedTime; firstPrice = PriceValues[0]; secondPrice = PriceValues[1]; firstTime= TimeValues[0]; secondTime = TimeValues[1]; barsAgo1 = CurrentBar - Bars.GetBar(firstTime); barsAgo2 = CurrentBar - Bars.GetBar(secondTime); Draw.ExtendedLine(this, "MyLine", false, firstTime, firstPrice, secondTime, secondPrice, Brushes.LimeGreen, DashStyleHelper.Dot, 2); double low1 = Low[barsAgo1]; double low2 = Low[barsAgo2]; //Print(firstPrice + " " + secondPrice); //Print(firstTime + " " + secondTime); //Print(barsAgo1 + " "+ barsAgo2); //Draw.ExtendedLine(this, "MyLine", false, firstTime, firstPrice, secondTime, secondPrice, Brushes.LimeGreen, DashStyleHelper.Dot, 2); Draw.ExtendedLine(this, "MyLine", false, firstTime, low1, secondTime, low2, Brushes.LimeGreen, DashStyleHelper.Dot, 2); } } }
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!
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
NinjaTrader
support needed onclick mouse draw line
Collapse
X
-
support needed onclick mouse draw line
Hi i have the following code and when i want to have the lows of the barsago1 and barsago2 to draw an extended line, i have an error message: unhandled exception: index was out of range. must be non negative and less then the size of the collection. parameter name: index. i don't have the error message if i don't use low1 and low2 but the price directly. your help would be appreciated
Code:Tags: None
-
Hello cbadr,
Thanks for your post.
This unhandled exception error indicates you are trying to access an index value that is out of range.
In the OnBarUpdate() section of the script, you could add a CurrentBars check to make sure you have enough bars processing in the data series you are accessing.
See the help guide documentation below for more information.
CurrentBar - https://ninjatrader.com/support/help...currentbar.htm
Make sure you have enough bars - https://ninjatrader.com/support/help...nough_bars.htm
Further, debugging prints should be added to the script that print out the index you are accessing and the CurrentBar to see how your logic is evaluating.
Below is a link to a forum post that demonstrates how to use prints to understand behavior.
https://ninjatrader.com/support/foru...121#post791121Brandon H.NinjaTrader Customer Service
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by carnitron, Today, 08:42 PM
|
0 responses
5 views
0 likes
|
Last Post
by carnitron
Today, 08:42 PM
|
||
Started by strategist007, Today, 07:51 PM
|
0 responses
6 views
0 likes
|
Last Post Today, 07:51 PM | ||
Started by StockTrader88, 03-06-2021, 08:58 AM
|
44 responses
3,974 views
3 likes
|
Last Post
by jhudas88
Today, 06:59 PM
|
||
Started by rbeckmann05, Today, 06:48 PM
|
0 responses
8 views
0 likes
|
Last Post
by rbeckmann05
Today, 06:48 PM
|
||
Started by rhyminkevin, Today, 04:58 PM
|
4 responses
58 views
0 likes
|
Last Post
by dp8282
Today, 06:13 PM
|
Comment