-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfirst_strategy.py
318 lines (259 loc) · 10.9 KB
/
first_strategy.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
import datetime
import time
import os.path
import sys
import backtrader as bt
import backtrader.feeds as btfeed
#import ccxt
import csv
import io
import pandas as pd
from collections import OrderedDict
from multiprocessing import Pool, cpu_count
import math
import os
# DECLARE MODE FOR PROGRAM - OPTOMISATION OR STRATEGY
opt_mode = True
DEBUG = False
period=0
rsi_low=0
last_rsi_high=0
# LOG OUTPUT TO FILE
class Logger(object):
def __init__(self, filename="Default.log"):
self.terminal = sys.stdout
self.log = open(filename, "a")
def write(self, message):
self.terminal.write(message)
self.log.write(message)
def flush(self):
pass
# CSV INPUT FILE FORMAT CONFIGURATION
class dataFeed(btfeed.GenericCSVData):
params = (
('dtformat', '%Y-%m-%d %H:%M:%S'),
('datetime', 0),
('open', 1),
('high', 2),
('low', 3),
('close', 4),
('volume', 5),
('openinterest', -1)
)
class OrderObserver(bt.observer.Observer):
lines = ('created', 'expired',)
plotinfo = dict(plot=True, subplot=True, plotlinelabels=True)
plotlines = dict(
created=dict(marker='*', markersize=8.0, color='lime', fillstyle='full'),
expired=dict(marker='s', markersize=8.0, color='red', fillstyle='full')
)
def next(self):
for order in self._owner._orderspending:
if order.data is not self.data:
continue
if not order.isbuy():
continue
# Only interested in "buy" orders, because the sell orders
# in the strategy are Market orders and will be immediately
# executed
if order.status in [bt.Order.Accepted, bt.Order.Submitted]:
self.lines.created[0] = order.created.price
elif order.status in [bt.Order.Expired]:
self.lines.expired[0] = order.created.price
# MAIN STRATEGY DEFINITION - DEFINE VALUES HERE FOR NON-OPTOMISATION MODE
class firstStrategy(bt.Strategy):
global i
i = 1
params = (
("period", 12),
("rsi_low", 49),
("rsi_high", 42),
)
#TRADE LOGGING FUNCTION
def log(self, txt, dt=None):
''' Logging function fot this strategy'''
if not opt_mode:
dt = dt or self.datas[0].datetime.datetime(0)
print('%s, %s' % (dt.isoformat(), txt))
def __init__(self):
self.startcash = self.broker.getvalue()
self.rsi = bt.indicators.RSI_SMA(self.data.close, period=self.params.period, safediv=True)
#TRADE LOGGING FUNCTION
def notify_trade(self, trade):
if not trade.isclosed and not opt_mode:
return
self.log('TRADE INFO, PRICE %.2f, GROSS %.2f, NET %.2f' %
(trade.price, trade.pnl, trade.pnlcomm))
def next(self):
global i
i = i + 1
if i % 10000 == 0:
sys.stdout.write('.')
sys.stdout.flush()
if not self.position:
if self.rsi < self.params.rsi_low:
self.buy(size=10)
else:
if self.rsi > self.params.rsi_high:
self.sell(size=10)
if opt_mode and DEBUG:
print('period: {}, rsi low: {}, rsi high {}'.format(self.params.period,self.params.rsi_low,self.params.rsi_high))
if opt_mode == False:
def printTradeAnalysis(analyzer):
'''
Function to print the Technical Analysis results in a nice format.
'''
#Get the results we are interested in
total_open = analyzer.total.open
total_closed = analyzer.total.closed
total_won = analyzer.won.total
total_lost = analyzer.lost.total
win_streak = analyzer.streak.won.longest
lose_streak = analyzer.streak.lost.longest
pnl_net = round(analyzer.pnl.net.total,2)
strike_rate = round((total_won / total_closed) * 100,2)
#Designate the rows
h1 = ['Total Open', 'Total Closed', 'Total Won', 'Total Lost']
h2 = ['Strike Rate','Win Streak', 'Losing Streak', 'PnL Net']
r1 = [total_open, total_closed,total_won,total_lost]
r2 = [strike_rate, win_streak, lose_streak, pnl_net]
#Check which set of headers is the longest.
if len(h1) > len(h2):
header_length = len(h1)
else:
header_length = len(h2)
#Print the rows
print_list = [h1,r1,h2,r2]
row_format ="{:<15}" * (header_length + 1)
print("Trade Analysis Results:")
for row in print_list:
print(row_format.format('',*row))
def printSQN(analyzer):
sqn = round(analyzer.sqn,2)
print('SQN: {}'.format(sqn))
# INPUT CONDITIONS TO FEED INTO CEREBRO IS ADDED HERE
if __name__ == '__main__':
sys.stdout = Logger("firststrategy.log")
periods = pd.DataFrame(columns=['FROM','TO'],index=[1,2,3,4,5,6,7,8,9,10,11,12])
periods.loc[1] = ('2017-01-01','2017-02-01')
periods.loc[2] = ('2017-02-01','2017-03-01')
periods.loc[3] = ('2017-03-01','2017-04-01')
periods.loc[4] = ('2017-04-01','2017-05-01')
periods.loc[5] = ('2017-05-01','2017-06-01')
periods.loc[6] = ('2017-06-01','2017-07-01')
periods.loc[7] = ('2017-07-01','2017-08-01')
periods.loc[8] = ('2017-08-01','2017-09-01')
periods.loc[9] = ('2017-09-01','2017-10-01')
periods.loc[10] = ('2017-10-01','2017-11-01')
periods.loc[11] = ('2017-11-01','2017-12-01')
periods.loc[12] = ('2017-12-01','2017-12-31')
for index, row in periods.iterrows():
# Variable for our starting cash
startcash = 10000
# Create an instance of cerebro
cerebro = bt.Cerebro(optreturn=False)
# Timing the whole operation
time_at_start = time.time()
if opt_mode:
# ADD STRATEGY OPTIMISATION
cerebro.optstrategy(firstStrategy, period=range(11, 20), rsi_low=range(10, 50), rsi_high=range(51, 85))
#cerebro.optstrategy(firstStrategy, period=range(12, 13), rsi_low=range(41, 42), rsi_high=range(47, 48))
else:
#ADD STRATEGY
cerebro.addstrategy(firstStrategy)
# DATA FEED FROM EXCHANGE
symbol = str('ETH/USDT')
timeframe = str('15m')
exchange = str('poloniex')
exchange_out = str(exchange)
start_date = str('2017-1-1 00:00:00')
get_data = False
#So, let's say, you are fetching 2 days of 5m timeframe:
#(1440 minutes in one day * 7 days) / 15 minutes = 576 candles
num_of_candles = 672
def to_unix_time(timestamp):
epoch = datetime.datetime.utcfromtimestamp(0) # start of epoch time
my_time = datetime.datetime.strptime(timestamp, "%Y-%m-%d %H:%M:%S") # plugin your time object
delta = my_time - epoch
return delta.total_seconds() * 1000
# CSV File Name
symbol_out = symbol.replace("/", "")
filename = '{}-{}-{}.csv'.format(exchange_out, symbol_out, timeframe)
out_filename = '{}-{}-{}-out.csv'.format(exchange_out, symbol_out, timeframe)
# Get data if needed
if get_data:
# Get our Exchange
exchange = getattr(ccxt, exchange)()
exchange.load_markets()
hist_start_date = int(to_unix_time(start_date))
#data = exchange.fetch_ohlcv(symbol, timeframe, since=hist_start_date, limit=num_of_candles)
data = exchange.fetch_ohlcv(symbol, timeframe, since=hist_start_date)
header = ['Timestamp', 'Open', 'High', 'Low', 'Close', 'Volume']
df = pd.DataFrame(data, columns=header)
df['Timestamp'] = pd.to_datetime(df['Timestamp'], unit='ms')
#Precision
df = df.round(3)
# Save it
df.to_csv(filename, index= False)
#format dates for datafeed object
fy,fm,fd = periods['FROM'][index].split('-')
ty,tm,td = periods['TO'][index].split('-')
#READ DATA FROM CSV FILE
modpath = os.path.dirname(os.path.abspath(sys.argv[0]))
datapath = os.path.join(modpath,str(filename))
data = dataFeed(dataname=datapath, timeframe=bt.TimeFrame.Minutes, compression=15,
fromdate=datetime.datetime(int(fy),int(fm),int(fd)),
todate=datetime.datetime(int(ty),int(tm),int(td)),)
# Add the data to Cerebro
cerebro.adddata(data)
# Set our desired cash start
cerebro.broker.setcash(startcash)
if not opt_mode:
# Add the analyzers we are interested in
cerebro.addanalyzer(bt.analyzers.TradeAnalyzer, _name="ta")
cerebro.addanalyzer(bt.analyzers.SQN, _name="sqn")
#WRITER TEST
#cerebro.addwriter(bt.WriterFile, csv=True, rounding=2)
# RUN STRATEGY THROUGH CEREBRO USING INPUT DATA
# Timing the operation
time_at_end = time.time()
time_elapsed = round(time_at_end - time_at_start,2)
print('Time elapsed: {} seconds'.format(time_elapsed))
print ('Running Cerebro')
if not opt_mode: opt_runs = cerebro.run(tradehistory=False)
else: opt_runs = cerebro.run(tradehistory=False, runonce = False, Exactbars=True)
firstStrat = opt_runs[0]
if opt_mode:
# CREATE A LIST VARIABLE THAT CONTAINS RESULTS
final_results_list = []
for run in opt_runs:
for strategy in run:
value = round(strategy.broker.get_value(), 2)
PnL = round(value - startcash, 2)
period = strategy.params.period
rsi_low = strategy.params.rsi_low
rsi_high = strategy.params.rsi_high
final_results_list.append([period, rsi_low, rsi_high, PnL])
# Sort Results List
by_period = sorted(final_results_list, key=lambda x: x[0])
by_PnL = sorted(final_results_list, key=lambda x: x[3], reverse=True)
# PRINT RESULTS IN OPTIMISATION AND FILTER TOP 3
result_number = 0
print('Results: Ordered by Profit:')
for result in by_PnL:
if result_number < 3:
print('Asset: {} Start: {}, End: {}, Period: {}, rsi_low: {}, rsi_high: {}, PnL: {}'.format(filename, periods['FROM'][index], periods['TO'][index], result[0], result[1], result[2], result[3]))
result_number = result_number + 1
# Timing the operation
time_at_end = time.time()
time_elapsed = round(time_at_end - time_at_start,2)
print('Time elapsed: {} seconds'.format(time_elapsed))
if opt_mode == False:
# print the analyzers
printTradeAnalysis(firstStrat.analyzers.ta.get_analysis())
printSQN(firstStrat.analyzers.sqn.get_analysis())
#Get final portfolio Value
portvalue = cerebro.broker.getvalue()
#Print out the final result
print('Final Portfolio Value: ${}'.format(portvalue))
cerebro.plot(style='candlestick')