From e00ceedeb01bc8617d854b4717a48b3978cf6250 Mon Sep 17 00:00:00 2001 From: moshferatu Date: Tue, 19 Nov 2024 07:55:31 -0800 Subject: [PATCH] Initial commit of Put / Call Ratio indicator --- indicators/put-call-ratio/PutCallRatio.cs | 108 ++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 indicators/put-call-ratio/PutCallRatio.cs diff --git a/indicators/put-call-ratio/PutCallRatio.cs b/indicators/put-call-ratio/PutCallRatio.cs new file mode 100644 index 0000000..15c1bb7 --- /dev/null +++ b/indicators/put-call-ratio/PutCallRatio.cs @@ -0,0 +1,108 @@ +#region Using declarations +using NinjaTrader.Gui; +using System.Windows.Media; +#endregion + +namespace NinjaTrader.NinjaScript.Indicators +{ + public class PutCallRatio : Indicator + { + private const int PrimaryBars = 0; + private const int PutVolumeBars = 1; + private const int CallColumeBars = 2; + + protected override void OnStateChange() + { + if (State == State.SetDefaults) + { + Name = "Put / Call Ratio"; + Description = @"Total Put Options Volume / Total Call Options Volume"; + Calculate = Calculate.OnPriceChange; + IsOverlay = false; + + AddPlot(new Stroke(Brushes.Yellow, DashStyleHelper.Solid, 3), PlotStyle.Line, "Put / Call Ratio"); + } + else if (State == State.Configure) + { + AddDataSeries("VPOT"); + AddDataSeries("VCOT"); + } + } + + protected override void OnBarUpdate() + { + if (PrimaryBars != BarsInProgress) + return; + + if (CurrentBars[PutVolumeBars] < 1 || CurrentBars[CallColumeBars] < 1) + return; + + double putVolume = BarsArray[PutVolumeBars].GetClose(BarsArray[PutVolumeBars].GetBar(Time[0])); + double callVolume = BarsArray[CallColumeBars].GetClose(BarsArray[CallColumeBars].GetBar(Time[0])); + + Value[0] = putVolume / callVolume; + } + + public override string DisplayName + { + get { return Name; } + } + } +} + +#region NinjaScript generated code. Neither change nor remove. + +namespace NinjaTrader.NinjaScript.Indicators +{ + public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase + { + private PutCallRatio[] cachePutCallRatio; + public PutCallRatio PutCallRatio() + { + return PutCallRatio(Input); + } + + public PutCallRatio PutCallRatio(ISeries input) + { + if (cachePutCallRatio != null) + for (int idx = 0; idx < cachePutCallRatio.Length; idx++) + if (cachePutCallRatio[idx] != null && cachePutCallRatio[idx].EqualsInput(input)) + return cachePutCallRatio[idx]; + return CacheIndicator(new PutCallRatio(), input, ref cachePutCallRatio); + } + } +} + +namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns +{ + public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase + { + public Indicators.PutCallRatio PutCallRatio() + { + return indicator.PutCallRatio(Input); + } + + public Indicators.PutCallRatio PutCallRatio(ISeries input ) + { + return indicator.PutCallRatio(input); + } + } +} + +namespace NinjaTrader.NinjaScript.Strategies +{ + public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase + { + public Indicators.PutCallRatio PutCallRatio() + { + return indicator.PutCallRatio(Input); + } + + public Indicators.PutCallRatio PutCallRatio(ISeries input ) + { + return indicator.PutCallRatio(input); + } + } +} + +#endregion