10 changed files with 221 additions and 106 deletions
@ -1,110 +1,68 @@ |
|||||||
package indicator |
package indicator |
||||||
|
|
||||||
import ( |
import "sig-pub/pkg/types" |
||||||
"sig-pub/pkg/types" |
|
||||||
) |
|
||||||
|
|
||||||
// Macd macd柱状图计算
|
type MACD struct { |
||||||
|
|
||||||
// Macd 拆分成: Macd(柱状图), MacdDIF线, MacdDEA(信号线)
|
|
||||||
// 计算 MacdDIF 线 (DIF): 反映短期趋势与长期趋势的“收敛/散度”
|
|
||||||
// Macd: https://www.investopedia.com/terms/m/macd.asp
|
|
||||||
type Macd struct { |
|
||||||
} |
} |
||||||
|
|
||||||
func (c *Macd) Meta() IndicatorMeta { |
func (c *MACD) Meta() IndicatorMeta { |
||||||
return IndicatorMeta{ |
return IndicatorMeta{ |
||||||
Name: "Macd", |
Name: "MACD", |
||||||
Input: []types.InputArg{ |
Input: []types.InputArg{ |
||||||
{Name: "fast", Type: types.InputTypeUInt, Desc: "快线周期"}, |
{Name: "fast", Type: types.InputTypeUInt, Desc: "快线周期"}, // 12
|
||||||
{Name: "slow", Type: types.InputTypeUInt, Desc: "慢线周期"}, |
{Name: "slow", Type: types.InputTypeUInt, Desc: "慢线周期"}, // 26
|
||||||
{Name: "singal", Type: types.InputTypeUInt, Desc: "信号线周期"}, |
{Name: "singal", Type: types.InputTypeUInt, Desc: "信号线周期"}, // 9
|
||||||
|
}, |
||||||
|
State: []string{"dif", "dea"}, |
||||||
|
Plot: Plot{ |
||||||
|
Series: PlotSeries{Type: PlotSeriesHistogram, Props: PlotProps{"color": ColorGreen}, State2Props: map[string]map[float64]PlotProps{ |
||||||
|
"vector": { |
||||||
|
-1: {"vector >= 0": 1, "color": ColorRed}, |
||||||
|
1: {"vector < 0": 1, "color": ColorGreen}, |
||||||
|
}, |
||||||
|
}}, |
||||||
|
StateSeries: []PlotSeries{ |
||||||
|
{State: "dif", Type: PlotSeriesLine, Props: PlotProps{"color": ColorYellow}}, |
||||||
|
{State: "dea", Type: PlotSeriesLine, Props: PlotProps{"color": ColorBlue}}, |
||||||
}, |
}, |
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
func (c *Macd) CandlePeriods(ctx IIndicatorContext) int16 { |
|
||||||
return max( |
|
||||||
ctx.Indicator("MacdDIF", ctx.Input()).CandlePeriods(), |
|
||||||
ctx.Indicator("MacdDEA", ctx.Input()).CandlePeriods(), |
|
||||||
) |
|
||||||
} |
|
||||||
|
|
||||||
func (c *Macd) Calculate(ctx IIndicatorContext) (vector float64) { |
|
||||||
macd_dea := ctx.Indicator("MacdDEA", ctx.Input()).Get(0) |
|
||||||
macd_dif := ctx.Indicator("MacdDIF", ctx.Input()).Get(0) |
|
||||||
vector = (macd_dif - macd_dea) * 2 |
|
||||||
return |
|
||||||
} |
|
||||||
|
|
||||||
type MacdDIF struct { |
|
||||||
} |
|
||||||
|
|
||||||
// indicator interface
|
|
||||||
func (c *MacdDIF) Meta() IndicatorMeta { |
|
||||||
return IndicatorMeta{ |
|
||||||
Name: "MacdDIF", |
|
||||||
Input: []types.InputArg{ |
|
||||||
{Name: "fast", Type: types.InputTypeUInt, Desc: "快线周期"}, |
|
||||||
{Name: "slow", Type: types.InputTypeUInt, Desc: "慢线周期"}, |
|
||||||
}, |
}, |
||||||
} |
} |
||||||
} |
} |
||||||
|
|
||||||
func (c *MacdDIF) CandlePeriods(ctx IIndicatorContext) int16 { |
func (c *MACD) CandlePeriods(ctx IIndicatorContext) int16 { |
||||||
return max( |
return max( |
||||||
ctx.Indicator("EMA", ctx.Input().Int16("fast")).CandlePeriods(), |
ctx.Indicator("EMA", ctx.Input().Int16("fast")).CandlePeriods(), |
||||||
ctx.Indicator("EMA", ctx.Input().Int16("slow")).CandlePeriods(), |
ctx.Indicator("EMA", ctx.Input().Int16("slow")).CandlePeriods(), |
||||||
) |
) + ctx.Input().Int16("singal") + 1 |
||||||
} |
} |
||||||
|
|
||||||
// Calculate 计算单根k线sma指标
|
// Calculate macd计算从第max(fast, slow)期开始稳定
|
||||||
func (c *MacdDIF) Calculate(ctx IIndicatorContext) (vector float64) { |
func (c *MACD) Calculate(ctx IIndicatorContext) (vector float64) { |
||||||
fast := ctx.Input().Int16("fast") // 12
|
fast := ctx.Input().Int16("fast") |
||||||
slow := ctx.Input().Int16("slow") // 26
|
slow := ctx.Input().Int16("slow") |
||||||
|
singal := ctx.Input().Int16("singal") |
||||||
|
|
||||||
// macd计算从第max(fast, slow)期开始稳定
|
// macd dif
|
||||||
fastEma := ctx.Indicator("EMA", fast).Get(0) |
fastEma := ctx.Indicator("EMA", fast).Get(0) |
||||||
slowEma := ctx.Indicator("EMA", slow).Get(0) |
slowEma := ctx.Indicator("EMA", slow).Get(0) |
||||||
macd := fastEma - slowEma |
macd_dif := fastEma - slowEma |
||||||
vector = macd |
ctx.State().Set("dif", macd_dif) |
||||||
return |
|
||||||
} |
|
||||||
|
|
||||||
// MacdDEA macd信号线计算
|
|
||||||
type MacdDEA struct { |
|
||||||
} |
|
||||||
|
|
||||||
func (c *MacdDEA) Meta() IndicatorMeta { |
|
||||||
return IndicatorMeta{ |
|
||||||
Name: "MacdDEA", |
|
||||||
Input: []types.InputArg{ |
|
||||||
{Name: "fast", Type: types.InputTypeUInt, Desc: "快线周期"}, |
|
||||||
{Name: "slow", Type: types.InputTypeUInt, Desc: "慢线周期"}, |
|
||||||
{Name: "singal", Type: types.InputTypeUInt, Desc: "信号线周期"}, |
|
||||||
}, |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
func (c *MacdDEA) CandlePeriods(ctx IIndicatorContext) int16 { |
// macd dea
|
||||||
return ctx.Indicator("MacdDIF", ctx.Input()).CandlePeriods() + ctx.Input().Int16("singal") + 1 |
deaPrev, ok := ctx.State().Get("dea", 1) |
||||||
} |
|
||||||
|
|
||||||
func (c *MacdDEA) Calculate(ctx IIndicatorContext) (vector float64) { |
|
||||||
singal := ctx.Input().Int16("singal") // 9
|
|
||||||
|
|
||||||
deaPrev, ok := ctx.State().Get("_vector", 1) |
|
||||||
if !ok { |
if !ok { |
||||||
// 初始值前9期的 MACD_DIF SMA
|
// 初始值前9期的 MACD_DIF SMA
|
||||||
macdDifs := ctx.Indicator("MacdDIF", ctx.Input()).Series(1, singal) |
difs, ok := ctx.State().Series("dif", 1, singal) |
||||||
deaPrev = macdDifs.Avg() |
if !ok { |
||||||
|
return |
||||||
|
} |
||||||
|
deaPrev = difs.Avg() |
||||||
} |
} |
||||||
macd_dif := ctx.Indicator("MacdDIF", ctx.Input()).Get(0) |
|
||||||
// 计算DEA
|
|
||||||
beta := 2 / float64(singal+1) |
|
||||||
dea := beta*macd_dif + (1-beta)*deaPrev |
|
||||||
ctx.State().Set("_vector", dea) |
|
||||||
|
|
||||||
vector = dea |
// macd hist
|
||||||
|
beta := 2 / float64(singal+1) |
||||||
|
macd_dea := beta*macd_dif + (1-beta)*deaPrev |
||||||
|
ctx.State().Set("dea", macd_dea) |
||||||
|
vector = (macd_dif - macd_dea) * 2 |
||||||
return |
return |
||||||
} |
} |
||||||
|
|||||||
@ -0,0 +1,110 @@ |
|||||||
|
package indicator |
||||||
|
|
||||||
|
import ( |
||||||
|
"sig-pub/pkg/types" |
||||||
|
) |
||||||
|
|
||||||
|
// Macd macd柱状图计算
|
||||||
|
|
||||||
|
// Macd 拆分成: Macd(柱状图), MacdDIF线, MacdDEA(信号线)
|
||||||
|
// 计算 MacdDIF 线 (DIF): 反映短期趋势与长期趋势的“收敛/散度”
|
||||||
|
// Macd: https://www.investopedia.com/terms/m/macd.asp
|
||||||
|
type Macd struct { |
||||||
|
} |
||||||
|
|
||||||
|
func (c *Macd) Meta() IndicatorMeta { |
||||||
|
return IndicatorMeta{ |
||||||
|
Name: "Macd", |
||||||
|
Input: []types.InputArg{ |
||||||
|
{Name: "fast", Type: types.InputTypeUInt, Desc: "快线周期"}, |
||||||
|
{Name: "slow", Type: types.InputTypeUInt, Desc: "慢线周期"}, |
||||||
|
{Name: "singal", Type: types.InputTypeUInt, Desc: "信号线周期"}, |
||||||
|
}, |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
func (c *Macd) CandlePeriods(ctx IIndicatorContext) int16 { |
||||||
|
return max( |
||||||
|
ctx.Indicator("MacdDIF", ctx.Input()).CandlePeriods(), |
||||||
|
ctx.Indicator("MacdDEA", ctx.Input()).CandlePeriods(), |
||||||
|
) |
||||||
|
} |
||||||
|
|
||||||
|
func (c *Macd) Calculate(ctx IIndicatorContext) (vector float64) { |
||||||
|
macd_dea := ctx.Indicator("MacdDEA", ctx.Input()).Get(0) |
||||||
|
macd_dif := ctx.Indicator("MacdDIF", ctx.Input()).Get(0) |
||||||
|
vector = (macd_dif - macd_dea) * 2 |
||||||
|
return |
||||||
|
} |
||||||
|
|
||||||
|
type MacdDIF struct { |
||||||
|
} |
||||||
|
|
||||||
|
// indicator interface
|
||||||
|
func (c *MacdDIF) Meta() IndicatorMeta { |
||||||
|
return IndicatorMeta{ |
||||||
|
Name: "MacdDIF", |
||||||
|
Input: []types.InputArg{ |
||||||
|
{Name: "fast", Type: types.InputTypeUInt, Desc: "快线周期"}, |
||||||
|
{Name: "slow", Type: types.InputTypeUInt, Desc: "慢线周期"}, |
||||||
|
}, |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
func (c *MacdDIF) CandlePeriods(ctx IIndicatorContext) int16 { |
||||||
|
return max( |
||||||
|
ctx.Indicator("EMA", ctx.Input().Int16("fast")).CandlePeriods(), |
||||||
|
ctx.Indicator("EMA", ctx.Input().Int16("slow")).CandlePeriods(), |
||||||
|
) |
||||||
|
} |
||||||
|
|
||||||
|
// Calculate 计算单根k线sma指标
|
||||||
|
func (c *MacdDIF) Calculate(ctx IIndicatorContext) (vector float64) { |
||||||
|
fast := ctx.Input().Int16("fast") // 12
|
||||||
|
slow := ctx.Input().Int16("slow") // 26
|
||||||
|
|
||||||
|
// macd计算从第max(fast, slow)期开始稳定
|
||||||
|
fastEma := ctx.Indicator("EMA", fast).Get(0) |
||||||
|
slowEma := ctx.Indicator("EMA", slow).Get(0) |
||||||
|
macd := fastEma - slowEma |
||||||
|
vector = macd |
||||||
|
return |
||||||
|
} |
||||||
|
|
||||||
|
// MacdDEA macd信号线计算
|
||||||
|
type MacdDEA struct { |
||||||
|
} |
||||||
|
|
||||||
|
func (c *MacdDEA) Meta() IndicatorMeta { |
||||||
|
return IndicatorMeta{ |
||||||
|
Name: "MacdDEA", |
||||||
|
Input: []types.InputArg{ |
||||||
|
{Name: "fast", Type: types.InputTypeUInt, Desc: "快线周期"}, |
||||||
|
{Name: "slow", Type: types.InputTypeUInt, Desc: "慢线周期"}, |
||||||
|
{Name: "singal", Type: types.InputTypeUInt, Desc: "信号线周期"}, |
||||||
|
}, |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
func (c *MacdDEA) CandlePeriods(ctx IIndicatorContext) int16 { |
||||||
|
return ctx.Indicator("MacdDIF", ctx.Input()).CandlePeriods() + ctx.Input().Int16("singal") + 1 |
||||||
|
} |
||||||
|
|
||||||
|
func (c *MacdDEA) Calculate(ctx IIndicatorContext) (vector float64) { |
||||||
|
singal := ctx.Input().Int16("singal") // 9
|
||||||
|
|
||||||
|
deaPrev, ok := ctx.State().Get("_vector", 1) |
||||||
|
if !ok { |
||||||
|
// 初始值前9期的 MACD_DIF SMA
|
||||||
|
macdDifs := ctx.Indicator("MacdDIF", ctx.Input()).Series(1, singal) |
||||||
|
deaPrev = macdDifs.Avg() |
||||||
|
} |
||||||
|
macd_dif := ctx.Indicator("MacdDIF", ctx.Input()).Get(0) |
||||||
|
// 计算DEA
|
||||||
|
beta := 2 / float64(singal+1) |
||||||
|
dea := beta*macd_dif + (1-beta)*deaPrev |
||||||
|
ctx.State().Set("_vector", dea) |
||||||
|
|
||||||
|
vector = dea |
||||||
|
return |
||||||
|
} |
||||||
Loading…
Reference in new issue