@@ -29,6 +29,7 @@ type PaperWallet struct {
29
29
orders []model.Order
30
30
assets map [string ]* assetInfo
31
31
avgPrice map [string ]float64
32
+ volume map [string ]float64
32
33
lastCandle map [string ]model.Candle
33
34
fistCandle map [string ]model.Candle
34
35
}
@@ -66,6 +67,7 @@ func NewPaperWallet(ctx context.Context, baseCoin string, options ...PaperWallet
66
67
fistCandle : make (map [string ]model.Candle ),
67
68
lastCandle : make (map [string ]model.Candle ),
68
69
avgPrice : make (map [string ]float64 ),
70
+ volume : make (map [string ]float64 ),
69
71
}
70
72
71
73
for _ , option := range options {
@@ -88,18 +90,29 @@ func (p *PaperWallet) Summary() {
88
90
var (
89
91
total float64
90
92
marketChange float64
93
+ volume float64
91
94
)
92
95
93
96
fmt .Println ("--------------" )
94
97
fmt .Println ("WALLET SUMMARY" )
95
98
fmt .Println ("--------------" )
99
+
96
100
for pair , price := range p .avgPrice {
97
101
asset , _ := SplitAssetQuote (pair )
98
102
quantity := p .assets [asset ].Free + p .assets [asset ].Lock
99
103
total += quantity * price
100
104
marketChange += (p .lastCandle [pair ].Close - p .fistCandle [pair ].Close ) / p .fistCandle [pair ].Close
101
105
fmt .Printf ("%f %s\n " , quantity , asset )
102
106
}
107
+
108
+ fmt .Println ()
109
+ fmt .Println ("TRADING VOLUME" )
110
+ for symbol , vol := range p .volume {
111
+ volume += vol
112
+ fmt .Printf ("%s = %.2f %s\n " , symbol , vol , p .baseCoin )
113
+ }
114
+ fmt .Println ()
115
+
103
116
avgMarketChange := marketChange / float64 (len (p .avgPrice ))
104
117
baseCoinValue := p .assets [p .baseCoin ].Free + p .assets [p .baseCoin ].Lock
105
118
profit := total + baseCoinValue - p .initialValue
@@ -109,6 +122,8 @@ func (p *PaperWallet) Summary() {
109
122
fmt .Println ("FINAL PORTFOLIO = " , total + baseCoinValue , p .baseCoin )
110
123
fmt .Printf ("GROSS PROFIT = %f %s (%.2f%%)\n " , profit , p .baseCoin , profit / p .initialValue * 100 )
111
124
fmt .Printf ("MARKET CHANGE = %.2f%%\n " , avgMarketChange * 100 )
125
+ fmt .Printf ("VOLUME = %.2f %s\n " , volume , p .baseCoin )
126
+ fmt .Printf ("COSTS (0.001*V) = %.2f %s (ESTIMATION) \n " , volume * 0.001 , p .baseCoin )
112
127
fmt .Println ("--------------" )
113
128
}
114
129
@@ -136,20 +151,25 @@ func (p *PaperWallet) OnCandle(candle model.Candle) {
136
151
continue
137
152
}
138
153
154
+ if _ , ok := p .volume [candle .Symbol ]; ! ok {
155
+ p .volume [candle .Symbol ] = 0
156
+ }
157
+
139
158
asset , quote := SplitAssetQuote (order .Symbol )
140
159
if order .Side == model .SideTypeBuy && order .Price <= candle .Close {
141
160
if _ , ok := p .assets [asset ]; ! ok {
142
161
p .assets [asset ] = & assetInfo {}
143
162
}
144
163
145
164
actualQty := p .assets [asset ].Free + p .assets [asset ].Lock
146
- orderValue := order .Price * order .Quantity
165
+ orderVolume := order .Price * order .Quantity
147
166
walletValue := p .avgPrice [candle .Symbol ] * actualQty
148
167
168
+ p .volume [candle .Symbol ] += orderVolume
149
169
p .orders [i ].Status = model .OrderStatusTypeFilled
150
- p .avgPrice [candle .Symbol ] = (walletValue + orderValue ) / (actualQty + order .Quantity )
170
+ p .avgPrice [candle .Symbol ] = (walletValue + orderVolume ) / (actualQty + order .Quantity )
151
171
p .assets [asset ].Free = p .assets [asset ].Free + order .Quantity
152
- p .assets [quote ].Lock = p .assets [quote ].Lock - orderValue
172
+ p .assets [quote ].Lock = p .assets [quote ].Lock - orderVolume
153
173
}
154
174
155
175
if order .Side == model .SideTypeSell {
@@ -168,7 +188,7 @@ func (p *PaperWallet) OnCandle(candle model.Candle) {
168
188
continue
169
189
}
170
190
171
- // cancel other others from same group
191
+ // Cancel other orders from same group
172
192
if order .GroupID != nil {
173
193
for j , groupOrder := range p .orders {
174
194
if groupOrder .GroupID != nil && * groupOrder .GroupID == * order .GroupID &&
@@ -183,10 +203,12 @@ func (p *PaperWallet) OnCandle(candle model.Candle) {
183
203
p .assets [quote ] = & assetInfo {}
184
204
}
185
205
206
+ orderVolume := order .Quantity * orderPrice
186
207
profitValue := order .Quantity * orderPrice - order .Quantity * p .avgPrice [candle .Symbol ]
187
208
percentage := profitValue / (order .Quantity * p .avgPrice [candle .Symbol ])
188
209
log .Infof ("PROFIT = %.4f %s (%.2f %%)" , profitValue , quote , percentage * 100 )
189
210
211
+ p .volume [candle .Symbol ] += orderVolume
190
212
p .orders [i ].UpdatedAt = candle .Time
191
213
p .orders [i ].Status = model .OrderStatusTypeFilled
192
214
p .assets [asset ].Lock = p .assets [asset ].Lock - order .Quantity
@@ -324,6 +346,12 @@ func (p *PaperWallet) OrderMarket(side model.SideType, symbol string, size float
324
346
p .assets [asset ].Free = p .assets [asset ].Free + size
325
347
}
326
348
349
+ if _ , ok := p .volume [symbol ]; ! ok {
350
+ p .volume [symbol ] = 0
351
+ }
352
+
353
+ p .volume [symbol ] += p .lastCandle [symbol ].Close * size
354
+
327
355
order := model.Order {
328
356
ExchangeID : p .ID (),
329
357
CreatedAt : p .lastCandle [symbol ].Time ,
0 commit comments