ADD: added first version

This commit is contained in:
hwinkel
2026-01-14 22:44:12 +01:00
commit 2b892123e1
18 changed files with 580 additions and 0 deletions

View 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
}

View File

@@ -0,0 +1,7 @@
package strategy
import "alpaca-bot/internal/model"
type Strategy interface {
OnBar(bar model.Bar) model.Signal
}