From 6f763721c679221e97e6e09e918783f77ca97b4e Mon Sep 17 00:00:00 2001 From: moshferatu Date: Tue, 19 Nov 2024 08:08:17 -0800 Subject: [PATCH] Initial commit of Put / Cal Ratio Highs and Put / Call Ratio Lows strategies --- .../PutCallRatioHighs.cs | 71 +++++++++++++++++++ .../PutCallRatioLows.cs | 71 +++++++++++++++++++ 2 files changed, 142 insertions(+) create mode 100644 strategies/put-call-ratio-highs-and-lows/PutCallRatioHighs.cs create mode 100644 strategies/put-call-ratio-highs-and-lows/PutCallRatioLows.cs diff --git a/strategies/put-call-ratio-highs-and-lows/PutCallRatioHighs.cs b/strategies/put-call-ratio-highs-and-lows/PutCallRatioHighs.cs new file mode 100644 index 0000000..24d6ac7 --- /dev/null +++ b/strategies/put-call-ratio-highs-and-lows/PutCallRatioHighs.cs @@ -0,0 +1,71 @@ +#region Using declarations +using NinjaTrader.NinjaScript.Indicators; +using System.ComponentModel.DataAnnotations; +#endregion + +namespace NinjaTrader.NinjaScript.Strategies +{ + public class PutCallRatioHighs : Strategy + { + private const int PrimaryBars = 0; + + private PutCallRatio PCR; + private MAX PCRHighs; + private SMA LongTermTrend; + + protected override void OnStateChange() + { + if (State == State.SetDefaults) + { + Name = "Put / Call Ratio Highs"; + Description = @"Based on chatper 9 of How Markets Really Work (2012) by Larry Connors"; + Calculate = Calculate.OnBarClose; + EntriesPerDirection = 1; + + HighPeriod = 5; + LongTermTrendPeriod = 200; + DaysToExit = 5; + } + else if (State == State.Configure) + { + AddDataSeries("VPOT"); + AddDataSeries("VCOT"); + } + else if (State == State.DataLoaded) + { + PCR = PutCallRatio(); + PCRHighs = MAX(PCR, HighPeriod); + LongTermTrend = SMA(LongTermTrendPeriod); + } + } + + protected override void OnBarUpdate() + { + if (PrimaryBars != BarsInProgress) + return; + + if (PCR[0] == PCRHighs[0] && Close[0] > LongTermTrend[0]) + EnterLong(); + + if (BarsSinceEntryExecution(PrimaryBars, "", 0) >= DaysToExit) + ExitLong(); + } + + public override string DisplayName + { + get { return Name; } + } + + [NinjaScriptProperty] + [Display(Name = "High Period", GroupName = "Put / Call Ratio Highs", Order = 1)] + public int HighPeriod { get; set; } + + [NinjaScriptProperty] + [Display(Name = "Long-Term Trend Period", GroupName = "Put / Call Ratio Highs", Order = 2)] + public int LongTermTrendPeriod { get; set; } + + [NinjaScriptProperty] + [Display(Name = "Days to Exit", GroupName = "Put / Call Ratio Highs", Order = 3)] + public int DaysToExit { get; set; } + } +} diff --git a/strategies/put-call-ratio-highs-and-lows/PutCallRatioLows.cs b/strategies/put-call-ratio-highs-and-lows/PutCallRatioLows.cs new file mode 100644 index 0000000..40d917b --- /dev/null +++ b/strategies/put-call-ratio-highs-and-lows/PutCallRatioLows.cs @@ -0,0 +1,71 @@ +#region Using declarations +using NinjaTrader.NinjaScript.Indicators; +using System.ComponentModel.DataAnnotations; +#endregion + +namespace NinjaTrader.NinjaScript.Strategies +{ + public class PutCallRatioLows : Strategy + { + private const int PrimaryBars = 0; + + private PutCallRatio PCR; + private MIN PCRLows; + private SMA LongTermTrend; + + protected override void OnStateChange() + { + if (State == State.SetDefaults) + { + Name = "Put / Call Ratio Lows"; + Description = @"Based on chatper 9 of How Markets Really Work (2012) by Larry Connors"; + Calculate = Calculate.OnBarClose; + EntriesPerDirection = 1; + + LowPeriod = 5; + LongTermTrendPeriod = 200; + DaysToExit = 5; + } + else if (State == State.Configure) + { + AddDataSeries("VPOT"); + AddDataSeries("VCOT"); + } + else if (State == State.DataLoaded) + { + PCR = PutCallRatio(); + PCRLows = MIN(PCR, LowPeriod); + LongTermTrend = SMA(LongTermTrendPeriod); + } + } + + protected override void OnBarUpdate() + { + if (PrimaryBars != BarsInProgress) + return; + + if (PCR[0] == PCRLows[0] && Close[0] > LongTermTrend[0]) + EnterLong(); + + if (BarsSinceEntryExecution(PrimaryBars, "", 0) >= DaysToExit) + ExitLong(); + } + + public override string DisplayName + { + get { return Name; } + } + + [NinjaScriptProperty] + [Display(Name = "Low Period", GroupName = "Put / Call Ratio Lows", Order = 1)] + public int LowPeriod { get; set; } + + [NinjaScriptProperty] + [Display(Name = "Long-Term Trend Period", GroupName = "Put / Call Ratio Lows", Order = 2)] + public int LongTermTrendPeriod { get; set; } + + [NinjaScriptProperty] + [Display(Name = "Days to Exit", GroupName = "Put / Call Ratio Lows", Order = 3)] + public int DaysToExit { get; set; } + } +}