Skip to content

Commit

Permalink
クローズフラグメント実装 *注 リファクタリング必須
Browse files Browse the repository at this point in the history
  • Loading branch information
T.K committed Feb 11, 2024
1 parent 434817b commit 80c0610
Show file tree
Hide file tree
Showing 32 changed files with 152 additions and 3,572 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: "LINKUSDT"
duration: "15m"
start: ""
end: ""
assetName: "AVAXUSDT"
duration: "1h"
start: "20220101"
end: "20230101"
simpleInterest: false
positionPersentage: 0.9
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ func main() {

start := time.Now()

strategey.RunEmaOptimize()
strategey.RunSTOptimize()
// strategey.RunEmaOptimize()
// strategey.RunSTOptimize()
strategey.RunEmaRsiOptimize()
// strategey.RunDonchainOptimize()

Expand Down
16 changes: 8 additions & 8 deletions pkg/analytics/profit_and_loss.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ func LongProfit(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 0.0
}
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 {
profit += (signal.Price - buyPrice) * signal.Size
}
Expand All @@ -111,12 +111,12 @@ func LongLoss(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 0.0
}
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 {
loss += (buyPrice - signal.Price) * signal.Size
}
Expand All @@ -139,12 +139,12 @@ func ShortProfit(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 0.0
}
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 {
profit += (sellPrice - signal.Price) * signal.Size
}
Expand All @@ -168,12 +168,12 @@ func ShortLoss(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 0.0
}
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 {
loss += (signal.Price - sellPrice) * signal.Size
}
Expand Down
22 changes: 20 additions & 2 deletions pkg/analytics/total_trade_counts.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,25 @@ import (
// return totalTrades
// }

// func TotalTrades(s *execute.SignalEvents) int {

// if s == nil {
// return 0
// }
// var totalTrades int

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

// if currentSignal.Side != nextSignal.Side {
// totalTrades++
// }
// }

// return totalTrades
// }

func TotalTrades(s *execute.SignalEvents) int {

if s == nil {
Expand All @@ -31,9 +50,8 @@ func TotalTrades(s *execute.SignalEvents) int {

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

if currentSignal.Side != nextSignal.Side {
if currentSignal.Side == "CLOSE" {
totalTrades++
}
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/analytics/winrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,13 @@ func TotalWinRate(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 {
lossCount++
}
} else if currentSignal.Side == "SELL" && nextSignal.Side == "BUY" {
} else if currentSignal.Side == "SELL" && nextSignal.Side == "CLOSE" {
if nextSignal.Price < currentSignal.Price {
profitCount++
} else if nextSignal.Price > currentSignal.Price {
Expand Down
67 changes: 33 additions & 34 deletions pkg/execute/signal.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,33 +119,33 @@ func (s *SignalEvents) CanBuy(t time.Time) bool {
}

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

return true

}
return false
}

func (s *SignalEvents) CanLongClose(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]
if lastSignal.Side == "BUY" && lastSignal.Time.Before(t) {
if lastSignal.Side == "CLOSE" && lastSignal.Time.Before(t) {

return true

}
return false
}

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

lastSignal := s.Signals[lenSignals-1]
if lastSignal.Side == "BUY" && lastSignal.Time.Before(t) {
if lastSignal.Side == "BUY" && t.After(lastSignal.Time) {
return true
}
return false
Expand All @@ -158,19 +158,18 @@ func (s *SignalEvents) CanShortClose(t time.Time) bool {
}

lastSignal := s.Signals[lenSignals-1]
if lastSignal.Side == "SELL" && lastSignal.Time.Before(t) {
if lastSignal.Side == "SELL" && t.After(lastSignal.Time) {
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) {
func (s *SignalEvents) Buy(signalId uuid.UUID, 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, uuid.UUID{}
}

signalId := uuid.New()
signalEvent := SignalEvent{
SignalId: signalId,
Time: date,
Expand All @@ -194,12 +193,12 @@ func (s *SignalEvents) Buy(strategyName string, assetName string, duration strin
return true, signalId
}

func (s *SignalEvents) Sell(strategyName string, assetName string, duration string, date time.Time, price, size float64, accountBalance float64, save bool) (bool, uuid.UUID) {
func (s *SignalEvents) Sell(signalId uuid.UUID, 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, uuid.UUID{}
}
signalId := uuid.New()

signalEvent := SignalEvent{
SignalId: signalId,
Time: date,
Expand All @@ -223,28 +222,28 @@ func (s *SignalEvents) Sell(strategyName string, assetName string, duration stri

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

// return false
// }
signalEvent := SignalEvent{
SignalId: signalId,
Time: date,
StrategyName: strategyName,
AssetName: assetName,
Duration: duration,
Side: "CLOSE",
Price: price,
Size: size,
AccountBalance: accountBalance,
}

signalEvent := SignalEvent{
SignalId: signalId,
Time: date,
StrategyName: strategyName,
AssetName: assetName,
Duration: duration,
Side: "CLOSE",
Price: price,
Size: size,
AccountBalance: accountBalance,
}
// if save {
// signalEvent.Save()

// if save {
// signalEvent.Save()
// }

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

s.Signals = append(s.Signals, signalEvent)
return true
return false
}
128 changes: 0 additions & 128 deletions pkg/strategey/bb.go

This file was deleted.

Loading

0 comments on commit 80c0610

Please sign in to comment.