add ad,macd,rsi,sma,stoch,vwap,vwma to Series interface

This commit is contained in:
zenix 2022-04-04 13:14:17 +09:00
parent fac61f27dc
commit 567e7bd214
7 changed files with 155 additions and 0 deletions

View File

@ -41,6 +41,20 @@ func (inc *AD) Last() float64 {
return inc.Values[len(inc.Values)-1] return inc.Values[len(inc.Values)-1]
} }
func (inc *AD) Index(i int) float64 {
length := len(inc.Values)
if length == 0 || length - i - 1 < 0 {
return 0
}
return inc.Values[length - i - 1]
}
func (inc *AD) Length() int {
return len(inc.Values)
}
var _ types.Series = &AD{}
func (inc *AD) calculateAndUpdate(kLines []types.KLine) { func (inc *AD) calculateAndUpdate(kLines []types.KLine) {
for _, k := range kLines { for _, k := range kLines {
if inc.EndTime != zeroTime && k.EndTime.Before(inc.EndTime) { if inc.EndTime != zeroTime && k.EndTime.Before(inc.EndTime) {

View File

@ -89,3 +89,34 @@ func (inc *MACD) handleKLineWindowUpdate(interval types.Interval, window types.K
func (inc *MACD) Bind(updater KLineWindowUpdater) { func (inc *MACD) Bind(updater KLineWindowUpdater) {
updater.OnKLineWindowUpdate(inc.handleKLineWindowUpdate) updater.OnKLineWindowUpdate(inc.handleKLineWindowUpdate)
} }
type MACDValues struct {
*MACD
}
func (inc *MACDValues) Last() float64 {
if len(inc.Values) == 0 {
return 0.0
}
return inc.Values[len(inc.Values)-1]
}
func (inc *MACDValues) Index(i int) float64 {
length := len(inc.Values)
if length == 0 || length-1-i < 0 {
return 0.0
}
return inc.Values[length-1+i]
}
func (inc *MACDValues) Length() int {
return len(inc.Values)
}
func (inc *MACD) MACD() types.Series {
return &MACDValues{inc}
}
func (inc *MACD) Singals() types.Series {
return &inc.SignalLine
}

View File

@ -63,6 +63,20 @@ func (inc *RSI) Last() float64 {
return inc.Values[len(inc.Values)-1] return inc.Values[len(inc.Values)-1]
} }
func (inc *RSI) Index(i int) float64 {
length := len(inc.Values)
if length <= 0 || length - i - 1 < 0 {
return 0.0
}
return inc.Values[length - i - 1]
}
func (inc *RSI) Length() int {
return len(inc.Values)
}
var _ types.Series = &RSI{}
func (inc *RSI) calculateAndUpdate(kLines []types.KLine) { func (inc *RSI) calculateAndUpdate(kLines []types.KLine) {
var priceF = KLineClosePriceMapper var priceF = KLineClosePriceMapper

View File

@ -30,6 +30,21 @@ func (inc *SMA) Last() float64 {
return inc.Values[len(inc.Values)-1] return inc.Values[len(inc.Values)-1]
} }
func (inc *SMA) Index(i int) float64 {
length := len(inc.Values)
if length == 0 || length - i - 1 < 0 {
return 0.0
}
return inc.Values[length - i - 1]
}
func (inc *SMA) Length() int {
return len(inc.Values)
}
var _ types.Series = &SMA{}
func (inc *SMA) calculateAndUpdate(kLines []types.KLine) { func (inc *SMA) calculateAndUpdate(kLines []types.KLine) {
if len(kLines) < inc.Window { if len(kLines) < inc.Window {
return return

View File

@ -82,3 +82,55 @@ func (inc *STOCH) handleKLineWindowUpdate(interval types.Interval, window types.
func (inc *STOCH) Bind(updater KLineWindowUpdater) { func (inc *STOCH) Bind(updater KLineWindowUpdater) {
updater.OnKLineWindowUpdate(inc.handleKLineWindowUpdate) updater.OnKLineWindowUpdate(inc.handleKLineWindowUpdate)
} }
func (inc *STOCH) GetD() *DSeries {
return &DSeries{inc}
}
func (inc *STOCH) GetK() *KSeries {
return &KSeries{inc}
}
type DSeries struct {
*STOCH
}
func (inc *DSeries) Last() float64 {
return inc.LastD()
}
func (inc *DSeries) Length() int {
return len(inc.D)
}
func (inc *DSeries) Index(i int) float64 {
length := len(inc.D)
if length == 0 || length - i - 1 < 0 {
return 0
}
return inc.D[length - i - 1]
}
var _ types.Series = &DSeries{}
type KSeries struct {
*STOCH
}
func (inc *KSeries) Last() float64 {
return inc.LastK()
}
func (inc *KSeries) Index(i int) float64 {
length := len(inc.K)
if length == 0 || length - i - 1 < 0 {
return 0
}
return inc.K[length - i - 1]
}
func (inc *KSeries) Length() int {
return len(inc.K)
}
var _ types.Series = &KSeries{}

View File

@ -35,6 +35,21 @@ func (inc *VWAP) Last() float64 {
return inc.Values[len(inc.Values)-1] return inc.Values[len(inc.Values)-1]
} }
func (inc *VWAP) Index(i int) float64 {
length := len(inc.Values)
if length == 0 || length - i - 1 < 0 {
return 0
}
return inc.Values[length - i - 1]
}
func (inc *VWAP) Length() int {
return len(inc.Values)
}
var _ types.Series = &VWAP{}
func (inc *VWAP) Update(kLine types.KLine, priceF KLinePriceMapper) { func (inc *VWAP) Update(kLine types.KLine, priceF KLinePriceMapper) {
price := priceF(kLine) price := priceF(kLine)
volume := kLine.Volume.Float64() volume := kLine.Volume.Float64()

View File

@ -34,6 +34,20 @@ func (inc *VWMA) Last() float64 {
return inc.Values[len(inc.Values)-1] return inc.Values[len(inc.Values)-1]
} }
func (inc *VWMA) Index(i int) float64 {
length := len(inc.Values)
if length == 0 || length - i - 1 < 0 {
return 0
}
return inc.Values[length - i - 1]
}
func (inc *VWMA) Length() int {
return len(inc.Values)
}
var _ types.Series = &VWMA{}
func KLinePriceVolumeMapper(k types.KLine) float64 { func KLinePriceVolumeMapper(k types.KLine) float64 {
return k.Close.Mul(k.Volume).Float64() return k.Close.Mul(k.Volume).Float64()
} }