Fixed so many IndexedSeries bugs...

This commit is contained in:
Luke I. Wilson
2023-05-24 11:24:10 -05:00
parent c0de28664e
commit 46fd55ab8d
17 changed files with 345 additions and 2981 deletions

35
cmd/ichimoku.go Normal file
View File

@@ -0,0 +1,35 @@
//go:build ignore
package main
import auto "github.com/fivemoreminix/autotrader"
type IchimokuStrategy struct {
convPeriod, basePeriod, leadingPeriods int
}
func (s *IchimokuStrategy) Init(_ *auto.Trader) {
}
func (s *IchimokuStrategy) Next(t *auto.Trader) {
ichimoku := auto.Ichimoku(t.Data().Closes(), s.convPeriod, s.basePeriod, s.leadingPeriods)
time := t.Data().Date(-1)
// If the price crosses above the Conversion Line, buy.
if auto.CrossoverIndex(*time, t.Data().Closes(), ichimoku.Series("Conversion")) {
t.Buy(1000)
}
// If the price crosses below the Conversion Line, sell.
if auto.CrossoverIndex(*time, ichimoku.Series("Conversion"), t.Data().Closes()) {
t.Sell(1000)
}
}
func main() {
auto.Backtest(auto.NewTrader(auto.TraderConfig{
Broker: auto.NewTestBroker(nil, nil, 10000, 50, 0.0002, 0),
Strategy: &IchimokuStrategy{convPeriod: 9, basePeriod: 26, leadingPeriods: 52},
Symbol: "EUR_USD",
Frequency: "M15",
CandlesToKeep: 2500,
}))
}

18
cmd/oanda_testing.go Normal file
View File

@@ -0,0 +1,18 @@
//go:build ignore
package main
import (
"os"
"github.com/fivemoreminix/autotrader/oanda"
)
func main() {
broker := oanda.NewOandaBroker(os.Getenv("OANDA_TOKEN"), os.Getenv("OANDA_ACCOUNT_ID"), true)
candles, err := broker.Candles("EUR_USD", "D", 100)
if err != nil {
panic(err)
}
println(candles.String())
}

View File

@@ -1,7 +1,12 @@
//go:build ignore
package main
import (
"os"
auto "github.com/fivemoreminix/autotrader"
"github.com/fivemoreminix/autotrader/oanda"
)
type SMAStrategy struct {
@@ -12,27 +17,29 @@ func (s *SMAStrategy) Init(_ *auto.Trader) {
}
func (s *SMAStrategy) Next(t *auto.Trader) {
sma1 := t.Data().Closes().Rolling(s.period1).Mean()
sma2 := t.Data().Closes().Rolling(s.period2).Mean()
sma1 := t.Data().Closes().Copy().Rolling(s.period1).Mean()
sma2 := t.Data().Closes().Copy().Rolling(s.period2).Mean()
// If the shorter SMA crosses above the longer SMA, buy.
if auto.Crossover(sma1, sma2) {
if auto.CrossoverIndex(*t.Data().Date(-1), sma1, sma2) {
t.Buy(1000)
} else if auto.Crossover(sma2, sma1) {
} else if auto.CrossoverIndex(*t.Data().Date(-1), sma2, sma1) {
t.Sell(1000)
}
}
func main() {
data, err := auto.EURUSD()
/* data, err := auto.EURUSD()
if err != nil {
panic(err)
}
*/
broker := oanda.NewOandaBroker(os.Getenv("OANDA_TOKEN"), os.Getenv("OANDA_ACCOUNT_ID"), true)
auto.Backtest(auto.NewTrader(auto.TraderConfig{
Broker: auto.NewTestBroker(nil, data, 10000, 50, 0.0002, 0),
Strategy: &SMAStrategy{period1: 20, period2: 40},
Broker: auto.NewTestBroker(broker /* data, */, nil, 10000, 50, 0.0002, 0),
Strategy: &SMAStrategy{period1: 10, period2: 25},
Symbol: "EUR_USD",
Frequency: "D",
CandlesToKeep: 1000,
Frequency: "M15",
CandlesToKeep: 2500,
}))
}