refactor 1m rule with detector pattern

This commit is contained in:
c9s 2020-06-17 13:57:51 +08:00
parent ad41c14521
commit cf7760e72a

View File

@ -40,16 +40,14 @@ func (k KLine) Mid() float64 {
func (k KLine) BounceUp() bool { func (k KLine) BounceUp() bool {
mid := k.Mid() mid := k.Mid()
trend := k.GetTrend() trend := k.GetTrend()
thickness := k.GetThickness() return trend > 0 && k.GetOpen() > mid && k.GetClose() > mid
return trend > 0 && k.GetOpen() > mid && k.GetClose() > mid && thickness < (1.0/4.0)
} }
// red candle with open and close near low price // red candle with open and close near low price
func (k KLine) BounceDown() bool { func (k KLine) BounceDown() bool {
mid := k.Mid() mid := k.Mid()
trend := k.GetTrend() trend := k.GetTrend()
thickness := k.GetThickness() return trend > 0 && k.GetOpen() < mid && k.GetClose() < mid
return trend > 0 && k.GetOpen() < mid && k.GetClose() < mid && thickness < (1.0/4.0)
} }
func (k KLine) GetTrend() int { func (k KLine) GetTrend() int {
@ -300,6 +298,38 @@ func (k *KLineWindow) Truncate(size int) {
*k = (*k)[end-5 : end] *k = (*k)[end-5 : end]
} }
func (k KLineWindow) GetBody() float64 {
return k.GetChange()
}
func (k KLineWindow) GetThickness() float64 {
return math.Abs(k.GetChange()) / math.Abs(k.GetMaxChange())
}
func (k KLineWindow) GetUpperShadowRatio() float64 {
return k.GetUpperShadowHeight() / math.Abs(k.GetMaxChange())
}
func (k KLineWindow) GetUpperShadowHeight() float64 {
high := k.GetHigh()
if k.GetOpen() > k.GetClose() {
return high - k.GetOpen()
}
return high - k.GetClose()
}
func (k KLineWindow) GetLowerShadowRatio() float64 {
return k.GetLowerShadowHeight() / math.Abs(k.GetMaxChange())
}
func (k KLineWindow) GetLowerShadowHeight() float64 {
low := k.GetLow()
if k.GetOpen() < k.GetClose() {
return k.GetOpen() - low
}
return k.GetClose() - low
}
func formatVolume(val float64) string { func formatVolume(val float64) string {
return strconv.FormatFloat(val, 'f', 6, 64) return strconv.FormatFloat(val, 'f', 6, 64)