Exemplo n.º 1
0
    def test__0011(self):
        """
        compute_buy
        """
        ob_js = {
            "uid": "71e689f5d4884dddb17f3f3b660b40f1",
            "created__long": 1503543603464,
            "modified__long": 1503543603468,
            "exchange": "gdax",
            "product": "eth-usd",
            "asks": [
                {
                    "price__num": 316.19,
                    "size__num": 3.0,
                    "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)
        amount = 3000

        shares = helpers.compute_buy(amount, ob)
        direct_calculation = 3.0 + (amount - 316.19*3)/316.2
        assert shares == direct_calculation
Exemplo n.º 2
0
    def exec_gdax_buy(self, timestamp=-1):
        ticker = self.ticker
        gdax_amount = self.action_amount / self.capital_buffer_multiplier
        gdax_trading_account = self.trading_acc1
        gdax_ob = gdax_trading_account.get_order_book(ticker=ticker,
                                                      timestamp=timestamp)
        shares = helpers.compute_buy(gdax_amount, gdax_ob)
        buy_price_upperbound = helpers.compute_buy_upperbound(shares, gdax_ob)

        # trade
        gdax_trading_account \
            .place_limit_order('buy', ticker, buy_price_upperbound, shares, timestamp=timestamp)

        # audit info
        audit_info = {
            'ticker': ticker,
            'action_type': 'exec_gdax_buy',
            'shares': shares,
            'targeted_amount': gdax_amount,
            'buy_price_upperbound': buy_price_upperbound,
        }
        return audit_info
Exemplo n.º 3
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
Exemplo n.º 4
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