Skip to content

Commit

Permalink
add ad,macd,rsi,sma,stoch,vwap,vwma to Series interface
Browse files Browse the repository at this point in the history
  • Loading branch information
zenixls2 committed Apr 11, 2022
1 parent fac61f2 commit 567e7bd
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 0 deletions.
14 changes: 14 additions & 0 deletions pkg/indicator/ad.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,20 @@ func (inc *AD) Last() float64 {
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) {
for _, k := range kLines {
if inc.EndTime != zeroTime && k.EndTime.Before(inc.EndTime) {
Expand Down
31 changes: 31 additions & 0 deletions pkg/indicator/macd.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,34 @@ func (inc *MACD) handleKLineWindowUpdate(interval types.Interval, window types.K
func (inc *MACD) Bind(updater KLineWindowUpdater) {
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
}
14 changes: 14 additions & 0 deletions pkg/indicator/rsi.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,20 @@ func (inc *RSI) Last() float64 {
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) {
var priceF = KLineClosePriceMapper

Expand Down
15 changes: 15 additions & 0 deletions pkg/indicator/sma.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,21 @@ func (inc *SMA) Last() float64 {
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) {
if len(kLines) < inc.Window {
return
Expand Down
52 changes: 52 additions & 0 deletions pkg/indicator/stoch.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,55 @@ func (inc *STOCH) handleKLineWindowUpdate(interval types.Interval, window types.
func (inc *STOCH) Bind(updater KLineWindowUpdater) {
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{}
15 changes: 15 additions & 0 deletions pkg/indicator/vwap.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,21 @@ func (inc *VWAP) Last() float64 {
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) {
price := priceF(kLine)
volume := kLine.Volume.Float64()
Expand Down
14 changes: 14 additions & 0 deletions pkg/indicator/vwma.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,20 @@ func (inc *VWMA) Last() float64 {
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 {
return k.Close.Mul(k.Volume).Float64()
}
Expand Down

0 comments on commit 567e7bd

Please sign in to comment.