
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);
}
}
}

Comment