def get_signal(self, start_time, end_time, interval): signal = Signal(self.ticker_symbol) one_day = timedelta(days=1) five_days = timedelta(days=5) one_week = timedelta(days=7) one_month = timedelta(days=30) one_quarter = timedelta(days=90) if interval <= one_day: interval_str = "1d" elif interval <= five_days: interval_str = "5d" elif interval <= one_week: interval_str = "1wk" elif interval <= one_month: interval_str = "1mo" elif interval <= one_quarter: interval_str = "3mo" else: interval_str = "1mo" df = self.ticker.history( interval=interval_str, start=start_time, end=end_time) for date, values in df.iterrows(): closing_price = values['Close'] if math.isnan(closing_price): continue signal.add_data_point(Signal.DataPoint( datetime.fromisoformat(str(date)), closing_price)) return signal
def get_signal(self, start_time, end_time, interval): signal = Signal("Signal " + str(self.signals_produced)) self.signals_produced += 1 time = start_time value = 0 while time != (end_time + interval): signal.add_data_point(Signal.DataPoint(time, value)) value += random.uniform(10.0, 1000.0) time += interval return signal
def test_get_max_value(self): raw_signal = self.construct_test_signal() some_time = datetime.now() some_value = 1337 some_data_point = Signal.DataPoint(some_time, some_value) raw_signal.add_data_point(some_data_point) some_time = datetime.now() some_value = 42 some_data_point = Signal.DataPoint(some_time, some_value) raw_signal.add_data_point(some_data_point) self.assertEqual(raw_signal.get_max_value(), 1337)
def test_add_data_points(self): raw_signal = self.construct_test_signal() some_time = datetime.now() some_value = 1337 some_data_point = Signal.DataPoint(some_time, some_value) raw_signal.add_data_point(some_data_point) raw_signal.add_data_point(some_data_point) raw_signal.add_data_point(some_data_point)
def test_construction(self): some_time = datetime.now() some_value = 1337 Signal.DataPoint(some_time, some_value)
def construct_test_signal(self): return Signal("Some Signal")
def test_interpolation(self): signal = Signal("Some Signal") first_time = datetime.fromisoformat("2019-01-01") first_value = 0.0 first_data_point = Signal.DataPoint(first_time, first_value) signal.add_data_point(first_data_point) second_time = datetime.fromisoformat("2020-01-01") second_value = 1.0 second_data_point = Signal.DataPoint(second_time, second_value) signal.add_data_point(first_data_point) signal.add_data_point(second_data_point) query_time = datetime.fromisoformat("2019-07-01") interpolated_data = signal.get_value(query_time) self.assertAlmostEqual(interpolated_data, 0.5, places=2)