コード例 #1
0
ファイル: exchange_backend.py プロジェクト: kenuku/btrccts
 def setUp(self):
     dates = pandas.to_datetime(
         ['2017-01-01 1:00', '2017-01-01 1:01', '2017-01-01 1:02'],
         utc=True)
     self.init_timeframe = Timeframe(
         pd_start_date=dates[0],
         pd_end_date=dates[-1],
         pd_interval=pandas.Timedelta(minutes=1))
     data = {
         'open': [4, 7, 11],
         'high': [5, 8, 12],
         'low': [3, 6, 10],
         'close': [7, 11, 15],
         'volume': [101, 105, 110]
     }
     self.init_ohlcvs = pandas.DataFrame(data=data, index=dates)
     dates = pandas.date_range(start='2017-01-01 1:01',
                               end='2017-01-01 1:20',
                               freq='1T',
                               tz='UTC')
     data = {
         'open': [4 + 4 * i for i in range(0, 20)],
         'high': [5 + 4 * i for i in range(0, 20)],
         'low': [3 + 4 * i for i in range(0, 20)],
         'close': [8 + 4 * i for i in range(0, 20)],
         'volume': [100 + 4 * i for i in range(0, 20)]
     }
     self.fetch_ohlcv_ohlcvs = pandas.DataFrame(data=data, index=dates)
     self.fetch_ohlcv_timeframe = Timeframe(
         pd_start_date=dates[17],
         pd_end_date=dates[-1],
         pd_interval=pandas.Timedelta(minutes=1))
     self.fetch_ohlcv_timeframe.add_timedelta()
コード例 #2
0
ファイル: context.py プロジェクト: fodrh1201/btrccts
 def test__date(self):
     t = Timeframe(pd_start_date=pd_ts('2017-01-01 1:00'),
                   pd_end_date=pd_ts('2017-01-01 1:35'),
                   pd_interval=pandas.Timedelta(minutes=1))
     context = LiveContext(timeframe=t, conf_dir='')
     self.assertEqual(context.date(), pd_ts('2017-01-01 1:00'))
     t.add_timedelta()
     self.assertEqual(context.date(), pd_ts('2017-01-01 1:01'))
コード例 #3
0
ファイル: context.py プロジェクト: fodrh1201/btrccts
 def test__date(self):
     t = Timeframe(pd_start_date=pd_ts('2017-01-01 1:00'),
                   pd_end_date=pd_ts('2017-01-01 1:35'),
                   pd_interval=pandas.Timedelta(minutes=1))
     backtest = BacktestContext(timeframe=t)
     self.assertEqual(backtest.date(), pd_ts('2017-01-01 1:00'))
     t.add_timedelta()
     self.assertEqual(backtest.date(), pd_ts('2017-01-01 1:01'))
コード例 #4
0
ファイル: timeframe.py プロジェクト: ElisaBee/btrccts
 def test__init__timedelta_negative(self):
     with self.assertRaises(ValueError) as e:
         Timeframe(pd_start_date=pd_ts('2017-01-01 1:00'),
                   pd_end_date=pd_ts('2017-01-01 1:03'),
                   pd_timedelta=pandas.Timedelta(minutes=-1))
     self.assertEqual(str(e.exception),
                      'Timeframe: timedelta needs to be positive')
コード例 #5
0
ファイル: context.py プロジェクト: fodrh1201/btrccts
 async def test__create_exchange__default_exchange_backend_parameters(
         self, async_init_mock, base_init_mock, exchange_backend):
     base_init_mock.return_value = None
     async_init_mock.return_value = None
     timeframe = Timeframe(pd_start_date=pd_ts('2017-01-01 1:00'),
                           pd_end_date=pd_ts('2017-01-01 1:03'),
                           pd_interval=pandas.Timedelta(minutes=1))
     backtest = BacktestContext(timeframe=timeframe)
     # Create two instances, to see if they get the same backend
     backtest.create_exchange('binance', {'some': 'test'}, async_ccxt=True)
     async_init_mock.assert_called_once_with(
         config={'some': 'test'},
         exchange_backend=exchange_backend.return_value)
     exchange = backtest.create_exchange('binance', {'some': 'test'})
     self.assertEqual(exchange.__class__.__bases__,
                      (BacktestExchangeBase, ccxt.binance))
     exchange_backend.assert_called_once_with(timeframe=timeframe)
     self.assertEqual(base_init_mock.mock_calls, [
         call(config={'some': 'test'},
              exchange_backend=exchange_backend.return_value)
     ])
     self.assertEqual(async_init_mock.mock_calls, [
         call(config={'some': 'test'},
              exchange_backend=exchange_backend.return_value)
     ])
