-
Notifications
You must be signed in to change notification settings - Fork 0
/
score.go
169 lines (149 loc) · 3.98 KB
/
score.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package malgova
import (
"fmt"
"math"
"sort"
"gonum.org/v1/gonum/stat"
)
type tradeData struct {
algoName string
symbol string
orders []orderEntry
score AlgoScore
trades []tradeEntry
}
// AlgoScore struct
type AlgoScore struct {
AlgoName string
Symbol string
// stats and scores
OrdersCount int
TradesCount int
TradesWon int
TradesLost int
WinStreak int
LossStreak int
NetPnl float64
NetPnlPercentAverage float64
NetPnlPercentStdDev float64
SQN float64
}
func (t AlgoScore) String() string {
return fmt.Sprintf("%12s|%20s|%5d|%4d|%4d:%4d|%3d:%3d| %9.2f |%9.2f|%9.2f| %7.3f", t.AlgoName, t.Symbol, t.OrdersCount, t.TradesCount, t.TradesWon, t.TradesLost, t.WinStreak, t.LossStreak, t.NetPnl, t.NetPnlPercentAverage, t.NetPnlPercentStdDev, t.SQN)
}
type tradeEntry struct {
orders int
buyValue float64
sellValue float64
pnl float64
pnlPercentage float64
}
type algoTradeData struct {
bySymbolTrades map[string]*tradeData
}
func (a *tradeData) add(t orderEntry) {
a.orders = append(a.orders, t)
}
// reset score
func (a *tradeData) resetScore() {
a.trades = make([]tradeEntry, 0)
a.score = AlgoScore{
AlgoName: a.algoName,
Symbol: a.symbol,
}
}
func (a *tradeData) consolidateTrades() {
//sort orders by time
sort.Slice(a.orders, func(i, j int) bool {
return a.orders[i].at.Before(a.orders[j].at)
})
// consolidate orders into trades
pos := 0
openTrade := tradeEntry{}
for _, o := range a.orders {
if pos == 0 {
openTrade.orders = 0
openTrade.buyValue = 0
openTrade.sellValue = 0
}
pos += o.qty
if o.qty > 0 {
openTrade.buyValue = float64(o.qty) * o.price
} else {
openTrade.sellValue = -float64(o.qty) * o.price
}
openTrade.orders++
if pos == 0 {
openTrade.pnl = openTrade.sellValue - openTrade.buyValue
if openTrade.buyValue > 0 {
openTrade.pnlPercentage = (openTrade.pnl)
} else if openTrade.pnl == 0 {
openTrade.pnlPercentage = 0
} else if openTrade.pnl < 0 {
openTrade.pnlPercentage = -100
} else {
openTrade.pnlPercentage = 100
}
a.trades = append(a.trades, openTrade)
}
}
}
func (a *tradeData) processScore() {
a.resetScore()
a.consolidateTrades()
a.score.TradesCount = len(a.trades)
pnl := make([]float64, 0)
winStreak := 0
lossStreak := 0
if a.score.TradesCount > 0 {
for _, t := range a.trades {
a.score.OrdersCount += t.orders
if t.pnl > 0 {
winStreak++
lossStreak = 0
a.score.TradesWon++
} else {
winStreak = 0
lossStreak++
a.score.TradesLost++
}
a.score.NetPnl += t.pnl
pnl = append(pnl, t.pnlPercentage)
if a.score.WinStreak < winStreak {
a.score.WinStreak = winStreak
}
if a.score.LossStreak < lossStreak {
a.score.LossStreak = lossStreak
}
}
a.score.NetPnlPercentAverage = stat.Mean(pnl, nil)
a.score.NetPnlPercentStdDev = stat.StdDev(pnl, nil)
if a.score.NetPnlPercentStdDev != 0 {
a.score.SQN = math.Sqrt(float64(a.score.TradesCount)) * a.score.NetPnlPercentAverage / a.score.NetPnlPercentStdDev
}
}
}
func calculateAlgoScores(orders []orderEntry) []AlgoScore {
scores := make([]AlgoScore, 0)
mapAlgoData := make(map[string]*algoTradeData)
for _, t := range orders {
if _, ok := mapAlgoData[t.algoName]; !ok {
mapAlgoData[t.algoName] = new(algoTradeData)
mapAlgoData[t.algoName].bySymbolTrades = make(map[string]*tradeData)
}
if _, ok := mapAlgoData[t.algoName].bySymbolTrades[t.symbol]; !ok {
mapAlgoData[t.algoName].bySymbolTrades[t.symbol] = new(tradeData)
mapAlgoData[t.algoName].bySymbolTrades[t.symbol].orders = make([]orderEntry, 0)
mapAlgoData[t.algoName].bySymbolTrades[t.symbol].algoName = t.algoName
mapAlgoData[t.algoName].bySymbolTrades[t.symbol].symbol = t.symbol
}
mapAlgoData[t.algoName].bySymbolTrades[t.symbol].add(t)
}
for _, a := range mapAlgoData {
for _, st := range a.bySymbolTrades {
st.processScore()
scores = append(scores, st.score)
}
}
return scores
}