|
|
|
@ -5,92 +5,51 @@ import ( |
|
|
|
"sig-pub/pkg/types" |
|
|
|
"sig-pub/pkg/types" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
// BollMB 布林带中轨
|
|
|
|
// Boll 布林带
|
|
|
|
type BollMB struct { |
|
|
|
type Boll struct { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (c *BollMB) Meta() IndicatorMeta { |
|
|
|
func (c *Boll) Meta() IndicatorMeta { |
|
|
|
return IndicatorMeta{ |
|
|
|
return IndicatorMeta{ |
|
|
|
Name: "BollMB", |
|
|
|
Name: "Boll", |
|
|
|
Input: []types.InputArg{ |
|
|
|
Input: []types.InputArg{ |
|
|
|
{Name: "window", Type: types.InputTypeUInt, Desc: "窗口大小"}, |
|
|
|
{Name: "window", Type: types.InputTypeUInt, Desc: "窗口大小"}, |
|
|
|
|
|
|
|
{Name: "pt", Type: types.InputTypeKPriceType, Desc: "k线序列类型"}, |
|
|
|
}, |
|
|
|
}, |
|
|
|
} |
|
|
|
State: []string{"ub", "lb"}, |
|
|
|
} |
|
|
|
Plots: []Plot{ |
|
|
|
|
|
|
|
{Name: "中轨", State: "vector", Type: PlotHistogram, Props: PlotProps{"color": ColorOrange}}, |
|
|
|
func (c *BollMB) CandlePeriods(ctx IIndicatorContext) int16 { |
|
|
|
{Name: "上轨", State: "ub", Type: PlotLine, Props: PlotProps{"color": ColorRed2}}, |
|
|
|
return ctx.Input().Int16("window") |
|
|
|
{Name: "下轨", State: "lb", Type: PlotLine, Props: PlotProps{"color": ColorRed2}}, |
|
|
|
} |
|
|
|
{Name: "布林带阴影", State: "ub,lb", Type: PlotShadow, Props: PlotProps{"color": "rgba(247, 169, 167, 0.3)"}}, |
|
|
|
|
|
|
|
|
|
|
|
func (c *BollMB) Calculate(ctx IIndicatorContext) (vector float64) { |
|
|
|
|
|
|
|
window := ctx.Input().Int16("window") |
|
|
|
|
|
|
|
closeSeries := ctx.Series(0, int16(window)).Close() |
|
|
|
|
|
|
|
vector = closeSeries.Avg() |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// BollUB 布林带上轨
|
|
|
|
|
|
|
|
type BollUB struct { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (c *BollUB) Meta() IndicatorMeta { |
|
|
|
|
|
|
|
return IndicatorMeta{ |
|
|
|
|
|
|
|
Name: "BollUB", |
|
|
|
|
|
|
|
Input: []types.InputArg{ |
|
|
|
|
|
|
|
{Name: "window", Type: types.InputTypeUInt, Desc: "窗口大小"}, |
|
|
|
|
|
|
|
}, |
|
|
|
}, |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (c *BollUB) CandlePeriods(ctx IIndicatorContext) int16 { |
|
|
|
func (c *Boll) CandlePeriods(ctx IIndicatorContext) int16 { |
|
|
|
return ctx.Input().Int16("window") |
|
|
|
return ctx.Input().Int16("window") |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (c *BollUB) Calculate(ctx IIndicatorContext) (vector float64) { |
|
|
|
func (c *Boll) Calculate(ctx IIndicatorContext) (vector float64) { |
|
|
|
window := ctx.Input().Int16("window") |
|
|
|
window := ctx.Input().Int16("window") |
|
|
|
closeSeries := ctx.Series(0, int16(window)).Close() |
|
|
|
pt := ctx.Input().PriceType() |
|
|
|
mb := closeSeries.Avg() |
|
|
|
priceSeries := ctx.Series(0, int16(window)).Price(pt) |
|
|
|
|
|
|
|
mb := priceSeries.Avg() // 中轨
|
|
|
|
|
|
|
|
vector = mb |
|
|
|
|
|
|
|
|
|
|
|
// 标准差σ_t = sqrt(∑(P-MB)^2 / (n-1))
|
|
|
|
// 标准差σ_t = sqrt(∑(P-MB)^2 / (n-1))
|
|
|
|
sst := float64(0) |
|
|
|
sst := float64(0) |
|
|
|
for _, p := range closeSeries { |
|
|
|
for _, p := range priceSeries { |
|
|
|
sst += math.Pow(p-mb, 2) |
|
|
|
sst += math.Pow(p-mb, 2) |
|
|
|
} |
|
|
|
} |
|
|
|
sigma := math.Sqrt(sst / float64(window-1)) |
|
|
|
sigma := math.Sqrt(sst / float64(window-1)) |
|
|
|
|
|
|
|
|
|
|
|
vector = mb + 2*sigma |
|
|
|
// BollUB 布林带上轨
|
|
|
|
return |
|
|
|
ub := mb + 2*sigma |
|
|
|
} |
|
|
|
ctx.State().Set("ub", ub) |
|
|
|
|
|
|
|
|
|
|
|
// BollLB 布林带下轨
|
|
|
|
// BollLB 布林带下轨
|
|
|
|
type BollLB struct { |
|
|
|
lb := mb - 2*sigma |
|
|
|
} |
|
|
|
ctx.State().Set("lb", lb) |
|
|
|
|
|
|
|
|
|
|
|
func (c *BollLB) Meta() IndicatorMeta { |
|
|
|
|
|
|
|
return IndicatorMeta{ |
|
|
|
|
|
|
|
Name: "BollLB", |
|
|
|
|
|
|
|
Input: []types.InputArg{ |
|
|
|
|
|
|
|
{Name: "window", Type: types.InputTypeUInt, Desc: "窗口大小"}, |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (c *BollLB) CandlePeriods(ctx IIndicatorContext) int16 { |
|
|
|
|
|
|
|
return ctx.Input().Int16("window") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (c *BollLB) Calculate(ctx IIndicatorContext) (vector float64) { |
|
|
|
|
|
|
|
window := ctx.Input().Int16("window") |
|
|
|
|
|
|
|
closeSeries := ctx.Series(0, int16(window)).Close() |
|
|
|
|
|
|
|
mb := closeSeries.Avg() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 标准差σ_t = sqrt(∑(P-MB)^2 / (n-1))
|
|
|
|
|
|
|
|
sst := float64(0) |
|
|
|
|
|
|
|
for _, p := range closeSeries { |
|
|
|
|
|
|
|
sst += math.Pow(p-mb, 2) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
sigma := math.Sqrt(sst / float64(window-1)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
vector = mb - 2*sigma |
|
|
|
|
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
|