Created
August 27, 2024 18:34
-
-
Save danielnegri/c38f7702e57d0cab6b9524c9c55ddbe7 to your computer and use it in GitHub Desktop.
NinjaTrader 8 - Power VWAP Indicator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 PowerVWAP : Indicator | |
{ | |
private Series<double> cumVol; | |
private Series<double> cumPV; | |
protected override void OnStateChange() | |
{ | |
if (State == State.SetDefaults) | |
{ | |
Description = @"Display VWAP"; | |
Name = "Power VWAP"; | |
Calculate = Calculate.OnBarClose; | |
IsOverlay = true; | |
DisplayInDataBox = true; | |
DrawOnPricePanel = true; | |
ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right; | |
//Disable this property if your indicator requires custom values that cumulate with each new market data event. | |
//See Help Guide for additional information. | |
IsSuspendedWhileInactive = true; | |
AddPlot(Brushes.Magenta, "VWAP"); | |
} | |
else if (State == State.DataLoaded) | |
{ | |
cumVol = new Series<double>(this); | |
cumPV = new Series<double>(this); | |
} else if (State == State.Historical) { | |
// Displays a message if the bartype is not intraday | |
if (!Bars.BarsType.IsIntraday) | |
{ | |
Draw.TextFixed(this, "NinjaScriptInfo", "Power VWAP Indicator only supports Intraday charts", TextPosition.BottomRight); | |
Log("Power VWAP only supports Intraday charts", LogLevel.Error); | |
} | |
} | |
} | |
protected override void OnBarUpdate() | |
{ | |
if(Bars.IsFirstBarOfSession) | |
{ | |
if(CurrentBar > 0) Values[0].Reset(1); | |
cumVol[1] = 0; | |
cumPV[1] = 0; | |
} | |
cumPV[0] = cumPV[1] + (Typical[0] * Volume[0]); | |
cumVol[0] = cumVol[1] + Volume[0]; | |
// plot VWAP value | |
Values[0][0] = cumPV[0] / (cumVol[0] == 0 ? 1 : cumVol[0]); | |
} | |
} | |
} | |
#region NinjaScript generated code. Neither change nor remove. | |
namespace NinjaTrader.NinjaScript.Indicators | |
{ | |
public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase | |
{ | |
private PowerVWAP[] cachePowerVWAP; | |
public PowerVWAP PowerVWAP() | |
{ | |
return PowerVWAP(Input); | |
} | |
public PowerVWAP PowerVWAP(ISeries<double> input) | |
{ | |
if (cachePowerVWAP != null) | |
for (int idx = 0; idx < cachePowerVWAP.Length; idx++) | |
if (cachePowerVWAP[idx] != null && cachePowerVWAP[idx].EqualsInput(input)) | |
return cachePowerVWAP[idx]; | |
return CacheIndicator<PowerVWAP>(new PowerVWAP(), input, ref cachePowerVWAP); | |
} | |
} | |
} | |
namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns | |
{ | |
public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase | |
{ | |
public Indicators.PowerVWAP PowerVWAP() | |
{ | |
return indicator.PowerVWAP(Input); | |
} | |
public Indicators.PowerVWAP PowerVWAP(ISeries<double> input ) | |
{ | |
return indicator.PowerVWAP(input); | |
} | |
} | |
} | |
namespace NinjaTrader.NinjaScript.Strategies | |
{ | |
public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase | |
{ | |
public Indicators.PowerVWAP PowerVWAP() | |
{ | |
return indicator.PowerVWAP(Input); | |
} | |
public Indicators.PowerVWAP PowerVWAP(ISeries<double> input ) | |
{ | |
return indicator.PowerVWAP(input); | |
} | |
} | |
} | |
#endregion |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment