Created
August 24, 2024 04:12
-
-
Save danielnegri/f8720e2306a66712ba7b64687dc0e11c to your computer and use it in GitHub Desktop.
NinjaTrader 8 - Power Volume Indicator
This file contains 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
// | |
// Copyright (C) 2023, NinjaTrader LLC <www.ninjatrader.com>. | |
// NinjaTrader reserves the right to modify or overwrite this NinjaScript component with each release. | |
// | |
#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.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 | |
{ | |
/// <summary> | |
/// Power Volume changes the color according to the number of shares (or contracts) traded during a specified time frame (e.g. hour, day, week, month, etc). | |
/// </summary> | |
public class PowerVolume : Indicator | |
{ | |
private SMA sma; | |
protected override void OnStateChange() | |
{ | |
if (State == State.SetDefaults) | |
{ | |
Description = @"Displays power candles."; | |
Name = "Power Volume"; | |
BarsRequiredToPlot = 256; | |
Calculate = Calculate.OnEachTick; | |
DrawOnPricePanel = false; | |
IsSuspendedWhileInactive = true; | |
Period = 20; | |
AddPlot(new Stroke(Brushes.Gray, 2), PlotStyle.Bar, "Volume"); | |
AddPlot(Brushes.Goldenrod, "SMA"); | |
AddLine(Brushes.DarkGray, 0, Custom.Resource.NinjaScriptIndicatorZeroLine); | |
} | |
else if (State == State.DataLoaded) { | |
sma = SMA(Volume, Period); | |
} | |
else if (State == State.Historical) | |
{ | |
if (Calculate == Calculate.OnPriceChange) | |
{ | |
Draw.TextFixed(this, "NinjaScriptInfo", string.Format(Custom.Resource.NinjaScriptOnPriceChangeError, Name), TextPosition.BottomRight); | |
Log(string.Format(Custom.Resource.NinjaScriptOnPriceChangeError, Name), LogLevel.Error); | |
} | |
} | |
} | |
protected override void OnBarUpdate() | |
{ | |
double average0 = sma[0]; | |
double volume0 = Volume[0]; | |
Vol[0] = Instrument.MasterInstrument.InstrumentType == InstrumentType.CryptoCurrency ? Core.Globals.ToCryptocurrencyVolume((long)volume0) : volume0; | |
Average[0] = Instrument.MasterInstrument.InstrumentType == InstrumentType.CryptoCurrency ? Core.Globals.ToCryptocurrencyVolume((long)average0) : average0; | |
if (volume0 > average0) { | |
PlotBrushes[0][0] = Close[0] > Close[1] ? Brushes.Green : Brushes.Red; | |
} else { | |
PlotBrushes[0][0] = Brushes.Gray; | |
} | |
} | |
#region Properties | |
[Browsable(false)] | |
[XmlIgnore()] | |
public Series<double> Vol | |
{ | |
get { return Values[0]; } | |
} | |
[Browsable(false)] | |
[XmlIgnore()] | |
public Series<double> Average | |
{ | |
get { return Values[1]; } | |
} | |
[Range(1, int.MaxValue), NinjaScriptProperty] | |
[Display(ResourceType = typeof(Custom.Resource), Name = "Period", GroupName = "NinjaScriptParameters", Order = 0)] | |
public int Period | |
{ get; set; } | |
#endregion | |
} | |
} | |
#region NinjaScript generated code. Neither change nor remove. | |
namespace NinjaTrader.NinjaScript.Indicators | |
{ | |
public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase | |
{ | |
private PowerVolume[] cachePowerVolume; | |
public PowerVolume PowerVolume(int period) | |
{ | |
return PowerVolume(Input, period); | |
} | |
public PowerVolume PowerVolume(ISeries<double> input, int period) | |
{ | |
if (cachePowerVolume != null) | |
for (int idx = 0; idx < cachePowerVolume.Length; idx++) | |
if (cachePowerVolume[idx] != null && cachePowerVolume[idx].Period == period && cachePowerVolume[idx].EqualsInput(input)) | |
return cachePowerVolume[idx]; | |
return CacheIndicator<PowerVolume>(new PowerVolume(){ Period = period }, input, ref cachePowerVolume); | |
} | |
} | |
} | |
namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns | |
{ | |
public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase | |
{ | |
public Indicators.PowerVolume PowerVolume(int period) | |
{ | |
return indicator.PowerVolume(Input, period); | |
} | |
public Indicators.PowerVolume PowerVolume(ISeries<double> input , int period) | |
{ | |
return indicator.PowerVolume(input, period); | |
} | |
} | |
} | |
namespace NinjaTrader.NinjaScript.Strategies | |
{ | |
public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase | |
{ | |
public Indicators.PowerVolume PowerVolume(int period) | |
{ | |
return indicator.PowerVolume(Input, period); | |
} | |
public Indicators.PowerVolume PowerVolume(ISeries<double> input , int period) | |
{ | |
return indicator.PowerVolume(input, period); | |
} | |
} | |
} | |
#endregion |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment