Skip to content

Commit

Permalink
dfキャンドルの取得期間の選択機能を追加
Browse files Browse the repository at this point in the history
  • Loading branch information
T.K committed Feb 5, 2024
1 parent 5be2872 commit 0add38a
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 101 deletions.
9 changes: 4 additions & 5 deletions config.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@




assetName: "LINKUSDT"

#バックテストの基本設定 startとendの月は必ず二桁になるように。
assetName: "SOLUSDT"
duration: "15m"

limit: 17000
start: "2021-04"
end: "2022-011"
9 changes: 3 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,9 @@ func main() {

start := time.Now()

strategey.RunBacktestEma()
strategey.RunBacktestEmaChoppy()
strategey.RunBacktestDonchain()
strategey.RunBacktestDonchainChoppy()
strategey.RunBacktestST()
strategey.RunBacktestSuperTrend()
strategey.RunEmaOptimize()
// strategey.RunBacktestDonchainChoppy()
// strategey.RunBacktestSuperTrend()

// assetName := "TIAUSDT"
// duration := "4h"
Expand Down
3 changes: 2 additions & 1 deletion pkg/config/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ var data = "config.yml"
type Config struct {
AssetName string `yaml:"assetName"`
Dration string `yaml:"duration"`
Limit int `yaml:"limit"`
Start string `yaml:"start"`
End string `yaml:"end"`
}

func Yaml() (Config, error) {
Expand Down
11 changes: 5 additions & 6 deletions pkg/strategey/donchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
// return "DBO"
// }

func (df *DataFrameCandle) DonchainStrategy(period int, account *trader.Account) *execute.SignalEvents {
func (df *DataFrameCandleCsv) DonchainStrategy(period int, account *trader.Account) *execute.SignalEvents {
var StrategyName = "DBO"

lenCandles := len(df.Candles)
Expand Down Expand Up @@ -63,7 +63,7 @@ func (df *DataFrameCandle) DonchainStrategy(period int, account *trader.Account)
return signalEvents

}
func (df *DataFrameCandle) OptimizeDonchainGoroutin() (performance float64, bestPeriod int) {
func (df *DataFrameCandleCsv) OptimizeDonchainGoroutin() (performance float64, bestPeriod int) {

bestPeriod = 40
var mu sync.Mutex
Expand Down Expand Up @@ -128,15 +128,14 @@ func RunBacktestDonchain() {

fmt.Println("--------------------------------------------")

// strategyName := getStrageyNameDonchain()
assetName := btcfg.AssetName
duration := btcfg.Dration

// limit := btcfg.Limit
start := btcfg.Start
end := btcfg.End

account := trader.NewAccount(1000)

df, _ := GetCandleData(assetName, duration)
df, _ := GetCsvDataFrame(assetName, duration, start, end)

performancePayOffRatio, bestPayOffRatioPeriod := df.OptimizeDonchainGoroutin()

Expand Down
38 changes: 13 additions & 25 deletions pkg/strategey/donchain_choppy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ package strategey

import (
"fmt"
"log"
"sync"
"v1/pkg/analytics"
"v1/pkg/config"
"v1/pkg/execute"
"v1/pkg/indicator/indicators"
"v1/pkg/management/risk"
Expand All @@ -16,7 +14,7 @@ import (
// return "DBO"
// }

func (df *DataFrameCandle) DonchainChoppyStrategy(period int, choppy int, account *trader.Account) *execute.SignalEvents {
func (df *DataFrameCandleCsv) DonchainChoppyStrategy(period int, choppy int, account *trader.Account) *execute.SignalEvents {
var StrategyName = "DBO_CHOPPY"

lenCandles := len(df.Candles)
Expand Down Expand Up @@ -68,7 +66,7 @@ func (df *DataFrameCandle) DonchainChoppyStrategy(period int, choppy int, accoun

}

func (df *DataFrameCandle) OptimizeDonchainChoppyGoroutin() (performance float64, bestPeriod int, bestChoppy int) {
func (df *DataFrameCandleCsv) OptimizeDonchainChoppyGoroutin() (performance float64, bestPeriod int, bestChoppy int) {

bestPeriod = 40
bestChoppy = 13
Expand All @@ -81,7 +79,7 @@ func (df *DataFrameCandle) OptimizeDonchainChoppyGoroutin() (performance float64
limit := 1000
slots := make(chan struct{}, limit)

for period := 10; period < 250; period += 3 {
for period := 5; period < 100; period += 1 {
for choppy := 5; choppy < 18; choppy += 1 {
wg.Add(1)
slots <- struct{}{}
Expand Down Expand Up @@ -135,27 +133,9 @@ func (df *DataFrameCandle) OptimizeDonchainChoppyGoroutin() (performance float64
return performance, bestPeriod, bestChoppy
}

func RunBacktestDonchainChoppy() {
func RunDonchainOptimize() {

var err error

// account := trader.NewAccount(1000)
btcfg, err := config.Yaml()
if err != nil {
log.Fatalf("error: %v", err)
}

fmt.Println("--------------------------------------------")

// strategyName := getStrageyNameDonchain()
assetName := btcfg.AssetName
duration := btcfg.Dration

// limit := btcfg.Limit

account := trader.NewAccount(1000)

df, _ := GetCandleData(assetName, duration)
df, account, _ := RadyBacktest()

p, bestPeriod, bestChoppy := df.OptimizeDonchainChoppyGoroutin()

Expand All @@ -167,3 +147,11 @@ func RunBacktestDonchainChoppy() {
}

}

func DonchainBacktest() {

df, account, _ := RadyBacktest()

df.Signal = df.DonchainChoppyStrategy(40, 13, account)
Result(df.Signal)
}
9 changes: 5 additions & 4 deletions pkg/strategey/ema.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/markcheno/go-talib"
)

func (df *DataFrameCandle) EmaStrategy(period1, period2 int, account *trader.Account) *execute.SignalEvents {
func (df *DataFrameCandleCsv) EmaStrategy(period1, period2 int, account *trader.Account) *execute.SignalEvents {

var StrategyName = "EMA"
lenCandles := len(df.Candles)
Expand Down Expand Up @@ -61,7 +61,7 @@ func (df *DataFrameCandle) EmaStrategy(period1, period2 int, account *trader.Acc
return signalEvents
}

func (df *DataFrameCandle) OptimizeEma() (performance float64, bestPeriod1 int, bestPeriod2 int) {
func (df *DataFrameCandleCsv) OptimizeEma() (performance float64, bestPeriod1 int, bestPeriod2 int) {
runtime.GOMAXPROCS(10)
bestPeriod1 = 5
bestPeriod2 = 21
Expand Down Expand Up @@ -173,11 +173,12 @@ func RunBacktestEma() {

assetName := btcfg.AssetName
duration := btcfg.Dration
// limit := btcfg.Limit
start := btcfg.Start
end := btcfg.End

account := trader.NewAccount(1000)

df, _ := GetCandleData(assetName, duration)
df, _ := GetCsvDataFrame(assetName, duration, start, end)

performance, bestPeriod1, bestPeriod2 := df.OptimizeEma()

Expand Down
39 changes: 15 additions & 24 deletions pkg/strategey/ema_choppy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@ package strategey

import (
"fmt"
"log"
"runtime"
"sync"
"v1/pkg/analytics"
"v1/pkg/config"
"v1/pkg/execute"
"v1/pkg/management/risk"
"v1/pkg/trader"

"github.com/markcheno/go-talib"
)

func (df *DataFrameCandle) EmaChoppyStrategy(period1, period2 int, choppy int, account *trader.Account) *execute.SignalEvents {
func (df *DataFrameCandleCsv) EmaChoppyStrategy(period1, period2 int, choppy int, account *trader.Account) *execute.SignalEvents {

var StrategyName = "EMA_CHOPPY"
lenCandles := len(df.Candles)
Expand Down Expand Up @@ -66,7 +64,7 @@ func (df *DataFrameCandle) EmaChoppyStrategy(period1, period2 int, choppy int, a
return signalEvents
}

func (df *DataFrameCandle) OptimizeEmaChoppy() (performance float64, bestPeriod1 int, bestPeriod2 int, bestChoppy int) {
func (df *DataFrameCandleCsv) OptimizeEmaChoppy() (performance float64, bestPeriod1 int, bestPeriod2 int, bestChoppy int) {
runtime.GOMAXPROCS(10)
bestPeriod1 = 5
bestPeriod2 = 21
Expand All @@ -81,8 +79,8 @@ func (df *DataFrameCandle) OptimizeEmaChoppy() (performance float64, bestPeriod1
var mu sync.Mutex
var wg sync.WaitGroup

for period1 := 3; period1 < 92; period1 += 3 {
for period2 := 5; period2 < 260; period2 += 3 {
for period1 := 1; period1 < 34; period1 += 2 {
for period2 := 5; period2 < 250; period2 += 3 {
for choppy := 5; choppy < 18; choppy += 1 {

wg.Add(1)
Expand Down Expand Up @@ -144,25 +142,9 @@ func (df *DataFrameCandle) OptimizeEmaChoppy() (performance float64, bestPeriod1
return performance, bestPeriod1, bestPeriod2, bestChoppy
}

func RunBacktestEmaChoppy() {
func RunEmaOptimize() {

var err error

// account := trader.NewAccount(1000)
btcfg, err := config.Yaml()
if err != nil {
log.Fatalf("error: %v", err)
}

fmt.Println("--------------------------------------------")

assetName := btcfg.AssetName
duration := btcfg.Dration
// limit := btcfg.Limit

account := trader.NewAccount(1000)

df, _ := GetCandleData(assetName, duration)
df, account, _ := RadyBacktest()

performance, bestPeriod1, bestPeriod2, bestChoppy := df.OptimizeEmaChoppy()

Expand All @@ -174,3 +156,12 @@ func RunBacktestEmaChoppy() {
}

}

func EmaBacktest() {

df, account, _ := RadyBacktest()

df.Signal = df.EmaChoppyStrategy(5, 21, 12, account)
Result(df.Signal)

}
28 changes: 27 additions & 1 deletion pkg/strategey/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strings"
"time"
"v1/pkg/analytics"
"v1/pkg/config"
"v1/pkg/data"
dbquery "v1/pkg/data/query"
"v1/pkg/execute"
Expand Down Expand Up @@ -321,6 +322,29 @@ func (df *DataFrameCandleCsv) Hlc3() []float64 {
return s
}

func RadyBacktest() (*DataFrameCandleCsv, *trader.Account, error) {

var err error

btcfg, err := config.Yaml()
if err != nil {
return &DataFrameCandleCsv{}, &trader.Account{}, nil
}
account := trader.NewAccount(1000)

assetName := btcfg.AssetName
duration := btcfg.Dration
start := btcfg.Start
end := btcfg.End

df, err := GetCsvDataFrame(assetName, duration, start, end)
if err != nil {
return &DataFrameCandleCsv{}, &trader.Account{}, err
}

return df, account, nil
}

func Result(s *execute.SignalEvents) {

if s == nil || len(s.Signals) == 0 {
Expand All @@ -345,7 +369,7 @@ func Result(s *execute.SignalEvents) {

fmt.Println("🌟", name, "🌟")
fmt.Println("初期残高", initialBalance)
fmt.Println("最終残高", l, lr)
fmt.Println("最終残高", l, "USD", lr, "倍")

fmt.Println("勝率", analytics.WinRate(s)*100, "%")
fmt.Println("総利益", analytics.Profit(s))
Expand Down Expand Up @@ -374,4 +398,6 @@ func Result(s *execute.SignalEvents) {
// fmt.Println("バルサラの破産確率", analytics.BalsaraAxum(s))

// fmt.Println(s)

fmt.Println("--------------------------------------------")
}
35 changes: 12 additions & 23 deletions pkg/strategey/supertorend_choppy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@ package strategey

import (
"fmt"
"log"
"runtime"
"sync"
"v1/pkg/analytics"
"v1/pkg/config"
"v1/pkg/execute"
"v1/pkg/indicator/indicators"
"v1/pkg/management/risk"
"v1/pkg/trader"
)

func (df *DataFrameCandle) SuperTrendChoppyStrategy(atrPeriod int, factor float64, choppy int, account *trader.Account) *execute.SignalEvents {
func (df *DataFrameCandleCsv) SuperTrendChoppyStrategy(atrPeriod int, factor float64, choppy int, account *trader.Account) *execute.SignalEvents {

var StrategyName = "SUPERTREND_CHOPPY"
// var err error
Expand Down Expand Up @@ -81,7 +79,7 @@ func (df *DataFrameCandle) SuperTrendChoppyStrategy(atrPeriod int, factor float6
return signalEvents
}

func (df *DataFrameCandle) OptimizeSuperTrend() (performance float64, bestAtrPeriod int, bestFactor float64, bestChoppy int) {
func (df *DataFrameCandleCsv) OptimizeSuperTrend() (performance float64, bestAtrPeriod int, bestFactor float64, bestChoppy int) {
runtime.GOMAXPROCS(10)
bestAtrPeriod = 21
bestFactor = 3.0
Expand Down Expand Up @@ -161,25 +159,7 @@ func (df *DataFrameCandle) OptimizeSuperTrend() (performance float64, bestAtrPer

func RunBacktestSuperTrend() {

var err error

// account := trader.NewAccount(1000)
btcfg, err := config.Yaml()
if err != nil {
log.Fatalf("error: %v", err)
}

fmt.Println("--------------------------------------------")

// strategyName := getStrageyNameDonchain()
assetName := btcfg.AssetName
duration := btcfg.Dration

// limit := btcfg.Limit

account := trader.NewAccount(1000)

df, _ := GetCandleData(assetName, duration)
df, account, _ := RadyBacktest()

performance, bestAtrPeriod, bestFactor, bestChoppy := df.OptimizeSuperTrend()

Expand All @@ -191,3 +171,12 @@ func RunBacktestSuperTrend() {
}

}

func SuperTrendBacktest() {

df, account, _ := RadyBacktest()

df.Signal = df.SuperTrendChoppyStrategy(21, 3.0, 13, account)
Result(df.Signal)

}
Loading

0 comments on commit 0add38a

Please sign in to comment.