public override double Update(Bar bar) { double mahl = _ema.Update(bar.HighestPrice - bar.LowestPrice); _mahl.Add(mahl); int index = _mahl.Length < _interval ? 0 : _mahl.Length - _interval; if (_mahl[index] == 0.0) { return(0.0); } else { return((_mahl[_mahl.Length - 1] - _mahl[index]) / _mahl[index] * 100.0); } }
public override double Update(StockAnalysis.Share.Bar bar) { double truePrice = (bar.HighestPrice + bar.LowestPrice + 2 * bar.ClosePrice) / 4; _truePrices.Add(truePrice); double maTruePrice = _maTruePrice.Update(truePrice); double sum = 0.0; for (int i = 0; i < _truePrices.Length; ++i) { sum += Math.Abs(_truePrices[i] - maTruePrice); } double d = sum / _truePrices.Length; return(d == 0.0 ? 0.0 : (truePrice - maTruePrice) / d / Alpha); }
public override double[] Update(StockAnalysis.Share.Bar bar) { // calculate +DM and -DM double pdm, ndm; if (_firstBar) { pdm = 0.0; ndm = 0.0; } else { pdm = Math.Max(0.0, bar.HighestPrice - _prevBar.HighestPrice); ndm = Math.Max(0.0, _prevBar.LowestPrice - bar.LowestPrice); if (pdm > ndm) { ndm = 0.0; } else if (pdm < ndm) { pdm = 0.0; } else { pdm = ndm = 0.0; } } // Calculate +DI and -DI double tr, pdi, ndi; if (_firstBar) { tr = bar.HighestPrice - bar.LowestPrice; } else { tr = Math.Max(Math.Abs(bar.HighestPrice - bar.LowestPrice), Math.Max(Math.Abs(bar.HighestPrice - _prevBar.ClosePrice), Math.Abs(bar.LowestPrice - _prevBar.ClosePrice))); } pdi = pdm * 100.0 / tr; ndi = ndm * 100.0 / tr; // calculate +DIM and -DIM double mspdm = _msPdm.Update(pdm); double msndm = _msNdm.Update(ndm); double mstr = _msTr.Update(tr); double pdim = mspdm * 100.0 / mstr; double ndim = msndm * 100.0 / mstr; // calculate DX and ADX double dx = (pdim + ndim) == 0.0 ? 0.0 : Math.Abs(pdim - ndim) / (pdim + ndim); double adx = _maDx.Update(dx); // calculate ADXR _adx.Add(adx); double adxr = (_adx[_adx.Length - 1] + _adx[0]) / 2.0; // update internal status _prevBar = bar; _firstBar = false; // return result return(new double[4] { pdim, ndim, adx, adxr }); }