Skip to content

Conversation

@pofenglin079
Copy link

No description provided.

backtrader and others added 30 commits July 31, 2018 00:10
This is a draft implementation - to be continued

Signed-off-by: Ed Bartosh <[email protected]>
Instead of using last_id added new property _last_ts to
track last data timestamp.

This should fix a bug where no data was added when
using a time based frame.
Check if exchange supports OHLCV fetching in _fetch_ohlcv method.

This should prevent code duplication because this was not handled
in the "start" method.
Fetched data since the last timestamp instead of fetching the
whole data feed which made it more slower.

This makes it more efficient to feed the live data, so for
each iteration it will fetch only the newer data.
Fetched OHLCV data in chunks of ohlcv_limit bars.
Do it while there is new data available.

This should fix hangups and crashes when a lot of history
data requested.

Signed-off-by: Ed Bartosh <[email protected]>
We'll need to handle request timeouts and other things,
so it makes sense to move all ccxt-related functionality
to the separate module to be able to call it from broker
and feed classes.

Signed-off-by: Ed Bartosh <[email protected]>
Retry exchange queries when network error[s] occurs.

Signed-off-by: Ed Bartosh <[email protected]>
Signed-off-by: Ed Bartosh <[email protected]>
Checked if requested timeframe/compression is supported
by the exchange.

Signed-off-by: Ed Bartosh <[email protected]>
_load method of the feed should returned False when price
data is not yet available. This caused cerebro to stop.
It should return None in this case to indicate timeout
getting the data.

Signed-off-by: Ed Bartosh <[email protected]>
Don't process ohlcv bars that contain None prices

Signed-off-by: Ed Bartosh <[email protected]>
Signed-off-by: Ed Bartosh <[email protected]>
Default order of output historical bars is not the same
for different exchanges.
Explicitly specifying it with params={"reverse": False}
seems not having any effect.

Had to sort the output in the feed code to solve this.

Signed-off-by: Ed Bartosh <[email protected]>
Used public order properties 'side' and 'amount' instead of
original unparsed properties ['info']['side'] and ['info']['original_amount'].
The latter are exchange-specific and may not be provided by
all exchanges.

This should fix crashes like this:
Traceback (most recent call last):
  File "machine_engin.py", line 161, in <module>
    cerebro.run()
  File "backtrader/cerebro.py", line 1127, in run
    runstrat = self.runstrategies(iterstrat)
  File "backtrader/cerebro.py", line 1295, in runstrategies
    self._runnext(runstrats)
  File "backtrader/cerebro.py", line 1626, in _runnext
    strat._next()
  File "backtrader/strategy.py", line 325, in _next
    super(Strategy, self)._next()
  File "backtrader/lineiterator.py", line 266, in _next
    self.next()
  File "machine_engin_prod.py", line 138, in next
    price=self.data0.close[0])
  File "backtrader/strategy.py", line 945, in sell
    **kwargs)
  File "backtrader/brokers/ccxtbroker.py", line 96, in sell
    return self._submit(owner, data, exectype, 'sell', size, price, kwargs)
  File "backtrader/brokers/ccxtbroker.py", line 82, in _submit
    order = CCXTOrder(owner, data, _order)
  File "backtrader/metabase.py", line 88, in __call__
    _obj, args, kwargs = cls.doinit(_obj, *args, **kwargs)
  File "backtrader/metabase.py", line 78, in doinit
    _obj.__init__(*args, **kwargs)
  File "backtrader/brokers/ccxtbroker.py", line 34, in __init__
    self.size = float(ccxt_order['info']['original_amount'])
KeyError: 'original_amount'

Signed-off-by: Ed Bartosh <[email protected]>
ExchangeError occurs randomly from time to time on some exchanges.
Added it to the list of exceptions in retry decorator to
retry when it occurs.

Signed-off-by: Ed Bartosh <[email protected]>
Added get_value and get_cash methods to the CCXTBroker to fix
this error and similar error for get_cash:

Traceback (most recent call last):
    strategy = cerebro.run()[0]
  File "backtrader-ccxt/backtrader/cerebro.py", line 1127, in run
    runstrat = self.runstrategies(iterstrat)
  File "backtrader-ccxt/backtrader/cerebro.py", line 1290, in runstrategies
    self._runonce(runstrats)
  File "backtrader-ccxt/backtrader/cerebro.py", line 1691, in _runonce
    strat._oncepost(dt0)
  File "backtrader-ccxt/backtrader/strategy.py", line 293, in _oncepost
    self._next_analyzers(minperstatus, once=True)
  File "backtrader-ccxt/backtrader/strategy.py", line 364, in _next_analyzers
    analyzer._nextstart()  # only called for the 1st value
  File "backtrader-ccxt/backtrader/analyzer.py", line 180, in _nextstart
    child._nextstart()
  File "backtrader-ccxt/backtrader/analyzer.py", line 182, in _nextstart
    self.nextstart()
  File "backtrader-ccxt/backtrader/analyzer.py", line 235, in nextstart
    self.next()
  File "backtrader-ccxt/backtrader/analyzers/positions.py", line 78, in next
    pvals = [self.strategy.broker.get_value([d]) for d in self.datas]
  File "backtrader-ccxt/backtrader/analyzers/positions.py", line 78, in <listcomp>
    pvals = [self.strategy.broker.get_value([d]) for d in self.datas]
AttributeError: 'CCXTBroker' object has no attribute 'get_value'

Signed-off-by: Ed Bartosh <[email protected]>
Used ccxt_order['info'] data structure to fix this exception:

File "backtrader/brokers/ccxtbroker.py", line 33, in __init__
    self.ordtype = self.Buy if ccxt_order['side'] == 'buy' else self.Sell
KeyError: 'side'

Signed-off-by: Ed Bartosh <[email protected]>
bartosh and others added 7 commits September 9, 2018 12:02
This should make all order keys available
on the first level (order["amount"] vs order["info"]["amount"])
for all supported exchanges.

Signed-off-by: Ed Bartosh <[email protected]>
self._last_id was set initially to ''.
meaning this condition never resulted true ('if self._last_id is None:')
This commit caused ccxt feed code to crash with
ValueError: invalid literal for int() with base 10

reverting

This reverts commit bf5407c.

Fixes: bartosh#20
As self._last_id is set to empty string that condition
was always false.

Signed-off-by: Ed Bartosh <[email protected]>
Used the same store object for one exchange for any feed or
broker that uses that exchange.

Fixes: bartosh#18

Signed-off-by: Ed Bartosh <[email protected]>
Some exchanges don't set 'amount' for the order object.
This causes crash in ccxt store code:
  File "backtrader\stores\ccxtstore.py", line 39, in __init__
    self.size = float(ccxt_order['amount'])
TypeError: float() argument must be a string or a number, not 'NoneType'

Added check for 'amount' to be present in the order before checking it.

Signed-off-by: Ed Bartosh <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants