39 private readonly
Symbol _referenceSymbol;
44 private readonly
Symbol _targetSymbol;
49 private readonly decimal _alphaPeriod;
64 private decimal _alpha;
69 private readonly
Beta _beta;
84 public override bool IsReady => _targetROC.IsReady && _beta.IsReady && _referenceROC.IsReady;
99 if (targetSymbol == referenceSymbol)
101 throw new ArgumentException(
"The target and reference symbols cannot be the same.");
107 throw new ArgumentException(
"The period must be equal or greater than 1.");
113 throw new ArgumentException(
"The beta period must be equal or greater than 2.");
116 _targetSymbol = targetSymbol;
117 _referenceSymbol = referenceSymbol;
118 _alphaPeriod = alphaPeriod;
119 _riskFreeInterestRateModel = riskFreeRateModel;
121 _targetROC =
new RateOfChange($
"{name}_TargetROC", alphaPeriod);
122 _referenceROC =
new RateOfChange($
"{name}_ReferenceROC", alphaPeriod);
124 _beta =
new Beta($
"{name}_Beta", _targetSymbol, _referenceSymbol, betaPeriod);
126 WarmUpPeriod = alphaPeriod >= betaPeriod ? alphaPeriod + 1 : betaPeriod + 1;
140 public Alpha(
string name,
Symbol targetSymbol,
Symbol referenceSymbol,
int alphaPeriod,
int betaPeriod, decimal? riskFreeRate =
null)
153 public Alpha(
Symbol targetSymbol,
Symbol referenceSymbol,
int alphaPeriod,
int betaPeriod, decimal? riskFreeRate =
null)
154 : this($
"ALPHA({targetSymbol},{referenceSymbol},{alphaPeriod},{betaPeriod},{riskFreeRate})", targetSymbol, referenceSymbol, alphaPeriod, betaPeriod, new
ConstantRiskFreeRateInterestRateModel(riskFreeRate ?? 0m))
165 public Alpha(
Symbol targetSymbol,
Symbol referenceSymbol,
int period, decimal? riskFreeRate =
null)
166 : this($
"ALPHA({targetSymbol},{referenceSymbol},{period},{riskFreeRate})", targetSymbol, referenceSymbol, period, period, new
ConstantRiskFreeRateInterestRateModel(riskFreeRate ?? 0m))
178 public Alpha(
string name,
Symbol targetSymbol,
Symbol referenceSymbol,
int period, decimal? riskFreeRate =
null)
192 : this($
"ALPHA({targetSymbol},{referenceSymbol},{alphaPeriod},{betaPeriod})", targetSymbol, referenceSymbol, alphaPeriod, betaPeriod, riskFreeRateModel)
204 : this($
"ALPHA({targetSymbol},{referenceSymbol},{period})", targetSymbol, referenceSymbol, period, period, riskFreeRateModel)
217 : this(name, targetSymbol, referenceSymbol, period, period, riskFreeRateModel)
230 public Alpha(
string name,
Symbol targetSymbol,
Symbol referenceSymbol,
int alphaPeriod,
int betaPeriod, PyObject riskFreeRateModel)
243 public Alpha(
Symbol targetSymbol,
Symbol referenceSymbol,
int alphaPeriod,
int betaPeriod, PyObject riskFreeRateModel)
244 : this($
"ALPHA({targetSymbol},{referenceSymbol},{alphaPeriod},{betaPeriod})", targetSymbol, referenceSymbol, alphaPeriod, betaPeriod,
RiskFreeInterestRateModelPythonWrapper.FromPyObject(riskFreeRateModel))
255 public Alpha(
Symbol targetSymbol,
Symbol referenceSymbol,
int period, PyObject riskFreeRateModel)
268 public Alpha(
string name,
Symbol targetSymbol,
Symbol referenceSymbol,
int period, PyObject riskFreeRateModel)
283 throw new ArgumentNullException(nameof(input));
285 var inputSymbol = input.
Symbol;
287 if (inputSymbol == _targetSymbol)
291 else if (inputSymbol == _referenceSymbol)
297 throw new ArgumentException($
"The input symbol {inputSymbol} is not the target or reference symbol.");
302 if (_targetROC.Samples == _referenceROC.Samples && _referenceROC.Samples > 0)
313 private void ComputeAlpha()
315 if (!_beta.IsReady || !_targetROC.IsReady || !_referenceROC.IsReady)
321 var targetMean = _targetROC.Current.Value / _alphaPeriod;
322 var referenceMean = _referenceROC.Current.Value / _alphaPeriod;
324 var riskFreeRate = _riskFreeInterestRateModel.GetInterestRate(_targetROC.Current.Time);
326 _alpha = targetMean - (riskFreeRate + _beta.Current.Value * (referenceMean - riskFreeRate));
335 _referenceROC.Reset();