Add indicator for displaying LVNs (Low Volume Nodes)
This commit is contained in:
parent
e6161e4814
commit
0034a25dcb
212
indicators/LVN.cs
Normal file
212
indicators/LVN.cs
Normal file
@ -0,0 +1,212 @@
|
||||
#region Using declarations
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
using System.Xml.Serialization;
|
||||
using NinjaTrader.Cbi;
|
||||
using NinjaTrader.Gui;
|
||||
using NinjaTrader.Gui.Chart;
|
||||
using NinjaTrader.Gui.SuperDom;
|
||||
using NinjaTrader.Gui.Tools;
|
||||
using NinjaTrader.Data;
|
||||
using NinjaTrader.NinjaScript;
|
||||
using NinjaTrader.Core.FloatingPoint;
|
||||
using NinjaTrader.NinjaScript.DrawingTools;
|
||||
#endregion
|
||||
|
||||
//This namespace holds Indicators in this folder and is required. Do not change it.
|
||||
namespace NinjaTrader.NinjaScript.Indicators
|
||||
{
|
||||
public class LVN : Indicator
|
||||
{
|
||||
private const int TickBars = 1;
|
||||
|
||||
// TODO: Utilize the existing VP indicator rather than duplicating it here.
|
||||
private Dictionary<double, long> volumeProfile = new Dictionary<double, long>();
|
||||
|
||||
protected override void OnStateChange()
|
||||
{
|
||||
if (State == State.SetDefaults)
|
||||
{
|
||||
Description = @"LVNs (Low Volume Nodes)";
|
||||
Name = "LVN";
|
||||
Calculate = Calculate.OnEachTick;
|
||||
IsOverlay = true;
|
||||
DisplayInDataBox = true;
|
||||
DrawOnPricePanel = true;
|
||||
DrawHorizontalGridLines = true;
|
||||
DrawVerticalGridLines = true;
|
||||
PaintPriceMarkers = true;
|
||||
ScaleJustification = ScaleJustification.Right;
|
||||
IsSuspendedWhileInactive = false;
|
||||
SurroundingLevels = 25;
|
||||
LVNStroke = new Stroke(Brushes.Yellow, DashStyleHelper.Solid, 2);
|
||||
MaxPixelWidth = 500;
|
||||
Opacity = 100;
|
||||
}
|
||||
else if (State == State.Configure)
|
||||
{
|
||||
AddDataSeries(Instrument.FullName, new BarsPeriod {
|
||||
BarsPeriodType = BarsPeriodType.Tick, Value = 1 }, Bars.TradingHours.Name);
|
||||
}
|
||||
else if (State == State.Historical)
|
||||
{
|
||||
SetZOrder(-1); // Display behind bars on chart.
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnBarUpdate()
|
||||
{
|
||||
if (TickBars == BarsInProgress)
|
||||
{
|
||||
if (Bars.IsFirstBarOfSession)
|
||||
volumeProfile.Clear(); // Reset the volume profile for the new session.
|
||||
|
||||
double lastPrice = Closes[TickBars][0];
|
||||
if (!volumeProfile.ContainsKey(lastPrice))
|
||||
volumeProfile[lastPrice] = 0;
|
||||
|
||||
volumeProfile[lastPrice] += (long)Volumes[TickBars][0];
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
|
||||
{
|
||||
base.OnRender(chartControl, chartScale);
|
||||
|
||||
// TODO: Rather than just rendering the LVNs, make them accessible to other indicators / strategies.
|
||||
var sortedPrices = volumeProfile.Keys.ToList();
|
||||
sortedPrices.Sort();
|
||||
|
||||
for (int i = 0; i < sortedPrices.Count; i++)
|
||||
{
|
||||
double price = sortedPrices[i];
|
||||
long volumeAtPrice = volumeProfile[price];
|
||||
bool isLVN = true;
|
||||
|
||||
// Do not go out of bounds.
|
||||
int rangeStart = Math.Max(i - SurroundingLevels, 0);
|
||||
int rangeEnd = Math.Min(i + SurroundingLevels, sortedPrices.Count - 1);
|
||||
|
||||
for (int j = rangeStart; j <= rangeEnd; j++)
|
||||
{
|
||||
if (j == i) continue;
|
||||
|
||||
double surroundingPrice = sortedPrices[j];
|
||||
long surroundingVolume = volumeProfile[surroundingPrice];
|
||||
|
||||
if (volumeAtPrice >= surroundingVolume)
|
||||
{
|
||||
isLVN = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (isLVN)
|
||||
{
|
||||
int y = chartScale.GetYByValue(price);
|
||||
DrawHorizontalLine(ChartPanel.X + ChartPanel.W - MaxPixelWidth, ChartPanel.X + ChartPanel.W, y, LVNStroke);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void DrawHorizontalLine(int startX, int endX, int y, Stroke stroke)
|
||||
{
|
||||
SharpDX.Direct2D1.Brush dxBrush = stroke.Brush.ToDxBrush(RenderTarget);
|
||||
RenderTarget.DrawLine(
|
||||
new SharpDX.Vector2(startX, y),
|
||||
new SharpDX.Vector2(endX, y),
|
||||
dxBrush, stroke.Width, stroke.StrokeStyle
|
||||
);
|
||||
dxBrush.Dispose();
|
||||
}
|
||||
|
||||
public override string DisplayName
|
||||
{
|
||||
get { return Name; }
|
||||
}
|
||||
|
||||
[NinjaScriptProperty]
|
||||
[Range(1, int.MaxValue)]
|
||||
[Display(Name = "Window Size", Description = "Number of profile levels to compare to in either direction", GroupName = "LVN", Order = 1)]
|
||||
public int SurroundingLevels { get; set; }
|
||||
|
||||
[NinjaScriptProperty]
|
||||
[XmlIgnore]
|
||||
[Display(Name = "LVN", Description = "Stroke for LVNs drawn on chart", GroupName = "LVN", Order = 2)]
|
||||
public Stroke LVNStroke { get; set; }
|
||||
|
||||
[NinjaScriptProperty]
|
||||
[Display(Name = "Max Width (Pixels)", Description = "Maximum pixel width for LVN levels", Order = 3, GroupName = "LVN")]
|
||||
public int MaxPixelWidth { get; set; }
|
||||
|
||||
[NinjaScriptProperty]
|
||||
[Display(Name = "Opacity (%)", Description = "Opacity of LVN level", Order = 4, GroupName = "LVN")]
|
||||
public int Opacity
|
||||
{ get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
#region NinjaScript generated code. Neither change nor remove.
|
||||
|
||||
namespace NinjaTrader.NinjaScript.Indicators
|
||||
{
|
||||
public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
|
||||
{
|
||||
private LVN[] cacheLVN;
|
||||
public LVN LVN(int surroundingLevels, Stroke lVNStroke, int maxPixelWidth, int opacity)
|
||||
{
|
||||
return LVN(Input, surroundingLevels, lVNStroke, maxPixelWidth, opacity);
|
||||
}
|
||||
|
||||
public LVN LVN(ISeries<double> input, int surroundingLevels, Stroke lVNStroke, int maxPixelWidth, int opacity)
|
||||
{
|
||||
if (cacheLVN != null)
|
||||
for (int idx = 0; idx < cacheLVN.Length; idx++)
|
||||
if (cacheLVN[idx] != null && cacheLVN[idx].SurroundingLevels == surroundingLevels && cacheLVN[idx].LVNStroke == lVNStroke && cacheLVN[idx].MaxPixelWidth == maxPixelWidth && cacheLVN[idx].Opacity == opacity && cacheLVN[idx].EqualsInput(input))
|
||||
return cacheLVN[idx];
|
||||
return CacheIndicator<LVN>(new LVN(){ SurroundingLevels = surroundingLevels, LVNStroke = lVNStroke, MaxPixelWidth = maxPixelWidth, Opacity = opacity }, input, ref cacheLVN);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
|
||||
{
|
||||
public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
|
||||
{
|
||||
public Indicators.LVN LVN(int surroundingLevels, Stroke lVNStroke, int maxPixelWidth, int opacity)
|
||||
{
|
||||
return indicator.LVN(Input, surroundingLevels, lVNStroke, maxPixelWidth, opacity);
|
||||
}
|
||||
|
||||
public Indicators.LVN LVN(ISeries<double> input , int surroundingLevels, Stroke lVNStroke, int maxPixelWidth, int opacity)
|
||||
{
|
||||
return indicator.LVN(input, surroundingLevels, lVNStroke, maxPixelWidth, opacity);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
namespace NinjaTrader.NinjaScript.Strategies
|
||||
{
|
||||
public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
|
||||
{
|
||||
public Indicators.LVN LVN(int surroundingLevels, Stroke lVNStroke, int maxPixelWidth, int opacity)
|
||||
{
|
||||
return indicator.LVN(Input, surroundingLevels, lVNStroke, maxPixelWidth, opacity);
|
||||
}
|
||||
|
||||
public Indicators.LVN LVN(ISeries<double> input , int surroundingLevels, Stroke lVNStroke, int maxPixelWidth, int opacity)
|
||||
{
|
||||
return indicator.LVN(input, surroundingLevels, lVNStroke, maxPixelWidth, opacity);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
Loading…
Reference in New Issue
Block a user