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

     1  package realtime
     2  
     3  import (
     4  	"gitee.com/quant1x/engine/factors"
     5  	"gitee.com/quant1x/pandas"
     6  	. "gitee.com/quant1x/pandas/formula"
     7  )
     8  
     9  // MovingAverageConvergenceDivergence 平滑异同移动平均线(Moving Average Convergence Divergence)
    10  //
    11  //	返回最新一条macd的5个值
    12  //	DIF:EMA(CLOSE,SHORT)-EMA(CLOSE,LONG);
    13  //	DEA:EMA(DIF,MID)
    14  //	MACD:(DIF-DEA)*2,COLORSTICK;
    15  func MovingAverageConvergenceDivergence(CLOSE pandas.Series, pShort, pLong, pMid int) (SHORT, LONG, DIF, DEA, MACD float64) {
    16  	//dif1 := df1["d1"].(float64)
    17  	//	dif2 := df1["d2"].(float64)
    18  	//	dif1 = EmaIncr(lastClose, dif1, AlphaOfEMA(SHORT))
    19  	//	dif2 = EmaIncr(lastClose, dif2, AlphaOfEMA(LONG))
    20  	//	dif := dif1 - dif2
    21  	//	lastDif := df1["DIF"].(float64)
    22  	//	lastDea := df1["DEA"].(float64)
    23  	//	fmt.Println("lastDif", lastDif)
    24  	//	alpha := AlphaOfEMA(MID)
    25  	//	fmt.Println("dea-alpha:", alpha)
    26  	//	//t1 := -0.446
    27  	//	//fmt.Println("xx:", (1-alpha)*lastDif+alpha*dif)
    28  	//	dea := EmaIncr(dif, lastDea, AlphaOfEMA(MID))
    29  	//	macd := (dif - dea) * 2
    30  	s := EMA(CLOSE, pShort)
    31  	l := EMA(CLOSE, pLong)
    32  	dif := s.Sub(l)
    33  	dea := EMA(dif, pMid)
    34  	macd := dif.Sub(dea).Mul(2)
    35  	SHORT = s.IndexOf(-1).(float64)
    36  	LONG = l.IndexOf(-1).(float64)
    37  	DIF = dif.IndexOf(-1).(float64)
    38  	DEA = dea.IndexOf(-1).(float64)
    39  	MACD = macd.IndexOf(-1).(float64)
    40  	return
    41  }
    42  
    43  // IncrementalMovingAverageConvergenceDivergence 增量的MACD
    44  //
    45  //	price 为现价
    46  //	lastShort, lastLong, lastDea, 缓存的短,长周期的ema, 以及最后一条dea
    47  //	pShort, pLong, pMid, 为短期,长期和中期周期数
    48  func IncrementalMovingAverageConvergenceDivergence(price, lastShort, lastLong, lastDea float64, pShort, pLong, pMid int) (DIF, DEA, MACD float64) {
    49  	s := IncrementalExponentialMovingAverage(price, lastShort, AlphaOfExponentialMovingAverage(pShort))
    50  	l := IncrementalExponentialMovingAverage(price, lastLong, AlphaOfExponentialMovingAverage(pLong))
    51  	dif := s - l
    52  	dea := IncrementalExponentialMovingAverage(dif, lastDea, AlphaOfExponentialMovingAverage(pMid))
    53  	macd := (dif - dea) * 2
    54  	DIF = dif
    55  	DEA = dea
    56  	MACD = macd
    57  	return
    58  }
    59  
    60  // DynamicMovingAverageConvergenceDivergence 动态的MACD
    61  //
    62  //	返回当前值以及最高值和最低值
    63  //	price 为现价
    64  //	lastShort, lastLong, lastDea, 缓存的短,长周期的ema, 以及最后一条dea
    65  //	pShort, pLong, pMid, 为短期,长期和中期周期数
    66  func DynamicMovingAverageConvergenceDivergence(snapshot factors.QuoteSnapshot, lastShort, lastLong, lastDea float64, pShort, pLong, pMid int) (macd, macdHigh, macdLow float64) {
    67  	_, _, macd = IncrementalMovingAverageConvergenceDivergence(snapshot.Price, lastShort, lastLong, lastDea, pShort, pLong, pMid)
    68  	_, _, macdHigh = IncrementalMovingAverageConvergenceDivergence(snapshot.High, lastShort, lastLong, lastDea, pShort, pLong, pMid)
    69  	_, _, macdLow = IncrementalMovingAverageConvergenceDivergence(snapshot.Low, lastShort, lastLong, lastDea, pShort, pLong, pMid)
    70  	return
    71  }