Skip to content

Commit

Permalink
Closeフラグメント完全実装
Browse files Browse the repository at this point in the history
  • Loading branch information
T.K committed Feb 16, 2024
1 parent d044186 commit 6790dc6
Show file tree
Hide file tree
Showing 6 changed files with 484 additions and 90 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: "SOLUSDT"
duration: "15m"
assetName: "ETHUSDT"
duration: "30m"
start: ""
end: ""
simpleInterest: false
Expand Down
98 changes: 79 additions & 19 deletions pkg/analytics/profit_and_loss.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ func PLSlice(s *execute.SignalEvents) []float64 {
var pl []float64 // profit or loss slice
var buyPrice float64
var sellPrice float64
var lastSide string

if s.Signals == nil || len(s.Signals) == 0 {
return nil
Expand All @@ -386,26 +387,75 @@ func PLSlice(s *execute.SignalEvents) []float64 {
}
if signal.Side == "BUY" {
buyPrice = signal.Price
// 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
buyPrice = 0
}
lastSide = "BUY"

}
if signal.Side == "SELL" {
sellPrice = signal.Price
// if there is a previous buy price, calculate the profit or loss
if buyPrice != 0 {
lastSide = "SELL"

}
if signal.Side == "CLOSE" {
if lastSide == "BUY" {
pl = append(pl, (sellPrice-buyPrice)*signal.Size)
buyPrice = 0
}
if lastSide == "SELL" {
pl = append(pl, (sellPrice-buyPrice)*signal.Size)
// reset the buy price
sellPrice = 0
}
lastSide = ""
}
}
return pl
}

// func TotalProfitSlice(s *execute.SignalEvents) []float64 {
// if s == nil {
// return nil
// }
// var profit []float64 // total profit slice
// var buyPrice float64
// var sellPrice float64
// var longProfit float64
// var shortProfit float64

// if s.Signals == nil || len(s.Signals) == 0 {
// return nil
// }
// for _, signal := range s.Signals {

// if signal.Side != "BUY" && signal.Side != "SELL" && signal.Side != "CLOSE" {
// return nil
// }
// if signal.Side == "BUY" {
// buyPrice = signal.Price
// // if there is a previous sell price, calculate the short profit
// if sellPrice != 0 {
// shortProfit = (sellPrice - buyPrice) * signal.Size
// profit = append(profit, shortProfit)
// // reset the sell price and short profit
// sellPrice = 0
// shortProfit = 0
// }
// }
// if signal.Side == "SELL" {
// sellPrice = signal.Price
// // if there is a previous buy price, calculate the long profit
// if buyPrice != 0 {
// longProfit = (sellPrice - buyPrice) * signal.Size
// profit = append(profit, longProfit)
// // reset the buy price and long profit
// buyPrice = 0
// longProfit = 0
// }
// }
// }

// return profit
// }

func TotalProfitSlice(s *execute.SignalEvents) []float64 {
if s == nil {
return nil
Expand All @@ -415,6 +465,7 @@ func TotalProfitSlice(s *execute.SignalEvents) []float64 {
var sellPrice float64
var longProfit float64
var shortProfit float64
var lastSide string

if s.Signals == nil || len(s.Signals) == 0 {
return nil
Expand All @@ -426,26 +477,35 @@ func TotalProfitSlice(s *execute.SignalEvents) []float64 {
}
if signal.Side == "BUY" {
buyPrice = signal.Price
// if there is a previous sell price, calculate the short profit
if sellPrice != 0 {
shortProfit = (sellPrice - buyPrice) * signal.Size
profit = append(profit, shortProfit)
// reset the sell price and short profit
sellPrice = 0
shortProfit = 0
}
lastSide = "BUY"
}
if signal.Side == "SELL" {
sellPrice = signal.Price
// if there is a previous buy price, calculate the long profit
if buyPrice != 0 {
longProfit = (sellPrice - buyPrice) * signal.Size
lastSide = "SELL"
}

if signal.Side == "CLOSE" {
// if the last opened order side is BUY, calculate the long profit
if lastSide == "BUY" {
longProfit = (signal.Price - buyPrice) * signal.Size
profit = append(profit, longProfit)
// reset the buy price and long profit
buyPrice = 0
longProfit = 0
}
// if the last opened order side is SELL, calculate the short profit
if lastSide == "SELL" {
shortProfit = (sellPrice - signal.Price) * signal.Size
profit = append(profit, shortProfit)
// reset the sell price and short profit
sellPrice = 0
shortProfit = 0
}
// reset the last opened order side
lastSide = ""

}

}

return profit
Expand Down
2 changes: 1 addition & 1 deletion pkg/strategey/buy&hold.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func BuyAndHoldingStrategy(account *trader.Account) (profit float64, multiple fl
buySize := account.TradeSize(1)
account.HolderBuy(close[0], buySize)

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

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

0 comments on commit 6790dc6

Please sign in to comment.