def test_bar_append_new_low(): trade_1 = Trade(123456, price=3.0, amount=1.0) trade_2 = Trade(123456, price=2.9, amount=1.0) trade_3 = Trade(123456, price=3.1, amount=1.0) bar = Bar(trade_1) bar.append(trade_2) bar.append(trade_3) assert bar.low == trade_2.price
def test_tib_bar_generation_tb_sequence(publisher, tib_generator): for i in range(5): trade = Trade(12345, price=10.0 - i, amount=1.0) publisher.publish(['test-BTCUSD', 'new-trade'], trade) assert len(tib_generator.b_ts) == 5 assert tib_generator.b_ts == [0.0, -1.0, -1.0, -1.0, -1.0] assert tib_generator.theta == -4
def test_bar_initial_attrs_from_trade(): trade = Trade(123456, 3.0, 1.5) bar = Bar(trade) assert bar.dollar_value == trade.dollar_value assert bar.time == trade.timestamp assert bar.open == trade.price assert bar.high == trade.price assert bar.low == trade.price assert bar.close == trade.price assert bar.count == 1
def test_tib_bar_generation_generates(publisher, subscriber, hub): bars = [] def new_bar_handler(key, bar): bars.append(bar) subscriber.add_sync_listener(events.AnyNewBar, new_bar_handler) tib_generator = TickImbalanceBarGenerator(hub, initial_theta=10) n = 100 for p in np.random.normal(scale=0.1,size=n) + np.linspace(1, 20, n): trade = Trade(12345, price=p, amount=1.0) publisher.publish(['test-BTCUSD', 'new-trade'], trade) assert len(bars) > 1
def test_mullti_strategy_on_new_bar_is_called(): hub = Hub() publisher = Publisher(hub, 'test-publisher') trade = Trade(12345, price=1.0, amount=1.0) bar = Bar(trade) exchange = 'test' market = Markets.BTCEUR broker = BacktestBroker(hub) strat = NewBarTestMultiStrategy(hub, broker) publisher.publish([f'{exchange}-{market}', 'new-bar'], bar) assert strat.on_new_bar_called
def test_tick_bar_generation_after_threshold(): hub = Hub() publisher = Publisher(hub, 'test-publisher') generator = TickBarGenerator(hub, threshold=3) subscriber = Subscriber(hub, 'test-subscriber') bars = [] def new_bar_handler(key, bar): bars.append(bar) subscriber.add_sync_listener(events.AnyNewBar, new_bar_handler) assert len(bars) == 0 for i in range(5): trade = Trade(12345, price=1.0, amount=1.0) publisher.publish(['test-BTCUSD', 'new-trade'], trade) assert len(bars) == 1
def test_bar_append_increases_dollar_value(): trade_1 = Trade(123456, price=3.0, amount=1.0) trade_2 = Trade(123456, price=2.9, amount=1.0) bar = Bar(trade_1) bar.append(trade_2) assert bar.dollar_value == (trade_1.dollar_value + trade_2.dollar_value)
def test_bar_append_increases_volume(): trade_1 = Trade(123456, price=3.0, amount=1.0) trade_2 = Trade(123456, price=2.9, amount=1.0) bar = Bar(trade_1) bar.append(trade_2) assert bar.volume == (trade_1.amount + trade_2.amount)
def test_bar_append_increases_count(): trade_1 = Trade(123456, price=3.0, amount=1.0) trade_2 = Trade(123456, price=2.9, amount=1.0) bar = Bar(trade_1) bar.append(trade_2) assert bar.count == 2
def test_bar_append_changes_close(): trade_1 = Trade(123456, price=3.0, amount=1.0) trade_2 = Trade(123456, price=2.9, amount=1.0) bar = Bar(trade_1) bar.append(trade_2) assert bar.close == trade_2.price
def test_bar_append_does_not_change_open(): trade_1 = Trade(123456, price=3.0, amount=1.0) trade_2 = Trade(123456, price=2.9, amount=1.0) bar = Bar(trade_1) bar.append(trade_2) assert bar.open == trade_1.price