Render price range levels in OnRender() instead of using Draw.Line()

This commit is contained in:
moshferatu 2024-01-12 13:06:20 -08:00
parent 52925af126
commit 5a052ebb1b

View File

@ -48,6 +48,14 @@ namespace NinjaTrader.NinjaScript.Strategies
private int NumberOfTradesAtSessionStart = 0;
private class PriceRange
{
public double High { get; set; }
public double Low { get; set; }
}
private Dictionary<DateTime, PriceRange> PriceRanges = new Dictionary<DateTime, PriceRange>();
protected override void OnStateChange()
{
if (State == State.SetDefaults)
@ -139,9 +147,6 @@ namespace NinjaTrader.NinjaScript.Strategies
{
if (NeutralBias == Bias[0])
Draw.Text(this, CurrentBar + " No Trade", "No Trade Taken", 0, High[0]);
Draw.Line(this, CurrentBar + " Range High", 20, RangeHigh[0], 0, RangeHigh[0], Brushes.Yellow);
Draw.Line(this, CurrentBar + " Range Low", 20, RangeLow[0], 0, RangeLow[0], Brushes.Yellow);
}
InTradeWindow[0] = CurrentlyInWindow(TradeWindowStart, TradeWindowEnd);
@ -181,17 +186,24 @@ namespace NinjaTrader.NinjaScript.Strategies
{
if (CurrentBar > 3)
{
DateTime currentDate = Time[0].Date;
if (!InPriceRangeWindow[1])
{
// First bar in price range window.
RangeHigh[0] = High[0];
RangeLow[0] = Low[0];
PriceRanges[currentDate] = new PriceRange();
}
else
{
RangeHigh[0] = Math.Max(RangeHigh[1], High[0]);
RangeLow[0] = Math.Min(RangeLow[1], Low[0]);
}
PriceRanges[currentDate].High = RangeHigh[0];
PriceRanges[currentDate].Low = RangeLow[0];
}
}
@ -277,6 +289,40 @@ namespace NinjaTrader.NinjaScript.Strategies
}
}
protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
{
base.OnRender(chartControl, chartScale);
foreach (var priceRange in PriceRanges)
{
if (priceRange.Value.High == double.MinValue || priceRange.Value.Low == double.MinValue)
continue;
TimeSpan rangeStartTime = ConvertIntToTimeSpan(PriceRangeWindowStart);
TimeSpan rangeEndTime = ConvertIntToTimeSpan(BiasWindowEnd);
int startX = chartControl.GetXByTime(priceRange.Key + rangeStartTime);
int endX = chartControl.GetXByTime(priceRange.Key + rangeEndTime);
int highY = chartScale.GetYByValue(priceRange.Value.High);
RenderTarget.DrawLine(new SharpDX.Vector2(startX, highY), new SharpDX.Vector2(endX, highY),
Brushes.LimeGreen.ToDxBrush(RenderTarget), 3);
int lowY = chartScale.GetYByValue(priceRange.Value.Low);
RenderTarget.DrawLine(new SharpDX.Vector2(startX, lowY), new SharpDX.Vector2(endX, lowY),
Brushes.Red.ToDxBrush(RenderTarget), 3);
}
}
private TimeSpan ConvertIntToTimeSpan(int time)
{
int hours = time / 10000;
int minutes = (time % 10000) / 100;
int seconds = time % 100;
return new TimeSpan(hours, minutes, seconds);
}
public override string DisplayName
{
get { return Name; }