Beispiel #1
0
class TestLiveSimulation(unittest.TestCase):

    TEST_TICKER_IDX = 0  #Doesnt matter here, but we need the input regardless

    #Pre-test simulation init for each test
    def setUp(self):
        data = pd.read_csv(TEST_DATA)
        pred = pd.read_csv(TEST_PRED)
        self.db = DBController()
        self.sim = LiveSimulation(self.db)

    #Test that variables for training are initialized properly
    def test_init(self):
        print("testing live simulation init.")
        controller = self.sim.controller
        self.assertTrue(isinstance(controller, DBController))
        print("Success")

    #Test buy shares reward function.
    def test_buy_stocks(self):
        print("testing historical sim for regular buy")
        pre_share_num = self.db.get_held_shares(TEST_SESSION_ID)
        self.sim.buy_shares(1, np.float32(TEST_USER_ID), np.float32(5000.0),
                            "v", TEST_SESSION_ID, self.TEST_TICKER_IDX)
        post_share_num = self.db.get_held_shares(TEST_SESSION_ID)
        user_funds = self.db.get_user(TEST_NAME).bank[0]
        self.assertTrue(pre_share_num + 1 == post_share_num)
        self.assertTrue(user_funds < 5000)
        print("Success")

        print("testing historical sim for regular buy W/ no money.")
        pre_share_num = self.db.get_held_shares(TEST_SESSION_ID)
        self.sim.buy_shares(1, np.float32(TEST_USER_ID), np.float32(0), "v",
                            TEST_SESSION_ID, self.TEST_TICKER_IDX)
        post_share_num = self.db.get_held_shares(TEST_SESSION_ID)
        user_funds = self.db.get_user(TEST_NAME).bank[0]
        self.assertTrue(pre_share_num == post_share_num)
        print("Success")

    #Test sell shares reward function.
    def test_sell_stocks(self):
        print("testing historical sim for regular sell")
        pre_share_num = self.db.get_held_shares(TEST_SESSION_ID)
        user_funds_before = self.db.get_user(TEST_NAME).bank[0]
        self.sim.buy_shares(1, np.float32(TEST_USER_ID),
                            np.float32(user_funds_before), "v",
                            TEST_SESSION_ID, self.TEST_TICKER_IDX)
        user_funds_buy = self.db.get_user(TEST_NAME).bank[0]
        self.sim.sell_shares(1, 1, np.float32(TEST_USER_ID),
                             np.float32(user_funds_buy), "v", TEST_SESSION_ID,
                             self.TEST_TICKER_IDX)
        user_funds_after = self.db.get_user(TEST_NAME).bank[0]
        post_share_num = self.db.get_held_shares(TEST_SESSION_ID)
        self.assertTrue(
            pre_share_num == post_share_num
        )  #buy then sell, no change in vol. Buy is already proven to add +1
        self.assertTrue(user_funds_after == user_funds_before)
        print("Success")

        print("testing historical sim for regular sell W/ no shares.")
        pre_share_num = self.db.get_held_shares(TEST_SESSION_ID)
        self.sim.sell_shares(
            1, 0, np.float32(TEST_USER_ID), np.float32(5000.0), "v",
            TEST_SESSION_ID, self.TEST_TICKER_IDX
        )  #aapl test stock has no purchases made ever for test session.
        post_share_num = self.db.get_held_shares(TEST_SESSION_ID)
        self.assertTrue(pre_share_num == post_share_num)
        print("Success")

    #Test sell shares reward function.
    def test_sell_all_stocks(self):
        print("testing historical sim for regular sell all")
        pre_share_num = self.db.get_held_shares(TEST_SESSION_ID)
        self.sim.buy_shares(1, np.float32(TEST_USER_ID), np.float32(5000.0),
                            "v", TEST_SESSION_ID, self.TEST_TICKER_IDX)
        self.sim.buy_shares(1, np.float32(TEST_USER_ID), np.float32(5000.0),
                            "v", TEST_SESSION_ID, self.TEST_TICKER_IDX)
        self.sim.buy_shares(1, np.float32(TEST_USER_ID), np.float32(5000.0),
                            "v", TEST_SESSION_ID, self.TEST_TICKER_IDX)
        post_share_num = self.db.get_held_shares(TEST_SESSION_ID)
        self.assertTrue(
            pre_share_num + 3 == post_share_num
        )  #buy then sell, no change in vol. Buy is already proven to add +1
        self.sim.sell_all(3, np.float32(TEST_USER_ID), np.float32(5000.0), "v",
                          TEST_SESSION_ID, self.TEST_TICKER_IDX)
        post_share_num = self.db.get_held_shares(TEST_SESSION_ID)
        self.assertTrue(post_share_num == pre_share_num)
        print("Success")

    #Test the state used as input for the RL Agent
    def test_get_state(self):
        print("testing historical get state")
        state = self.sim.get_state(TEST_SESSION_ID,
                                   self.db.get_user(TEST_NAME).bank[0], "v",
                                   self.TEST_TICKER_IDX)
        price = state[0]
        pred = state[1]
        funds = state[2]
        shares = state[3]
        self.assertTrue(isinstance(price, float))
        self.assertTrue(isinstance(pred, float))
        self.assertTrue(isinstance(funds, float))
        self.assertTrue(isinstance(shares, float))
        self.assertTrue(
            abs(price - self.sim.get_price("v", self.TEST_TICKER_IDX)) <=
            0.50)  #Could fluctuate in live
        self.assertTrue(
            pred == self.sim.get_predicted_price("v", self.TEST_TICKER_IDX))
        self.assertTrue(funds == self.db.get_user(TEST_NAME).bank[0])
        self.assertTrue(shares == self.db.get_held_shares(TEST_SESSION_ID))
        print("Success")

    #Test get price
    def test_get_price(self):
        print("testing historical get price for all stocks")
        for tick in stocks:
            price = self.sim.get_price(tick, self.TEST_TICKER_IDX)
            ticker = Ticker(tick)
            cur_price = ticker.price[tick]["regularMarketPrice"]
            self.assertTrue(
                abs(price - cur_price) <= 0.50)  #Could fluctuate in live
            self.assertTrue(isinstance(price, float))
            self.assertTrue(price > 0)
        print("Success")

    #Test get pred, statement coverage of pred is obtained here.
    def test_get_pred_price(self):
        print("testing historical get pred price")
        pred = self.sim.get_predicted_price("v", self.TEST_TICKER_IDX)
        self.assertTrue(isinstance(pred, int))
        self.assertTrue(
            pred == 1 or pred == 0
        )  #In live we cant guarantee statement coverage in our testing since it is dependent on our ML output. And at any given time we cannot have Both a trend up and a trend down.
