Skip to content

Commit

Permalink
アナリティクスのビジュアルを整理
Browse files Browse the repository at this point in the history
  • Loading branch information
T.K committed Feb 9, 2024
1 parent cfdc452 commit 19916c6
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 130 deletions.
10 changes: 5 additions & 5 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@


#バックテストの基本設定
assetName: "SOLUSDT"
duration: "30m"
start: "20200801"
end: "20230930"
simpleInterest: false
assetName: "AVAXUSDT"
duration: "15m"
start: "20221201"
end: "20231230"
simpleInterest: true
positionPersentage: 0.9
6 changes: 3 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ func main() {
// strategey.RunDonchainOptimize()
// strategey.RunBetterRsiOptimize()

// strategey.DonchainBacktest()
// strategey.EmaBacktest()
// strategey.SuperTrendBacktest()
strategey.DonchainBacktest()
strategey.EmaBacktest()
strategey.SuperTrendBacktest()
strategey.RSIBetterBacktest()

// strategey.EmaBacktest()
Expand Down
3 changes: 1 addition & 2 deletions pkg/analytics/drawdown.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package analytics

import (
"fmt"
"math"
"sort"
dbquery "v1/pkg/data/query"
Expand Down Expand Up @@ -92,7 +91,7 @@ func MaxDrawdownPercent(s *execute.SignalEvents) float64 {
// TODO: Handle other sides if necessary
continue
}
fmt.Println(signal.AccountBalance)

if signal.AccountBalance > peak { // Update peak value if account balance is higher
peak = signal.AccountBalance
}
Expand Down
23 changes: 18 additions & 5 deletions pkg/analytics/profit_and_loss.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func FinalBalance(s *execute.SignalEvents) (float64, float64) {
return 0, 0
}

finalBlanceValue := accountBalance + LongNetProfit(s)
finalBlanceValue := accountBalance + TotalNetProfit(s)
finalBlanceRatio := finalBlanceValue / accountBalance

return finalBlanceValue, finalBlanceRatio
Expand Down Expand Up @@ -272,12 +272,13 @@ func GainPainRatio(s *execute.SignalEvents) float64 {

}

func ReturnProfitLoss(s *execute.SignalEvents) []float64 {
func PLSlice(s *execute.SignalEvents) []float64 {
if s == nil {
return nil
}
var pl []float64 // profit or loss slice
var buyPrice float64
var sellPrice float64

if s.Signals == nil || len(s.Signals) == 0 {
return nil
Expand All @@ -289,9 +290,21 @@ func ReturnProfitLoss(s *execute.SignalEvents) []float64 {
}
if signal.Side == "BUY" {
buyPrice = signal.Price
} else if signal.Side == "SELL" && buyPrice != 0 {
pl = append(pl, (signal.Price-buyPrice)*signal.Size) // append the profit or loss of the trade to the slice
buyPrice = 0 // Reset buy price after a sell
// if there is a previous sell price, calculate the profit or loss
if sellPrice != 0 {
pl = append(pl, (sellPrice-buyPrice)*signal.Size)
// reset the sell price
sellPrice = 0
}
}
if signal.Side == "SELL" {
sellPrice = signal.Price
// if there is a previous buy price, calculate the profit or loss
if buyPrice != 0 {
pl = append(pl, (sellPrice-buyPrice)*signal.Size)
// reset the buy price
buyPrice = 0
}
}
}

Expand Down
96 changes: 76 additions & 20 deletions pkg/analytics/winrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,84 @@ type Winrate_arg struct {
Totall_trade int
}

// func WinRate(s *execute.SignalEvents) float64 {

// if s == nil {
// return 0.0
// }
// var profitCount, lossCount float64

// for i := 0; i < len(s.Signals)-1; i += 2 {
// buySignal := s.Signals[i]
// sellSignal := s.Signals[i+1]

// if sellSignal.Price > buySignal.Price {
// profitCount++
// } else if sellSignal.Price < buySignal.Price {
// lossCount++
// }
// }

// totalCount := profitCount + lossCount
// if totalCount == 0 {
// return 0
// }

// return profitCount / totalCount
// }

// func ShortWinRate(s *execute.SignalEvents) float64 {

// if s == nil {
// return 0.0
// }
// var profitCount, lossCount float64

// for i := 0; i < len(s.Signals)-1; i += 2 {
// sellSignal := s.Signals[i]
// buySignal := s.Signals[i+1]

// if buySignal.Price < sellSignal.Price {
// profitCount++
// } else if buySignal.Price > sellSignal.Price {
// lossCount++
// }
// }

// totalCount := profitCount + lossCount
// if totalCount == 0 {
// return 0
// }

// return profitCount / totalCount
// }

func WinRate(s *execute.SignalEvents) float64 {

if s == nil {
return 0.0
}
var profitCount, lossCount float64

for i := 0; i < len(s.Signals)-1; i += 2 {
buySignal := s.Signals[i]
sellSignal := s.Signals[i+1]
for i := 0; i < len(s.Signals)-1; i++ {
currentSignal := s.Signals[i]
nextSignal := s.Signals[i+1]

if sellSignal.Price > buySignal.Price {
profitCount++
} else if sellSignal.Price < buySignal.Price {
lossCount++
if currentSignal.Side == "BUY" && nextSignal.Side == "SELL" {
if nextSignal.Price > currentSignal.Price {
profitCount++
} else if nextSignal.Price < currentSignal.Price {
lossCount++
}
}
}

totalCount := profitCount + lossCount
if totalCount == 0 {
longCount := profitCount + lossCount
if longCount == 0 {
return 0
}

return profitCount / totalCount
return profitCount / longCount
}

func ShortWinRate(s *execute.SignalEvents) float64 {
Expand All @@ -40,23 +94,25 @@ func ShortWinRate(s *execute.SignalEvents) float64 {
}
var profitCount, lossCount float64

for i := 0; i < len(s.Signals)-1; i += 2 {
sellSignal := s.Signals[i]
buySignal := s.Signals[i+1]
for i := 0; i < len(s.Signals)-1; i++ {
currentSignal := s.Signals[i]
nextSignal := s.Signals[i+1]

if buySignal.Price < sellSignal.Price {
profitCount++
} else if buySignal.Price > sellSignal.Price {
lossCount++
if currentSignal.Side == "SELL" && nextSignal.Side == "BUY" {
if nextSignal.Price < currentSignal.Price {
profitCount++
} else if nextSignal.Price > currentSignal.Price {
lossCount++
}
}
}

totalCount := profitCount + lossCount
if totalCount == 0 {
shortCount := profitCount + lossCount
if shortCount == 0 {
return 0
}

return profitCount / totalCount
return profitCount / shortCount
}

func TotalWinRate(s *execute.SignalEvents) float64 {
Expand Down
38 changes: 25 additions & 13 deletions pkg/execute/signal.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (s *SignalEvents) CanBuy(t time.Time) bool {
return false
}

func (s *SignalEvents) CanClose(t time.Time) bool {
func (s *SignalEvents) CanLongClose(t time.Time) bool {
lenSignals := len(s.Signals)
if lenSignals == 0 {
return false
Expand All @@ -141,7 +141,7 @@ func (s *SignalEvents) CanClose(t time.Time) bool {
func (s *SignalEvents) CanSell(t time.Time) bool {
lenSignals := len(s.Signals)
if lenSignals == 0 {
return false
return true
}

lastSignal := s.Signals[lenSignals-1]
Expand All @@ -151,10 +151,23 @@ func (s *SignalEvents) CanSell(t time.Time) bool {
return false
}

func (s *SignalEvents) Buy(strategyName string, assetName string, duration string, date time.Time, price, size float64, accountBalance float64, save bool) bool {
func (s *SignalEvents) CanShortClose(t time.Time) bool {
lenSignals := len(s.Signals)
if lenSignals == 0 {
return false
}

lastSignal := s.Signals[lenSignals-1]
if lastSignal.Side == "SELL" && lastSignal.Time.Before(t) {
return true
}
return false
}

func (s *SignalEvents) Buy(strategyName string, assetName string, duration string, date time.Time, price, size float64, accountBalance float64, save bool) (bool, uuid.UUID) {

if !s.CanBuy(date) {
return false
return false, uuid.UUID{}
}

signalId := uuid.New()
Expand All @@ -178,14 +191,13 @@ func (s *SignalEvents) Buy(strategyName string, assetName string, duration strin
// }
s.Signals = append(s.Signals, signalEvent)

return true
return true, signalId
}

func (s *SignalEvents) Sell(strategyName string, assetName string, duration string, date time.Time, price, size float64, accountBalance float64, save bool) bool {
func (s *SignalEvents) Sell(strategyName string, assetName string, duration string, date time.Time, price, size float64, accountBalance float64, save bool) (bool, uuid.UUID) {

if !s.CanSell(date) {

return false
return false, uuid.UUID{}
}
signalId := uuid.New()
signalEvent := SignalEvent{
Expand All @@ -206,23 +218,23 @@ func (s *SignalEvents) Sell(strategyName string, assetName string, duration stri
// }

s.Signals = append(s.Signals, signalEvent)
return true
return true, signalId
}

func (s *SignalEvents) Close(strategyName string, assetName string, duration string, date time.Time, price, size float64, accountBalance float64, save bool) bool {
func (s *SignalEvents) Close(signalId uuid.UUID, strategyName string, assetName string, duration string, date time.Time, price, size float64, accountBalance float64, save bool) bool {

if !s.CanClose(date) {
if !s.CanLongClose(date) || !s.CanShortClose(date) {

return false
}
signalId := uuid.New()

signalEvent := SignalEvent{
SignalId: signalId,
Time: date,
StrategyName: strategyName,
AssetName: assetName,
Duration: duration,
Side: "SELL",
Side: "CLOSE",
Price: price,
Size: size,
AccountBalance: accountBalance,
Expand Down
Loading

0 comments on commit 19916c6

Please sign in to comment.