def main(args): load_config(args.config) fiat = config.read_string('trading.fiat') strategy = strategies[args.strategy]( fiat, config.read_int('trading.interval'), ) adapter = LiveMarketAdapter(MarketHistory(), fiat) fund = Fund(strategy, adapter) fund.run_live()
def backtests( fund: Fund, start_times: List[str] ) -> Iterable[List[float]]: for i, start_time in enumerate(start_times[:-1]): end_time = start_times[i + 1] logger.info(f'Testing from {start_time} to {end_time}') yield list(fund.run_backtest(start_time, end_time))
def test_strategy_step(): """Strategies can step forward. """ fiat = 'BTC' today = Timestamp('2017-05-01') initial_balances = {fiat: 1.0} strategy = BuffedCoinStrategy(fiat, 86400) adapter = BacktestMarketAdapter( fiat, MarketHistoryMock(), initial_balances, ) fund = Fund(strategy, adapter) new_value = fund.step(today) assert new_value == 1318.21
def test_strategies_mixed_initial_balance(strategy_cls, expected): """Strategies should produce their expected values when starting with a mixed portfolio. """ fiat = 'BTC' start = '2017-05-01' end = '2017-06-01' initial_balances = {fiat: 1.0, 'ETH': 12.3, 'XRP': 5.3, 'LTC': 0.5} strategy = strategy_cls(fiat, 86400) adapter = BacktestMarketAdapter( fiat, MarketHistoryMock(), initial_balances, ) fund = Fund(strategy, adapter) results = list(fund.run_backtest(start, end)) assert results == expected
def test_strategies(expected_results): ''' Strategies should produce their expected values ''' # The start and end of our test period start = '2017-05-01' end = '2017-06-01' fiat = config.read_string('trading.fiat') interval = config.read_int('trading.interval') for expected in expected_results: strategy = expected['strategy'](fiat, interval) adapter = BacktestMarketAdapter( MarketHistoryMock(), {'BTC': 1.0}, fiat, ) fund = Fund(strategy, adapter) res = list(fund.begin_backtest(start, end)) # print(res) assert res == expected['values']
def main(args): load_config(args.config) fiat = config.read_string('trading.fiat') strategy = strategies[args.strategy]( fiat, config.read_int('trading.interval'), ) # TODO: Shouldn't be necessary to provide initial balances for live trading adapter = PoloniexMarketAdapter( fiat, MarketHistory(), {}, # Actual balances will be fetched from Poloniex ) fund = Fund(strategy, adapter) if args.force_rebalance is True: confirm = input('Are you sure you want to rebalance your fund? [y/N] ') if confirm.strip().lower() == 'y': fund.force_rebalance_next_step = True fund.run_live()
def test_strategy_force_rebalance(): """Strategies can force a rebalance by passing `force_rebalance=True` into `Fund::step`. """ fiat = 'BTC' start = '2017-05-01' end = '2017-05-20' initial_balances = {fiat: 100, 'ETH': 3141.5926, 'XRP': 500} strategy = BuffedCoinStrategy(fiat, 86400) adapter_a = BacktestMarketAdapter( fiat, MarketHistoryMock(), initial_balances.copy(), ) fund_a = Fund(strategy, adapter_a) adapter_b = BacktestMarketAdapter( fiat, MarketHistoryMock(), initial_balances.copy(), ) fund_b = Fund(strategy, adapter_b) # First we'll run a backtest on *both* funds, and see that the final values # are what we expect results_a = list(fund_a.run_backtest(start, end)) results_b = list(fund_b.run_backtest(start, end)) assert results_a[-1] == 945757.92 assert results_b[-1] == 945757.92 ts = Timestamp('2017-06-01') # Fund A steps without a rebalance, while B *does* rebalance value_without_rebalance = fund_a.step(ts, force_rebalance=False) value_with_rebalance = fund_b.step(ts, force_rebalance=True) assert value_without_rebalance != value_with_rebalance
def main(args): load_config(args.config) fiat = config.read_string('trading.fiat') strategy = strategies[args.strategy]( fiat, config.read_int('trading.interval'), ) adapter = BacktestMarketAdapter( fiat, MarketHistory(), {'BTC': 1.0}, ) fund = Fund(strategy, adapter) summary = evaluate( fund, '2017-01-01', '2017-06-29', duration_days=30, window_distance_days=14, ) print(summary)