def mk_audit_js(): gdax_account = self.trading_acc1.get_account() cex_account = self.trading_acc2.get_account() transaction_t = epoch.current_milli_time( ) if timestamp == -1 else timestamp audit_js = OrderedDict() audit_js['strategy_run_id'] = self.run_id audit_js['timestamp'] = epoch.to_str(transaction_t) audit_js['timestamp__long'] = transaction_t audit_js['ticker'] = self.ticker audit_js['strategy_info'] = self._strategy_info audit_js['signal'] = OrderedDict() audit_js['signal']['signal__gdax_has_usd'] = signal__gdax_has_usd audit_js['signal']['signal__gdax_has_eth'] = signal__gdax_has_eth audit_js['signal']['signal__cex_has_eth'] = signal__cex_has_eth audit_js['signal'][ 'signal__arbitrage_delta'] = signal__arbitrage_delta audit_js['total_usd__num'] = gdax_account.js[ 'usd__num'] + cex_account.js['usd__num'] audit_js['total_eth__num'] = gdax_account.js[ 'eth__num'] + cex_account.js['eth__num'] audit_js['gdax_account'] = gdax_account.js audit_js['cex_account'] = cex_account.js return audit_js
def get_order_book(self, product_id=None, ticker=None, timestamp=-1): """ Search from timstamp, and work backward until one is found. :return: OrderbookModel """ if timestamp == -1: timestamp = epoch.current_milli_time() search_window = 11000 # 11 seconds search_window = 15 * 60000 # 15 minutes search_window = 1000 * 60000 # 1000 minutes (t0, t1) = timestamp - search_window, timestamp + 500 query = { "size": 1, "sort": [ { "timestamp__long": { "order": "desc" } } ], "query": { "bool": { "must": [ { "range": { "timestamp__long": { "gte": t0, "lte": t1 } } }, { "match": { "exchange.raw": self.exh } }, { "match": { "product.raw": 'eth-usd' } } ] } } } params = default_es_get_params() result = es.search("order_book", "data", query, params=params) # validation if result['hits']['total'] == 0: logger.info(pretty_json(query)) logger.info(timestamp) logger.info(epoch.to_str(timestamp)) raise RuntimeError('Cannot find orderbook in backtesting') ob_js = result['hits']['hits'][0]['_source'] ob = OrderBookModel.build(ob_js) return ob
def start(self, fail_fast=True): logger.info('Starting strategy: {0}'.format( str(self.strategy.strategy_name))) logger.info("Strategy withdraw threshold: " + str(self.strategy.THRESHOLD_WITHDRAW_DELTA)) logger.info("Strategy deposit threshold: " + str(self.strategy.THRESHOLD_DEPOSIT_DELTA)) # How long? How often? length = (self.execution_window[1] - self.execution_window[0] ) # In millisecond interval = self.execution_interval # In millisecond # length = 86400000 # one day # interval = 600 # 5 minutes counter_limit = length / interval timestamp = self.execution_window[0] logger.info('====================================') logger.info('Starting time : {0}'.format(epoch.to_str(timestamp))) logger.info('Interval in seconds: {0}'.format(str(interval / 1000))) logger.info('====================================') counter = 0 while counter <= counter_limit: counter += 1 timestamp += interval snap_t = self.strategy.trading_acc1.get_snapping_timestamp( timestamp) self.snap(snap_t, fail_fast=fail_fast) # main execution path # logging info line execution_timestamp = '-1' if timestamp == -1 else epoch.to_str( timestamp) info_line = '{0} Runner snapping, current time: {1} | execution timestamp: {2}' \ .format(str(counter), epoch.current_time(), str(execution_timestamp)) logger.info(info_line) self.strategy.trading_acc1.sleep(interval / 1000) # Sleeping in seconds logger.info('Finished strategy runner : {0}'.format( epoch.current_time()))
def collect_order_book(): gdax_order_book = gdax_client.get_order_book(ticker='eth', level=2) gob_model = OrderBookModel.build(gdax_order_book) gob_model.db_save(es) cex_order_book = cex_client.get_order_book(ticker='eth', level=2) cob_model = OrderBookModel.build(cex_order_book) cob_model.db_save(es) timestamp = gob_model.js['timestamp__long'] logger.info("saved data: {0}, {1}".format(str(timestamp), epoch.to_str(timestamp)))
def get_ds_trading_result(check_window, check_interval, amount, holding_period, threshold_delta, gdax_trading_account, cex_trading_account): """ data series in dates that: 1. show trading results """ window = tuple(epoch.to_long(x) for x in check_window) interval = MILLIS_IN_MINUTE * check_interval x = [] y1 = [] y2 = [] # Use the strategy to calculate deltas strategy001 = Strat1(None, None, [gdax_trading_account, cex_trading_account]) n = (window[1] - window[0]) / interval timestamp = window[0] for i in range(n): withdraw_signal = strategy001.get_signal__withdraw_delta(timestamp) withdraw_delta = withdraw_signal['withdraw_delta'] if withdraw_delta > threshold_delta: result = trade_result(amount, holding_period, timestamp, gdax_trading_account, cex_trading_account) x.append(timestamp) y1.append(withdraw_delta) y2.append(result) # print i, timestamp, epoch.to_str(timestamp), withdraw_delta, result # next timestamp timestamp += interval # checking progress if i % 50 == 0: logger.info("[{}] timestamp:{}|date:{}".format(str(i), str(timestamp), epoch.to_str(timestamp))) x_index = pd.to_datetime(x, unit='ms') ds1 = pd.Series(index=x_index, data=y1) ds2 = pd.Series(index=x_index, data=y2) logger.info(check_window) logger.info(window) logger.info(ds1.head()) logger.info(ds1.head()) return ds1, ds2
def get_two_deltas(check_window, check_interval): window = tuple(epoch.to_long(x) for x in check_window) interval = MILLIS_IN_MINUTE * check_interval x = [] y1 = [] y2 = [] # Use the strategy to calculate deltas gdax_t_account = BacktestingTradingAccount('backtesting_gdx_001', 'gdax') cex_t_account = BacktestingTradingAccount('backtesting_cex_001', 'cex') strategy001 = Strat1(None, None, [gdax_t_account, cex_t_account]) n = (window[1] - window[0]) / interval timestamp = window[0] for i in range(n): withdraw_signal = strategy001.get_signal__withdraw_delta(timestamp) deposit_signal = strategy001.get_signal__deposit_delta(timestamp) withdraw_delta = withdraw_signal['withdraw_delta'] deposit_delta = deposit_signal['deposit_delta'] x.append(timestamp) y1.append(withdraw_delta) y2.append(deposit_delta) # print i, timestamp, epoch.to_str(timestamp) # next timestamp timestamp += interval # checking progress if i % 50 == 0: logger.info("timestamp:{0}|date:{1}".format(str(timestamp), epoch.to_str(timestamp))) x_index = pd.to_datetime(x, unit='ms') ds1 = pd.Series(index=x_index, data=y1) ds2 = pd.Series(index=x_index, data=y2) logger.info(check_window) logger.info(window) logger.info(ds1.head()) logger.info(ds1.head()) return ds1, ds2
def get_x_and_y(amount, starting_time, interval, increments, search_window): """ :param amount: :param starting_time: :param interval: pytz.timedelta, example: pytz.HOUR :param increments: :param search_window: :return: """ dt = interval.total_seconds() * 1000.0 x = [] y = [] for i in range(increments): t = starting_time + dt * i ts = epoch.to_str(t) diff = quick.delta_buy_gdax_sell_cex(starting_amount, t, search_window) x.append(t) y.append(diff) return x, y
import time import pytz import datetime from arb.utils.epoch import current_milli_time, to_str, to_long if __name__ == '__main__': t = current_milli_time() s = to_str(t) t2 = to_long(s) print t print s print t2 time1 = '2017-08-28 09:00:00 PDT' time2 = '2017-08-29 09:00:00 PDT' print to_long(time1) print to_long(time2) pass
def get_results_for_a_mock_strategy(check_window, check_interval, amount, holding_period, threshold_delta, gdax_trading_account, cex_trading_account): """ keep track of how we we are doing with one trade at a time """ window = tuple(epoch.to_long(x) for x in check_window) interval = MILLIS_IN_MINUTE * check_interval x = [] y1 = [] y2 = [] # Use the strategy to calculate deltas strategy001 = Strat1(None, None, [gdax_trading_account, cex_trading_account]) n = (window[1] - window[0]) / interval ONE_DAY_IN_MINUTES =1440 cash = amount eth = 0.0 waiting_liquidate_ticks = 0 waiting_capital_ticks = ONE_DAY_IN_MINUTES # def signal__has_eth(): # return eth > 0.0 timestamp = window[0] for i in range(n): waiting_liquidate_ticks += check_interval waiting_capital_ticks += check_interval withdraw_signal = strategy001.get_signal__withdraw_delta(timestamp) withdraw_delta = withdraw_signal['withdraw_delta'] # handling gdax if withdraw_delta >= threshold_delta and eth == 0.0 and waiting_capital_ticks >= ONE_DAY_IN_MINUTES: gdax_ob = gdax_trading_account.get_order_book(ticker='eth', timestamp=timestamp) shares = helpers.compute_buy(amount, gdax_ob) usd_used = helpers.compute_usd_spent(shares, gdax_ob) # accounting cash = cash - usd_used eth = eth + shares * (1 - 0.003) # Including fees waiting_liquidate_ticks = 0 waiting_capital_ticks = 0 # x.append(timestamp) # y1.append(cash) # y2.append(eth) if eth > 0.0 and waiting_liquidate_ticks >= holding_period: cex_ob = cex_trading_account.get_order_book(ticker='eth', timestamp=timestamp) shares = eth usd_gotten = helpers.compute_usd_made(shares, cex_ob) * (1 - 0.002) # Including fees # accounting cash = cash + usd_gotten eth = 0 x.append(timestamp) y1.append(cash) y2.append(eth) print i, epoch.to_str(timestamp) print "cash: {} | eth: {}".format(str(cash), str(eth)) print "capital tick: {} | liquidate tick: {}".format(waiting_capital_ticks, waiting_liquidate_ticks) # next timestamp timestamp += interval # checking progress # print i, epoch.to_str(timestamp) # print "cash: {} | eth: {}".format(str(cash), str(eth)) # print "capital tick: {} | liquidate tick: {}".format(waiting_capital_ticks, waiting_liquidate_ticks) if i % 50 == 0: print i, epoch.to_str(timestamp) print "cash: {} | eth: {}".format(str(cash), str(eth)) print "capital tick: {} | liquidate tick: {}".format(waiting_capital_ticks, waiting_liquidate_ticks) x_index = pd.to_datetime(x, unit='ms') ds1 = pd.Series(index=x_index, data=y1) ds2 = pd.Series(index=x_index, data=y2) logger.info(check_window) logger.info(window) logger.info(ds1.head()) return ds1, ds2
stacktrace = traceback.format_exc() logger.error(stacktrace) return ans if __name__ == '__main__': # earliest time "2017-08-20 16:27:09 PDT" 1503271629989 search_window = 11000 # 11 seconds starting_amount = 1000 starting_time = 1503271629989 for i in range(48): t = i * MILLIS_IN_HOUR + starting_time ts = epoch.to_str(t) diff = delta_buy_gdax_sell_cex(starting_amount, t, search_window) print 'time is {0}: {1}'.format(ts, str(diff))