scrtlabs / catalyst

An Algorithmic Trading Library for Crypto-Assets in Python
http://enigma.co
Apache License 2.0
2.49k stars 725 forks source link

Problems running Live Trade on Bitfinex #411

Closed avolution closed 6 years ago

avolution commented 6 years ago

Hey guys,

i have a problem running live trade on bitfinex I use catalyst version 0.5.17 installed via conda with the latest env file

Here the code:

from catalyst.api import record, symbol, order_target_value
from catalyst.utils.run_algo import run_algorithm

NAMESPACE = 'LIVE_TEST'

def initialize(context):
  set_max_leverage(1.0)
  return

def handle_data(context, data):
  print('handle_data {}'.format(data.current_dt))
  order_target_value(symbol('btc_usd'), 100.0)
  return

def analyze(context, results):
  return

run_algorithm(
  capital_base=1000.0,
  initialize=initialize,
  handle_data=handle_data,
  analyze=analyze,
  exchange_name='bitfinex',
  live=True,
  simulate_orders=False,
  algo_namespace=NAMESPACE,
  quote_currency='usd'
)

Here the exception when try to submit the order:

Traceback (most recent call last):
  File "open_pos.py", line 32, in <module>
    quote_currency='usd'
  File "catalyst3/lib/python3.6/site-packages/catalyst/utils/run_algo.py", line 596, in run_algorithm
    stats_output=stats_output
  File "catalyst3/lib/python3.6/site-packages/catalyst/utils/run_algo.py", line 358, in _run
    overwrite_sim_params=False,
  File "/catalyst3/lib/python3.6/site-packages/catalyst/exchange/exchange_algorithm.py", line 1051, in run
    data, overwrite_sim_params
  File "/catalyst3/lib/python3.6/site-packages/catalyst/exchange/exchange_algorithm.py", line 359, in run
    data, overwrite_sim_params
  File "/catalyst3/lib/python3.6/site-packages/catalyst/algorithm.py", line 725, in run
    for perf in self.get_generator():
  File "/catalyst3/lib/python3.6/site-packages/catalyst/gens/tradesimulation.py", line 224, in transform
    for capital_change_packet in every_bar(dt):
  File "/catalyst3/lib/python3.6/site-packages/catalyst/gens/tradesimulation.py", line 137, in every_bar
    handle_data(algo, current_data, dt_to_use)
  File "/catalyst3/lib/python3.6/site-packages/catalyst/utils/events.py", line 216, in handle_data
    dt,
  File "/catalyst3/lib/python3.6/site-packages/catalyst/utils/events.py", line 235, in handle_data
    self.callback(context, data)
  File "/catalyst3/lib/python3.6/site-packages/catalyst/exchange/exchange_algorithm.py", line 905, in handle_data
    self._handle_data(self, data)
  File "/open_pos.py", line 16, in handle_data
    order_target_value(symbol('btc_usd'), 100.0)
  File "/catalyst3/lib/python3.6/site-packages/catalyst/utils/api_support.py", line 57, in wrapped
    return getattr(algo_instance, f.__name__)(*args, **kwargs)
  File "/catalyst3/lib/python3.6/site-packages/catalyst/utils/api_support.py", line 126, in wrapped_method
    return method(self, *args, **kwargs)
  File "/catalyst3/lib/python3.6/site-packages/catalyst/algorithm.py", line 1972, in order_target_value
    style=style)
  File "/catalyst3/lib/python3.6/site-packages/catalyst/utils/api_support.py", line 126, in wrapped_method
    return method(self, *args, **kwargs)
  File "/catalyst3/lib/python3.6/site-packages/catalyst/algorithm.py", line 1469, in order
    return self.blotter.order(asset, amount, style)
  File "/catalyst3/lib/python3.6/site-packages/catalyst/exchange/exchange_blotter.py", line 170, in order
    if amount == 0:
  File "/catalyst3/lib/python3.6/site-packages/catalyst/exchange/exchange_blotter.py", line 186, in order
    args=(asset, amount, style),
  File "/catalyst3/lib/python3.6/site-packages/redo/__init__.py", line 162, in retry
    return action(*args, **kwargs)
  File "/catalyst3/lib/python3.6/site-packages/catalyst/exchange/exchange_blotter.py", line 164, in exchange_order
    asset, amount, style
  File "/catalyst3/lib/python3.6/site-packages/catalyst/exchange/exchange.py", line 855, in order
    return self.create_order(asset, amount, is_buy, style)
  File "/catalyst3/lib/python3.6/site-packages/catalyst/exchange/ccxt/ccxt_exchange.py", line 902, in create_order
    prec_amount = self.api.amount_to_precision(symbol, adj_amount)
  File "/catalyst3/lib/python3.6/site-packages/ccxt/bitfinex.py", line 346, in amount_to_precision
    return self.decimal_to_precision(amount, TRUNCATE, self.markets[symbol]['precision']['amount'], self.precisionMode)
