Bitmex issues in Live Trade


#1

Hello there, I was wondering if it is possible to use Bitmex in live trading. Since it is supported by CCXT, I gave it a try but found two issues that are blocking me right now.

The problem is that Bitmex uses BTC as the base currency for the BTC/USD pair. You can use BTC to go either long or short, so basically, you don’t own USD when selling BTC.

This is the test algo:

from catalyst.api import order, record, symbol, order_target_percent

def initialize(context):
    context.asset = symbol('btc_usd')
    context.i = 0
    context.set_slippage(spread=0.01)

def handle_data(context, data):
    context.i += 1

    print("========")
    print(context.i)
    print("========")

    if context.i == 1:
        order(context.asset, 10)
        #order_target_percent(context.asset, 1)

    elif context.i == 2:
        order(context.asset, -10)
        #order_target_percent(context.asset, -1)

    record(AAPL=data.current(context.asset, 'price'))

If I use btc as the capital base, this happens:

catalyst live -f test_exchange.py -x bitmex -c btc --capital-base 0.01 -n bitmex --no-simulate-orders

Traceback (most recent call last):
  File "/usr/local/bin/catalyst", line 11, in <module>
    load_entry_point('enigma-catalyst', 'console_scripts', 'catalyst')()
  File "/usr/local/lib/python3.5/site-packages/click/core.py", line 722, in __call__
    return self.main(*args, **kwargs)
  File "/usr/local/lib/python3.5/site-packages/click/core.py", line 697, in main
    rv = self.invoke(ctx)
  File "/usr/local/lib/python3.5/site-packages/click/core.py", line 1066, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/usr/local/lib/python3.5/site-packages/click/core.py", line 895, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/local/lib/python3.5/site-packages/click/core.py", line 535, in invoke
    return callback(*args, **kwargs)
  File "/catalyst-src/catalyst/__main__.py", line 104, in _
    return f(*args, **kwargs)
  File "/usr/local/lib/python3.5/site-packages/click/decorators.py", line 17, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "/catalyst-src/catalyst/__main__.py", line 497, in live
    stats_output=None,
  File "/catalyst-src/catalyst/utils/run_algo.py", line 342, in _run
    overwrite_sim_params=False,
  File "/catalyst-src/catalyst/exchange/exchange_algorithm.py", line 330, in run
    data, overwrite_sim_params
  File "/catalyst-src/catalyst/algorithm.py", line 724, in run
    for perf in self.get_generator():
  File "/catalyst-src/catalyst/gens/tradesimulation.py", line 224, in transform
    for capital_change_packet in every_bar(dt):
  File "/catalyst-src/catalyst/gens/tradesimulation.py", line 137, in every_bar
    handle_data(algo, current_data, dt_to_use)
  File "/catalyst-src/catalyst/utils/events.py", line 216, in handle_data
    dt,
  File "/catalyst-src/catalyst/utils/events.py", line 235, in handle_data
    self.callback(context, data)
  File "/catalyst-src/catalyst/exchange/exchange_algorithm.py", line 865, in handle_data
    self._handle_data(self, data)
  File "test_exchange.py", line 16, in handle_data
    order(context.asset, 10)
  File "/catalyst-src/catalyst/utils/api_support.py", line 57, in wrapped
    return getattr(algo_instance, f.__name__)(*args, **kwargs)
  File "/catalyst-src/catalyst/utils/api_support.py", line 126, in wrapped_method
    return method(self, *args, **kwargs)
  File "/catalyst-src/catalyst/algorithm.py", line 1462, in order
    return self.blotter.order(asset, amount, style)
  File "/catalyst-src/catalyst/exchange/exchange_blotter.py", line 169, in order
    if amount == 0:
  File "/catalyst-src/catalyst/exchange/exchange_blotter.py", line 185, in order
    args=(asset, amount, style),
  File "/usr/local/lib/python3.5/site-packages/redo/__init__.py", line 162, in retry
    return action(*args, **kwargs)
  File "/catalyst-src/catalyst/exchange/exchange_blotter.py", line 163, in exchange_order
    asset, amount, style
  File "/catalyst-src/catalyst/exchange/exchange.py", line 825, in order
    algo_currency=self.base_currency
