def test_simple_trade(self): s = TradeStrategy(buy_trigger="hi::>::10.0", sell_trigger="lo::<::11.0", stop_trigger="lo::<::11.0", quantity=10) t = Trade(symbol="mock", strategy=s, mock=True) self.assertIsNotNone(t.to_json())
def test_persist_and_load_json(self): path = "temp/" s = TradeStrategy(buy_trigger="hi::>::10.0", sell_trigger="lo::<::11.0", stop_trigger="lo::<::11.0", quantity=10) t = Trade(symbol="mock", strategy=s, mock=True) t.persist(path=path) refreshed = t.refresh() self.assertIsNotNone(refreshed) self.assertEqual(refreshed.strategy.buy_trigger, s.buy_trigger) self.assertEqual(refreshed.strategy.sell_trigger, s.sell_trigger) self.assertEqual(refreshed.strategy.stop_trigger, s.stop_trigger)
def test_persist_and_load_json_different_file(self): path = "temp/" path_refresh = "resources/sample_trade_2.json" s = TradeStrategy(buy_trigger="hi::>::10.0", sell_trigger="lo::<::11.0", stop_trigger="lo::<::11.0", quantity=10) t = Trade(symbol="mock", strategy=s, mock=True) t.persist(path=path) refreshed = t.refresh(path=path_refresh) self.assertIsNotNone(refreshed) self.assertEqual(refreshed.strategy.buy_trigger, "price::>::99.0") self.assertEqual(refreshed.strategy.sell_trigger, "price::<::999.0") self.assertEqual(refreshed.strategy.stop_trigger, "price::>::555.0")
def test_refresh_json_string(self): path_refresh = "resources/sample_trade_2.json" s = TradeStrategy(buy_trigger="hi::>::10.0", sell_trigger="lo::<::11.0", stop_trigger="lo::<::11.0", quantity=10) t = Trade(symbol="mock", strategy=s, mock=True) with open(path_refresh, "r") as f: json_body = f.read() refreshed = t.refresh(json_string=json_body) self.assertIsNotNone(refreshed) self.assertEqual(refreshed.strategy.buy_trigger, "price::>::99.0") self.assertEqual(refreshed.strategy.sell_trigger, "price::<::999.0") self.assertEqual(refreshed.strategy.stop_trigger, "price::>::555.0")
def test_trader_config(self): s = TradeStrategy(buy_trigger="hi::>::10.0", sell_trigger="lo::<::11.0", stop_trigger="lo::<::11.0", quantity=10) t = Trade(symbol="lee", strategy=s, mock=True) trader = MockTrader(t) self.assertIsNotNone(trader) self.assertEqual(trader.config.env, "test") self.assertEqual(trader.config.feeder_type, MarketDataFeederType.MOCK_DATA) self.assertEqual(trader.config.symbol, "lee")
def test_full_lifecycle_mock(self): e = MockEmitter() eval2 = StrategyEval() eval2.eval_with_loss_of_investment(20, 30, 2000, "25%", commission=20) strategy = eval2.create_trade_strategy() trade = Trade(symbol="mock", strategy=strategy, mock=True) trader = MockTrader(trade) while e.not_finished() and trader.trade.active: trader.follow_course(data=e.emit()) self.assertIsNotNone(trader.trade.summary) self.assertIsNotNone(trader.trade.summary.gain)
def test_load_json_file(self): path_load = "resources/sample_trade_2.json" refreshed = Trade().load(path=path_load) # refreshed = Trade(symbol='a').load(path=path_load) self.assertIsNotNone(refreshed) self.assertEqual(refreshed.symbol, "aapl") self.assertEqual(refreshed.active, False) self.assertEqual(refreshed.mock, False) self.assertEqual(refreshed.strategy.buy_trigger, "price::>::99.0") self.assertEqual(refreshed.strategy.sell_trigger, "price::<::999.0") self.assertIsNotNone(refreshed.trade_id) self.assertFalse(refreshed.trade_id.__contains__("None"))
def test_load_json_string(self): path_load = "resources/sample_trade_2.json" with open(path_load, "r") as f: json_body = f.read() refreshed = Trade().load(json_string=json_body) self.assertIsNotNone(refreshed) self.assertEqual(refreshed.symbol, "aapl") self.assertEqual(refreshed.active, False) self.assertEqual(refreshed.mock, False) self.assertEqual(refreshed.strategy.buy_trigger, "price::>::99.0") self.assertEqual(refreshed.strategy.sell_trigger, "price::<::999.0")
def test_trade_dont_buy(self): e = MockEmitter() strategy = TradeStrategy(buy_trigger="price::>::20.0", sell_trigger="price::>::7.0", stop_trigger="price::<::5.0", quantity=10) trade = Trade(symbol="mock", strategy=strategy, mock=True) trader = MockTrader(trade) while e.not_finished() and trader.trade.active: trader.follow_course(data=e.emit()) self.assertTrue(trader.trade.active) self.assertEqual(trader.trade.state, TradeState.WATCHING)
def test_trade_buy_sell(self): e = MockEmitter() strategy = TradeStrategy(buy_trigger="price::>::2.0", sell_trigger="price::>::7.0", stop_trigger="price::<::5.0", quantity=10) trade = Trade(symbol="mock", strategy=strategy, mock=True) trader = MockTrader(trade) while e.not_finished() and trader.trade.active: trader.follow_course(data=e.emit()) self.assertFalse(trader.trade.active) self.assertEqual(trader.trade.state, TradeState.FINISHED) self.assertIsNotNone(trader.trade.state_history)
def test_full_lifecycle_apple(self): e = MockEmitter(data_path="resources/aapl::2018-06-01::1min.csv") eval2 = StrategyEval() eval2.eval_with_loss_of_investment(188.2, "3%", 2000, "10%", commission=10) strategy = eval2.create_trade_strategy() trade = Trade(symbol="aapl", strategy=strategy, mock=True) trader = MockTrader(trade) while e.not_finished() and trader.trade.active: trader.follow_course(data=e.emit()) print(eval2.json(False)) print(trader.trade.summary.json(False)) self.assertGreater(trader.trade.summary.gain, 0) trader.trade.persist("temp")
def test_trader_custom_config(self): s = TradeStrategy(buy_trigger="hi::>::10.0", sell_trigger="lo::<::11.0", stop_trigger="lo::<::11.0", quantity=10) t = Trade(symbol="lee", strategy=s) config = TraderConfig( env="dev", feeder_type=MarketDataFeederType.REAL_DATA_REPLAY, market_data_interval="5min", market_data_pull_interval_sec=10) trader = MockTrader(t, config) self.assertIsNotNone(trader) self.assertEqual(trader.config.env, config.env) self.assertEqual(trader.config.feeder_type, config.feeder_type) self.assertEqual(trader.config.symbol, config.symbol)
def test_trade_buy_stop_activated(self): e = MockEmitter() # stop should sell strategy = TradeStrategy(buy_trigger="price::>::2.0", sell_trigger="price::>::20.0", stop_trigger="price::<::10.0", quantity=10) trade = Trade(symbol="mock", strategy=strategy, mock=True) trader = MockTrader(trade) while e.not_finished() and trader.trade.active: trader.follow_course(data=e.emit()) self.assertFalse(trader.trade.active) self.assertEqual(trader.trade.state, TradeState.FINISHED) self.assertIsNotNone(trader.trade.summary) print(trader.trade.summary.to_json())
def test_trade_buy_holding(self): e = MockEmitter() strategy = TradeStrategy(buy_trigger="price::>::2.0", sell_trigger="price::>::20.0", stop_trigger="price::<::0.5", quantity=10) trade = Trade(symbol="mock", strategy=strategy, mock=True) trader = MockTrader(trade) while e.not_finished() and trader.trade.active: trader.follow_course(data=e.emit()) self.assertTrue(trader.trade.active) self.assertEqual(trader.trade.state, TradeState.HOLDING) self.assertIsNotNone(trader.trade.summary) self.assertIsNotNone(trader.trade.summary.gain) self.assertNotEqual(trader.trade.summary.gain, 0)
def test_trade_buy_sell_technical_indicator(self): df = pd.read_csv("resources/aapl::2018-06-01::1min.csv") df = preparedata.add_moving_averages(df) e = MockEmitter(data=df) # stop should sell strategy = TradeStrategy( buy_trigger="sma_200::<::ema_20::type=ti", sell_trigger="sma_200::>::sma_50::type=technical_indicator", stop_trigger="last::<::1.0", quantity=10) trade = Trade(symbol="mock", strategy=strategy, mock=True) trader = MockTrader(trade) while e.not_finished() and trader.trade.active: trader.follow_course(data=e.emit()) self.assertFalse(trader.trade.active) self.assertEqual(trader.trade.state, TradeState.FINISHED) print(trader.trade.summary.to_json())