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