Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Polygon.io Backtesting example is failing #256

Open
c0indev3l opened this issue Jul 14, 2023 · 5 comments
Open

Polygon.io Backtesting example is failing #256

c0indev3l opened this issue Jul 14, 2023 · 5 comments

Comments

@c0indev3l
Copy link

Hello,

from datetime import datetime
from lumibot.backtesting import PolygonDataBacktesting
from lumibot.strategies import Strategy

class MyStrategy(Strategy):
    parameters = {
        "symbol": "AAPL",
    }

    def initialize(self):
        self.sleeptime = "1D"

    def on_trading_iteration(self):
        if self.first_iteration:
            symbol = self.parameters["symbol"]
            price = self.get_last_price(symbol)
            qty = self.portfolio_value / price
            order = self.create_order(symbol, quantity=qty, side="buy")
            self.submit_order(order)

if __name__ == "__main__":
    backtesting_start = datetime(2023, 1, 1)
    backtesting_end = datetime(2023, 5, 1)

    MyStrategy.backtest(
        PolygonDataBacktesting,
        backtesting_start,
        backtesting_end,
        polygon_api_key="YOUR_POLYGON_API_KEY",
        polygon_has_paid_subscription=False,
    )

raises

>python bot.py
Starting backtest for MyStrategy...
ERROR:root:Error getting the risk free rate: 401 Client Error: Unauthorized for url: https://query2.finance.yahoo.com/v10/finance/quoteSummary/%5EIRX?modules=summaryProfile%2CfinancialData%2CquoteType%2CdefaultKeyStatistics%2CassetProfile%2CsummaryDetail&ssl=true
INFO:backtest_stats:Starting backtest...
Progress |█--------------------------------------------------------|   2.00%  [Elapsed: 0:00:03 ETA: 0:02:33]
Getting pricing data for AAPL from Polygon...
2023-07-14 16:37:06,634: root: ERROR: unsupported operand type(s) for /: 'float' and 'NoneType'
2023-07-14 16:37:06,637: root: ERROR: Traceback (most recent call last):
  File "C:\Users\w4c\AppData\Roaming\Python\Python310\site-packages\lumibot\strategies\strategy_executor.py", line 570, in run
    self._run_trading_session()
  File "C:\Users\w4c\AppData\Roaming\Python\Python310\site-packages\lumibot\strategies\strategy_executor.py", line 538, in _run_trading_session
    self._on_trading_iteration()
  File "C:\Users\w4c\AppData\Roaming\Python\Python310\site-packages\lumibot\strategies\strategy_executor.py", line 261, in func_output
    result = func_input(self, *args, **kwargs)
  File "C:\Users\w4c\AppData\Roaming\Python\Python310\site-packages\lumibot\strategies\strategy_executor.py", line 285, in func_output
    result = func_input(self, *args, **kwargs)
  File "C:\Users\w4c\AppData\Roaming\Python\Python310\site-packages\lumibot\strategies\strategy_executor.py", line 69, in func_output
    return func_input(self, *args, **kwargs)
  File "C:\Users\w4c\AppData\Roaming\Python\Python310\site-packages\lumibot\strategies\strategy_executor.py", line 339, in _on_trading_iteration
    on_trading_iteration()
  File "C:\Users\w4c\AppData\Roaming\Python\Python310\site-packages\lumibot\tools\decorators.py", line 62, in func_output
    frame, result = call_function_get_frame(func_input, *args, **kwargs)
  File "C:\Users\w4c\AppData\Roaming\Python\Python310\site-packages\lumibot\tools\decorators.py", line 30, in call_function_get_frame
    result = func(*args, **kwargs)
  File "C:\Users\w4c\trading\lumibot\bot.py", line 17, in on_trading_iteration
    qty = self.portfolio_value / price
TypeError: unsupported operand type(s) for /: 'float' and 'NoneType'

after changing API key I run into an other problem...

