public static decimal Get(ISymbol symbol, int bar, int period, MaMethod method = MaMethod.Sma, AppliedPrice appliedPrice = AppliedPrice.Close) { decimal price = 0M; for (int i = 0; i < period; ++i) { int idx = Math.Min(bar + i, symbol.Candles.Count - 1); switch (appliedPrice) { case AppliedPrice.Close: price += symbol.Candles[idx].Close; break; case AppliedPrice.Open: price += symbol.Candles[idx].Open; break; case AppliedPrice.High: price += symbol.Candles[idx].High; break; case AppliedPrice.Low: price += symbol.Candles[idx].Low; break; } } return(price / ((decimal)period)); }
/// <summary> /// 移動平均を取得します。 /// </summary> /// <param name="data">対象データ</param> /// <param name="method">移動平均の計算方法</param> /// <param name="prev_ma">前回の移動平均</param> /// <returns>移動平均</returns> public static decimal GetMovingAverage(decimal[] data, MaMethod method, decimal?prev_ma) { switch (method) { case MaMethod.Sma: { return(data.Average()); } case MaMethod.Ema: case MaMethod.Smma: { // 係数 decimal a = 0; if (method == MaMethod.Ema) { a = 2.0m / (decimal)(data.Length + 1); } else { a = 1.0m / (decimal)data.Length; } // 初回の移動平均 if (prev_ma == null) { prev_ma = data.Last(); } return(a * data[0] + (1 - a) * prev_ma.Value); } case MaMethod.Lwma: { decimal sum1 = 0, sum2 = 0; for (int i = 0; i < data.Length; i++) { sum1 += (data.Length - i) * data[i]; sum2 += i + 1; } return(sum1 / sum2); } } return(0); }
// todo: total, ma_shift and maMethod are not used atm... public static decimal OnArray(decimal[] array, int total, int maPeriod, int ma_shift, MaMethod maMethod, int shift) { decimal price = 0M; for (int i = 0; i < maPeriod; ++i) { price += array[shift + i]; } return(price / ((decimal)maPeriod)); }