catalyst.exchange.exchange_errors.MismatchingBaseCurrencies: Unable to trade with base currency usd when the algorithm uses btc.

If I use usd as the capital base, this happens:

catalyst live -f test_exchange.py -x bitmex -c usd --capital-base 10 -n bitmex --no-simulate-orders

Running in live trading mode.
[2018-04-11 03:28:28.175392] WARNING: run_algo: Catalyst is currently in ALPHA. It is going through rapid development and it is subject to errors. Please use carefully. We encourage you to report any issue on GitHub: https://github.com/enigmampc/catalyst/issues
[2018-04-11 03:28:28.175672] INFO: run_algo: Catalyst version 0.5.8+1.g4c84ea8.dirty
[2018-04-11 03:28:31.178769] INFO: run_algo: running algo in live-trading mode
Traceback (most recent call last):
  File "/usr/local/bin/catalyst", line 11, in <module>
    load_entry_point('enigma-catalyst', 'console_scripts', 'catalyst')()
  File "/usr/local/lib/python3.5/site-packages/click/core.py", line 722, in __call__
    return self.main(*args, **kwargs)
  File "/usr/local/lib/python3.5/site-packages/click/core.py", line 697, in main
    rv = self.invoke(ctx)
  File "/usr/local/lib/python3.5/site-packages/click/core.py", line 1066, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/usr/local/lib/python3.5/site-packages/click/core.py", line 895, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/local/lib/python3.5/site-packages/click/core.py", line 535, in invoke
    return callback(*args, **kwargs)
  File "/catalyst-src/catalyst/__main__.py", line 104, in _
    return f(*args, **kwargs)
  File "/usr/local/lib/python3.5/site-packages/click/decorators.py", line 17, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "/catalyst-src/catalyst/__main__.py", line 497, in live
    stats_output=None,
  File "/catalyst-src/catalyst/utils/run_algo.py", line 342, in _run
    overwrite_sim_params=False,
  File "/catalyst-src/catalyst/exchange/exchange_algorithm.py", line 330, in run
    data, overwrite_sim_params
  File "/catalyst-src/catalyst/algorithm.py", line 724, in run
    for perf in self.get_generator():
  File "/catalyst-src/catalyst/gens/tradesimulation.py", line 224, in transform
    for capital_change_packet in every_bar(dt):
  File "/catalyst-src/catalyst/gens/tradesimulation.py", line 137, in every_bar
    handle_data(algo, current_data, dt_to_use)
  File "/catalyst-src/catalyst/utils/events.py", line 216, in handle_data
    dt,
  File "/catalyst-src/catalyst/utils/events.py", line 235, in handle_data
    self.callback(context, data)
  File "/catalyst-src/catalyst/exchange/exchange_algorithm.py", line 855, in handle_data
    cleanup=lambda: log.warn('Ordering again.')
  File "/usr/local/lib/python3.5/site-packages/redo/__init__.py", line 162, in retry
    return action(*args, **kwargs)
  File "/catalyst-src/catalyst/exchange/exchange_algorithm.py", line 674, in synchronize_portfolio
    cash=required_cash,
  File "/catalyst-src/catalyst/exchange/exchange.py", line 719, in sync_positions
    cash=cash,
catalyst.exchange.exchange_errors.NotEnoughCashError: Total usd amount on bitmex is lower than the cash reserved for this algo: 0.0 < 10.0. While trades can be made on the exchange accounts outside of the algo, exchange must have enough free usd to cover the algo cash.

Thank you!


#2

Thanks @koffar.lul for reporting this!

I was able to reconstruct the issue you have experienced.
By using usd as a base currency and setting capital base to zero I was able to place one order until something else went wrong (issue in fetching orders).
We will further investigate this. Could you please open a Github issue, as it more track-able there?

Thanks!


#3

@koffar.lul,

Unfortunately, after more investigations, we’ve come to the conclusion that catalyst support at Bitmex is not yet complete and more work is needed in that area. You can try using Poloniex, Bittrex, Bitfinex, Binanace, Gdax in the meantime.
Supporting Bitmex is added to our task list, we will be happy to hear its degree of importance for you.

Thanks again for raising this and sorry for the inconvenience,

Lena


#4

Hi, I am also experiencing this issue and would really like to use Bitmex for all my trading given the leverage and fee structure.