forked from Connie-Wild/ChannelBreakoutBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptimization.py
199 lines (181 loc) · 8.56 KB
/
optimization.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
#_*_ coding: utf-8 _*_
#https://sshuhei.com
import os.path
import json
import logging
import time
import itertools
import pandas as pd
from src import channel
from concurrent.futures import ProcessPoolExecutor
def eq(a, b):
return (a == b) | (pd.isnull(a) & pd.isnull(b))
def describe(params):
i, j, k, l, m, cost, fileName, core, useBlackList = params
channelBreakOut = channel.ChannelBreakOut()
channelBreakOut.entryTerm = i[0]
channelBreakOut.closeTerm = i[1]
channelBreakOut.rangeTh = j[0]
channelBreakOut.rangeTerm = j[1]
channelBreakOut.waitTerm = k[0]
channelBreakOut.waitTh = k[1]
channelBreakOut.rangePercent = l[0]
channelBreakOut.rangePercentTerm = l[1]
channelBreakOut.candleTerm = str(m) + "T"
channelBreakOut.cost = cost
channelBreakOut.fileName = fileName
if core == 1:
logging.info('================================')
logging.info('entryTerm:%s closeTerm:%s rangePercent:%s rangePercentTerm:%s rangeTerm:%s rangeTh:%s waitTerm:%s waitTh:%s candleTerm:%s',i[0],i[1],l[0],l[1],j[1],j[0],k[0],k[1],m)
else:
pass
# ブラックリスト判定
is_blacklist = False
if useBlackList:
bl = read_blacklist()
co = bl.columns.values
is_blacklist = ((bl[co[0]] == m) &
(eq(bl[co[1]], i[0])) &
(eq(bl[co[2]], i[1])) &
(eq(bl[co[3]], j[0])) &
(eq(bl[co[4]], j[1])) &
(eq(bl[co[5]], k[1])) &
(eq(bl[co[6]], k[0])) &
(eq(bl[co[7]], l[0])) &
(eq(bl[co[8]], l[1]))).any()
if is_blacklist:
pl = 0
profitFactor = 0
else:
#テスト
pl, profitFactor, maxLoss, winPer, ev = channelBreakOut.describeResult()
return [pl, profitFactor, i, l, j, k, m, is_blacklist]
def optimization(cost, fileName, core, useBlackList):
#optimizeList.jsonの読み込み
f = open('config/optimizeList.json', 'r', encoding="utf-8")
config = json.load(f)
entryAndCloseTerm = config["entryAndCloseTerm"]
rangeThAndrangeTerm = config["rangeThAndrangeTerm"]
waitTermAndwaitTh = config["waitTermAndwaitTh"]
rangePercentList = config["rangePercentList"]
linePattern = config["linePattern"]
termUpper = config["termUpper"]
candleTerm = config["candleTerm"]
if "COMB" in linePattern:
entryAndCloseTerm = list(itertools.product(range(2,termUpper), range(2,termUpper)))
total = len(entryAndCloseTerm) * len(rangeThAndrangeTerm) * len(waitTermAndwaitTh) * len(rangePercentList) * len(candleTerm)
paramList = []
params = []
for i, j, k, l, m in itertools.product(entryAndCloseTerm, rangeThAndrangeTerm, waitTermAndwaitTh, rangePercentList, candleTerm):
params.append([i, j, k, l, m, cost, fileName, core, useBlackList])
black_list = read_blacklist()
if core == 1:
# 同期処理
for param in params:
result = describe(param)
paramList.append(result)
logging.info('[%s/%s]',len(paramList),total)
else:
# 非同期処理
with ProcessPoolExecutor(max_workers=core) as executor:
for result in executor.map(describe, params):
skiped = '(skip)' if result[7] == True else ''
paramList.append(result)
logging.info('[%s/%s] result%s:%s',len(paramList),total,skiped,paramList[-1])
# ブラックリスト追加
if (useBlackList == True) & (result[0] < 0) & (result[7] == False):
new_bl = pd.DataFrame(
[[result[6], result[2][0], result[2][1], result[4][0], result[4][1], result[5][1], result[5][0], result[3][0], result[3][1]]], columns=black_list.columns.values)
black_list = black_list.append(new_bl)
# ブラックリスト書き込み
if useBlackList: black_list.to_csv('blacklist.csv', index=False, sep=',')
pF = [i[1] for i in paramList]
pL = [i[0] for i in paramList]
logging.info("======Optimization finished======")
logging.info('Search pattern :%s', len(paramList))
logging.info("Parameters:")
logging.info("[entryTerm, closeTerm], [rangePercent, rangePercentTerm], [rangeTh, rangeTerm], [waitTerm, waitTh], [candleTerm]")
logging.info("ProfitFactor max:")
logging.info(paramList[pF.index(max(pF))])
logging.info("PL max:")
logging.info(paramList[pL.index(max(pL))])
message = "Optimization finished.\n ProfitFactor max:{}\n PL max:{}".format(paramList[pF.index(max(pF))], paramList[pL.index(max(pL))])
channel.ChannelBreakOut().lineNotify(message)
#config.json設定用ログ
print("*********PF max*********")
print("PL", paramList[pF.index(max(pF))][0])
print("PF", paramList[pF.index(max(pF))][1])
print(" \"entryTerm\" : ", paramList[pF.index(max(pF))][2][0], ",", sep="")
print(" \"closeTerm\" : ", paramList[pF.index(max(pF))][2][1], ",", sep="")
if paramList[pF.index(max(pF))][3][0] is None:
print(" \"rangePercent\" : ", "null,", sep="")
else:
print(" \"rangePercent\" : ", paramList[pF.index(max(pF))][3][0], ",", sep="")
if paramList[pF.index(max(pF))][3][1] is None:
print(" \"rangePercentTerm\" : ", "null,", sep="")
else:
print(" \"rangePercentTerm\" : ", paramList[pF.index(max(pF))][3][1], ",", sep="")
if paramList[pF.index(max(pF))][4][1] is None:
print(" \"rangeTerm\" : ", "null,", sep="")
else:
print(" \"rangeTerm\" : ", paramList[pF.index(max(pF))][4][1], ",", sep="")
if paramList[pF.index(max(pF))][4][0] is None:
print(" \"rangeTh\" : ", "null,", sep="")
else:
print(" \"rangeTh\" : ", paramList[pF.index(max(pF))][4][0], ",", sep="")
print(" \"waitTerm\" : ", paramList[pF.index(max(pF))][5][0], ",", sep="")
print(" \"waitTh\" : ", paramList[pF.index(max(pF))][5][1], ",", sep="")
print(" \"candleTerm\" : \"", paramList[pF.index(max(pF))][6], "T\",", sep="")
print("*********PL max*********")
print("PL", paramList[pL.index(max(pL))][0])
print("PF", paramList[pL.index(max(pL))][1])
print(" \"entryTerm\" : ", paramList[pL.index(max(pL))][2][0], ",", sep="")
print(" \"closeTerm\" : ", paramList[pL.index(max(pL))][2][1], ",", sep="")
if paramList[pL.index(max(pL))][3][0] is None:
print(" \"rangePercent\" : ", "null,", sep="")
else:
print(" \"rangePercent\" : ", paramList[pL.index(max(pL))][3][0], ",", sep="")
if paramList[pL.index(max(pL))][3][1] is None:
print(" \"rangePercentTerm\" : ", "null,", sep="")
else:
print(" \"rangePercentTerm\" : ", paramList[pL.index(max(pL))][3][1], ",", sep="")
if paramList[pL.index(max(pL))][4][1] is None:
print(" \"rangeTerm\" : ", "null,", sep="")
else:
print(" \"rangeTerm\" : ", paramList[pL.index(max(pL))][4][1], ",", sep="")
if paramList[pL.index(max(pL))][4][0] is None:
print(" \"rangeTh\" : ", "null,", sep="")
else:
print(" \"rangeTh\" : ", paramList[pL.index(max(pL))][4][0], ",", sep="")
print(" \"waitTerm\" : ", paramList[pL.index(max(pL))][5][0], ",", sep="")
print(" \"waitTh\" : ", paramList[pL.index(max(pL))][5][1], ",", sep="")
print(" \"candleTerm\" : \"", paramList[pF.index(max(pF))][6], "T\",", sep="")
def read_blacklist():
if os.path.exists('blacklist.csv'):
return pd.read_csv('blacklist.csv', header=0, sep=',')
else:
return pd.read_csv('blacklist_default.csv', header=0, sep=',')
if __name__ == '__main__':
#logging設定
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s %(levelname)s: %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
logfile=logging.handlers.TimedRotatingFileHandler(
filename = 'log/optimization.log',
when = 'midnight'
)
logfile.setLevel(logging.INFO)
logfile.setFormatter(logging.Formatter(
fmt='%(asctime)s %(levelname)s: %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'))
logging.getLogger('').addHandler(logfile)
logging.info('Wait...')
#config.jsonの読み込み
f = open('config/config.json', 'r', encoding="utf-8")
config = json.load(f)
logging.info('cost:%s core:%s fileName:%s',config["cost"],config["core"],config["fileName"])
#最適化
start = time.time()
optimization(cost=config["cost"], fileName=config["fileName"], core=config["core"], useBlackList=config["useBlackList"])
logging.info('total processing time: %s', time.time() - start)