Пример #1
0
    def test__007(self):
        ob_js = {
            "uid": "71e689f5d4884dddb17f3f3b660b40f1",
            "created__long": 1503543603464,
            "modified__long": 1503543603468,
            "exchange": "gdax",
            "product": "eth-usd",
            "asks": [
                {
                    "price__num": 316.19,
                    "size__num": 25.86,
                    "num_orders__int": 1.0
                },
                {
                    "price__num": 316.2,
                    "size__num": 8.8439152,
                    "num_orders__int": 1.0
                },
                {
                    "price__num": 316.25,
                    "size__num": 7.45,
                    "num_orders__int": 2.0
                }
            ]
        }
        ob = OrderBookModel.parse(ob_js)
        shares = 40.0

        usd = helpers.compute_usd_spent(shares, ob)
        direct_calculation = 316.19 * 25.86 + 316.2 * 8.8439152 + 316.25 * (shares - 25.86 - 8.8439152)
        assert usd == direct_calculation
Пример #2
0
    def test__004(self):
        timestamp = epoch.to_long('2017-08-23 20:00:00 PDT')
        shares = 25.86
        trading_acc1 = BacktestingTradingAccount(None, 'gdax')
        ob = trading_acc1.get_order_book(ticker='eth-usd', timestamp=timestamp)

        usd = helpers.compute_usd_spent(shares, ob)
        direct_calculation = 316.19 * 25.86
        assert usd == direct_calculation
Пример #3
0
    def _buy_eth(self, shares, ob):
        amount = helpers.compute_usd_spent(shares, ob)

        # update account
        account = self.get_account()
        account.js['usd__num'] = account.js['usd__num'] - amount
        account.js['eth__num'] = account.js['eth__num'] + shares
        account.db_save(es)

        return amount
Пример #4
0
    def _buy_ticker(self, ticker, shares, timestamp=-1):
        if timestamp == -1:
            raise NotImplementedError()  # safety

        ob = self.get_order_book(ticker=ticker, timestamp=timestamp)
        amount = helpers.compute_usd_spent(shares, ob)

        # update account
        account = self.get_account()
        account.js['usd__num'] = account.js['usd__num'] - amount
        account.js['eth__num'] = account.js['eth__num'] + shares
        account.db_save(es)

        return amount
Пример #5
0
def trade_result(amount, holding_period, timestamp,
                 gdax_trading_account, cex_trading_account):
    """
    - trade an amount
    - max out on the number of eth
    - sell after the holding_period
    - how much did it made

    holding_period in Minutes
    """
    # print gdax_ob
    # print cex_ob
    t0 = timestamp
    t1 = timestamp + holding_period * 60 * 1000
    gdax_ob_t0 = gdax_trading_account.get_order_book(ticker='eth', timestamp=t0)
    cex_ob_t1 = cex_trading_account.get_order_book(ticker='eth', timestamp=t1)

    shares = helpers.compute_buy(amount, gdax_ob_t0)

    money_used = helpers.compute_usd_spent(shares, gdax_ob_t0)
    money_gotten = helpers.compute_usd_made(shares, cex_ob_t1)
    diff = money_gotten - money_used

    return diff / amount
Пример #6
0
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