mirror of
https://github.com/lukewilson2002/autotrader.git
synced 2025-08-02 13:19:32 +00:00
Working on trading and backtesting loop
This commit is contained in:
@@ -1,49 +1,46 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
auto "github.com/fivemoreminix/autotrader"
|
||||
)
|
||||
|
||||
type SMAStrategy struct {
|
||||
i int
|
||||
period1, period2 int
|
||||
}
|
||||
|
||||
func (s *SMAStrategy) Init(_trader *auto.Trader) {
|
||||
fmt.Println("Init")
|
||||
s.i = 0
|
||||
func (s *SMAStrategy) Init(_ *auto.Trader) {
|
||||
}
|
||||
|
||||
func (s *SMAStrategy) Next(_trader *auto.Trader) {
|
||||
fmt.Println("Next " + fmt.Sprint(s.i))
|
||||
s.i++
|
||||
func (s *SMAStrategy) Next(t *auto.Trader) {
|
||||
sma1 := t.Data().Closes().Rolling(s.period1).Mean()
|
||||
sma2 := t.Data().Closes().Rolling(s.period2).Mean()
|
||||
log.Println(t.Data().Close(-1) - sma1.Float(-1))
|
||||
// If the shorter SMA crosses above the longer SMA, buy.
|
||||
if crossover(sma1, sma2) {
|
||||
t.Buy(1000)
|
||||
} else if crossover(sma2, sma1) {
|
||||
t.Sell(1000)
|
||||
}
|
||||
}
|
||||
|
||||
// crossover returns true if s1 crosses above s2 at the latest float.
|
||||
func crossover(s1, s2 auto.Series) bool {
|
||||
return s1.Float(-1) > s2.Float(-1) && s1.Float(-2) <= s2.Float(-2)
|
||||
}
|
||||
|
||||
func main() {
|
||||
// token := os.Environ["OANDA_TOKEN"]
|
||||
// accountId := os.Environ["OANDA_ACCOUNT_ID"]
|
||||
|
||||
// if token == "" || accountId == "" {
|
||||
// fmt.Println("Please set OANDA_TOKEN and OANDA_ACCOUNT_ID environment variables")
|
||||
// os.Exit(1)
|
||||
// }
|
||||
|
||||
data, err := auto.EURUSD()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
auto.Backtest(auto.NewTrader(auto.TraderConfig{
|
||||
// auto.NewOandaBroker(auto.OandaConfig{
|
||||
// Token: "YOUR_TOKEN",
|
||||
// AccountID: "101-001-14983263-001",
|
||||
// DemoAccount: true,
|
||||
// }),
|
||||
Broker: auto.NewTestBroker(nil, data, 10000, 50, 0.0002, 0),
|
||||
Strategy: &SMAStrategy{},
|
||||
Strategy: &SMAStrategy{period1: 20, period2: 40},
|
||||
Symbol: "EUR_USD",
|
||||
Frequency: "D",
|
||||
CandlesToKeep: 100,
|
||||
CandlesToKeep: 1000,
|
||||
}))
|
||||
}
|
||||
|
Reference in New Issue
Block a user