Example #1
0
def test_transfer():

    exchange = mock.Mock()
    price = Decimal(9750.19).quantize(Decimal(10)**-2)
    exchange.quote_price = lambda pair: price
    exchange.name = "coinbase"

    order = mock.Mock()
    order.path_id = "fake_id"

    exchange_pair = ExchangePair(exchange, USD / BTC)

    source = Wallet(exchange, 18903.89 * USD)
    source.lock(917.07 * USD, order, "test")

    target = Wallet(exchange, 3.79283997 * BTC)

    quantity = (256.19 * USD).lock_for("fake_id")
    commission = (2.99 * USD).lock_for("fake_id")

    Wallet.transfer(source, target, quantity, commission, exchange_pair,
                    "transfer")

    source = Wallet(exchange, 3.79283997 * BTC)
    source.lock(3.00000029 * BTC, order, "test")

    target = Wallet(exchange, 18903.89 * USD)

    quantity = (2.19935873 * BTC).lock_for("fake_id")
    commission = (0.00659732 * BTC).lock_for("fake_id")

    Wallet.transfer(source, target, quantity, commission, exchange_pair,
                    "transfer")
Example #2
0
def execute_sell_order(order: 'Order', base_wallet: 'Wallet',
                       quote_wallet: 'Wallet', current_price: float,
                       options: 'ExchangeOptions', clock: 'Clock') -> 'Trade':
    if order.type == TradeType.LIMIT and order.price > current_price:
        return None

    filled = order.remaining.contain(order.exchange_pair)

    commission = options.commission * filled
    quantity = filled - commission

    if commission.size < Decimal(10)**-quantity.instrument.precision:
        order.cancel("COMMISSION IS LESS THAN PRECISION.")
        return None

    # Transfer Funds from Quote Wallet to Base Wallet
    transfer = Wallet.transfer(source=quote_wallet,
                               target=base_wallet,
                               quantity=quantity,
                               commission=commission,
                               exchange_pair=order.exchange_pair,
                               reason="SELL")

    trade = Trade(order_id=order.id,
                  step=clock.step,
                  exchange_pair=order.exchange_pair,
                  side=TradeSide.SELL,
                  trade_type=order.type,
                  quantity=transfer.quantity,
                  price=transfer.price,
                  commission=transfer.commission)

    return trade
Example #3
0
def execute_buy_order(order: 'Order', base_wallet: 'Wallet',
                      quote_wallet: 'Wallet', current_price: float,
                      options: 'ExchangeOptions', clock: 'Clock') -> 'Trade':
    if order.type == TradeType.LIMIT and order.price < current_price:
        return None

    filled = order.remaining.contain(order.exchange_pair)

    if order.type == TradeType.MARKET:
        scale = order.price / max(current_price, order.price)
        filled = scale * filled

    commission = options.commission * filled
    quantity = filled - commission

    if commission.size < Decimal(10)**-quantity.instrument.precision:
        order.cancel("COMMISSION IS LESS THAN PRECISION.")
        return None

    transfer = Wallet.transfer(source=base_wallet,
                               target=quote_wallet,
                               quantity=quantity,
                               commission=commission,
                               exchange_pair=order.exchange_pair,
                               reason="BUY")

    trade = Trade(order_id=order.id,
                  step=clock.step,
                  exchange_pair=order.exchange_pair,
                  side=TradeSide.BUY,
                  trade_type=order.type,
                  quantity=transfer.quantity,
                  price=transfer.price,
                  commission=transfer.commission)

    return trade