gitee.com/quant1x/engine@v1.8.4/realtime/moving_average.go (about)

     1  package realtime
     2  
     3  import (
     4  	"gitee.com/quant1x/engine/factors"
     5  	"gitee.com/quant1x/num"
     6  	"gitee.com/quant1x/pandas"
     7  	"gitee.com/quant1x/pandas/formula"
     8  )
     9  
    10  // MovingAverage 计算均线范围
    11  func MovingAverage(CLOSE, HIGH, LOW pandas.Series, PN int) (ma, half, maMax, maMin pandas.Series) {
    12  	half = formula.MA(CLOSE, PN-1)
    13  	r1Half := formula.REF(half, 1)
    14  	maMax = r1Half.Mul(PN - 1).Add(HIGH).Div(PN)
    15  	maMin = r1Half.Mul(PN - 1).Add(LOW).Div(PN)
    16  	ma = r1Half.Mul(PN - 1).Add(CLOSE).Div(PN)
    17  	return ma, half, maMax, maMin
    18  }
    19  
    20  // IncrementalMovingAverage 增量计算移动平均线
    21  //
    22  //	period 周期数
    23  //	previousHalfValue 前period-1的平均值
    24  //	price 现价
    25  func IncrementalMovingAverage(previousHalfValue float64, period int, price float64) float64 {
    26  	n := float64(period)
    27  	sum := previousHalfValue*(n-1) + price
    28  	ma := num.Decimal(sum / n)
    29  	return ma
    30  }
    31  
    32  // DynamicMovingAverage 增量计算移动平均线的范围
    33  //
    34  //	period 周期数
    35  //	previousHalfValue 前period-1的平均值
    36  //	price 现价
    37  func DynamicMovingAverage(previousHalfValue float64, period int, snapshot factors.QuoteSnapshot) (ma, maHigh, maLow float64) {
    38  	ma = IncrementalMovingAverage(previousHalfValue, period, snapshot.Price)
    39  	maHigh = IncrementalMovingAverage(previousHalfValue, period, snapshot.High)
    40  	maLow = IncrementalMovingAverage(previousHalfValue, period, snapshot.Low)
    41  	return ma, maHigh, maLow
    42  }