コード例 #6
0
ファイル: timeframe.py プロジェクト: ElisaBee/btrccts
 def test__init__end_date_smaller_than_start_date(self):
     with self.assertRaises(ValueError) as e:
         Timeframe(pd_start_date=pd_ts('2017-02-01'),
                   pd_end_date=pd_ts('2017-01-01'),
                   pd_timedelta=pandas.Timedelta(minutes=1))
     self.assertEqual(str(e.exception),
                      'Timeframe: end date is smaller then start date')
コード例 #7
0
ファイル: run.py プロジェクト: kenuku/btrccts
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__main_loop__live(self):
        algo = LiveTestAlgo(MagicMock(), MagicMock())
        time_params = LiveTestAlgo.get_test_time_parameters()
        timeframe = Timeframe(pd_start_date=time_params['pd_start_date'],
                              pd_end_date=time_params['pd_end_date'],
                              pd_timedelta=time_params['pd_timedelta'])
        result = main_loop(timeframe=timeframe, algorithm=algo, live=True)

        assert_test_live_algo_result(self, result, time_params)
コード例 #9
0
ファイル: run.py プロジェクト: fodrh1201/btrccts
 async def run_algo(self, Algo, async_algo):
     algo = Algo(MagicMock(), MagicMock())
     time_params = await Algo.get_test_time_parameters()
     timeframe = Timeframe(pd_start_date=time_params['pd_start_date'],
                           pd_end_date=time_params['pd_end_date'],
                           pd_interval=time_params['pd_interval'])
     result = await main_loop(timeframe=timeframe,
                              algorithm=algo,
                              live=True)
     assert_test_live_algo_result(self, result, time_params, False,
                                  async_algo)
コード例 #10
0
ファイル: exchange_backend.py プロジェクト: kenuku/btrccts
 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': {}
         })
コード例 #11
0
 def template__main_loop__exit_exception_during_sleep(
         self, exception_class, log_str, sleep_mock):
     algorithm = Mock(spec=AlgorithmBase)
     sleep_mock.side_effect = [exception_class('aa')]
     # We need to use future dates, because we are in live mode
     timeframe = Timeframe(pd_start_date=pd_ts('2217-01-01 1:00'),
                           pd_end_date=pd_ts('2217-01-01 1:03'),
                           pd_interval=pandas.Timedelta(minutes=1))
     with self.assertLogs('btrccts') as cm:
         result = main_loop(timeframe=timeframe,
                            algorithm=algorithm,
                            live=True)
     self.assertEqual(
         algorithm.mock_calls,
         [call.next_iteration(),
          call.exit(reason=ExitReason.STOPPED)])
     self.assertEqual(cm.output,
                      ['INFO:btrccts:Starting main_loop', log_str])
     self.assertEqual(algorithm, result)
コード例 #12
0
ファイル: timeframe.py プロジェクト: ElisaBee/btrccts
 def test__different_timedelta(self):
     t = Timeframe(pd_start_date=pd_ts('2017-01-01 1:00'),
                   pd_end_date=pd_ts('2017-01-01 1:35'),
                   pd_timedelta=pandas.Timedelta(minutes=15))
     t.add_timedelta()
     self.assertEqual(t.date(), pd_ts('2017-01-01 1:15'))
     t.add_timedelta()
     self.assertEqual(t.date(), pd_ts('2017-01-01 1:30'))
     t.add_timedelta()
     self.assertEqual(t.date(), None)