>python bot.py
Starting backtest for MyStrategy...
ERROR:root:Error getting the risk free rate: 401 Client Error: Unauthorized for url: https://query2.finance.yahoo.com/v10/finance/quoteSummary/%5EIRX?modules=summaryProfile%2CfinancialData%2CquoteType%2CdefaultKeyStatistics%2CassetProfile%2CsummaryDetail&ssl=true
INFO:backtest_stats:Starting backtest...
Progress |█--------------------------------------------------------|   2.00%  [Elapsed: 0:00:03 ETA: 0:02:27]
Getting pricing data for AAPL from Polygon...

Sleeping 60 seconds getting pricing data for AAPL from Polygon because we don't have a paid subscription and we don't want to hit the rate limit. If you want to avoid this, you can get a paid subscription at https://polygon.io/pricing and set `polygon_has_paid_subscription=True` when starting the backtest.

Progress |█████████████████████████████████████████████████████████| 100.00%  [Elapsed: 0:01:08 ETA: 0:00:00]2023-07-14 16:40:00,490: root: INFO: MyStrategy : --- MyStrategy Strategy Performance  ---
2023-07-14 16:40:00,509: root: INFO: MyStrategy : Total Return: 30.47%
2023-07-14 16:40:00,509: root: INFO: MyStrategy : CAGR 132.76%
2023-07-14 16:40:00,510: root: INFO: MyStrategy : Volatility 23.27%
2023-07-14 16:40:00,510: root: INFO: MyStrategy : Sharpe 5.71
2023-07-14 16:40:00,510: root: INFO: MyStrategy : Max Drawdown 6.43% on 2023-03-02
2023-07-14 16:40:00,511: root: INFO: MyStrategy : RoMaD 2,065.44%
2023-07-14 16:40:00,511: root: INFO: MyStrategy : --- SPY Benchmark Performance ---

Getting pricing data for SPY from Polygon...

Sleeping 60 seconds getting pricing data for SPY from Polygon because we don't have a paid subscription and we don't want to hit the rate limit. If you want to avoid this, you can get a paid subscription at https://polygon.io/pricing and set `polygon_has_paid_subscription=True` when starting the backtest.

