ninjatrader/indicators/VP.cs

188 lines
6.0 KiB
C#
Raw Normal View History

2023-10-16 19:19:02 +00:00
#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 VP : Indicator
{
private const int TickBars = 1;
private Dictionary<double, long> volumeProfile = new Dictionary<double, long>();
private int maxPixelWidth = 500;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Volume Profile";
Name = "Volume Profile";
Calculate = Calculate.OnEachTick;
IsOverlay = true;
DisplayInDataBox = true;
DrawOnPricePanel = true;
DrawHorizontalGridLines = true;
DrawVerticalGridLines = true;
PaintPriceMarkers = true;
ScaleJustification = ScaleJustification.Right;
IsSuspendedWhileInactive = false;
ProfileColor = Brushes.DimGray;
Opacity = 25;
}
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);
Brush profileColorWithTransparency = new SolidColorBrush(
Color.FromArgb((byte)(255 * (Opacity / 100.0)),
((SolidColorBrush)ProfileColor).Color.R,
((SolidColorBrush)ProfileColor).Color.G,
((SolidColorBrush)ProfileColor).Color.B)
);
SharpDX.Direct2D1.Brush profileBrush = profileColorWithTransparency.ToDxBrush(RenderTarget);
long maxVolume = volumeProfile.Values.Max();
double tickSize = Bars.Instrument.MasterInstrument.TickSize;
foreach (var entry in volumeProfile)
{
double priceLevel = entry.Key;
long volumeAtPrice = entry.Value;
int yLower = chartScale.GetYByValue(priceLevel - tickSize / 2);
int yUpper = chartScale.GetYByValue(priceLevel + tickSize / 2);
int height = Math.Abs(yUpper - yLower);
int barWidth = (int)((volumeAtPrice / (double)maxVolume) * maxPixelWidth);
int startX = ChartPanel.X + ChartPanel.W - barWidth;
RenderTarget.FillRectangle(new SharpDX.RectangleF(startX, yUpper, barWidth, height), profileBrush);
}
profileBrush.Dispose();
}
public override string DisplayName
{
get { return Name; }
}
[Display(Name = "Profile Color", Description = "Color of the volume profile", Order = 1, GroupName = "Volume Profile")]
public Brush ProfileColor
{ get; set; }
[Display(Name = "Max Width (Pixels)", Description = "Maximum pixel width for volume bars", Order = 2, GroupName = "Volume Profile")]
public int MaxPixelWidth
{
get { return maxPixelWidth; }
set { maxPixelWidth = value; }
}
[Display(Name = "Opacity (%)", Description = "Opacity of volume profile", Order = 3, GroupName = "Volume Profile")]
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 VP[] cacheVP;
public VP VP()
2023-10-16 19:19:02 +00:00
{
return VP(Input);
2023-10-16 19:19:02 +00:00
}
public VP VP(ISeries<double> input)
2023-10-16 19:19:02 +00:00
{
if (cacheVP != null)
for (int idx = 0; idx < cacheVP.Length; idx++)
if (cacheVP[idx] != null && cacheVP[idx].EqualsInput(input))
2023-10-16 19:19:02 +00:00
return cacheVP[idx];
return CacheIndicator<VP>(new VP(), input, ref cacheVP);
2023-10-16 19:19:02 +00:00
}
}
}
namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
{
public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
{
public Indicators.VP VP()
2023-10-16 19:19:02 +00:00
{
return indicator.VP(Input);
2023-10-16 19:19:02 +00:00
}
public Indicators.VP VP(ISeries<double> input )
2023-10-16 19:19:02 +00:00
{
return indicator.VP(input);
2023-10-16 19:19:02 +00:00
}
}
}
namespace NinjaTrader.NinjaScript.Strategies
{
public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
{
public Indicators.VP VP()
2023-10-16 19:19:02 +00:00
{
return indicator.VP(Input);
2023-10-16 19:19:02 +00:00
}
public Indicators.VP VP(ISeries<double> input )
2023-10-16 19:19:02 +00:00
{
return indicator.VP(input);
2023-10-16 19:19:02 +00:00
}
}
}
#endregion