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