ADD: added first version
This commit is contained in:
60
internal/strategy/ema_cross.go
Normal file
60
internal/strategy/ema_cross.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package strategy
|
||||
|
||||
import (
|
||||
"alpaca-bot/internal/model"
|
||||
|
||||
talib "github.com/markcheno/go-talib"
|
||||
)
|
||||
|
||||
type EMACross struct {
|
||||
FastPeriod int
|
||||
SlowPeriod int
|
||||
prices []float64
|
||||
lastFast float64
|
||||
lastSlow float64
|
||||
init bool
|
||||
}
|
||||
|
||||
func NewEMACross(fast, slow int) *EMACross {
|
||||
return &EMACross{
|
||||
FastPeriod: fast,
|
||||
SlowPeriod: slow,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *EMACross) OnBar(bar model.Bar) model.Signal {
|
||||
s.prices = append(s.prices, bar.Close)
|
||||
|
||||
if len(s.prices) < s.SlowPeriod {
|
||||
return model.Hold
|
||||
}
|
||||
|
||||
fast := talib.Ema(s.prices, s.FastPeriod)
|
||||
slow := talib.Ema(s.prices, s.SlowPeriod)
|
||||
|
||||
curFast := fast[len(fast)-1]
|
||||
curSlow := slow[len(slow)-1]
|
||||
|
||||
if !s.init {
|
||||
s.lastFast = curFast
|
||||
s.lastSlow = curSlow
|
||||
s.init = true
|
||||
return model.Hold
|
||||
}
|
||||
|
||||
if s.lastFast <= s.lastSlow && curFast > curSlow {
|
||||
s.lastFast = curFast
|
||||
s.lastSlow = curSlow
|
||||
return model.Buy
|
||||
}
|
||||
|
||||
if s.lastFast >= s.lastSlow && curFast < curSlow {
|
||||
s.lastFast = curFast
|
||||
s.lastSlow = curSlow
|
||||
return model.Sell
|
||||
}
|
||||
|
||||
s.lastFast = curFast
|
||||
s.lastSlow = curSlow
|
||||
return model.Hold
|
||||
}
|
||||
Reference in New Issue
Block a user