2023-07-14 16:41:00,900: root: ERROR: Error getting bars for SPY: You are requesting minute data from a daily data source. This is not supported.
2023-07-14 16:41:00,905: root: ERROR: Traceback (most recent call last):
  File "C:\Users\w4c\AppData\Roaming\Python\Python310\site-packages\lumibot\data_sources\pandas_data.py", line 318, in _pull_source_symbol_bars_between_dates
    res = data.get_bars_between_dates(
  File "C:\Users\w4c\AppData\Roaming\Python\Python310\site-packages\lumibot\entities\data.py", line 577, in get_bars_between_dates
    raise ValueError(
ValueError: You are requesting minute data from a daily data source. This is not supported.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\w4c\AppData\Roaming\Python\Python310\site-packages\lumibot\strategies\strategy_executor.py", line 587, in run
    self._on_strategy_end()
  File "C:\Users\w4c\AppData\Roaming\Python\Python310\site-packages\lumibot\strategies\strategy_executor.py", line 261, in func_output
    result = func_input(self, *args, **kwargs)
  File "C:\Users\w4c\AppData\Roaming\Python\Python310\site-packages\lumibot\strategies\strategy_executor.py", line 360, in _on_strategy_end
    self.strategy._dump_stats()
  File "C:\Users\w4c\AppData\Roaming\Python\Python310\site-packages\lumibot\strategies\_strategy.py", line 569, in _dump_stats
    bars = self.data_source.get_historical_prices_between_dates(
  File "C:\Users\w4c\AppData\Roaming\Python\Python310\site-packages\lumibot\backtesting\polygon_backtesting.py", line 191, in get_historical_prices_between_dates
    response = super()._pull_source_symbol_bars_between_dates(
  File "C:\Users\w4c\AppData\Roaming\Python\Python310\site-packages\lumibot\data_sources\pandas_data.py", line 323, in _pull_source_symbol_bars_between_dates
    raise ValueError(f"Error getting bars for {asset}: {e}")
ValueError: Error getting bars for SPY: You are requesting minute data from a daily data source. This is not supported.

2023-07-14 16:41:00,907: root: INFO: MyStrategy : Executing the on_bot_crash event method
2023-07-14 16:41:00,916: root: INFO: MyStrategy : --- MyStrategy Strategy Performance  ---
2023-07-14 16:41:00,932: root: INFO: MyStrategy : Total Return: 30.47%
2023-07-14 16:41:00,932: root: INFO: MyStrategy : CAGR 132.76%
2023-07-14 16:41:00,932: root: INFO: MyStrategy : Volatility 23.27%
2023-07-14 16:41:00,933: root: INFO: MyStrategy : Sharpe 5.71
2023-07-14 16:41:00,933: root: INFO: MyStrategy : Max Drawdown 6.43% on 2023-03-02
2023-07-14 16:41:00,933: root: INFO: MyStrategy : RoMaD 2,065.44%
2023-07-14 16:41:00,934: root: INFO: MyStrategy : --- SPY Benchmark Performance ---
Exception in thread MyStrategy:
Traceback (most recent call last):
  File "C:\Users\w4c\AppData\Roaming\Python\Python310\site-packages\lumibot\data_sources\pandas_data.py", line 318, in _pull_source_symbol_bars_between_dates
    res = data.get_bars_between_dates(
  File "C:\Users\w4c\AppData\Roaming\Python\Python310\site-packages\lumibot\entities\data.py", line 577, in get_bars_between_dates
    raise ValueError(
ValueError: You are requesting minute data from a daily data source. This is not supported.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\w4c\AppData\Roaming\Python\Python310\site-packages\lumibot\strategies\strategy_executor.py", line 587, in run
    self._on_strategy_end()
  File "C:\Users\w4c\AppData\Roaming\Python\Python310\site-packages\lumibot\strategies\strategy_executor.py", line 261, in func_output
    result = func_input(self, *args, **kwargs)
  File "C:\Users\w4c\AppData\Roaming\Python\Python310\site-packages\lumibot\strategies\strategy_executor.py", line 360, in _on_strategy_end
    self.strategy._dump_stats()
  File "C:\Users\w4c\AppData\Roaming\Python\Python310\site-packages\lumibot\strategies\_strategy.py", line 569, in _dump_stats
    bars = self.data_source.get_historical_prices_between_dates(
  File "C:\Users\w4c\AppData\Roaming\Python\Python310\site-packages\lumibot\backtesting\polygon_backtesting.py", line 191, in get_historical_prices_between_dates
    response = super()._pull_source_symbol_bars_between_dates(
  File "C:\Users\w4c\AppData\Roaming\Python\Python310\site-packages\lumibot\data_sources\pandas_data.py", line 323, in _pull_source_symbol_bars_between_dates
    raise ValueError(f"Error getting bars for {asset}: {e}")
ValueError: Error getting bars for SPY: You are requesting minute data from a daily data source. This is not supported.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\w4c\AppData\Roaming\Python\Python310\site-packages\lumibot\data_sources\pandas_data.py", line 318, in _pull_source_symbol_bars_between_dates
    res = data.get_bars_between_dates(
  File "C:\Users\w4c\AppData\Roaming\Python\Python310\site-packages\lumibot\entities\data.py", line 577, in get_bars_between_dates
    raise ValueError(
ValueError: You are requesting minute data from a daily data source. This is not supported.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\tools\Anaconda3\lib\threading.py", line 1016, in _bootstrap_inner
    self.run()
  File "C:\Users\w4c\AppData\Roaming\Python\Python310\site-packages\lumibot\strategies\strategy_executor.py", line 591, in run
    self._on_bot_crash(e)
  File "C:\Users\w4c\AppData\Roaming\Python\Python310\site-packages\lumibot\strategies\strategy_executor.py", line 273, in func_output
    result = func_input(self, *args, **kwargs)
  File "C:\Users\w4c\AppData\Roaming\Python\Python310\site-packages\lumibot\strategies\strategy_executor.py", line 371, in _on_bot_crash
    self.strategy._dump_stats()
  File "C:\Users\w4c\AppData\Roaming\Python\Python310\site-packages\lumibot\strategies\_strategy.py", line 569, in _dump_stats
    bars = self.data_source.get_historical_prices_between_dates(
  File "C:\Users\w4c\AppData\Roaming\Python\Python310\site-packages\lumibot\backtesting\polygon_backtesting.py", line 191, in get_historical_prices_between_dates
    response = super()._pull_source_symbol_bars_between_dates(
  File "C:\Users\w4c\AppData\Roaming\Python\Python310\site-packages\lumibot\data_sources\pandas_data.py", line 323, in _pull_source_symbol_bars_between_dates
    raise ValueError(f"Error getting bars for {asset}: {e}")
ValueError: Error getting bars for SPY: You are requesting minute data from a daily data source. This is not supported.
2023-07-14 16:41:00,959: backtest_stats: INFO: Backtest took 0:02:08.247696 for a speed of 0.000
2023-07-14 16:41:00,962: root: WARNING: Cannot plot returns because the benchmark returns are missing
Creating indicators plot...
Creating tearsheet...
Traceback (most recent call last):
  File "C:\Users\w4c\trading\lumibot\bot.py", line 25, in <module>
    MyStrategy.backtest(
  File "C:\Users\w4c\AppData\Roaming\Python\Python310\site-packages\lumibot\strategies\_strategy.py", line 1049, in backtest
    strategy.tearsheet(
  File "C:\Users\w4c\AppData\Roaming\Python\Python310\site-packages\lumibot\strategies\_strategy.py", line 695, in tearsheet
    create_tearsheet(
  File "C:\Users\w4c\AppData\Roaming\Python\Python310\site-packages\lumibot\tools\indicators.py", line 642, in create_tearsheet
    _df2 = df2.copy()
AttributeError: 'NoneType' object has no attribute 'copy'

Kind regards

@c0indev3l c0indev3l changed the title Polygon.io Backtesting is failing Polygon.io Backtesting example is failing Jul 14, 2023
@c0indev3l
Copy link
Author

c0indev3l commented Jul 14, 2023

Moreover, in such a case requests doesn't seems to be cached as after an other run I'm still seeing "Getting pricing data ..."

@grzesir
Copy link
Contributor

grzesir commented Jul 14, 2023 via email

@grzesir
Copy link
Contributor

grzesir commented Jul 14, 2023 via email

@c0indev3l
Copy link
Author

>pip show lumibot
Name: lumibot
Version: 2.7.0
Summary: Backtesting and Trading Library, Made by Lumiwealth
Home-page: https://github.com/Lumiwealth/lumibot
Author: Robert Grzesik
Author-email: rob@lumiwealth.com
License:
Location: c:\users\w4c\appdata\roaming\python\python310\site-packages
Requires: alpaca-trade-api, alpha-vantage, bcrypt, ccxt, email-validator, flask-marshmallow, flask-security, flask-socketio, flask-sqlalchemy, ibapi, jsonpickle, marshmallow-sqlalchemy, matplotlib, pandas, pandas-datareader, pandas-market-calendars, plotly, polygon, pydantic, pytest, quandl, quantstats, scipy, termcolor, yfinance
Required-by:

>pip show yfinance
Name: yfinance
Version: 0.2.22
Summary: Download market data from Yahoo! Finance API
Home-page: https://github.com/ranaroussi/yfinance
Author: Ran Aroussi
Author-email: ran@aroussi.com
License: Apache
Location: c:\users\w4c\appdata\roaming\python\python310\site-packages
Requires: appdirs, beautifulsoup4, frozendict, html5lib, lxml, multitasking, numpy, pandas, pytz, requests
Required-by: lumibot, QuantStats

@grzesir
Copy link
Contributor

grzesir commented Aug 18, 2023

@c0indev3l to fix the yahoo finance issue you should upgrade lumibot to the latest version

for your initial question, did you replace "YOUR_POLYGON_API_KEY" with your actual polygon api key?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants