-
Notifications
You must be signed in to change notification settings - Fork 57
/
machineLearning.py
149 lines (135 loc) · 6.57 KB
/
machineLearning.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
#_*_ coding: utf-8 _*_
#https://sshuhei.com
import json
import logging
import logging.handlers
import time
import itertools
from src import channel
from hyperopt import fmin, tpe, hp
def describe(params):
i, j, k, l, m, cost, mlMode, fileName = 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
logging.info("===========Test pattern===========")
logging.info('candleTerm:%s',channelBreakOut.candleTerm)
logging.info('entryTerm:%s closeTerm:%s',channelBreakOut.entryTerm,channelBreakOut.closeTerm)
logging.info('rangePercent:%s rangePercentTerm:%s',channelBreakOut.rangePercent,channelBreakOut.rangePercentTerm)
logging.info('rangeTerm:%s rangeTh:%s',channelBreakOut.rangeTerm,channelBreakOut.rangeTh)
logging.info('waitTerm:%s waitTh:%s',channelBreakOut.waitTerm,channelBreakOut.waitTh)
logging.info("===========Backtest===========")
pl, profitFactor, maxLoss, winPer, ev = channelBreakOut.describeResult()
if "PFDD" in mlMode:
result = profitFactor/maxLoss
elif "PL" in mlMode:
result = -pl
elif "PF" in mlMode:
result = -profitFactor
elif "DD" in mlMode:
result = -maxLoss
elif "WIN" in mlMode:
result = -winPer
elif "EV" in mlMode:
result = -ev
logging.info("===========Assessment===========")
logging.info('Result:%s',result)
return result
def optimization(cost, fileName, hyperopt, mlMode, showTradeDetail):
#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)
logging.info('Total pattern:%s Searches:%s',total,hyperopt)
logging.info("======Optimization start======")
#hyperoptによる最適値の算出
space = [hp.choice('i',entryAndCloseTerm), hp.choice('j',rangeThAndrangeTerm), hp.choice('k',waitTermAndwaitTh), hp.choice('l',rangePercentList), hp.choice('m',candleTerm), cost, mlMode, fileName]
result = fmin(describe,space,algo=tpe.suggest,max_evals=hyperopt)
logging.info("======Optimization finished======")
channelBreakOut = channel.ChannelBreakOut()
channelBreakOut.entryTerm = entryAndCloseTerm[result['i']][0]
channelBreakOut.closeTerm = entryAndCloseTerm[result['i']][1]
channelBreakOut.rangeTh = rangeThAndrangeTerm[result['j']][0]
channelBreakOut.rangeTerm = rangeThAndrangeTerm[result['j']][1]
channelBreakOut.waitTerm = waitTermAndwaitTh[result['k']][0]
channelBreakOut.waitTh = waitTermAndwaitTh[result['k']][1]
channelBreakOut.rangePercent = rangePercentList[result['l']][0]
channelBreakOut.rangePercentTerm = rangePercentList[result['l']][1]
channelBreakOut.candleTerm = str(candleTerm[result['m']]) + "T"
channelBreakOut.cost = cost
channelBreakOut.fileName = fileName
channelBreakOut.showTradeDetail = showTradeDetail
logging.info("======Best pattern======")
logging.info('candleTerm:%s mlMode:%s',channelBreakOut.candleTerm,mlMode)
logging.info('entryTerm:%s closeTerm:%s',channelBreakOut.entryTerm,channelBreakOut.closeTerm)
logging.info('rangePercent:%s rangePercentTerm:%s',channelBreakOut.rangePercent,channelBreakOut.rangePercentTerm)
logging.info('rangeTerm:%s rangeTh:%s',channelBreakOut.rangeTerm,channelBreakOut.rangeTh)
logging.info('waitTerm:%s waitTh:%s',channelBreakOut.waitTerm,channelBreakOut.waitTh)
logging.info("======Backtest======")
channelBreakOut.describeResult()
#config.json設定用ログ
print("======config======")
print(" \"entryTerm\" : ", channelBreakOut.entryTerm, ",", sep="")
print(" \"closeTerm\" : ", channelBreakOut.closeTerm, ",", sep="")
if channelBreakOut.rangePercent is None:
print(" \"rangePercent\" : ", "null,", sep="")
else:
print(" \"rangePercent\" : ", channelBreakOut.rangePercent, ",", sep="")
if channelBreakOut.rangePercentTerm is None:
print(" \"rangePercentTerm\" : ", "null,", sep="")
else:
print(" \"rangePercentTerm\" : ", channelBreakOut.rangePercentTerm, ",", sep="")
if channelBreakOut.rangeTerm is None:
print(" \"rangeTerm\" : ", "null,", sep="")
else:
print(" \"rangeTerm\" : ", channelBreakOut.rangeTerm, ",", sep="")
if channelBreakOut.rangeTh is None:
print(" \"rangeTh\" : ", "null,", sep="")
else:
print(" \"rangeTh\" : ", channelBreakOut.rangeTh, ",", sep="")
print(" \"waitTerm\" : ", channelBreakOut.waitTerm, ",", sep="")
print(" \"waitTh\" : ", channelBreakOut.waitTh, ",", sep="")
print(" \"candleTerm\" : \"", channelBreakOut.candleTerm, "\",", sep="")
print("==================")
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 mlMode:%s fileName:%s',config["cost"],config["mlMode"],config["fileName"])
#最適化
start = time.time()
optimization(cost=config["cost"], fileName=config["fileName"], hyperopt=config["hyperopt"], mlMode=config["mlMode"], showTradeDetail=config["showTradeDetail"])
logging.info('total processing time: %s', time.time() - start)