-
Notifications
You must be signed in to change notification settings - Fork 0
/
pairs_usdchf_eurusd.py
234 lines (175 loc) · 7.16 KB
/
pairs_usdchf_eurusd.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
from datetime import date
import matplotlib.pyplot as plt
import numpy as np
from quotesFromCsv import loadData
from tester import Tester
from instrument import Instrument
from strategy import Strategy
from order import Order
import calendar
from forexSessions import *
from trade import *
from ma import ema
import quotesFromDS
from sklearn import preprocessing
from scipy.stats.stats import pearsonr
startTest = date(2010, 1, 1)
stopTest = date(2010, 5, 1)
usdchf = loadData('/home/mage/PycharmProjects/cbTester/data/usdchf_1m_200516.csv_trimmed.csv')
eurusd = loadData('/home/mage/PycharmProjects/cbTester/data/eurusd_1m_120516.csv_trimmed.csv')
class PairMeanRev(Strategy):
#params
def __init__(self, engine, params):
self.engine = engine
self.lotSizeInUSD = 1000000
self.commissionPerPip = self.lotSizeInUSD / 1000000 * 25
self.result = []
self.pOptimization = params['pOptimization']
if self.pOptimization == True:
self.pOpt = params['pOpt']
self.queue = []
self.queueClose = []
self.cc = []
self.lastBarInst1 = []
self.lastBarInst2 = []
def onBar(self, bar):
if bar[0].date() < startTest or bar[0].date() > stopTest:
return
if bar[11] == self.engine.data[0].name:
self.lastBarInst1 = bar
if bar[11] == self.engine.data[1].name:
self.lastBarInst2 = bar
if self.lastBarInst1 == [] or self.lastBarInst2 == []:
return
if self.lastBarInst1[0] != self.lastBarInst2[0]:
return
if bar[0].minute != 59:
return
e = self.engine.getHistoryBars(self.engine.data[0].name, 60*24, 0)
c = self.engine.getHistoryBars(self.engine.data[1].name, 60*24, 0)
if e == []:
return
if c == []:
return
eOOS = self.engine.data[0].data[self.engine.data[0].currentBar : self.engine.data[0].currentBar + 24*60, 4]
cOOS = self.engine.data[1].data[self.engine.data[0].currentBar : self.engine.data[0].currentBar + 24*60, 4]
esrc = e[:,4]
csrc = c[:,4]
e = e[:,4]
c = c[:,4]
e = e-np.mean(e)
c = c-np.mean(c)
stde = np.std(e)
stdc = np.std(c)
if stde == 0 or stdc == 0:
return
e = e / np.std(e)
c = c / np.std(c)
c = c * -1
#self.cc.append([pearsonr(e,c)[0], bar[0].hour])
if pearsonr(e,c)[0] > 0.95:
print str(pearsonr(e,c)[0]) + " " + str(pearsonr(eOOS,cOOS)[0])
if pearsonr(e,c)[0] > 10.95:
print pearsonr(e,c)[0]
print pearsonr(eOOS,cOOS)[0]
plt.figure(1)
plt.subplot(511)
plt.plot(e, label='eurusd')
plt.plot(c, label='usdchf')
plt.legend()
plt.subplot(512)
plt.plot(e-c)
plt.subplot(513)
plt.plot(esrc-np.mean(esrc)+csrc-np.mean(csrc))
plt.subplot(514)
plt.plot(eOOS-np.mean(eOOS)+cOOS-np.mean(cOOS))
plt.subplot(515)
plt.plot(eOOS-eOOS[0])
plt.plot(cOOS-cOOS[0])
plt.show()
return
#if get15minBarNum(bar[0]) not in range(22, 34):
# return
for o in self.queue:
if bar[11] == o[0]:
self.engine.sendOrder(Order(o[0], o[1], 0, 0, 0, o[2], 0, 120, market=True), bar)
self.queue.remove(o)
for p in reversed(self.queueClose):
if bar[11] == p.order.instrument:
if p in self.engine.getPositions():
self.engine.closePosition(p, bar, market=True)
self.queueClose.remove(p)
e = self.engine.getHistoryBars(self.engine.data[0].name, 2 * 60, 0)
c = self.engine.getHistoryBars(self.engine.data[1].name, 2 * 60, 0)
if e == []:
return
if c == []:
return
pcHighE = np.max(e[:, 6])
pcLowE = np.min(e[:, 3])
pcHighC = np.max(c[:, 6])
pcLowC = np.min(c[:, 3])
if pcHighE == pcLowE or pcHighC == pcLowC:
return
pe = (e[len(e) - 1, 4] - pcLowE)/(pcHighE - pcLowE)
pc = (c[len(c) - 1, 4] - pcLowC)/(pcHighC - pcLowC)
r = pe-pc
positions = self.engine.getPositions()
if len(positions) > 0:
profit = 0
for p in reversed(positions):
profit += (self.engine.getHistoryBars(p.order.instrument, 1, 0)[0, 4] - p.order.price) * p.order.orderType * self.lotSizeInUSD - self.commissionPerPip
if profit > 5000:
for p in reversed(positions):
if p not in self.queueClose:
self.queueClose.append(p)
if len(positions) == 0:
if pe > 0.85 and pc > 0.85:
if bar[11] == "eurusd":
self.engine.sendOrder(Order("eurusd", 1, 0, 0, 0, 1, 0, 120, market=True), bar)
self.queue.append(["usdchf", 1, 1.4])
if bar[11] == "usdchf":
self.engine.sendOrder(Order("usdchf", 1, 0, 0, 0, 1.4, 0, 120, market=True), bar)
self.queue.append(["eurusd", 1, 1])
self.result.append(r)
def onStop(self):
import matplotlib.pyplot as plt
"""intraDayCc = []
res = np.array(self.cc)
for i in range(24):
currentHour = res[res[:,1]==i]
currentHour = currentHour[:,0]
intraDayCc.append(np.average(currentHour))
plt.plot(intraDayCc)
plt.show()"""
res = np.array(self.cc)
plt.plot(res[:,0])
plt.show()
#autocorelation of kk
#kk mean
#kk stdev
if self.pOptimization is False:
self.engine.printTrades(self.lotSizeInUSD, self.commissionPerPip)
self.engine.generateTextReport(self.lotSizeInUSD, self.commissionPerPip)
self.engine.showEquity(self.lotSizeInUSD, self.commissionPerPip)
self.engine.getProfitsByTimeOfDay(self.lotSizeInUSD, self.commissionPerPip,)
self.engine.getFilterAnalyze(self.lotSizeInUSD, self.commissionPerPip)
self.engine.getMonthlyReturns(self.lotSizeInUSD, self.commissionPerPip)
self.engine.getProfitsByDayOfWeek(self.lotSizeInUSD, self.commissionPerPip)
self.engine.getProfitsByTimeOfDay(self.lotSizeInUSD, self.commissionPerPip)
self.engine.getPointAnalyze(self.lotSizeInUSD, self.commissionPerPip, 1)
import matplotlib.pyplot as plt
plt.plot(self.result)
plt.show()
else:
self.engine.generateTextReport(self.lotSizeInUSD, self.commissionPerPip)
def onGetStatOnPositionOpen(self, position, bar):
return
def onGetStatOnPositionClose(self, position, bar):
return
optimization = False
if optimization == True:
for opt in range(30,190,30):
tester = Tester([Instrument('eurusd', eurusd), Instrument('usdchf', usdchf)], PairMeanRev, {'pOptimization': True, 'pOpt':opt}, True)
else:
tester = Tester([Instrument('eurusd', eurusd), Instrument('usdchf', usdchf)], PairMeanRev, {'pOptimization': False}, True)