ninjatrader/indicators/VPOC.cs

198 lines
6.4 KiB
C#
Raw Permalink Normal View History

#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;
using NinjaTrader.NinjaScript.BarsTypes;
#endregion
//This namespace holds Indicators in this folder and is required. Do not change it.
namespace NinjaTrader.NinjaScript.Indicators
{
public class VPOC : Indicator
{
private class POC
{
public double Level { get; set; }
public int StartBarIndex { get; set; }
public int EndBarIndex { get; set; }
}
private List<POC> DetectedPOCs = new List<POC>();
// Represents the current day's volume profile.
private Dictionary<double, long> VP = new Dictionary<double, long>();
private const int PrimaryBars = 0;
private const int TickBars = 1;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Name = "VPOC";
Description = @"Virgin (Volume) Point of Control";
Calculate = Calculate.OnEachTick;
IsOverlay = true;
DrawOnPricePanel = true;
ScaleJustification = ScaleJustification.Right;
IsSuspendedWhileInactive = false;
VPOCStroke = new Stroke(Brushes.Yellow, DashStyleHelper.Solid, 3);
}
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()
{
// Assuming primary bars are processed first each session.
if (PrimaryBars == BarsInProgress && IsFirstTickOfBar)
{
if (Bars.IsFirstBarOfSession && CurrentBar > 1)
{
double pointOfControl = VP.OrderByDescending(p => p.Value).First().Key;
DetectedPOCs.Add(new POC
{
Level = pointOfControl,
StartBarIndex = CurrentBar
});
VP.Clear(); // Reset the volume profile for the new session.
}
foreach (POC poc in DetectedPOCs)
{
if (poc.EndBarIndex == 0 &&
(CrossAbove(Close, poc.Level, 1) || CrossBelow(Close, poc.Level, 1)))
{
poc.EndBarIndex = CurrentBar;
}
}
}
if (TickBars == BarsInProgress)
{
double lastPrice = Closes[TickBars][0];
if (!VP.ContainsKey(lastPrice))
VP[lastPrice] = 0;
VP[lastPrice] += (long)Volumes[TickBars][0];
}
}
protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
{
base.OnRender(chartControl, chartScale);
foreach (POC poc in DetectedPOCs)
{
int startX = chartControl.GetXByBarIndex(ChartBars, poc.StartBarIndex);
int endX = poc.EndBarIndex == 0 ? ChartPanel.X + ChartPanel.W :
chartControl.GetXByBarIndex(ChartBars, poc.EndBarIndex);
DrawHorizontalLine(startX, endX, chartScale.GetYByValue(poc.Level), VPOCStroke);
}
}
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]
[Display(Name = "VPOC", Description = "VPOC line drawn on chart", Order = 1, GroupName = "VPOC")]
public Stroke VPOCStroke
{ get; set; }
}
}
#region NinjaScript generated code. Neither change nor remove.
namespace NinjaTrader.NinjaScript.Indicators
{
public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
{
private VPOC[] cacheVPOC;
public VPOC VPOC(Stroke vPOCStroke)
{
return VPOC(Input, vPOCStroke);
}
public VPOC VPOC(ISeries<double> input, Stroke vPOCStroke)
{
if (cacheVPOC != null)
for (int idx = 0; idx < cacheVPOC.Length; idx++)
if (cacheVPOC[idx] != null && cacheVPOC[idx].VPOCStroke == vPOCStroke && cacheVPOC[idx].EqualsInput(input))
return cacheVPOC[idx];
return CacheIndicator<VPOC>(new VPOC(){ VPOCStroke = vPOCStroke }, input, ref cacheVPOC);
}
}
}
namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
{
public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
{
public Indicators.VPOC VPOC(Stroke vPOCStroke)
{
return indicator.VPOC(Input, vPOCStroke);
}
public Indicators.VPOC VPOC(ISeries<double> input , Stroke vPOCStroke)
{
return indicator.VPOC(input, vPOCStroke);
}
}
}
namespace NinjaTrader.NinjaScript.Strategies
{
public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
{
public Indicators.VPOC VPOC(Stroke vPOCStroke)
{
return indicator.VPOC(Input, vPOCStroke);
}
public Indicators.VPOC VPOC(ISeries<double> input , Stroke vPOCStroke)
{
return indicator.VPOC(input, vPOCStroke);
}
}
}
#endregion