AttributeError: 'bitfinex' object has no attribute 'decimal_to_precision'

It is working in papertrade mode -> simulate_orders=True My API are set correctly

avolution commented 6 years ago

Problem with the outdated ccxt? The current requirement in catalyst is ccxt==1.12.131

Newest version is ccxt==1.16.88

Here is an linked issue---> https://github.com/ccxt/ccxt/issues/2699

lenak25 commented 6 years ago

@avolution, thanks for reporting and providing the additional information, it is very helpful. We will address this.

avolution commented 6 years ago

I think the solution is just to update ccxt.

It is a bug happend by refactoring on site of ccxt in this particular version. (have patched ccxt as a workaround on my system and its working)

I have tried to update the ccxt to a newer version but this will in result other errors(so not compatible to catalyst yet).

kooomix commented 6 years ago

@lenak25 Thanks for connecting me with this thread. can you update about the status of the issue? Do you have any suggestion of how to handle it until it is fixed? (only thing I can do now is to not place orders which I estimate the amount will be higher than 9,999).

EmbarAlmog commented 6 years ago

Hi @avolution, we are planning to upgrade catalyst to use a more recent ccxt version (1.17.94). You have mentioned that you have witnessed some issues when using an updated CCXT version with catalyst, when using Bitfinex. Could you please elaborate on the issues you have observed? Thanks!

avolution commented 6 years ago

@EmbarAlmog I just tried a higher CCXT version by updating the dependency. In result a run will crash immediately because I think there are some structural changes in CCXT(and Catalyst called ccxt in the way of the old version). I did't go deeper then. To reproduce it just update ccxt version the conda env on the current version and run a backtest/live run.

kooomix commented 6 years ago

@EmbarAlmog Would be great if you can also refer to the issue @lenak25 referenced to this thread.. Thanks.

avolution commented 6 years ago

@EmbarAlmog Another point is that CCXT has two Bitfinex implementations:

'bitfinex' and the implementation of api v2 'bitfinex2'

It would be better when catalyst development focuses on 'bitfinex2' cause refer to #415 margin trading is impossible with 'bitfinex' CCXT implementation even in the newest version.

I think CCXT Development is focusing on the v2 Implementation 'bitfinex2' and the v1 implementation 'bitfinex' is just legacy.

brandsimon commented 6 years ago

@avolution Margin trading is not possible with catalyst in its current version. Margin trading is possible with ccxt bitfinex implementation (I am doing it).

EmbarAlmog commented 6 years ago

Hi @avolution, I've tested catalyst with Bitfinex (using the bitfinex module, not the bitfinex2) with CCXT to version 1.17.94 and everything worked as expected. Could you please elaborate on the issues you experienced? Please specify the exact functions you have been used. Thanks

lenak25 commented 6 years ago

In catalyst 0.5.19 CCXT was upgraded to 1.17.94, which fixes this issue.

markudevelop commented 5 years ago

@brandsimon are you doing it with catalyst or own implementation on ccxt? I'm also looking into this would love to get some info. Thanks.

brandsimon commented 5 years ago

@voidale I am doing it with my own implementation on ccxt, since it is not supported in catalyst.