garethdmm / gryphon

Powerful, proven, and extensible framework for building trading strategies at any frequency, with a focus on crypto currencies. Battle-tested with billions traded.
http://www.gryphonframework.org
Other
1.05k stars 151 forks source link

CurrencyMismatch: unsupported operation between money in 'EUR' and 'USD': '<'. #35

Open asmodehn opened 5 years ago

asmodehn commented 5 years ago

I am running this strategy :

from gryphon.execution.strategies.base import Strategy
from gryphon.lib import arbitrage as arb
from gryphon.lib.exchange.consts import Consts

class MoneyLtIssue(Strategy):

    def __init__(self, db, harness=None, strategy_configuration=None):
        self.done = False
        super(MoneyLtIssue, self).__init__(db, harness, strategy_configuration)

    def tick(self, open_orders):
        crosses = arb.detect_crosses_between_many_orderbooks(
            [self.harness.bitstamp_eth_eur.get_orderbook(),
             self.harness.bitstamp_eth_btc.get_orderbook(),
            self.harness.bitstamp_eth_usd.get_orderbook()], ignore_unprofitable=False
        )

        for cross in crosses:
            print(cross)
            self.done = True

    def is_complete(self):
        return self.done

When multiple crosses are detected (hence why I need to pass 3 orderbooks), this happens :

ERROR:gryphon.execution.live_runner:[CurrencyMismatch] unsupported operation between money in 'EUR' and 'USD': '<'. Use XMoney for automatic currency conversion.
Traceback (most recent call last):
  File "/opt/Projects/gryphon/gryphon/execution/live_runner.py", line 281, in live_run
    harness.tick()
  File "harness.pyx", line 161, in gryphon.execution.harness.harness.Harness.tick (/home/alexv/.pyxbld/temp.linux-x86_64-2.7/pyrex/gryphon/execution/harness/harness.c:3195)
  File "/opt/Projects/gryphon/gryphon/lib/util/profile.py", line 59, in inner
    t, result = timeit.timeit(run_func, number=1)
  File "/usr/lib/python2.7/timeit.py", line 237, in timeit
    return Timer(stmt, setup, timer).timeit(number)
  File "/usr/lib/python2.7/timeit.py", line 202, in timeit
    timing = self.inner(it, self.timer)
  File "/opt/Projects/gryphon/gryphon/lib/util/monkeypatch_timeit.py", line 14, in inner
    retval = _func()
  File "/opt/Projects/gryphon/gryphon/lib/util/profile.py", line 57, in run_func
    return func(*args, **kwargs)
  File "harness.pyx", line 169, in gryphon.execution.harness.harness.Harness.tick_algo (/home/alexv/.pyxbld/temp.linux-x86_64-2.7/pyrex/gryphon/execution/harness/harness.c:3394)
  File "money_lt_issue.pyx", line 13, in strategies.money_lt_issue.MoneyLtIssue.tick (/home/alexv/.pyxbld/temp.linux-x86_64-2.7/pyrex/strategies/money_lt_issue.c:1228)
  File "/opt/Projects/gryphon/gryphon/lib/arbitrage.py", line 234, in detect_crosses_between_many_orderbooks
    crosses = sorted(crosses, key=lambda c: c.profit, reverse=True)
  File "/home/alexv/.virtualenvs/gryphon2/local/lib/python2.7/site-packages/money/money.py", line 54, in __lt__
    raise CurrencyMismatch(self.currency, other.currency, '<')
CurrencyMismatch: unsupported operation between money in 'EUR' and 'USD': '<'. Use XMoney for automatic currency conversion.
garethdmm commented 5 years ago

Interesting, I do see the issue, when you have multiple crosses with different 'base' price currencies their profitability is recorded in different currencies, and the sort function on them fails. Should be an easy fix, I'll take a close look early next week if no one else gets to it before then. We should add some unit tests to tests/logic/libraries/arbitrage_test.py to catch this scenario in the future as well.