コード例 #13
0
 def setUp(self):
     self.timeframe = Timeframe(pd_start_date=pd_ts('2017-01-01 1:00'),
                                pd_end_date=pd_ts('2017-01-01 1:03'),
                                pd_interval=pandas.Timedelta(minutes=1))
コード例 #14
0
 def test__different_timedelta(self):
     t = Timeframe(pd_start_date=pd_ts('2017-01-01 1:00'),
                   pd_end_date=pd_ts('2017-01-01 1:35'),
                   pd_interval=pandas.Timedelta(minutes=15))
     t.add_timedelta()
     self.assertEqual(t.date(), pd_ts('2017-01-01 1:15'))
     t.add_timedelta()
     self.assertEqual(t.date(), pd_ts('2017-01-01 1:30'))
     self.assertEqual(t.finished(), False)
     t.add_timedelta()
     self.assertEqual(t.finished(), True)
     self.assertEqual(t.date(), pd_ts('2017-01-01 1:35'))
コード例 #15
0
 def test__add_timedelta_until__single(self):
     t = Timeframe(pd_start_date=pd_ts('2017-01-01 1:00'),
                   pd_end_date=pd_ts('2017-01-01 2:35'),
                   pd_interval=pandas.Timedelta(minutes=15))
     t.add_timedelta_until(pd_ts('2017-01-01 1:29'))
     self.assertEqual(t.date(), pd_ts('2017-01-01 1:15'))
コード例 #16
0
ファイル: exchange_backend.py プロジェクト: kenuku/btrccts
class ExchangeBackendTest(unittest.TestCase):
    def setUp(self):
        dates = pandas.to_datetime(
            ['2017-01-01 1:00', '2017-01-01 1:01', '2017-01-01 1:02'],
            utc=True)
        self.init_timeframe = Timeframe(
            pd_start_date=dates[0],
            pd_end_date=dates[-1],
            pd_interval=pandas.Timedelta(minutes=1))
        data = {
            'open': [4, 7, 11],
            'high': [5, 8, 12],
            'low': [3, 6, 10],
            'close': [7, 11, 15],
            'volume': [101, 105, 110]
        }
        self.init_ohlcvs = pandas.DataFrame(data=data, index=dates)
        dates = pandas.date_range(start='2017-01-01 1:01',
                                  end='2017-01-01 1:20',
                                  freq='1T',
                                  tz='UTC')
        data = {
            'open': [4 + 4 * i for i in range(0, 20)],
            'high': [5 + 4 * i for i in range(0, 20)],
            'low': [3 + 4 * i for i in range(0, 20)],
            'close': [8 + 4 * i for i in range(0, 20)],
            'volume': [100 + 4 * i for i in range(0, 20)]
        }
        self.fetch_ohlcv_ohlcvs = pandas.DataFrame(data=data, index=dates)
        self.fetch_ohlcv_timeframe = Timeframe(
            pd_start_date=dates[17],
            pd_end_date=dates[-1],
            pd_interval=pandas.Timedelta(minutes=1))
        self.fetch_ohlcv_timeframe.add_timedelta()

    def test__init__ohlcvs__index_start_bigger_than_start_date(self):
        with self.assertRaises(ValueError) as e:
            ExchangeBackend(timeframe=self.init_timeframe,
                            ohlcvs={'ETH/BTC': self.init_ohlcvs[1:]},
                            balances={})
        self.assertEqual(str(e.exception), 'ohlcv needs to cover timeframe')

    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')

    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))

    def test__init__ohlcvs__open_missing(self):
        self.template__init__ohlcvs__missing('open')

    def test__init__ohlcvs__high_missing(self):
        self.template__init__ohlcvs__missing('high')

    def test__init__ohlcvs__low_missing(self):
        self.template__init__ohlcvs__missing('low')

    def test__init__ohlcvs__close_missing(self):
        self.template__init__ohlcvs__missing('close')

    def test__init__ohlcvs__volume_missing(self):
        self.template__init__ohlcvs__missing('volume')

    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')

    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')

    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'")

    @patch("btrccts.exchange_backend.ExchangeAccount")
    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)

    @patch("btrccts.exchange_backend.ExchangeAccount")
    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)())

    def test__create_order(self):
        self.template_exchange_account_method_propagated(
            kwargs={
                'market': {},
                'side': 'sell',
                'price': 5,
                'amount': 10,
                'type': 'limit'
            },
            methodname='create_order')

    def test__cancel_order(self):
        self.template_exchange_account_method_propagated(
            kwargs={
                'id': '123',
                'symbol': None
            }, methodname='cancel_order')

    def test__fetch_order(self):
        self.template_exchange_account_method_propagated(
            kwargs={
                'id': '123',
                'symbol': None
            }, methodname='fetch_order')

    def test__fetch_closed_orders(self):
        self.template_exchange_account_method_propagated(
            kwargs={
                'symbol': 'BTC/ETH',
                'since': 0,
                'limit': 15
            },
            methodname='fetch_closed_orders')

    def test__fetch_open_orders(self):
        self.template_exchange_account_method_propagated(
            kwargs={
                'symbol': 'BTC/ETH',
                'since': 110,
                'limit': 150
            },
            methodname='fetch_open_orders')

    def test__fetch_balance(self):
        self.template_exchange_account_method_propagated(
            kwargs={}, methodname='fetch_balance')

    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')

    def test__fetch_ohlcv_dataframe__access_future(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='1m',
                                          since=1483232550000,
                                          limit=18)
        self.assertEqual(
            str(e.exception),
            'ExchangeBackend: fetch_ohlcv: since.ceil(timeframe) + limit'
            ' * timeframe needs to be in the past')

    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')

    def test__fetch_ohlcv_dataframe__partial_data_agg(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='6m',
                                               since=1483232610000,
                                               limit=3)
        pandas.testing.assert_frame_equal(
            result,
            pandas.DataFrame(data={
                'open': [24.0, 48.0, 72.0],
                'high': [45.0, 69.0, 77.0],
                'low': [23.0, 47.0, 71.0],
                'close': [48.0, 72.0, 80.0],
                'volume': [780.0, 924.0, 340.0]
            },
                             dtype=float,
                             index=pandas.date_range('2017-01-01 01:06',
                                                     '2017-01-01 01:18',
                                                     3,
                                                     tz='UTC')))

    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')))

    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',
                                                     5,
                                                     tz='UTC')))

    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')))

    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')))

    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')))

    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')))

    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')

    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': {}
            })

    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')
