-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
360 lines (317 loc) · 14.8 KB
/
util.py
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
import os
from dotenv import load_dotenv
import math
import time
import random
import numpy as np
from time import sleep
from datetime import datetime, timezone, timedelta
import pymongo
import statistics
import plot
monthMap = {
"01": "31",
"02": "28",
"03": "31",
"04": "30",
"05": "31",
"06": "30",
"07": "31",
"08": "31",
"09": "30",
"10": "31",
"11": "30",
"12": "31",
}
def dateNow():
dt1 = datetime.utcnow().replace(tzinfo=timezone.utc)
dt2 = dt1.astimezone(timezone(timedelta(hours=8)))
return dt2.strftime("%Y-%m-%d")
def timeNow():
dt1 = datetime.utcnow().replace(tzinfo=timezone.utc)
dt2 = dt1.astimezone(timezone(timedelta(hours=8)))
return dt2.strftime("%Y-%m-%d %H-%M-%S")
# log txt file
def log(user, function):
path = './log.txt'
f = open(path, 'a', encoding='UTF-8')
print(f'time: {timeNow()}, user: {user}, {function}', file=f)
f.close()
async def copySimulate(traderName, margin = 10, lossPerPosition = 100, startDate = "2023-02-01"):
load_dotenv()
client = pymongo.MongoClient(os.getenv('MONGODB'))
drawdownDB = client.TraderDrawdown.traderDrawdown
# lastday = monthMap[month.split('-')[1]]
traderData = drawdownDB.find({"traderName": traderName})
traderData = (list(traderData))
if len(traderData) == 0 :
hasDrawdownData = False
# print('Cannot find trader data in DB.')
return False
else:
traderData = traderData[0]
positionCount = 0
suggestCapital = 0.00
today = dateNow()
startTimestamp = int(time.mktime(time.strptime(f"{ startDate } 00:00:00", "%Y-%m-%d %H:%M:%S")))
endTimestamp = int(time.mktime(time.strptime(f"{ today } 23:59:59", "%Y-%m-%d %H:%M:%S")))
pnlInDays = np.zeros(int((endTimestamp-startTimestamp)/86400), dtype=float)
traderShareRatio = 0.1
totalTraderShare = 0.00
copyProfit = 0.00
hasDrawdownData = True
history = traderData["history"]
traderId = traderData["traderId"]
countSL = 0
for each in history:
openTimestamp = int(time.mktime(time.strptime(each["openDate"], "%Y-%m-%d %H:%M:%S")))
closeTimestamp = int(time.mktime(time.strptime(each["closeDate"], "%Y-%m-%d %H:%M:%S")))
leverage = each["leverage"]
drawdown = each["drawdown"]
if startTimestamp <= openTimestamp and closeTimestamp <= endTimestamp:
positionCount += 1
day = int((closeTimestamp-startTimestamp)/86400)
# 手續費6%, 開倉平倉各付一次
fee = 0.0006 * margin * leverage * 2
if drawdown >= lossPerPosition :
pnl = (-1 * lossPerPosition / 100) * margin
countSL += 1
else:
pnl = (margin * float(each["revenue"])/100) - fee
if pnl > 0:
pnlInDays[day] += pnl * (1 - traderShareRatio)
totalTraderShare += pnl * traderShareRatio
copyProfit += pnl * (1 - traderShareRatio)
else:
pnlInDays[day] += pnl
copyProfit += pnl
# PLOT -------------------------------------
x = []
y = []
y.append(pnlInDays[0])
for i in range(len(pnlInDays)):
x.append(i + 1)
if i == 0:
pass
else:
y.append(pnlInDays[i] + y[i - 1])
filename = plot.plotLine(x, y, startDate, today)
# ------------------------------------------
client.close()
return {
"traderName": traderName, # 交易員名稱
"positionCount": positionCount, # 總計開倉數
"pnlInDays": pnlInDays, # 每日收益
"totalTraderShare": totalTraderShare, # 帶單員分潤收益
"copyProfit": round(copyProfit, 2), # 跟單收益
"suggestCapital": suggestCapital, # 建議本金
"countSL": countSL, # 共計止損?次
"filename": filename, # 圖表檔案名稱
}
def potentialExtremeMaxDrawdown(drawdownData, turns):
drawdownList = []
roundList = []
potentialMaxDrawdown = 0
for r in range(len(turns)):
roundList.append(turns[r][0])
for row in drawdownData:
drawdownList.append(round(float(row["drawdown"]) / 100, 2))
while (len(drawdownList) < sum(roundList)):
drawdownList.append(
drawdownList[random.randint(0, len(drawdownList) - 1)])
maxPosition = max(roundList)
drawdownList.sort(reverse=True)
for i in range(maxPosition):
potentialMaxDrawdown = potentialMaxDrawdown + drawdownList[i]
return potentialMaxDrawdown
async def analyzeTraderMDD(traderName, initialCapital = 10000, maxLossPercent = 20, startDate = "2023-02-01"):
# Initialize
load_dotenv()
client = pymongo.MongoClient(os.getenv('MONGODB'))
drawdownDB = client.TraderDrawdown.traderDrawdown
today = dateNow()
hasDrawdownData = False
# Query in drawdown DB
traderData = drawdownDB.find({"traderName": traderName})
traderData = (list(traderData))
# print(traderData)
if len(traderData) == 0 :
hasDrawdownData = False
# print('Cannot find trader data in DB.')
return False
else:
traderData = traderData[0]
hasDrawdownData = True
startTimestamp = int(time.mktime(time.strptime(f"{ startDate } 00:00:00", "%Y-%m-%d %H:%M:%S")))
endTimestamp = int(time.mktime(time.strptime(f"{ today } 23:59:59", "%Y-%m-%d %H:%M:%S")))
history = traderData["history"]
traderId = traderData["traderId"]
drawdownData = []
for each in history:
openTimestamp = int(time.mktime(time.strptime(each["openDate"], "%Y-%m-%d %H:%M:%S")))
closeTimestamp = int(time.mktime(time.strptime(each["closeDate"], "%Y-%m-%d %H:%M:%S")))
if startTimestamp <= openTimestamp and closeTimestamp <= endTimestamp:
drawdownData.append(each)
client.close()
# Round
turns = []
operating = []
RevenueVSDrawback = []
if hasDrawdownData == True:
for row in drawdownData:
remove = [] # 存放要刪除的筆數
# ---more data--
if row["drawdown"] <= 0: # 無回撤通常伴隨小收益,負回撤也歸類到1
RevenueVSDrawback.append(1)
else:
RevenueVSDrawback.append(row["revenue"] / row["drawdown"])
# --------------
for trade in operating:
# 新 trade 與在場上的 trade 時間不重疊
if time.mktime(time.strptime(row["closeDate"], "%Y-%m-%d %H:%M:%S")) < time.mktime(time.strptime(trade["openDate"], "%Y-%m-%d %H:%M:%S")):
remove.append(trade)
if len(remove) != 0:
draw = 0.0
num = 0
for trade in operating:
num = num + 1
draw = draw + trade["drawdown"]
turns.append([num, round(draw / 100, 2)])
for re in remove:
operating.remove(re)
operating.append(row)
# 最後一筆資料
draw = 0.0
num = 0
for trade in operating:
num = num + 1
draw = draw + trade["drawdown"]
turns.append([num, round(draw / 100, 2)])
# Result
# print(turns)
if len(turns) != 0:
# 最大回撤保證金(倉數) 回撤保證金倉數平均、 標準差
maxPosition = max([(a[0]) for a in turns])
drawdownHighestPosition = int(turns[np.argmax(turns, axis=0)[1]][0])
drawdownHighest = np.max(np.array(turns), axis=0)[1]
drawdownMean = statistics.mean((a[1]) for a in turns)
drawdownDev = statistics.pstdev((a[1]) for a in turns)
drawdownEstimate = drawdownMean + (3 * drawdownDev)
positionMean = statistics.mean((a[0]) for a in turns)
positionDev = statistics.pstdev((a[0]) for a in turns)
positionEstimate = str(round(positionMean + (2 * positionDev), 0)).split(".")[0]
# 收益/最大浮動虧損
#RD = statistics.NormalDist.from_samples(RevenueVSDrawback)
RDMean = statistics.mean(RevenueVSDrawback)
RDDev = statistics.pstdev(RevenueVSDrawback)
# RDmax = max(RevenueVSDrawback)
RDQuantiles = []
RevenueVSDrawback.sort()
for i in range(len(RevenueVSDrawback)):
for a in range(0, 11):
if i == int((len(RevenueVSDrawback) - 1) * a / 10):
RDQuantiles.append(round(RevenueVSDrawback[i], 2))
# Monte Carlo simulation
# monteCarloMaxDrawdown, monteCarloPosition = monteCarloSimulation(
# drawdownData, turns)
# Potential extreme maximum drawdown
potentialMaxDrawdown = potentialExtremeMaxDrawdown(drawdownData, turns)
# Position strategy
maxLoss = initialCapital * (maxLossPercent / 100)
strategyMaxDrawdown = max(drawdownHighest, drawdownEstimate)
safeTotalMargin = maxLoss / (strategyMaxDrawdown / drawdownHighestPosition)
safeMargin = round(safeTotalMargin / drawdownHighestPosition, 2)
# print(f"※常態分佈 = 保證金浮動虧損平均值 + 3 × 保證金浮動虧損標準差 (包含 99.73% 事件)")
# print(f"※潛在極端 = 將前 N 大單筆保證金浮動虧損組合進最大回合持倉內計算")
# print(f"※建議安全保證金 = (最大可接受虧損金額 ÷ 最大保證金浮動虧損 (%)) ÷ 最大保證金浮動虧損時持倉數")
# print(f"※建議安全總倉數 = 持倉數平均值 + 2 × 持倉數標準差 (包含 95% 事件)\n\n\n")
# print(f"※風報比 = 收益 ÷ 最大浮動虧損")
return {
"traderName": traderName,
"traderId": traderId,
"maxPosition": maxPosition, # 最大回合持倉數
"drawdownHighest": drawdownHighest, # 最大保證金浮動虧損 (倉)
"drawdownHighestPercent": round((drawdownHighest / drawdownHighestPosition) * 100, 2), # 最大保證金浮動虧損 (%)
"drawdownHighestPosition": drawdownHighestPosition, # 發生最大保證金浮動虧損時持倉數
"potentialMDD": round(potentialMaxDrawdown, 2), # 潛在極端最大保證金浮動虧損 (倉)
"potentialMDDPercent": round((potentialMaxDrawdown / maxPosition) * 100, 2), # 潛在極端最大保證金浮動虧損 (%)
"RDMean": round(RDMean, 2), # 風報比平均值
"RDDev": round(RDDev, 2), # 風報比標準差
"RDMid": RDQuantiles[5], # 風報比中位數
"initialCapital": initialCapital, # 初始資金(使用者設定)
"maxLossPercent": maxLossPercent, # 最大風險(使用者設定)
"safeMargin": safeMargin, # 建議安全保證金
"positionEstimate": positionEstimate, # 建議安全倉數上限
}
# My handmade function: FAILED
# def calcMaxMDD(traderId):
# load_dotenv()
# client = pymongo.MongoClient(os.getenv('MONGODB'))
# drawdownDB = client.TraderDrawdown.traderDrawdown
# traderData = drawdownDB.find({"traderId": traderId})
# traderData = (list(traderData)[0])
# # print(traderData)
# if len(traderData) == 0 :
# hasDrawdownData = False
# # print('Cannot find trader data in DB.')
# return False
# else:
# positionCount = 0
# positions = []
# suggestCapital = 0.00
# startTimestamp = int(time.mktime(time.strptime(f"2023-02-20 00:00:00", "%Y-%m-%d %H:%M:%S"))) + 3600*8
# endTimestamp = int(time.mktime(time.strptime(f"2023-03-10 23:59:59", "%Y-%m-%d %H:%M:%S"))) + 3600*8
# hasDrawdownData = True
# history = traderData["history"]
# for each in history:
# openTimestamp = int(time.mktime(time.strptime(each["openDate"], "%Y-%m-%d %H:%M:%S")))
# closeTimestamp = int(time.mktime(time.strptime(each["closeDate"], "%Y-%m-%d %H:%M:%S")))
# leverage = each["leverage"]
# drawdown = each["drawdown"]
# if startTimestamp <= openTimestamp and closeTimestamp <= endTimestamp:
# # print(f'{openTimestamp}, {closeTimestamp}')
# positions.append({
# "openTimestamp": openTimestamp,
# "closeTimestamp": closeTimestamp,
# "drawdown": drawdown,
# "overlapPos": []
# })
# overlap = np.zeros((len(positions), len(positions)), dtype="int_")
# for i in range(len(positions)):
# for j in range(len(positions)):
# if positions[i]["openTimestamp"] < positions[j]["closeTimestamp"] :
# overlap[i][j] = 1
# else :
# overlap[i][j] = 0
# for i in range(len(positions)):
# for j in range(len(positions)):
# if overlap[i][j] != overlap[j][i]:
# overlap[i][j] = 0
# overlap[j][i] = 0
# elif overlap[i][j] == 1 and overlap[j][i] == 1:
# positions[i]["overlapPos"].append(j)
# pass
# print(positions[64])
# # print(overlap)
# print(positions[256])
# MDD = 0
# for eachPos in positions :
# for eachOverlap in eachPos["overlapPos"]:
# MDD += positions[eachOverlap]["drawdown"]
# eachPos["overlapMDD"] = MDD
# MDD = 0
# return 0
if __name__ == "__main__":
response = copySimulate("4876423973")
# print(response["pnlInDays"])
# print(response["copyProfit"])
x = []
y = []
y.append(response["pnlInDays"][0])
for i in range(len(response["pnlInDays"])):
x.append(i + 1)
if i == 0:
pass
else:
y.append(response["pnlInDays"][i] + y[i - 1])
# print(analyzeTraderMDD("4876423973", 10000, 10, "2023-02"))