amor71 / LiuAlgoTrader

Framework for algorithmic trading
MIT License
784 stars 129 forks source link

Rename this here and in `crypto_get_symbol_data` and `get_symbols_data` #387

Closed github-actions[bot] closed 1 year ago

github-actions[bot] commented 1 year ago

Rename this here and in crypto_get_symbol_data and get_symbols_data

https://github.com/amor71/LiuAlgoTrader/blob/68941bc93fc23564f0101cfee7944ba2620e92f3/liualgotrader/data/alpaca.py#L275


    def crypto_get_symbol_data(
        self,
        symbol: str,
        start: datetime,
        end: datetime,
        timeframe: TimeFrame,
    ) -> pd.DataFrame:
        start = start.astimezone(tz=pytz.utc)
        end = end.astimezone(tz=pytz.utc)

        data = self.alpaca_crypto_data.get_crypto_bars(  # type:ignore
            CryptoBarsRequest(
                symbol_or_symbols=symbol,
                start=start,
                end=end,
                timeframe=timeframe,
            )
        ).df

        data = data.reset_index(level=0, drop=True)
        data = data.tz_convert("America/New_York")
        return self._extracted_from_get_symbols_data_14(data)

    def get_symbols_data(
        self,
        symbols: List[str],
        start: datetime,
        end: datetime,
        scale: TimeScale = TimeScale.minute,
    ) -> Dict[str, pd.DataFrame]:
        assert isinstance(symbols, list), f"{symbols} must be a list"

        start = start.astimezone(tz=pytz.utc)
        end = end.astimezone(tz=pytz.utc)

        t: TimeFrame = (
            TimeFrame(1, TimeFrameUnit.Minute)
            if scale == TimeScale.minute
            else TimeFrame(1, TimeFrameUnit.Day)
        )

        data = self.alpaca_stock_data.get_stock_bars(  # type:ignore
            StockBarsRequest(
                start=start,
                end=end,
                timeframe=t,
                symbol_or_symbols=symbols,
                adjustment=Adjustment.ALL,
            )
        ).df

        data = data.tz_convert("America/New_York", level=1)
        return self._extracted_from_get_symbols_data_14(data)

    # TODO Rename this here and in `crypto_get_symbol_data` and `get_symbols_data`
    def _extracted_from_get_symbols_data_14(self, data):
        data.rename(columns={"vwap": "average"}, inplace=True)
        data.rename(columns={"trade_count": "count"}, inplace=True)
        data["vwap"] = np.NaN
        return data

    def get_symbol_data(
        self,
        symbol: str,
        start: datetime,
        end: datetime,
        scale: TimeScale = TimeScale.minute,
    ) -> pd.DataFrame:
        t: TimeFrame = (
            TimeFrame(1, TimeFrameUnit.Minute)
            if scale == TimeScale.minute
            else TimeFrame(1, TimeFrameUnit.Day)
        )
        if scale == TimeScale.day and start.date() == end.date():
            start -= timedelta(days=1)
            end += timedelta(days=1)

        if _is_crypto_symbol(symbol):
            return self.crypto_get_symbol_data(
                symbol=symbol, start=start, end=end, timeframe=t
            )

        start = start.astimezone(tz=pytz.utc)
        end = end.astimezone(tz=pytz.utc)

        if config.detailed_dl_debug_enabled:
            tlog(f"symbol={symbol}, timeframe={t}, range=({start, end})")

        try:
            data = self.alpaca_stock_data.get_stock_bars(  # type:ignore
                StockBarsRequest(
                    start=start,
                    end=end,
                    timeframe=t,
                    symbol_or_symbols=symbol,
                    adjustment=Adjustment.ALL,
                )
            ).df
        except requests.exceptions.HTTPError as e:
            tlog(f"received HTTPError: {e}")
            if e.response.status_code in (500, 502, 504, 429):

d77f64d895f48591d83080a3875e009386766e0b

github-actions[bot] commented 1 year ago

Stale issue message