コード例 #17
0
ファイル: timeframe.py プロジェクト: ElisaBee/btrccts
 def test__add_timedelta__date(self):
     t = Timeframe(pd_start_date=pd_ts('2017-01-01 1:00'),
                   pd_end_date=pd_ts('2017-01-01 1:03'),
                   pd_timedelta=pandas.Timedelta(minutes=1))
     self.assertEqual(t.date(), pd_ts('2017-01-01 1:00'))
     # should return the same value
     self.assertEqual(t.date(), pd_ts('2017-01-01 1:00'))
     t.add_timedelta()
     self.assertEqual(t.date(), pd_ts('2017-01-01 1:01'))
     t.add_timedelta()
     t.add_timedelta()
     self.assertEqual(t.date(), pd_ts('2017-01-01 1:03'))
     t.add_timedelta()
     self.assertEqual(t.date(), None)
コード例 #18
0
ファイル: timeframe.py プロジェクト: ElisaBee/btrccts
 def test__add_timedelta_until__add_multiple_times(self):
     t = Timeframe(pd_start_date=pd_ts('2017-01-01 1:00'),
                   pd_end_date=pd_ts('2017-01-01 2:35'),
                   pd_timedelta=pandas.Timedelta(minutes=15))
     t.add_timedelta_until(pd_ts('2017-01-01 2:31'))
     self.assertEqual(t.date(), pd_ts('2017-01-01 2:30'))
コード例 #19
0
ファイル: timeframe.py プロジェクト: ElisaBee/btrccts
 def test__end_date(self):
     t = Timeframe(pd_start_date=pd_ts('2017-01-01 1:00'),
                   pd_end_date=pd_ts('2017-01-01 1:35'),
                   pd_timedelta=pandas.Timedelta(minutes=15))
     self.assertEqual(t.end_date(), pd_ts('2017-01-01 1:35'))