-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpaper.py
More file actions
294 lines (261 loc) · 11.2 KB
/
paper.py
File metadata and controls
294 lines (261 loc) · 11.2 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
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
#!/usr/bin/python3
"""
Bittensor Decider.
by: AlphaGriffin
"""
__author__ = "Eric Petersen @Ruckusist"
__copyright__ = "Copyright 2018, The Alpha Griffin Project"
__credits__ = ["Eric Petersen", "Shawn Wilson", "@alphagriffin"]
__license__ = "***"
__version__ = "0.0.1"
__maintainer__ = "Eric Petersen"
__email__ = "ruckusist@alphagriffin.com"
__status__ = "Beta"
#////////////////// | Imports | \\\\\\\\\\\\\\\#
# generic
import os, sys, time, datetime, collections
from decimal import Decimal as D
import numpy as np
import threading
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '5'
from timeit import default_timer as timer
runtime = timer()
from tqdm import tqdm, trange
# crypto
import ccxt
import asyncio
import ccxt.async as cryptosync
# Tensorflow
from ag.bittensor.ai.AI import Q_Trader as bot
# Dataset
from ag.bittensor.ai.make_data import MakeData
import pandas as pd
# GameEngine // need access to engine for reward data
import ag.bittensor.game.stats as stats
# Utilities
import ag.bittensor.utils.options as options
import ag.bittensor.utils.plotter as plotter
# import ag.bittensor.utils.sheets as sheets
import ag.bittensor.utils.slack as slack
import ag.bittensor.utils.talib as talib
import ag.bittensor.utils.printer as printer
# from ag.bittensor.strategy.macd import *
# import ag.logging as log
# Game Stuff
import ag.bittensor.game.stats as stats
# log.set(log.WARN)
class Signals(object):
@staticmethod
def MACD_signals(Smith, slow, fast, period='15m'):
Smith.candles = period
candles = Smith.candles
candles['slow'] = np.round(candles["Close"].rolling(window=slow, center=False).mean(), 8)
candles['fast'] = np.round(candles["Close"].rolling(window=fast, center=False).mean(), 8)
candles['macd'] = candles['fast'] - candles['slow']
## WHY USE CANDLE PRICE HERE?? SEEMS LIKE MADNESS...
# because this - that = positive is not in full dollar units... so the threshhold needs to
# be multipiled by the incoming coin price for the right amount of decimal places to be ahead or
# behind by. the value has to change 1/100 of the close price or it wont signal.
# now that i think about it this threshold could be in the options... i guess.
candles['macd_regime'] = np.where(candles['macd'] > candles['Close']*.01, 1, 0)
candles['macd_regime'] = np.where(candles['macd'] < -candles['Close']*.01, -1, candles['macd_regime'])
candles['macd_signal'] = candles['macd_regime'] - candles['macd_regime'].shift(1)
# candles.sort_index(inplace=True)
# candles['macd_signal'].plot().axhline(y = 0, color = "black", lw = 2)
return candles
@staticmethod
def momentum_signals(Smith, mom, period='15m'):
TA = talib.TALib()
Smith.candles = period
candles = Smith.candles
x = TA.MOM(candles, mom, 'Close')
x['Momentum_regime'] = np.where(x['Momentum_Close_{}'.format(mom)] > x['Close']*0.01, 1, 0)
x['Momentum_regime'] = np.where(x['Momentum_Close_{}'.format(mom)] < -x['Close']*0.01, -1, x['Momentum_regime'])
x['Momentum_signal'] = x['Momentum_regime'] - x['Momentum_regime'].shift(1)
return x
class Engine(object):
def __init__(self, options):
# set globals
self.options = options
self.P = printer.Printer(options)
self.slacker = slack.Slacker(options)
self.game = stats.Stats(options)
self.signals = Signals()
self.datasmith = MakeData(options)
self.reset_game_options()
self.reset_feedback()
self.P('Starting FauxTrader')
# self.slacker.Print('Decider Bot is coming online Now.')
def reset_game_options(self):
# moving averages
self.slow_period = 42
self.fast_period = 21
# momentum ... int works... does a timeref???
self.mom_period = 12
# time frame for candles... int works so does timeref... ie. 5T... T for mins
self.time_frame = '1H'
# Sorting the volume feels right.... High, Low, Banded, None
self.volume_sort = None
# if volume sort then use band high/low
self.vol_band_high = 333
self.vol_band_low = 13
def reset_feedback(self):
self.top_pairs = pd.DataFrame(columns=['Pair', 'Profits'])
self.winners = []
self.losers = []
self.all_total_trades = 0
self.winner_returns = []
self.losers_returns = []
self.candles = None
self.theReturn = 0
self.all_losing_returns = 0
self.all_winners_returns = 0
self.all_fees_paid = 0
self.start_cost = 0
self.start_time = None
self.end_time = None
def main(self, signal=['mom']):
start = timer()
# process all coins.
for i in trange(self.datasmith.total_coins):
filename = self.datasmith.next_filename
if filename is None: break
## SORT 1: ONLY BITCOIN PAIRS
if not 'BTC' in filename[:-4][-3:]:
continue
self.datasmith.dataframe = filename
## SORT 2: ONLY HIGH VOLUME PAIRS
self.datasmith.candles = self.time_frame
if self.volume_sort:
if 'High' in self.volume_sort:
if not self.datasmith.candles['Volume'].iloc[-1] >= 100:
continue
elif 'Low' in self.volume_sort:
## SORT 2a: ONLY LOW VOLUME PAIRS
if not self.datasmith.candles['Volume'].iloc[-1] <= 100:
continue
elif 'banded' in self.volume_sort:
## SORT 2a: ONLY LOW VOLUME PAIRS
if self.datasmith.candles['Volume'].iloc[-1] <= self.vol_band_low:
continue
if self.datasmith.candles['Volume'].iloc[-1] >= self.vol_band_high:
continue
""" Print the progress
tqdm.write('Processing Coin: {}, Volume: {:.2f}, signal: {}'.format(
filename[:-4], self.datasmith.candles['Volume'].iloc[-1],
'MACD_{}_{}_{}'.format(self.time_frame, self.slow_period, self.fast_period)
))
"""
# this signal should be PASSED IN
if signal:
for i in signal:
if 'macd' in i:
self.candles = self.signals.MACD_signals(
self.datasmith,
slow=self.slow_period,
fast=self.fast_period,
period=self.time_frame
)
if 'mom' in i:
self.candles = self.signals.momentum_signals(
self.datasmith,
mom=self.mom_period,
period = self.time_frame
)
# THIS PAIR OF THINGS RESETS AFTER EACH COIN... this is good.
self.game.reset_paperTrader()
profits = self.game.process_trades(self.candles)
self.all_fees_paid += self.game.all_fees_paid
# THIS IS NOT PYTHONIC
if profits > 0:
self.winners.append([filename, profits, len(self.game.paperTrader)])
self.top_pairs = self.top_pairs.append({
"Pair": filename[:-4],
"Profits": profits
},
ignore_index=True)
else:
self.losers.append([filename, profits, len(self.game.paperTrader)])
# break
# SET TIME PERIOD CHECKED
self.start_time = self.datasmith.dataframe.start_date
self.end_time = self.datasmith.dataframe.end_date
self.top_pairs.set_index('Profits')
self.top_pairs.sort_values('Profits', ascending=False)
# print some results
print('Took {:.2f} secs'.format(timer()-start))
self.get_results()
self.print_results('term')
# self.print_results('slack')
def get_results(self):
for i in sorted(self.winners):
if i[1] > 0: # scratch nan
self.winner_returns.append(i[1])
self.all_total_trades += i[2]
for i in sorted(self.losers):
if i[1] < 0: # scratch nan
self.losers_returns.append(i[1])
self.all_total_trades += i[2]
self.start_cost = 0.001 * (len(self.winners) + len(self.losers))
self.all_winners_returns = sum(self.winner_returns)
self.all_losing_returns = sum(self.losers_returns)
self.theReturn = self.start_cost + self.start_cost*(self.all_winners_returns + self.all_losing_returns)
def print_results(self, target='term'):
msgs = []
msgs.append( '_.|::_ *BitTensor BackTest Report* _::|._' )
msgs.append( '*Signal(s)*: MACD, s:{}, f:{}, t:{}'.format(self.slow_period, self.fast_period, self.time_frame) )
msgs.append( '*Time Period*:\n\ts: {}\n\te: {}'.format(self.start_time, self.end_time) )
msgs.append( '*Volume Sort*: {}'.format(self.volume_sort) )
if self.volume_sort:
if 'banded' in self.volume_sort:
msgs.append( '24H Volume *High* Limit: {}'.format(self.vol_band_high) )
msgs.append( '24H Volume *Low* Limit: {}'.format(self.vol_band_low) )
msgs.append( '*Number of Coins Winning/Losing*: {}/{}'.format(
len(self.winners), len(self.losers)
) )
if len(self.top_pairs) > 3:
msgs.append( 'Top Pairs:\n\t*{}* + {:.2f}%\n\t*{}* + {:.2f}%\n\t*{}* + {:.2f}%'.format(
self.top_pairs['Pair'].iloc[0], self.top_pairs['Profits'].iloc[0]*100,
self.top_pairs['Pair'].iloc[1], self.top_pairs['Profits'].iloc[1]*100,
self.top_pairs['Pair'].iloc[2], self.top_pairs['Profits'].iloc[2]*100,
))
msgs.append( 'Signal Produced _{:.2f}%_ trading all pairs in {} trades with {:.8f}b in total fees.'.format(
(self.all_winners_returns+self.all_losing_returns)*100,
self.all_total_trades,
self.game.all_fees_paid
) )
msgs.append('Total Cost/Return to trade all _{}_ pairs\n*Exchange {}*: _{:.4f}b_ / _{:.4f}b_'.format(
len(self.winners)+len(self.losers), 'Bittrex', self.start_cost, self.theReturn
))
else:
msgs.append('*This Strategy Sucks.*')
msgs.append('`END OF REPORT`')
if 'term' in target:
PRINTER = print
elif 'slack' in target:
PRINTER = self.slacker.Print
elif 'pretty' in target:
PRINTER = self.P
else:
# PRINTER = log.debug
pass
PRINTER('\n'.join([x for x in msgs]))
# NOT PYTHONIC
#for i in msgs:
# PRINTER(str(i))
# time.sleep(.25)
return True
def main():
"""Loads Options ahead of the app"""
config = options.Options('config/access_codes.yaml')
signal_ = 'mom'
app = Engine(config)
try:
app.main(signal_)
except KeyboardInterrupt:
pass
if __name__ == '__main__':
main()
# os.system('cls')
print('Thanks!')
print('BitTensor - AlphaGriffin | 2018')