-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTrader.py
More file actions
211 lines (193 loc) · 7.9 KB
/
Trader.py
File metadata and controls
211 lines (193 loc) · 7.9 KB
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
import pandas as pd
import numpy as np
import tpqoa
from datetime import datetime, timedelta, timezone
import time
class Trader(tpqoa.tpqoa):
def __init__(self, conf_file, instrument, bar_length, units, duration):
super().__init__(conf_file)
self.instrument = instrument
self.bar_length = pd.to_timedelta(bar_length)
self.tick_data = pd.DataFrame()
self.start = datetime.utcnow()
self.end = self.start + timedelta(minutes=duration)
self.raw_data = None
self.data = None
self.last_bar = None
self.units = units
self.position = 0
self.profits = []
self.duration = duration
self.start_trade_session()
# Begins trading session with error catching
def start_trade_session(self, days=5, max_attempts=None, sleep_period=15, sleep_increase=0):
attempt = 0
success = False
while True:
try:
self.get_most_recent(days)
self.stream_data(self.instrument)
except Exception as e:
print('Error:', e)
else:
success = True
break
finally:
attempt += 1
print('Attempt: {}'.format(attempt), end='\n')
if not success:
if max_attempts is not None and attempt >= max_attempts:
print('MAX ATTEMPTS REACHED')
try:
time.sleep(sleep_period)
self.terminate_session(
cause='TOO MANY ERRORS - SESSION TERMINATED.'
)
except Exception as e:
print('Error:', e)
print('COULD NOT TERMINATE SESSION.')
finally:
break
else: # TRY AGAIN
time.sleep(sleep_period)
sleep_period += sleep_increase
self.tick_data = pd.DataFrame()
# Get recent data, with specified time interval
def get_most_recent(self, days=5):
time.sleep(1)
print('-' * 50)
print('ATTEMPTING TO MERGE...')
print('REQUIRE UNDER {} SECONDS'.format(self.bar_length.seconds))
now = datetime.utcnow()
now = now - timedelta(microseconds=now.microsecond)
past = now - timedelta(days=days)
df = (self.get_history(
instrument = self.instrument,
start = past,
end = now,
granularity = 'S5',
price = 'M',
localize = True
)
.c.dropna()
.to_frame()
)
df.rename(columns = {'c':self.instrument}, inplace=True)
low = df.resample(self.bar_length, label='right').min().dropna()
high = df.resample(self.bar_length, label='right').max().dropna()
df = df.resample(self.bar_length, label='right').last().dropna().iloc[:-1]
self.raw_data = df.copy()
self.raw_data['High'] = high
self.raw_data['Low'] = low
# print(self.raw_data)
self.last_bar = self.raw_data.index[-1]
print('Seconds: {}'.format((datetime.utcnow() - self.last_bar).seconds))
if datetime.utcnow() - self.last_bar >= self.bar_length:
print('-----VERIFY THAT BOT IS RUNNING DURING TRADING HOURS-----')
self.get_most_recent()
else:
print('SUCCESSFULLY MERGED')
print('-' * 50)
def close_position(self):
if self.position == 1:
order = self.create_order(
self.instrument,
-self.units,
suppress = True,
ret = True
)
self.report_trade(order, 'GOING NEUTRAL')
elif self.position == -1:
order = self.create_order(
self.instrument,
self.units,
suppress = True,
ret = True
)
self.report_trade(order, 'GOING NEUTRAL')
print('\nSESSION OVER')
self.position = 0
# End the trading session
def end_trade_session(self):
self.stop_stream = True
if self.position != 0:
close_order = self.create_order(
self.instrument,
units = -self.position * self.units,
suppress = True,
ret = True
)
self.report_trade(close_order, "GOING NEUTRAL")
self.position = 0
print('\nENDING TRADING SESSION...')
def on_success(self, time, bid, ask):
recent_tick = pd.to_datetime(time).replace(tzinfo=None)
print(self.ticks, end='\r', flush=True)
if recent_tick >= self.end:
self.end_trade_session()
return
df = pd.DataFrame({self.instrument:(ask + bid) / 2}, index=[recent_tick])
self.tick_data = pd.concat([self.tick_data, df])
if self.ticks == 1 or recent_tick - self.last_bar > self.bar_length:
self.resample_and_join()
self.define_strategy()
self.execute_trades()
def resample_and_join(self):
temp = self.tick_data.resample(self.bar_length, label="right").last().ffill().iloc[:-1]
temp['High'] = self.tick_data.resample(self.bar_length, label='right').max().dropna()
temp['Low'] = self.tick_data.resample(self.bar_length, label='right').min().dropna()
self.raw_data = pd.concat([
self.raw_data,
temp
])
self.tick_data = self.tick_data.iloc[-1:]
# print(self.raw_data)
self.last_bar = self.raw_data.index[-1]
def define_strategy(self):
df = self.raw_data.copy()
df['position'] = 0
self.data = df.copy()
pass
def execute_trades(self):
if self.data['position'].iloc[-1] == 1:
if self.position == 0:
order = self.create_order(self.instrument, self.units, suppress=True, ret=True)
self.report_trade(order, 'GOING LONG')
elif self.position == -1:
order = self.create_order(self.instrument, self.units * 2, suppress=True, ret=True)
self.report_trade(order, 'GOING LONG')
else:
print('STAYING LONG')
self.position = 1
elif self.data['position'].iloc[-1] == -1:
if self.position == 0:
order = self.create_order(self.instrument, -self.units, suppress=True, ret=True)
self.report_trade(order, 'GOING SHORT')
elif self.position == 1:
order = self.create_order(self.instrument, -self.units * 2, suppress=True, ret=True)
self.report_trade(order, 'GOING SHORT')
else:
print('STAYING SHORT')
self.position = -1
elif self.data['position'].iloc[-1] == 0:
if self.position == -1:
order = self.create_order(self.instrument, self.units, suppress=True, ret=True)
self.report_trade(order, 'GOING NEUTRAL')
elif self.position == 1:
order = self.create_order(self.instrument, -self.units, suppress=True, ret=True)
self.report_trade(order, 'GOING NEUTRAL')
else:
print('STAYING NEUTRAL')
self.position = 0
# Print out trade statistics
def report_trade(self, order, going):
time = order['time']
units = order['units']
price = order['price']
pl = float(order['pl'])
self.profits.append(pl)
cumpl = sum(self.profits)
print('\n' + 50* '-')
print('{} | {}'.format(time, going))
print('{} | Units = {} | Price = {} | P&L = {} | Cumulative P&L = {}'.format(time, units, price, pl, cumpl))
print(50 * '-' + '\n')