def test__003(self): """ About capital signals """ timestamp = epoch.to_long('2017-08-21 06:00:00 PDT') acc1_js = ensure_test_data({ "uid": "backtesting_gdx_" + random_str(), "timestamp__long": timestamp, "exchange": "gdax", "country": "usa", "usd__num": 0.0, "eth__num": 0.0, "btc__num": 0.0 }) acc1 = AccountModel.build(acc1_js) acc1.db_save(es) acc2_js = ensure_test_data({ "uid": "backtesting_cex_" + random_str(), "timestamp__long": timestamp, "exchange": "cex", "country": "gbr", "usd__num": 1700.0, "eth__num": 0.0, "btc__num": 0.0 }) acc2 = AccountModel.build(acc2_js) acc2.db_save(es) trading_acc1 = BacktestingTradingAccount(acc1.uid, 'gdax') trading_acc2 = BacktestingTradingAccount(acc2.uid, 'cex') strat = Strat1(trading_acc1, trading_acc2) signal = strat.get_signal__available_to_deposit(timestamp) assert signal['signal'] == False
def test__003(self): """ go """ timestamp = epoch.to_long('2017-08-22 08:00:00 PDT') trading_acc1 = BacktestingTradingAccount(None, 'gdax') trading_acc2 = BacktestingTradingAccount(None, 'cex') strat = Strat1(trading_acc1, trading_acc2) signal = strat.get_signal__withdraw_delta(timestamp) assert signal['signal'] == True
def test__005(self): """ no go """ timestamp = epoch.to_long('2017-08-22 06:00:00 PDT') trading_acc1 = BacktestingTradingAccount(None, 'gdax') trading_acc2 = BacktestingTradingAccount(None, 'cex') strat = Strat1(trading_acc1, trading_acc2) signal = strat.get_signal__deposit_delta(timestamp) assert signal['signal'] == False
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
from arb.core.exh.backtest.accounts import BacktestingTradingAccount, MockTradingAccount from arb.core.exh.accounts import LiveTradingAccount from arb.strat.strat1 import Strat1 from arb.utils import epoch from arb import es, logger from arb.utils.string import pretty_json from arb.notebook import quick import pandas as pd # Use the strategy to calculate deltas _id = 'server_mock_live__004' gdax_t_account = MockTradingAccount(_id + '__gdax', 'gdax') cex_t_account = MockTradingAccount(_id + '__cex', 'cex') strategy001 = Strat1(None, None, gdax_t_account, cex_t_account) withdraw_signal = strategy001.get_signal__withdraw_delta() deposit_signal = strategy001.get_signal__deposit_delta() print '------------------------' print 'signals' print '------------------------' print pretty_json(withdraw_signal) print pretty_json(deposit_signal) # if __name__ == '__main__': # unittest.main() # # 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
account_id = 'gdax' exh = 'gdax' gdax_trading_account = LiveTradingAccount(account_id, exh) gdax_account = gdax_trading_account.sync_account_with_exh() # view cex account_id = 'cex' exh = 'cex' cex_trading_account = LiveTradingAccount(account_id, exh) cex_account = cex_trading_account.sync_account_with_exh() strategy_running_id = 'live_trade_01' strategy_desc = 'live trading with 500USD and 1.8ETH' strat1 = Strat1(strategy_running_id, strategy_desc, gdax_trading_account, cex_trading_account) strat1.THRESHOLD_WITHDRAW_DELTA = 0.02 strat1.THRESHOLD_DEPOSIT_DELTA = 0.01 strat1.CAPITAL_BUFFER_MULTIPLIER = 1.05 strat1.SNAP_REPETITION = 7 strat1.WITHDRAW_ACTION_AMOUNT = 100 strat1.DEPOSIT_ACTION_AMOUNT = 100 * 0.95 signal_available_withdraw = strat1.get_signal__available_to_withdraw() signal_available_deposit = strat1.get_signal__available_to_deposit() signal_withdraw_delta = strat1.get_signal__withdraw_delta() signal_deposit_delta = strat1.get_signal__deposit_delta() print epoch.current_milli_time(), epoch.current_milli_time() print 'total usd: ' + str(gdax_account.js['usd__num'] + cex_account.js['usd__num']) print 'total eth: ' + str(gdax_account.js['eth__num'] + cex_account.js['eth__num'])