Beispiel #2
0
class TestHistoricalSimulation(unittest.TestCase):

    TEST_TICKER_IDX = 42  #Arbitrary index from the historical sim to pull from and test

    #Pre-test simulation init for each test
    def setUp(self):
        data = pd.read_csv(TEST_DATA)
        pred = pd.read_csv(TEST_PRED)
        self.db = DBController()
        self.sim = HistoricalSimulation(self.db)

    #Test that variables for training are initialized properly
    def test_init(self):
        print("testing historical simulation init.")
        controller = self.sim.controller
        self.assertTrue(isinstance(controller, DBController))
        print("Success")

    #Test buy shares reward function.
    def test_buy_stocks(self):
        print("testing historical sim for regular buy")
        pre_share_num = self.db.get_held_shares(TEST_SESSION_ID)
        self.sim.buy_shares(1, np.float32(TEST_USER_ID), np.float32(5000.0),
                            "v", TEST_SESSION_ID, self.TEST_TICKER_IDX)
        post_share_num = self.db.get_held_shares(TEST_SESSION_ID)
        user_funds = self.db.get_user(TEST_NAME).bank[0]
        self.assertTrue(pre_share_num + 1 == post_share_num)
        self.assertTrue(user_funds < 5000)
        print("Success")

        print("testing historical sim for regular buy W/ no money.")
        pre_share_num = self.db.get_held_shares(TEST_SESSION_ID)
        self.sim.buy_shares(1, np.float32(TEST_USER_ID), np.float32(0), "v",
                            TEST_SESSION_ID, self.TEST_TICKER_IDX)
        post_share_num = self.db.get_held_shares(TEST_SESSION_ID)
        user_funds = self.db.get_user(TEST_NAME).bank[0]
        self.assertTrue(pre_share_num == post_share_num)
        print("Success")

    #Test sell shares reward function.
    def test_sell_stocks(self):
        print("testing historical sim for regular sell")
        pre_share_num = self.db.get_held_shares(TEST_SESSION_ID)
        user_funds_before = self.db.get_user(TEST_NAME).bank[0]
        self.sim.buy_shares(1, np.float32(TEST_USER_ID),
                            np.float32(user_funds_before), "v",
                            TEST_SESSION_ID, self.TEST_TICKER_IDX)
        user_funds_buy = self.db.get_user(TEST_NAME).bank[0]
        self.sim.sell_shares(1, 1, np.float32(TEST_USER_ID),
                             np.float32(user_funds_buy), "v", TEST_SESSION_ID,
                             self.TEST_TICKER_IDX)
        user_funds_after = self.db.get_user(TEST_NAME).bank[0]
        post_share_num = self.db.get_held_shares(TEST_SESSION_ID)
        self.assertTrue(
            pre_share_num == post_share_num
        )  #buy then sell, no change in vol. Buy is already proven to add +1
        self.assertTrue(user_funds_after == user_funds_before)
        print("Success")

        print("testing historical sim for regular sell W/ no shares.")
        pre_share_num = self.db.get_held_shares(TEST_SESSION_ID)
        self.sim.sell_shares(
            1, 0, np.float32(TEST_USER_ID), np.float32(5000.0), "v",
            TEST_SESSION_ID, self.TEST_TICKER_IDX
        )  #aapl test stock has no purchases made ever for test session.
        post_share_num = self.db.get_held_shares(TEST_SESSION_ID)
        self.assertTrue(pre_share_num == post_share_num)
        print("Success")

    #Test sell shares reward function.
    def test_sell_all_stocks(self):
        print("testing historical sim for regular sell all")
        pre_share_num = self.db.get_held_shares(TEST_SESSION_ID)
        self.sim.buy_shares(1, np.float32(TEST_USER_ID), np.float32(5000.0),
                            "v", TEST_SESSION_ID, self.TEST_TICKER_IDX)
        self.sim.buy_shares(1, np.float32(TEST_USER_ID), np.float32(5000.0),
                            "v", TEST_SESSION_ID, self.TEST_TICKER_IDX)
        self.sim.buy_shares(1, np.float32(TEST_USER_ID), np.float32(5000.0),
                            "v", TEST_SESSION_ID, self.TEST_TICKER_IDX)
        post_share_num = self.db.get_held_shares(TEST_SESSION_ID)
        self.assertTrue(
            pre_share_num + 3 == post_share_num
        )  #buy then sell, no change in vol. Buy is already proven to add +1
        self.sim.sell_all(3, np.float32(TEST_USER_ID), np.float32(5000.0), "v",
                          TEST_SESSION_ID, self.TEST_TICKER_IDX)
        post_share_num = self.db.get_held_shares(TEST_SESSION_ID)
        self.assertTrue(post_share_num == pre_share_num)
        print("Success")

    #Test the state used as input for the RL Agent
    def test_get_state(self):
        print("testing historical get state")
        state = self.sim.get_state(TEST_SESSION_ID,
                                   self.db.get_user(TEST_NAME).bank[0], "v",
                                   self.TEST_TICKER_IDX)
        price = state[0]
        pred = state[1]
        funds = state[2]
        shares = state[3]
        self.assertTrue(isinstance(price, float))
        self.assertTrue(isinstance(pred, float))
        self.assertTrue(isinstance(funds, float))
        self.assertTrue(isinstance(shares, float))
        self.assertTrue(price == self.sim.get_price("v", self.TEST_TICKER_IDX))
        self.assertTrue(
            pred == self.sim.get_predicted_price("v", self.TEST_TICKER_IDX))
        self.assertTrue(funds == self.db.get_user(TEST_NAME).bank[0])
        self.assertTrue(shares == self.db.get_held_shares(TEST_SESSION_ID))
        print("Success")

    #Test get price
    def test_get_price(self):
        print("testing historical get price for all stocks")
        for tick in stocks:
            price = self.sim.get_price(tick, self.TEST_TICKER_IDX)
            self.assertTrue(isinstance(price, float))
            self.assertTrue(price > 0)
        print("Success")

    #Test get pred, statement coverage of pred is obtained here.
    def test_get_pred_price(self):
        print("testing historical get pred price")
        pred1 = self.sim.get_predicted_price("v", self.TEST_TICKER_IDX)
        self.assertTrue(isinstance(pred1, int))
        self.assertTrue(pred1 == 1)

        #I manually found this ticker idx that returns pred of 0. Dont change please.
        pred2 = self.sim.get_predicted_price("v", self.TEST_TICKER_IDX + 119)
        self.assertTrue(isinstance(pred2, int))
        self.assertTrue(pred2 == 0)
        print("Success")