/// <summary> /// Calculate Ehlers' Zero Lag Exponential Moving Average, as described here: /// <see href="https://en.wikipedia.org/wiki/Zero_lag_exponential_moving_average"/> /// </summary> /// <param name="series">input time series</param> /// <param name="period">averaging length</param> /// <param name="parentId">caller cache id, optional</param> /// <param name="memberName">caller's member name, optional</param> /// <param name="lineNumber">caller line number, optional</param> /// <returns>ZLEMA as time series</returns> public static ITimeSeries <double> ZLEMA(this ITimeSeries <double> series, int period, CacheId parentId = null, [CallerMemberName] string memberName = "", [CallerLineNumber] int lineNumber = 0) { var cacheId = new CacheId(parentId, memberName, lineNumber, series.GetHashCode(), period); int lag = (int)Math.Round((period - 1.0) / 2.0); return(series .Add( series .Subtract( series .Delay(lag, cacheId), cacheId), cacheId) .EMA(period, cacheId)); }
override public void Run() { //---------- initialization // set simulation time frame StartTime = DateTime.Parse("01/01/2015"); EndTime = DateTime.Parse("12/31/2016"); // add instruments AddDataSource(_instrumentNick); //---------- simulation foreach (DateTime simTime in SimTimes) { // find our instrument. if we have only one instrument, // we could also just use Instruments.Values.First() Instrument instrument = FindInstrument(_instrumentNick); // calculate simple indicators // the output of an indicator is a time series ITimeSeries <double> ema26 = instrument.Close.EMA(26); ITimeSeries <double> ema12 = instrument.Close.EMA(12); // therefore, indicators can be calculated on top of indicators ITimeSeries <double> macd = ema12.Subtract(ema26); ITimeSeries <double> signal = macd.EMA(9); // plot our data _plotter.SelectChart("indicators vs time", "date"); _plotter.SetX(simTime.Date); _plotter.Plot(instrument.Symbol, instrument.Close[0] + _offsetPrice); _plotter.Plot("ema26", ema26[0] + _offsetPrice); _plotter.Plot("ema12", ema12[0] + _offsetPrice); _plotter.Plot("macd", macd[0]); _plotter.Plot("signal", signal[0]); } }
/// <summary> /// Calculate Commodity Channel Index of input time series, as described here: /// <see href="https://en.wikipedia.org/wiki/Commodity_channel_index"/> /// </summary> /// <param name="series">input time series</param> /// <param name="n">averaging length</param> /// <param name="parentId">caller cache id, optional</param> /// <param name="memberName">caller's member name, optional</param> /// <param name="lineNumber">caller line number, optional</param> /// <returns>CCI time series</returns> public static ITimeSeries <double> CCI(this ITimeSeries <double> series, int n = 20, CacheId parentId = null, [CallerMemberName] string memberName = "", [CallerLineNumber] int lineNumber = 0) { var cacheId = new CacheId(parentId, memberName, lineNumber, series.GetHashCode(), n); return(IndicatorsBasic.BufferedLambda( (v) => { ITimeSeries <double> delta = series .Subtract( series .SMA(n, cacheId), cacheId); ITimeSeries <double> meanDeviation = delta .AbsValue(cacheId) .SMA(n, cacheId); return delta[0] / Math.Max(1e-10, 0.015 * meanDeviation[0]); }, 0.5, cacheId)); }