gitee.com/quant1x/num@v0.3.2/cross.go (about)

     1  package num
     2  
     3  // 趋势变化
     4  const (
     5  	Unchanged       = 0  // 趋势不变
     6  	BreakThrough    = 1  // break through 突破
     7  	FallDrastically = -1 // fall drastically 跌破
     8  )
     9  
    10  // LinerTrend 线性趋势
    11  type LinerTrend struct {
    12  	X     int // 索引
    13  	State int // 状态
    14  }
    15  
    16  // Cross 上穿和下穿
    17  //
    18  //	a 上穿或者下穿b的状态集合
    19  func Cross[E Number](a, b []E) []LinerTrend {
    20  	length := len(a)
    21  	list := make([]LinerTrend, length)
    22  	count := 0
    23  	for i := 1; i < length; i++ {
    24  		front := i - 1
    25  		current := i
    26  		if a[front] < b[front] && a[current] > b[current] {
    27  			// a 上穿 b
    28  			list[count] = LinerTrend{X: current, State: BreakThrough}
    29  			count++
    30  		} else if a[front] > b[front] && a[current] < b[current] {
    31  			// a 下穿 b
    32  			list[count] = LinerTrend{X: current, State: FallDrastically}
    33  			count++
    34  		}
    35  	}
    36  	list = list[:count]
    37  	return list
    38  }