Skip to content

Commit

Permalink
スーパートレンド戦略実装完了
Browse files Browse the repository at this point in the history
  • Loading branch information
T.K committed Feb 5, 2024
1 parent 0db42a9 commit 5be2872
Show file tree
Hide file tree
Showing 12 changed files with 114 additions and 362 deletions.
4 changes: 2 additions & 2 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@



assetName: "TIAUSDT"
assetName: "LINKUSDT"

duration: "1h"
duration: "15m"

limit: 17000
55 changes: 12 additions & 43 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,56 +10,25 @@ func main() {

start := time.Now()

// close, _ := dbquery.GetCloseData("SOLUSDT", "1h")

// high, _ := dbquery.GetHighData("SOLUSDT", "1h")

// low, _ := dbquery.GetLowData("SOLUSDT", "1h")

// index := risk.ChoppySlice(close, high, low)

// e := risk.ChoppyEma(index)
// fmt.Println(e)
// strategey.RunBacktestEma()
// strategey.RunBacktestEmaChoppy()
// strategey.RunBacktestMacd()
// strategey.RunBacktestDonchain()
// strategey.RunBacktestDonchainChoppy()

// c, _ := strategey.GetCsvDataFrame("BTCUSDT", "4h", "2022-05", "2023-12")

// fmt.Println(c)
strategey.RunBacktestEma()
strategey.RunBacktestEmaChoppy()
strategey.RunBacktestDonchain()
strategey.RunBacktestDonchainChoppy()
strategey.RunBacktestST()
// strategey.RunBacktestSuperTrend()

// var err error

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

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

// // strategyName := getStrageyNameDonchain()
// assetName := btcfg.AssetName
// duration := btcfg.Dration
// assetName := "TIAUSDT"
// duration := "4h"

// df, _ := strategey.GetCandleData(assetName, duration)
// account := trader.NewAccount(1000)

// df.Signal = df.SuperTrendChoppyStrategy(17, 2.0, 13, account)

// hSeries := df.Candles.Col("High")
// lSeries := df.Candles.Col("Low")
// cSeries := df.Candles.Col("Close")
// h := df.Highs()
// l := df.Lows()
// c := df.Closes()

// h := hSeries.Float()
// l := lSeries.Float()
// c := cSeries.Float()
// a := risk.ChoppySlice(c, h, l)

// st, _ := indicators.SuperTrend(21, 3.0, h, l, c)
// fmt.Println(a)

end := time.Now()

Expand Down
42 changes: 21 additions & 21 deletions pkg/charts/candle_chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,35 +330,35 @@ func klineWithSuperTrend() *charts.Kline {

st, _ := indicators.SuperTrend(21, 3.0, h, l, c)

stLineData := make([]opts.LineData, len(st.SuperTrend))
for i, v := range st.SuperTrend {
stLineData[i] = opts.LineData{Value: v}
}
// stLineData := make([]opts.LineData, len(st.SuperTrend))
// for i, v := range st.SuperTrend {
// stLineData[i] = opts.LineData{Value: v}
// }

stLine := charts.NewLine()
stLine.SetXAxis(x).AddSeries("st", stLineData)
// stLine := charts.NewLine()
// stLine.SetXAxis(x).AddSeries("st", stLineData)

kline.Overlap(stLine)
// kline.Overlap(stLine)

// stupLineData := make([]opts.LineData, len(st.UpperBand))
// for i, v := range st.SuperTrend {
// stupLineData[i] = opts.LineData{Value: v}
// }
stupLineData := make([]opts.LineData, len(st.UpperBand))
for i, v := range st.SuperTrend {
stupLineData[i] = opts.LineData{Value: v}
}

// stLineHigh := charts.NewLine()
// stLineHigh.SetXAxis(x).AddSeries("st", stupLineData)
stLineHigh := charts.NewLine()
stLineHigh.SetXAxis(x).AddSeries("st", stupLineData)

// kline.Overlap(stLineHigh)
kline.Overlap(stLineHigh)

// stlowLineData := make([]opts.LineData, len(st.LowerBand))
// for i, v := range st.SuperTrend {
// stlowLineData[i] = opts.LineData{Value: v}
// }
stlowLineData := make([]opts.LineData, len(st.LowerBand))
for i, v := range st.SuperTrend {
stlowLineData[i] = opts.LineData{Value: v}
}

// stLineLow := charts.NewLine()
// stLineLow.SetXAxis(x).AddSeries("st", stlowLineData)
stLineLow := charts.NewLine()
stLineLow.SetXAxis(x).AddSeries("st", stlowLineData)

// kline.Overlap(stLineLow)
kline.Overlap(stLineLow)

kline.SetGlobalOptions(
charts.WithTitleOpts(opts.Title{
Expand Down
58 changes: 25 additions & 33 deletions pkg/indicator/indicators/supertrend.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ func SuperTrend(atrPeriod int, factor float64, high, low, close []float64) (Supe
atr := make([]float64, len(high))
for i := range high {
if i < atrPeriod {
atr[i] = math.NaN()
// Initialize atr[i] with the first TR value instead of NaN
atr[i] = tr1[0]
} else {
sum := 0.0
for j := i - atrPeriod + 1; j <= i; j++ {
Expand All @@ -48,7 +49,6 @@ func SuperTrend(atrPeriod int, factor float64, high, low, close []float64) (Supe
atr[i] = sum / float64(atrPeriod)
}
}

hl2 := make([]float64, len(high))
for i := range high {
hl2[i] = (high[i] + low[i]) / 2
Expand All @@ -57,42 +57,34 @@ func SuperTrend(atrPeriod int, factor float64, high, low, close []float64) (Supe
}

isUpTrend := make([]bool, len(high)) // ここを変更しました
prevUpperBand := upperBand[0]
prevLowerBand := lowerBand[0]

for i := range high {
current := i
previous := i - 1
if i == 0 {
superTrend[i] = hl2[i]
isUpTrend[i] = true
} else {
if close[i] > prevUpperBand {
isUpTrend[i] = true
} else if close[i] < prevLowerBand {
isUpTrend[i] = false
} else {
isUpTrend[i] = isUpTrend[i-1]
}
previous = 0
}

if close[current] > upperBand[previous] { // If the current close is above the previous upper band, then it is an uptrend
isUpTrend[current] = true
} else if close[current] < lowerBand[previous] { // If the current close is below the previous lower band, then it is a downtrend
isUpTrend[current] = false
} else { // Otherwise, the trend is the same as the previous one
isUpTrend[current] = isUpTrend[previous]

if isUpTrend[i] {
superTrend[i] = lowerBand[i]
if close[i] <= prevUpperBand {
upperBand[i] = prevUpperBand
} else {
upperBand[i] = hl2[i] + (factor * atr[i])
}
lowerBand[i] = hl2[i] - (factor * atr[i])
} else {
superTrend[i] = upperBand[i]
upperBand[i] = hl2[i] + (factor * atr[i])
if close[i] >= prevLowerBand {
lowerBand[i] = prevLowerBand
} else {
lowerBand[i] = hl2[i] - (factor * atr[i])
}
if isUpTrend[current] && lowerBand[current] < lowerBand[previous] { // If it is an uptrend and the current lower band is below the previous lower band, then use the previous lower band
lowerBand[current] = lowerBand[previous]
} else if !isUpTrend[current] && upperBand[current] > upperBand[previous] { // If it is a downtrend and the current upper band is above the previous upper band, then use the previous upper band
upperBand[current] = upperBand[previous]
}
}

prevUpperBand = upperBand[i]
prevLowerBand = lowerBand[i]
// If it is an uptrend, use the lower band as the super trend, otherwise use the upper band
if isUpTrend[current] {
superTrend[current] = lowerBand[current]
upperBand[current] = math.NaN() // Hide the upper band in an uptrend
} else {
superTrend[current] = upperBand[current]
lowerBand[current] = math.NaN() // Hide the lower band in a downtrend
}
}

Expand Down
6 changes: 5 additions & 1 deletion pkg/management/risk/market_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ func ChoppySlice(close []float64, high []float64, low []float64) []float64 {

var choppySlice []float64

for i := 30; i < len(close); i++ {
for i := 1; i < len(close); i++ {

if i < 30 {
continue
}

// iが30以上のときだけChoppyIndexの計算を行う

Expand Down
10 changes: 5 additions & 5 deletions pkg/strategey/donchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ func (df *DataFrameCandle) OptimizeDonchainGoroutin() (performance float64, best
var mu sync.Mutex
var wg sync.WaitGroup

a := trader.NewAccount(1000)
marketDefault, _ := BuyAndHoldingStrategy(a)
// a := trader.NewAccount(1000)
// marketDefault, _ := BuyAndHoldingStrategy(a)

for period := 10; period < 333; period++ {
wg.Add(1)
Expand All @@ -87,9 +87,9 @@ func (df *DataFrameCandle) OptimizeDonchainGoroutin() (performance float64, best
return
}

if analytics.NetProfit(signalEvents) < marketDefault {
return
}
// if analytics.NetProfit(signalEvents) < marketDefault {
// return
// }

// if analytics.WinRate(signalEvents) < 0.45 {
// return
Expand Down
16 changes: 8 additions & 8 deletions pkg/strategey/donchain_choppy.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ func (df *DataFrameCandle) OptimizeDonchainChoppyGoroutin() (performance float64
var mu sync.Mutex
var wg sync.WaitGroup

a := trader.NewAccount(1000)
marketDefault, _ := BuyAndHoldingStrategy(a)
// a := trader.NewAccount(1000)
// marketDefault, _ := BuyAndHoldingStrategy(a)

limit := 1000
slots := make(chan struct{}, limit)
Expand All @@ -95,15 +95,15 @@ func (df *DataFrameCandle) OptimizeDonchainChoppyGoroutin() (performance float64
return
}

if analytics.TotalTrades(signalEvents) < 3 {
if analytics.TotalTrades(signalEvents) < 5 {
<-slots
return
}

if analytics.NetProfit(signalEvents) < marketDefault {
// <-slots
return
}
// if analytics.NetProfit(signalEvents) < marketDefault {
// // <-slots
// return
// }

// if analytics.WinRate(signalEvents) < 0.45 {
// <-slots
Expand All @@ -130,7 +130,7 @@ func (df *DataFrameCandle) OptimizeDonchainChoppyGoroutin() (performance float64

wg.Wait()

fmt.Println("最高利益", performance, "最適なピリオド", bestPeriod, "最適なチョッピー", bestChoppy)
fmt.Println("最高SQN", performance, "最適なピリオド", bestPeriod, "最適なチョッピー", bestChoppy)

return performance, bestPeriod, bestChoppy
}
Expand Down
16 changes: 8 additions & 8 deletions pkg/strategey/ema.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ func (df *DataFrameCandle) OptimizeEma() (performance float64, bestPeriod1 int,
limit := 1000
slots := make(chan struct{}, limit)

a := trader.NewAccount(1000)
marketDefault, _ := BuyAndHoldingStrategy(a)
// a := trader.NewAccount(1000)
// marketDefault, _ := BuyAndHoldingStrategy(a)

var mu sync.Mutex
var wg sync.WaitGroup
Expand All @@ -90,15 +90,15 @@ func (df *DataFrameCandle) OptimizeEma() (performance float64, bestPeriod1 int,
return
}

if analytics.TotalTrades(signalEvents) < 3 {
if analytics.TotalTrades(signalEvents) < 5 {
<-slots
return
}

if analytics.NetProfit(signalEvents) < marketDefault {
<-slots
return
}
// if analytics.NetProfit(signalEvents) < marketDefault {
// <-slots
// return
// }

// if analytics.WinRate(signalEvents) < 0.50 {
// <-slots
Expand Down Expand Up @@ -130,7 +130,7 @@ func (df *DataFrameCandle) OptimizeEma() (performance float64, bestPeriod1 int,

wg.Wait()

fmt.Println("最高利益", performance, "最適な短期線", bestPeriod1, "最適な長期線", bestPeriod2)
fmt.Println("最高SQN", performance, "最適な短期線", bestPeriod1, "最適な長期線", bestPeriod2)

return performance, bestPeriod1, bestPeriod2
}
Expand Down
18 changes: 9 additions & 9 deletions pkg/strategey/ema_choppy.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ func (df *DataFrameCandle) OptimizeEmaChoppy() (performance float64, bestPeriod1
runtime.GOMAXPROCS(10)
bestPeriod1 = 5
bestPeriod2 = 21
bestChoppy = 50
bestChoppy = 13

limit := 1000
slots := make(chan struct{}, limit)

a := trader.NewAccount(1000)
marketDefault, _ := BuyAndHoldingStrategy(a)
// a := trader.NewAccount(1000)
// marketDefault, _ := BuyAndHoldingStrategy(a)

var mu sync.Mutex
var wg sync.WaitGroup
Expand All @@ -97,15 +97,15 @@ func (df *DataFrameCandle) OptimizeEmaChoppy() (performance float64, bestPeriod1
return
}

if analytics.TotalTrades(signalEvents) < 20 {
if analytics.TotalTrades(signalEvents) < 5 {
<-slots
return
}

if analytics.NetProfit(signalEvents) < marketDefault {
<-slots
return
}
// if analytics.NetProfit(signalEvents) < marketDefault {
// <-slots
// return
// }

// if analytics.WinRate(signalEvents) < 0.50 {
// <-slots
Expand Down Expand Up @@ -139,7 +139,7 @@ func (df *DataFrameCandle) OptimizeEmaChoppy() (performance float64, bestPeriod1

wg.Wait()

fmt.Println("最高パフォーマンス", performance, "最適な短期線", bestPeriod1, "最適な長期線", bestPeriod2, "最適なチョッピー", bestChoppy)
fmt.Println("最高SQN", performance, "最適な短期線", bestPeriod1, "最適な長期線", bestPeriod2, "最適なチョッピー", bestChoppy)

return performance, bestPeriod1, bestPeriod2, bestChoppy
}
Expand Down
Loading

0 comments on commit 5be2872

Please sign in to comment.