Skip to content

Commit

Permalink
クローズフラグメント 修正 part1
Browse files Browse the repository at this point in the history
  • Loading branch information
T.K committed Feb 12, 2024
1 parent 80c0610 commit d044186
Show file tree
Hide file tree
Showing 12 changed files with 110 additions and 66 deletions.
8 changes: 4 additions & 4 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@


#バックテストの基本設定
assetName: "AVAXUSDT"
duration: "1h"
start: "20220101"
end: "20230101"
assetName: "SOLUSDT"
duration: "15m"
start: ""
end: ""
simpleInterest: false
positionPersentage: 0.9
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ require (
github.com/ramya-rao-a/go-outline v0.0.0-20210608161538-9736a4bde949 // indirect
github.com/rivo/uniseg v0.4.6 // indirect
github.com/rocketlaunchr/dataframe-go v0.0.0-20211025052708-a1030444159b // indirect
github.com/shopspring/decimal v1.3.1 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
golang.org/x/crypto v0.18.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,8 @@ github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFR
github.com/ruudk/golang-pdf417 v0.0.0-20181029194003-1af4ab5afa58/go.mod h1:6lfFZQK844Gfx8o5WFuvpxWRwnSoipWe/p622j1v06w=
github.com/sandertv/go-formula/v2 v2.0.0-alpha.7/go.mod h1:Ag4V2fiOHWXct3SraXNN3dFzFtyu9vqBfrjfYWMGLhE=
github.com/shabbyrobe/xmlwriter v0.0.0-20200208144257-9fca06d00ffa/go.mod h1:Yjr3bdWaVWyME1kha7X0jsz3k2DgXNa1Pj3XGyUAbx8=
github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8=
github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
github.com/sirupsen/logrus v1.0.4-0.20170822132746-89742aefa4b2/go.mod h1:pMByvHTf9Beacp5x1UXfOR9xyW/9antXMhjMPG0dEzc=
github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
Expand Down
9 changes: 4 additions & 5 deletions pkg/analytics/profit_and_loss.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ func PLSlice(s *execute.SignalEvents) []float64 {
}
for _, signal := range s.Signals {

if signal.Side != "BUY" && signal.Side != "SELL" {
if signal.Side != "BUY" && signal.Side != "SELL" && signal.Side != "CLOSE" {
return nil
}
if signal.Side == "BUY" {
Expand All @@ -390,7 +390,7 @@ func PLSlice(s *execute.SignalEvents) []float64 {
if sellPrice != 0 {
pl = append(pl, (sellPrice-buyPrice)*signal.Size)
// reset the sell price
sellPrice = 0
buyPrice = 0
}
}
if signal.Side == "SELL" {
Expand All @@ -399,11 +399,10 @@ func PLSlice(s *execute.SignalEvents) []float64 {
if buyPrice != 0 {
pl = append(pl, (sellPrice-buyPrice)*signal.Size)
// reset the buy price
buyPrice = 0
sellPrice = 0
}
}
}

return pl
}

Expand All @@ -422,7 +421,7 @@ func TotalProfitSlice(s *execute.SignalEvents) []float64 {
}
for _, signal := range s.Signals {

if signal.Side != "BUY" && signal.Side != "SELL" {
if signal.Side != "BUY" && signal.Side != "SELL" && signal.Side != "CLOSE" {
return nil
}
if signal.Side == "BUY" {
Expand Down
17 changes: 0 additions & 17 deletions pkg/analytics/sharp_ratio.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,6 @@ func stdDev(data []float64) float64 {
variance := sqDiffSum / float64(len(data)-1)
return math.Sqrt(variance)
}

// func calculateReturns(s *execute.SignalEvents) []float64 {
// var returns []float64
// var buyPrice float64

// for _, signal := range s.Signals {
// if signal.Side == "BUY" {
// buyPrice = signal.Price
// } else if signal.Side == "SELL" && buyPrice != 0 {
// returns = append(returns, (signal.Price-buyPrice)/buyPrice)
// buyPrice = 0 // Reset buy price after a sell
// }
// }

// return returns
// }

func SharpeRatio(s *execute.SignalEvents, riskFreeRate float64) float64 {

if s == nil {
Expand Down
88 changes: 78 additions & 10 deletions pkg/analytics/total_trade_counts.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func LongWinningTrades(s *execute.SignalEvents) int {
for _, signal := range s.Signals {
if signal.Side == "BUY" {
buyPrice = signal.Price
} else if signal.Side == "SELL" && buyPrice != 0 {
} else if signal.Side == "CLOSE" && buyPrice != 0 {
if signal.Price > buyPrice {
winningTrades++
}
Expand All @@ -92,7 +92,7 @@ func LongLosingTrades(s *execute.SignalEvents) int {
for _, signal := range s.Signals {
if signal.Side == "BUY" {
buyPrice = signal.Price
} else if signal.Side == "SELL" && buyPrice != 0 {
} else if signal.Side == "CLOSE" && buyPrice != 0 {
if signal.Price < buyPrice {
losingTrades++
}
Expand All @@ -113,7 +113,7 @@ func ShortWinningTrades(s *execute.SignalEvents) int {
for _, signal := range s.Signals {
if signal.Side == "SELL" {
sellPrice = signal.Price
} else if signal.Side == "BUY" && sellPrice != 0 {
} else if signal.Side == "CLOSE" && sellPrice != 0 {
if signal.Price < sellPrice {
winningTrades++
}
Expand All @@ -134,7 +134,7 @@ func ShortLosingTrades(s *execute.SignalEvents) int {
for _, signal := range s.Signals {
if signal.Side == "SELL" {
sellPrice = signal.Price
} else if signal.Side == "BUY" && sellPrice != 0 {
} else if signal.Side == "CLOSE" && sellPrice != 0 {
if signal.Price > sellPrice {
losingTrades++
}
Expand Down Expand Up @@ -235,11 +235,12 @@ func AverageWinningHoldingBars(s *execute.SignalEvents) float64 {
var totalBars int
var winningTrades int
var buyPrice float64
var sellPrice float64

for i, signal := range s.Signals {
if signal.Side == "BUY" {
buyPrice = signal.Price
} else if signal.Side == "SELL" && buyPrice != 0 {
} else if signal.Side == "CLOSE" && buyPrice != 0 {
if signal.Price > buyPrice {
// Find the corresponding buy signal
for j := i - 1; j >= 0; j-- {
Expand All @@ -256,6 +257,25 @@ func AverageWinningHoldingBars(s *execute.SignalEvents) float64 {
}
buyPrice = 0 // Reset buy price after a sell
}
if signal.Side == "SELL" { // Assign sellPrice when signal is SELL
sellPrice = signal.Price
} else if signal.Side == "CLOSE" && sellPrice != 0 {
if signal.Price < sellPrice { // Count as a winning trade if signal price is lower than sell price
// Find the corresponding sell signal
for j := i - 1; j >= 0; j-- {
if s.Signals[j].Side == "SELL" { // Change the condition to SELL
// Calculate the number of bars for this trade
// Use the ConvertDuration function to get the bar period in minutes
barPeriod := ConvertDuration(signal.Duration)
bars := int(signal.Time.Sub(s.Signals[j].Time).Minutes() / barPeriod)
totalBars += bars
winningTrades++
break
}
}
}
sellPrice = 0 // Reset sell price after a close
}
}

if winningTrades == 0 {
Expand All @@ -273,11 +293,12 @@ func AverageLosingHoldingBars(s *execute.SignalEvents) float64 {
var totalBars int
var losingTrades int
var buyPrice float64
var sellPrice float64

for i, signal := range s.Signals {
if signal.Side == "BUY" {
buyPrice = signal.Price
} else if signal.Side == "SELL" && buyPrice != 0 {
} else if signal.Side == "CLOSE" && buyPrice != 0 {
if signal.Price < buyPrice {
// Find the corresponding buy signal
for j := i - 1; j >= 0; j-- {
Expand All @@ -294,6 +315,25 @@ func AverageLosingHoldingBars(s *execute.SignalEvents) float64 {
}
buyPrice = 0 // Reset buy price after a sell
}
if signal.Side == "SELL" { // Assign sellPrice when signal is SELL
sellPrice = signal.Price
} else if signal.Side == "CLOSE" && sellPrice != 0 {
if signal.Price > sellPrice { // Count as a losing trade if signal price is higher than sell price
// Find the corresponding sell signal
for j := i - 1; j >= 0; j-- {
if s.Signals[j].Side == "SELL" { // Change the condition to SELL
// Calculate the number of bars for this trade
// Use the ConvertDuration function to get the bar period in minutes
barPeriod := ConvertDuration(signal.Duration)
bars := int(signal.Time.Sub(s.Signals[j].Time).Minutes() / barPeriod)
totalBars += bars
losingTrades++
break
}
}
}
sellPrice = 0 // Reset sell price after a close
}
}

if losingTrades == 0 {
Expand All @@ -308,11 +348,12 @@ func MaxWinCount(s *execute.SignalEvents) int {
}
var maxWinStreak, winStreak int
var buyPrice float64
var sellPrice float64

for _, signal := range s.Signals {
if signal.Side == "BUY" {
buyPrice = signal.Price
} else if signal.Side == "SELL" && buyPrice != 0 {
} else if signal.Side == "CLOSE" && buyPrice != 0 {
if signal.Price > buyPrice {
winStreak++
if winStreak > maxWinStreak {
Expand All @@ -321,7 +362,20 @@ func MaxWinCount(s *execute.SignalEvents) int {
} else {
winStreak = 0
}
buyPrice = 0 // Reset buy price after a sell
buyPrice = 0
}
if signal.Side == "SELl" {
sellPrice = signal.Price
} else if signal.Side == "CLOSE" && sellPrice != 0 {
if signal.Price < sellPrice {
winStreak++
if winStreak > maxWinStreak {
maxWinStreak = winStreak
}
} else {
winStreak = 0
}
sellPrice = 0 // Reset buy price after a sell
}
}

Expand All @@ -334,11 +388,12 @@ func MaxLoseCount(s *execute.SignalEvents) int {
}
var maxLoseStreak, loseStreak int
var buyPrice float64
var sellPrice float64

for _, signal := range s.Signals {
if signal.Side == "BUY" {
buyPrice = signal.Price
} else if signal.Side == "SELL" && buyPrice != 0 {
} else if signal.Side == "CLOSE" && buyPrice != 0 {
if signal.Price < buyPrice {
loseStreak++
if loseStreak > maxLoseStreak {
Expand All @@ -347,7 +402,20 @@ func MaxLoseCount(s *execute.SignalEvents) int {
} else {
loseStreak = 0
}
buyPrice = 0 // Reset buy price after a sell
buyPrice = 0
}
if signal.Side == "SELL" {
sellPrice = signal.Price
} else if signal.Side == "CLOSE" && sellPrice != 0 {
if signal.Price > sellPrice {
loseStreak++
if loseStreak > maxLoseStreak {
maxLoseStreak = loseStreak
}
} else {
loseStreak = 0
}
sellPrice = 0
}
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/analytics/winrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func WinRate(s *execute.SignalEvents) float64 {
currentSignal := s.Signals[i]
nextSignal := s.Signals[i+1]

if currentSignal.Side == "BUY" && nextSignal.Side == "SELL" {
if currentSignal.Side == "BUY" && nextSignal.Side == "CLOSE" {
if nextSignal.Price > currentSignal.Price {
profitCount++
} else if nextSignal.Price < currentSignal.Price {
Expand Down Expand Up @@ -98,7 +98,7 @@ func ShortWinRate(s *execute.SignalEvents) float64 {
currentSignal := s.Signals[i]
nextSignal := s.Signals[i+1]

if currentSignal.Side == "SELL" && nextSignal.Side == "BUY" {
if currentSignal.Side == "SELL" && nextSignal.Side == "CLOSE" {
if nextSignal.Price < currentSignal.Price {
profitCount++
} else if nextSignal.Price > currentSignal.Price {
Expand Down
15 changes: 3 additions & 12 deletions pkg/execute/signal.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,24 +141,15 @@ func (s *SignalEvents) CanSell(t time.Time) bool {
}
return false
}
func (s *SignalEvents) CanLongClose(t time.Time) bool {
lenSignals := len(s.Signals)

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

func (s *SignalEvents) CanShortClose(t time.Time) bool {
func (s *SignalEvents) CanClose(t time.Time) bool {
lenSignals := len(s.Signals)
if lenSignals == 0 {
return false
}

lastSignal := s.Signals[lenSignals-1]
if lastSignal.Side == "SELL" && t.After(lastSignal.Time) {
if lastSignal.Side == "SELL" || lastSignal.Side == "BUY" && t.After(lastSignal.Time) {
return true
}
return false
Expand Down Expand Up @@ -222,7 +213,7 @@ func (s *SignalEvents) Sell(signalId uuid.UUID, strategyName string, assetName s

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.CanLongClose(date) || s.CanShortClose(date) {
if s.CanClose(date) {

signalEvent := SignalEvent{
SignalId: signalId,
Expand Down
4 changes: 2 additions & 2 deletions pkg/strategey/buy&hold.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ func BuyAndHoldingStrategy(account *trader.Account) (profit float64, multiple fl
}
// account := trader.NewAccount(1000)

buySize := account.TradeSize(1) / close[0]
buySize := account.TradeSize(1)
account.HolderBuy(close[0], buySize)

account.Exit(close[len(close)-1])
account.Exit(close[lenCandles-1])

profit = account.Balance - initialBalance
multiple = account.Balance / initialBalance
Expand Down
Loading

0 comments on commit d044186

Please sign in to comment.