Last active
June 7, 2023 18:23
-
-
Save itsnotyoutoday/a1d2a66f7467ab2b51ecc00ef93090fb to your computer and use it in GitHub Desktop.
Squeeze Momentum Indicator for CTrader Based on LazyBear's from TradingView
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
using System; | |
using cAlgo.API; | |
using cAlgo.API.Internals; | |
using cAlgo.API.Indicators; | |
// My crack at duplicating LazyBear's indicator from TradingView - RLB | |
namespace cAlgo.Indicators | |
{ | |
[Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)] | |
public class SqueezeMomentum : Indicator | |
{ | |
[Parameter("Bollinger Period", DefaultValue = 20)] | |
public int Boll_Period { get; set; } | |
[Parameter("Keltner Period", DefaultValue = 20)] | |
public int Keltner_Period { get; set; } | |
[Parameter("Keltner Mul", DefaultValue = 1.5)] | |
public double Keltner_Mul { get; set; } | |
[Output("Up Exhausted", LineColor = "#FF232E28", PlotType = PlotType.Histogram, Thickness = 7)] | |
public IndicatorDataSeries UpDark { get; set; } | |
[Output("Up Strong", LineColor = "#32CD32", PlotType = PlotType.Histogram, Thickness = 7)] | |
public IndicatorDataSeries UpLime { get; set; } | |
[Output("Down Strong", LineColor = "#ff0000", PlotType = PlotType.Histogram, Thickness = 7)] | |
public IndicatorDataSeries Down { get; set; } | |
[Output("Down Exhausted", LineColor = "#FF372124", PlotType = PlotType.Histogram, Thickness = 7)] | |
public IndicatorDataSeries DownMaroon { get; set; } | |
[Output("Off Squeeze", LineColor = "#FF3B3A3B", PlotType = PlotType.Points, Thickness = 8)] | |
public IndicatorDataSeries Off_S { get; set; } | |
[Output("On Squeeze", LineColor = "Green", PlotType = PlotType.Points, Thickness = 8)] | |
public IndicatorDataSeries On_S { get; set; } | |
[Output("No Squeeze", LineColor = "Blue", PlotType = PlotType.Points, Thickness = 8)] | |
public IndicatorDataSeries No_S { get; set; } | |
private IndicatorDataSeries _MACombined; | |
private IndicatorDataSeries _reg; | |
private LinearRegressionSlope _slope; | |
private LinearRegressionIntercept _intercept; | |
private IndicatorDataSeries KCU; | |
private IndicatorDataSeries KCL; | |
private TrueRange tri; | |
private IndicatorDataSeries _upperBB; | |
private IndicatorDataSeries _lowerBB; | |
private MovingAverage BB_MID; | |
private StandardDeviation _deviation; | |
private MovingAverage KMID; | |
protected override void Initialize() | |
{ | |
KMID = Indicators.MovingAverage(MarketSeries.Close, Keltner_Period, MovingAverageType.Simple); | |
KCU = CreateDataSeries(); | |
KCL = CreateDataSeries(); | |
tri = Indicators.TrueRange(); | |
BB_MID = Indicators.MovingAverage(MarketSeries.Close, Boll_Period, MovingAverageType.Simple); | |
_upperBB = CreateDataSeries(); | |
_lowerBB = CreateDataSeries(); | |
_deviation = Indicators.StandardDeviation(MarketSeries.Close, Boll_Period, MovingAverageType.Simple); | |
_MACombined = CreateDataSeries(); | |
_intercept = Indicators.LinearRegressionIntercept(_MACombined, Keltner_Period); | |
_slope = Indicators.LinearRegressionSlope(_MACombined, Keltner_Period); | |
_reg = CreateDataSeries(); | |
} | |
public override void Calculate(int index) | |
{ | |
KCU[index] = KMID.Result[index] + tri.Result[index] * Keltner_Mul; | |
KCL[index] = KMID.Result[index] - tri.Result[index] * Keltner_Mul; | |
double dev = Keltner_Mul * _deviation.Result[index]; | |
_upperBB[index] = BB_MID.Result[index] + dev; | |
_lowerBB[index] = BB_MID.Result[index] - dev; | |
_MACombined[index] = MarketSeries.Close[index] - ((KCU[index] + KCL[index] + KMID.Result[index]) / 3); | |
_reg[index] = ((Keltner_Period * _slope.Result[index] + _intercept.Result[index])); | |
bool sqzOn = (_lowerBB[index] > KCL[index]) && (_upperBB[index] < KCU[index]); | |
bool sqzOff = (_lowerBB[index] < KCL[index]) && (_upperBB[index] > KCU[index]); | |
bool noSqz = (sqzOn == false) && (sqzOff == false); | |
if (noSqz) | |
{ | |
No_S[index] = 0; | |
} | |
else if (sqzOn) | |
{ | |
On_S[index] = 0; | |
} | |
else | |
{ | |
Off_S[index] = 0; | |
} | |
if (_reg[index] > 0) | |
{ | |
if (_reg[index] > _reg[index - 1]) | |
{ | |
UpLime[index] = _reg[index]; | |
} | |
else | |
{ | |
UpDark[index] = _reg[index]; | |
} | |
} | |
else | |
{ | |
if (_reg[index] < _reg[index - 1]) | |
{ | |
Down[index] = _reg[index]; | |
} | |
else | |
{ | |
DownMaroon[index] = _reg[index]; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment