Пример #1
0
 def test__fetch_ticker(self):
     timeframe = Timeframe(pd_start_date=self.fetch_ohlcv_ohlcvs.index[0],
                           pd_end_date=self.fetch_ohlcv_ohlcvs.index[-1],
                           pd_interval=pandas.Timedelta(minutes=0.5))
     backend = ExchangeBackend(ohlcvs={'BTC/USD': self.fetch_ohlcv_ohlcvs},
                               timeframe=timeframe,
                               balances={})
     timeframe.add_timedelta()
     timeframe.add_timedelta()
     timeframe.add_timedelta()
     self.assertEqual(
         backend.fetch_ticker('BTC/USD'), {
             'symbol': 'BTC/USD',
             'timestamp': 1483232520000,
             'datetime': '2017-01-01T01:02:00.000Z',
             'high': 9.0,
             'low': 7.0,
             'bid': None,
             'bidVolume': None,
             'ask': None,
             'askVolume': None,
             'vwap': None,
             'open': 8.0,
             'close': 12.0,
             'last': None,
             'previousClose': None,
             'change': None,
             'percentage': None,
             'average': None,
             'baseVolume': None,
             'quoteVolume': None,
             'info': {}
         })
Пример #2
0
 def test__fetch_ticker__exception(self):
     backend = ExchangeBackend(ohlcvs={},
                               timeframe=self.fetch_ohlcv_timeframe,
                               balances={})
     with self.assertRaises(BadSymbol) as e:
         backend.fetch_ticker('BTC/USD')
     self.assertEqual(str(e.exception),
                      'ExchangeBackend: no prices for BTC/USD')
Пример #3
0
 def test__fetch_ohlcv_dataframe__no_data(self):
     backend = ExchangeBackend(ohlcvs={},
                               timeframe=MagicMock(),
                               balances={})
     with self.assertRaises(BadSymbol) as e:
         backend.fetch_ohlcv_dataframe('UNK/BTC', '1m')
     self.assertEqual(str(e.exception),
                      'ExchangeBackend: no prices for UNK/BTC')
Пример #4
0
 def test__fetch_ohlcv_dataframe__not_avail_past_values(self):
     symbol = 'BTC/USD'
     backend = ExchangeBackend(ohlcvs={symbol: self.fetch_ohlcv_ohlcvs},
                               timeframe=self.fetch_ohlcv_timeframe,
                               balances={})
     with self.assertRaises(BadRequest) as e:
         backend.fetch_ohlcv_dataframe(symbol=symbol, since=1483232330000)
     self.assertEqual(
         str(e.exception), 'ExchangeBackend: fetch_ohlcv: no '
         'date availabe at since')
Пример #5
0
 def test__fetch_ohlcv_dataframe__access_future__timeframe(self):
     symbol = 'BTC/USD'
     backend = ExchangeBackend(ohlcvs={symbol: self.fetch_ohlcv_ohlcvs},
                               timeframe=self.fetch_ohlcv_timeframe,
                               balances={})
     with self.assertRaises(BadRequest) as e:
         backend.fetch_ohlcv_dataframe(
             symbol=symbol, timeframe='2m', since=1483232610000, limit=10)
     self.assertEqual(
         str(e.exception),
         'ExchangeBackend: fetch_ohlcv: since.ceil(timeframe) + limit'
         ' * timeframe needs to be in the past')
Пример #6
0
 def test__create_exchange__parameters(self, base_init_mock):
     base_init_mock.return_value = None
     bitfinex_backend = ExchangeBackend(timeframe=None)
     binance_backend = ExchangeBackend(timeframe=None)
     backtest = BacktestContext(timeframe=None,
                                exchange_backends={
                                    'bitfinex': bitfinex_backend,
                                    'binance': binance_backend
                                })
     exchange = backtest.create_exchange('bitfinex', {'parameter': 123})
     base_init_mock.assert_called_once_with(
         config={'parameter': 123}, exchange_backend=bitfinex_backend)
     self.assertEqual(exchange.__class__.__bases__,
                      (BacktestExchangeBase, ccxt.bitfinex))
Пример #7
0
def execute_algorithm(exchange_names, symbols, AlgorithmClass, args,
                      start_balances,
                      pd_start_date, pd_end_date, pd_interval,
                      live, auth_aliases,
                      data_dir=USER_DATA_DIR,
                      conf_dir=USER_CONFIG_DIR):
    timeframe = Timeframe(pd_start_date=pd_start_date,
                          pd_end_date=pd_end_date,
                          pd_interval=pd_interval)
    ohlcv_dir = os.path.join(data_dir, 'ohlcv')
    if live:
        context = LiveContext(timeframe=timeframe,
                              conf_dir=conf_dir,
                              auth_aliases=auth_aliases)
    else:
        ohlcvs = load_ohlcvs(ohlcv_dir=ohlcv_dir,
                             exchange_names=exchange_names,
                             symbols=symbols)
        exchange_backends = {}
        for exchange_name in exchange_names:
            exchange_backends[exchange_name] = ExchangeBackend(
                timeframe=timeframe,
                balances=start_balances.get(exchange_name, {}),
                ohlcvs=ohlcvs.get(exchange_name, {}))
        context = BacktestContext(timeframe=timeframe,
                                  exchange_backends=exchange_backends)
    algorithm = AlgorithmClass(context=context,
                               args=args)
    return main_loop(timeframe=timeframe,
                     algorithm=algorithm,
                     live=live)
Пример #8
0
 def test__init__ohlcvs__wrong_frequency(self):
     df = self.init_ohlcvs.drop(self.init_ohlcvs.index[1])
     with self.assertRaises(ValueError) as e:
         ExchangeBackend(timeframe=self.init_timeframe,
                         ohlcvs={'ETH/BTC': df},
                         balances={})
     self.assertEqual(str(e.exception), 'ohlcv needs to be in 1T format')
Пример #9
0
 def test__init__ohlcvs__not_finite(self):
     df = self.init_ohlcvs.copy()
     df.iloc[1, 1] = float('inf')
     with self.assertRaises(ValueError) as e:
         ExchangeBackend(timeframe=self.init_timeframe,
                         ohlcvs={'ETH/BTC': df},
                         balances={})
     self.assertEqual(str(e.exception), 'ohlcv ohlcv needs to finite')
Пример #10
0
 def template__init__ohlcvs__missing(self, column):
     df = self.init_ohlcvs.drop(column, 1)
     with self.assertRaises(ValueError) as e:
         ExchangeBackend(timeframe=self.init_timeframe,
                         ohlcvs={'ETH/BTC': df},
                         balances={})
     self.assertEqual(str(e.exception),
                      'ohlcv {} needs to be provided'.format(column))
Пример #11
0
 def test__init__ohlcvs__not_convertable_to_float(self):
     df = self.init_ohlcvs.copy()
     df.iloc[1, 1] = 'asd'
     with self.assertRaises(ValueError) as e:
         ExchangeBackend(timeframe=self.init_timeframe,
                         ohlcvs={'ETH/BTC': df},
                         balances={})
     self.assertEqual(str(e.exception),
                      "ohlcv could not convert string to float: 'asd'")
Пример #12
0
 def test__fetch_ohlcv_dataframe__limit(self):
     symbol = 'BTC/USD'
     backend = ExchangeBackend(ohlcvs={symbol: self.fetch_ohlcv_ohlcvs},
                               timeframe=self.fetch_ohlcv_timeframe,
                               balances={})
     result = backend.fetch_ohlcv_dataframe(symbol=symbol, limit=3)
     pandas.testing.assert_frame_equal(
         result,
         pandas.DataFrame(
             data={
                 'open': [4, 8, 12],
                 'high': [5, 9, 13],
                 'low': [3, 7, 11],
                 'close': [8, 12, 16],
                 'volume': [100, 104, 108]},
             dtype=float,
             index=pandas.date_range(
                 '2017-01-01 1:01', '2017-01-01 1:03', 3, tz='UTC')))
Пример #13
0
 def test__fetch_ohlcv_dataframe(self):
     symbol = 'BTC/USD'
     backend = ExchangeBackend(ohlcvs={symbol: self.fetch_ohlcv_ohlcvs},
                               timeframe=self.fetch_ohlcv_timeframe,
                               balances={})
     result = backend.fetch_ohlcv_dataframe(symbol=symbol)
     pandas.testing.assert_frame_equal(
         result,
         pandas.DataFrame(
             data={
                 'open': [4, 8, 12, 16, 20],
                 'high': [5, 9, 13, 17, 21],
                 'low': [3, 7, 11, 15, 19],
                 'close': [8, 12, 16, 20, 24],
                 'volume': [100, 104, 108, 112, 116]},
             dtype=float,
             index=pandas.date_range(
                 '2017-01-01 1:01', '2017-01-01 1:05',
                 freq='1T', tz='UTC')))
Пример #14
0
 def test__fetch_ohlcv_dataframe__partial_data(self):
     symbol = 'BTC/USD'
     backend = ExchangeBackend(ohlcvs={symbol: self.fetch_ohlcv_ohlcvs},
                               timeframe=self.fetch_ohlcv_timeframe,
                               balances={})
     result = backend.fetch_ohlcv_dataframe(
         symbol=symbol, timeframe='3m', since=1483233000000, limit=3)
     pandas.testing.assert_frame_equal(
         result,
         pandas.DataFrame(
             data={
                 'open': [48.0, 60.0, 72.0],
                 'high': [57.0, 69.0, 77.0],
                 'low': [47.0, 59.0, 71.0],
                 'close': [60.0, 72.0, 80.0],
                 'volume': [444.0, 480.0, 340.0]},
             dtype=float,
             index=pandas.date_range(
                 '2017-01-01 01:12', '2017-01-01 01:18', 3, tz='UTC')))
Пример #15
0
 def test__fetch_ohlcv_dataframe__since(self):
     symbol = 'BTC/USD'
     backend = ExchangeBackend(ohlcvs={symbol: self.fetch_ohlcv_ohlcvs},
                               timeframe=self.fetch_ohlcv_timeframe,
                               balances={})
     result = backend.fetch_ohlcv_dataframe(symbol=symbol,
                                            since=1483232790000)
     pandas.testing.assert_frame_equal(
         result,
         pandas.DataFrame(
             data={
                 'open': [28, 32, 36, 40, 44],
                 'high': [29, 33, 37, 41, 45],
                 'low': [27, 31, 35, 39, 43],
                 'close': [32, 36, 40, 44, 48],
                 'volume': [124, 128, 132, 136, 140]},
             dtype=float,
             index=pandas.date_range(
                 '2017-01-01 1:07', '2017-01-01 1:11', 5, tz='UTC')))
Пример #16
0
 def test__init(self, mock):
     ohlcvs_mock = MagicMock()
     timeframe_mock = MagicMock()
     balances_mock = MagicMock()
     ExchangeBackend(ohlcvs=ohlcvs_mock,
                     timeframe=timeframe_mock,
                     balances=balances_mock)
     mock.assert_called_once_with(ohlcvs=ohlcvs_mock,
                                  timeframe=timeframe_mock,
                                  balances=balances_mock)
Пример #17
0
 def test__fetch_ohlcv_dataframe__resample(self):
     symbol = 'BTC/USD'
     backend = ExchangeBackend(ohlcvs={symbol: self.fetch_ohlcv_ohlcvs},
                               timeframe=self.fetch_ohlcv_timeframe,
                               balances={})
     result = backend.fetch_ohlcv_dataframe(symbol=symbol,
                                            since=1483232490000,
                                            limit=3,
                                            timeframe='4m')
     pandas.testing.assert_frame_equal(
         result,
         pandas.DataFrame(
             data={
                 'open': [16, 32, 48],
                 'high': [29, 45, 61],
                 'low': [15, 31, 47],
                 'close': [32, 48, 64],
                 'volume': [472, 536, 600]},
             dtype=float,
             index=pandas.date_range(
                 '2017-01-01 1:04', '2017-01-01 1:12', 3, tz='UTC')))
Пример #18
0
 def test__fetch_ohlcv_dataframe__resample_other_freq(self):
     symbol = 'BTC/USD'
     backend = ExchangeBackend(ohlcvs={symbol: self.fetch_ohlcv_ohlcvs},
                               timeframe=self.fetch_ohlcv_timeframe,
                               balances={})
     result = backend.fetch_ohlcv_dataframe(symbol=symbol,
                                            since=1483232590000,
                                            limit=3,
                                            timeframe='3m')
     pandas.testing.assert_frame_equal(
         result,
         pandas.DataFrame(
             data={
                 'open': [24, 36, 48],
                 'high': [33, 45, 57],
                 'low': [23, 35, 47],
                 'close': [36, 48, 60],
                 'volume': [372, 408, 444]},
             dtype=float,
             index=pandas.date_range(
                 '2017-01-01 1:06', '2017-01-01 1:12', 3, tz='UTC')))
Пример #19
0
 def template_exchange_account_method_propagated(self, mock, kwargs,
                                                 methodname):
     ohlcvs = {}
     timeframe_mock = MagicMock()
     balances = {}
     backend = ExchangeBackend(ohlcvs=ohlcvs,
                               timeframe=timeframe_mock,
                               balances=balances)
     result = getattr(backend, methodname)(**kwargs)
     mock.assert_called_once_with(ohlcvs=ohlcvs,
                                  timeframe=timeframe_mock,
                                  balances=balances)
     getattr(mock(), methodname).assert_called_once_with(**kwargs)
     self.assertEqual(result, getattr(mock(), methodname)())
Пример #20
0
 def test__init__ohlcvs__index_end_lower_than_end_date(self):
     with self.assertRaises(ValueError) as e:
         ExchangeBackend(timeframe=self.init_timeframe,
                         ohlcvs={'ETH/BTC': self.init_ohlcvs[:2]},
                         balances={})
     self.assertEqual(str(e.exception), 'ohlcv needs to cover timeframe')