def collect_candles_for_day(self, day: date, symbol: str) -> Optional[SymbolDay]:
        """
        Uses Polygon to collect candles for the given day.
        Does NOT save the newly-collected candles.
        """
        candles = None
        try:
            candles = self._parse_ticks_in_intervals(symbol, [[
                datetime.combine(day, OPEN_TIME),
                datetime.combine(day, CLOSE_TIME)
            ]])
        except Exception as e:
            self.debug_msgs.append(f'Error collecting {symbol} candles from polygon for {day:%m-%d-%Y}:')
            self.debug_msgs.append(traceback.format_exc())

        return SymbolDay(symbol, day, candles)
Example #2
0
 def create_dummy_day(self, symbol: str, day_date: date,
                      num_candles: int) -> SymbolDay:
     """Creates a SymbolDay with mock price data."""
     dummy_candles = []
     dummy_moment = datetime.combine(day_date, OPEN_TIME)
     for i in range(num_candles):
         dummy_candles.append(
             Candle(
                 moment=dummy_moment,
                 open=0.001,
                 high=0.001,
                 low=0.001,
                 close=0.001,
                 volume=999,
             ))
         dummy_moment += timedelta(seconds=1)
     return SymbolDay(symbol, day_date, dummy_candles)
Example #3
0
    def _train_on_stream_data(self, symbol: str, day_date: date) -> bool:
        """
        Loads cached polygon-stream data and trains the symbol on it.
        Returns False if the data is invalid; True otherwise.
        """
        # Validate polygon-stream data
        stream_data = SymbolDay(symbol=symbol,
                                day_date=day_date,
                                candles=self.redis().get_cached_candles(
                                    symbol=symbol, day_date=day_date))
        if not SymbolDay.validate_candles(stream_data.candles):
            return False

        # Train models on polygon-stream data
        self.model_feeder.train_models(symbol=symbol,
                                       day_date=day_date,
                                       day_data=stream_data,
                                       stable=True)
        return True