def testTicksDataWith3StocksInSameSeconds(self):
        ticks_data = [None, None, None]
        dt1 = datetime.strptime("2014-05-11 19:15:23.009", DataType.DATE_TIME_FORMAT_LONG_MILLISECS)
        dt2 = datetime.strptime("2014-05-11 19:15:23.010", DataType.DATE_TIME_FORMAT_LONG_MILLISECS)

        tick_series = ticks_data[1]
        tick_series, is_replacement = RunHFTModel.append_tick_data_to_series(dt1, 20, tick_series)
        tick_series, is_replacement = RunHFTModel.append_tick_data_to_series(dt2, 21, tick_series)
        ticks_data[1] = tick_series

        expected = [None, np.array([[  7.35364802e+05,   21]]), None]
        self.check_array(expected, ticks_data, 3)
    def testTicksDataWith3StocksInSameSeconds(self):
        ticks_data = [None, None, None]
        dt1 = datetime.strptime("2014-05-11 19:15:23.009",
                                DataType.DATE_TIME_FORMAT_LONG_MILLISECS)
        dt2 = datetime.strptime("2014-05-11 19:15:23.010",
                                DataType.DATE_TIME_FORMAT_LONG_MILLISECS)

        tick_series = ticks_data[1]
        tick_series, is_replacement = RunHFTModel.append_tick_data_to_series(
            dt1, 20, tick_series)
        tick_series, is_replacement = RunHFTModel.append_tick_data_to_series(
            dt2, 21, tick_series)
        ticks_data[1] = tick_series

        expected = [None, np.array([[7.35364802e+05, 21]]), None]
        self.check_array(expected, ticks_data, 3)
    def testCalculateParamsSTAndLTBetasOn2Stocks(self):
        stocks_data = []

        test_stock_data1 = StockData(create_stock_contract(self.stock))
        test_stock_data1.set_is_storing_long_term()
        test_stock_data1.add_historical_data_point(50, self.dt_1)
        test_stock_data1.add_historical_data_point(60, self.dt_2)
        test_stock_data1.set_finished_storing()
        test_stock_data1.set_is_storing_short_term()
        test_stock_data1.add_historical_data_point(20, self.dt_1)
        test_stock_data1.add_historical_data_point(30, self.dt_2)
        stocks_data.append(test_stock_data1)

        test_stock_data2 = StockData(self.test_contract)
        test_stock_data2.set_is_storing_long_term()
        test_stock_data2.add_historical_data_point(10, self.dt_1)
        test_stock_data2.add_historical_data_point(12, self.dt_2)
        test_stock_data2.set_finished_storing()
        test_stock_data2.set_is_storing_short_term()
        test_stock_data2.add_historical_data_point(40, self.dt_1)
        test_stock_data2.add_historical_data_point(60, self.dt_2)
        stocks_data.append(test_stock_data2)

        RunHFTModel.calculate_params(stocks_data)
    def testCalculateParamsSTAndLTBetasOn2Stocks(self):
        stocks_data = []

        test_stock_data1 = StockData(create_stock_contract(self.stock))
        test_stock_data1.set_is_storing_long_term()
        test_stock_data1.add_historical_data_point(50, self.dt_1)
        test_stock_data1.add_historical_data_point(60, self.dt_2)
        test_stock_data1.set_finished_storing()
        test_stock_data1.set_is_storing_short_term()
        test_stock_data1.add_historical_data_point(20, self.dt_1)
        test_stock_data1.add_historical_data_point(30, self.dt_2)
        stocks_data.append(test_stock_data1)

        test_stock_data2 = StockData(self.test_contract)
        test_stock_data2.set_is_storing_long_term()
        test_stock_data2.add_historical_data_point(10, self.dt_1)
        test_stock_data2.add_historical_data_point(12, self.dt_2)
        test_stock_data2.set_finished_storing()
        test_stock_data2.set_is_storing_short_term()
        test_stock_data2.add_historical_data_point(40, self.dt_1)
        test_stock_data2.add_historical_data_point(60, self.dt_2)
        stocks_data.append(test_stock_data2)

        RunHFTModel.calculate_params(stocks_data)
    def testTruncateTickSeries(self):
        test_stock_data1 = StockData(create_stock_contract(self.stock))
        test_stock_data1.set_is_storing_short_term()
        test_stock_data1.add_historical_data_point(50, "20140511  19:15:24")
        test_stock_data1.add_historical_data_point(60, "20140511  19:15:25")
        test_stock_data1.add_historical_data_point(70, "20140511  19:15:26")
        test_stock_data1.add_historical_data_point(80, "20140511  19:15:27")
        test_stock_data1.add_historical_data_point(90, "20140511  19:15:28")
        test_stock_data1.add_historical_data_point(100, "20140511  19:15:29")
        test_stock_data1.set_finished_storing()

        length = 5
        chart_ds = test_stock_data1.get_historical_short_term_chart_data_set()
        tick_series = chart_ds.get_prices()
        new_tick_series = RunHFTModel.truncate_tick_series(tick_series, length)

        expected = [60, 70, 80, 90, 100]
        self.check_array(expected, new_tick_series)
    def testTruncateTickSeries(self):
        test_stock_data1 = StockData(create_stock_contract(self.stock))
        test_stock_data1.set_is_storing_short_term()
        test_stock_data1.add_historical_data_point(50, "20140511  19:15:24")
        test_stock_data1.add_historical_data_point(60, "20140511  19:15:25")
        test_stock_data1.add_historical_data_point(70, "20140511  19:15:26")
        test_stock_data1.add_historical_data_point(80, "20140511  19:15:27")
        test_stock_data1.add_historical_data_point(90, "20140511  19:15:28")
        test_stock_data1.add_historical_data_point(100, "20140511  19:15:29")
        test_stock_data1.set_finished_storing()

        length = 5
        chart_ds = test_stock_data1.get_historical_short_term_chart_data_set()
        tick_series = chart_ds.get_prices()
        new_tick_series = RunHFTModel.truncate_tick_series(tick_series, length)

        expected = [60, 70, 80, 90, 100]
        self.check_array(expected, new_tick_series)
 def testAIsOverBought(self):
     fairPrices = [50, 40 ,30]
     result = RunHFTModel.get_is_overbought_or_oversold(fairPrices)
     expected = True, False
     self.assertEqual(expected, result)
 def testAIsOverSold(self):
     fairPrices = [50, 60 ,70]
     result = RunHFTModel.get_is_overbought_or_oversold(fairPrices)
     expected = False, True
     self.assertEqual(expected, result)
 def testAIsNeitherOverboughtOrOversold(self):
     fairPrices = [50, 60 ,30]
     result = RunHFTModel.get_is_overbought_or_oversold(fairPrices)
     expected = False, False
     self.assertEqual(expected, result)
    def testBridgeBootstrap(self):
        # Store historical ticks
        test_stock_data1 = StockData(create_stock_contract(self.stock))
        test_stock_data1.set_is_storing_short_term()
        test_stock_data1.add_historical_data_point(50, "20140511  19:15:24")
        test_stock_data1.add_historical_data_point(60, "20140511  19:15:25")
        test_stock_data1.set_finished_storing()

        stocks_data = [test_stock_data1]

        # Store incoming ticks
        p1 = 20
        p2 = 21
        p3 = 22
        p4 = 23
        p5 = 24

        dt1 = datetime.strptime("2014-05-11 19:15:23", DataType.DATE_TIME_FORMAT_LONG)
        dt2 = datetime.strptime("2014-05-11 19:15:24", DataType.DATE_TIME_FORMAT_LONG)
        dt3 = datetime.strptime("2014-05-11 19:15:25", DataType.DATE_TIME_FORMAT_LONG)
        dt4 = datetime.strptime("2014-05-11 19:15:26", DataType.DATE_TIME_FORMAT_LONG)
        dt5 = datetime.strptime("2014-05-11 19:15:27", DataType.DATE_TIME_FORMAT_LONG)

        tick_1 = np.array([dates.date2num(dt1), p1])
        tick_2 = np.array([dates.date2num(dt2), p2])
        tick_3 = np.array([dates.date2num(dt3), p3])
        tick_4 = np.array([dates.date2num(dt4), p4])
        tick_5 = np.array([dates.date2num(dt5), p5])

        # Create test ticks data.
        tick_series = np.array(tick_1)
        tick_series = np.vstack([tick_series, tick_2])
        tick_series = np.vstack([tick_series, tick_3])
        tick_series = np.vstack([tick_series, tick_4])
        tick_series = np.vstack([tick_series, tick_5])
        ticks_data = [tick_series]

        # Bridge ticks and retrieve results
        end_time = datetime.strptime("2014-05-11 19:15:35", DataType.DATE_TIME_FORMAT_LONG)
        RunHFTModel.bridge_historical_and_present_ticks(stocks_data, ticks_data, end_time)
        chart_ds = stocks_data[0].get_historical_short_term_chart_data_set()

        # Test for prices
        result_prices = chart_ds.get_prices()
        expected_prices = [50, 60, 23, 24,
                           24, 24, 24, 24,
                           24, 24, 24, 24]

        self.check_array(expected_prices, result_prices)
        # Test date labels
        expected_datelabels = ['20140511 19:15:24'
                                , '20140511 19:15:25'
                                , '20140511 19:15:26'
                                , '20140511 19:15:27'
                                , '20140511 19:15:28'
                                , '20140511 19:15:29'
                                , '20140511 19:15:30'
                                , '20140511 19:15:31'
                                , '20140511 19:15:32'
                                , '20140511 19:15:33'
                                , '20140511 19:15:34'
                                , '20140511 19:15:35'
                                ]
        result_datelabels = chart_ds.get_dates_labels()
        self.check_array(expected_datelabels, result_datelabels)
 def testAIsOverBought(self):
     fairPrices = [50, 40, 30]
     result = RunHFTModel.get_is_overbought_or_oversold(fairPrices)
     expected = True, False
     self.assertEqual(expected, result)
 def testAIsOverSold(self):
     fairPrices = [50, 60, 70]
     result = RunHFTModel.get_is_overbought_or_oversold(fairPrices)
     expected = False, True
     self.assertEqual(expected, result)
 def testAIsNeitherOverboughtOrOversold(self):
     fairPrices = [50, 60, 30]
     result = RunHFTModel.get_is_overbought_or_oversold(fairPrices)
     expected = False, False
     self.assertEqual(expected, result)
    def testBridgeBootstrap(self):
        # Store historical ticks
        test_stock_data1 = StockData(create_stock_contract(self.stock))
        test_stock_data1.set_is_storing_short_term()
        test_stock_data1.add_historical_data_point(50, "20140511  19:15:24")
        test_stock_data1.add_historical_data_point(60, "20140511  19:15:25")
        test_stock_data1.set_finished_storing()

        stocks_data = [test_stock_data1]

        # Store incoming ticks
        p1 = 20
        p2 = 21
        p3 = 22
        p4 = 23
        p5 = 24

        dt1 = datetime.strptime("2014-05-11 19:15:23",
                                DataType.DATE_TIME_FORMAT_LONG)
        dt2 = datetime.strptime("2014-05-11 19:15:24",
                                DataType.DATE_TIME_FORMAT_LONG)
        dt3 = datetime.strptime("2014-05-11 19:15:25",
                                DataType.DATE_TIME_FORMAT_LONG)
        dt4 = datetime.strptime("2014-05-11 19:15:26",
                                DataType.DATE_TIME_FORMAT_LONG)
        dt5 = datetime.strptime("2014-05-11 19:15:27",
                                DataType.DATE_TIME_FORMAT_LONG)

        tick_1 = np.array([dates.date2num(dt1), p1])
        tick_2 = np.array([dates.date2num(dt2), p2])
        tick_3 = np.array([dates.date2num(dt3), p3])
        tick_4 = np.array([dates.date2num(dt4), p4])
        tick_5 = np.array([dates.date2num(dt5), p5])

        # Create test ticks data.
        tick_series = np.array(tick_1)
        tick_series = np.vstack([tick_series, tick_2])
        tick_series = np.vstack([tick_series, tick_3])
        tick_series = np.vstack([tick_series, tick_4])
        tick_series = np.vstack([tick_series, tick_5])
        ticks_data = [tick_series]

        # Bridge ticks and retrieve results
        end_time = datetime.strptime("2014-05-11 19:15:35",
                                     DataType.DATE_TIME_FORMAT_LONG)
        RunHFTModel.bridge_historical_and_present_ticks(
            stocks_data, ticks_data, end_time)
        chart_ds = stocks_data[0].get_historical_short_term_chart_data_set()

        # Test for prices
        result_prices = chart_ds.get_prices()
        expected_prices = [50, 60, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24]

        self.check_array(expected_prices, result_prices)
        # Test date labels
        expected_datelabels = [
            '20140511 19:15:24', '20140511 19:15:25', '20140511 19:15:26',
            '20140511 19:15:27', '20140511 19:15:28', '20140511 19:15:29',
            '20140511 19:15:30', '20140511 19:15:31', '20140511 19:15:32',
            '20140511 19:15:33', '20140511 19:15:34', '20140511 19:15:35'
        ]
        result_datelabels = chart_ds.get_dates_labels()
        self.check_array(expected_datelabels, result_datelabels)