Making trade charting more reliable and informative

This commit is contained in:
Luke Wilson
2024-11-04 12:14:56 -06:00
parent d755f07d38
commit f1c45dbb74
7 changed files with 136 additions and 35 deletions

View File

@@ -20,10 +20,13 @@ func (s *SMAStrategy) Init(_ *auto.Trader) {
func (s *SMAStrategy) Next(t *auto.Trader) {
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 the shorter SMA (sma1) crosses above the longer SMA (sma2), buy.
if auto.CrossoverIndex(*t.Data().Date(-1), sma1, sma2) {
t.CloseOrdersAndPositions()
t.Buy(1000, 0, 0)
} else if auto.CrossoverIndex(*t.Data().Date(-1), sma2, sma1) {
t.CloseOrdersAndPositions()
t.Sell(1000, 0, 0)
}
}
@@ -41,8 +44,8 @@ func main() {
}
auto.Backtest(auto.NewTrader(auto.TraderConfig{
Broker: auto.NewTestBroker(broker /* data, */, nil, 10000, 50, 0.0002, 0),
Strategy: &SMAStrategy{period1: 10, period2: 25},
Broker: auto.NewTestBroker(broker, nil, 10000, 50, 0.0002, 0),
Strategy: &SMAStrategy{period1: 7, period2: 20},
Symbol: "EUR_USD",
Frequency: "M15",
CandlesToKeep: 2500,

49
cmd/spread_example.go Normal file
View File

@@ -0,0 +1,49 @@
//go:build ignore
package main
import (
"fmt"
"os"
auto "github.com/fivemoreminix/autotrader"
"github.com/fivemoreminix/autotrader/oanda"
)
type SpreadPayupStrategy struct {
}
func (s *SpreadPayupStrategy) Init(t *auto.Trader) {
t.Broker.SignalConnect(auto.OrderFulfilled, s, func(a ...any) {
order := a[0].(auto.Order)
order.Position().Close() // Immediately close the position so we only pay spread.
})
}
func (s *SpreadPayupStrategy) Next(t *auto.Trader) {
_, err := t.Sell(1000, 0, 0)
if err != nil {
panic(err)
}
}
func main() {
/* data, err := auto.EURUSD()
if err != nil {
panic(err)
}
*/
broker, err := oanda.NewOandaBroker(os.Getenv("OANDA_TOKEN"), os.Getenv("OANDA_ACCOUNT_ID"), true)
if err != nil {
fmt.Println("error:", err)
return
}
auto.Backtest(auto.NewTrader(auto.TraderConfig{
Broker: auto.NewTestBroker(broker, nil, 10000, 50, 0.0002, 0),
Strategy: &SpreadPayupStrategy{},
Symbol: "EUR_USD",
Frequency: "M15",
CandlesToKeep: 1000,
}))
}