nicknochnack / MLTradingBot

807 stars 335 forks source link

Issue with version control - lumibot.backtesting not working! #8

Open AAlhendi1707 opened 10 months ago

AAlhendi1707 commented 10 months ago

I was folloing the instructions, I have created conda env and all required installed successfully with no error.

conda create -n trader python=3.10
conda activate trader
pip install lumibot timedelta alpaca-trade-api
pip install torch torchvision torchaudio transformers

I've added my alpaca API key then when I run tradingbot.py hit the following error, which seems to be associated with

(trader) (base) someuser@trading_bot % (trader) % /opt/homebrew/Caskroom/miniconda/base/envs/trader/bin/python /Users
/ahmedalhendi/Dropbox/Mac/Desktop/trading_bot/tradingbot.py
ERROR:yfinance:Failed to get ticker '^IRX' reason: HTTPSConnectionPool(host='fc.yahoo.com', port=443): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x17c5f63e0>: Failed to establish a new connection: [Errno 61] Connection refused'))
ERROR:yfinance:^IRX: No price data found, symbol may be delisted (period=7d)
ERROR:root:Error getting the risk free rate: unsupported operand type(s) for /: 'NoneType' and 'int'
Starting backtest for MLTrader...
ERROR:yfinance:Failed to get ticker '^IRX' reason: HTTPSConnectionPool(host='fc.yahoo.com', port=443): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x17c5f4e20>: Failed to establish a new connection: [Errno 61] Connection refused'))
ERROR:yfinance:^IRX: No price data found, symbol may be delisted (period=7d)
ERROR:root:Error getting the risk free rate: unsupported operand type(s) for /: 'NoneType' and 'int'
INFO:backtest_stats:Starting backtest...
Progress |-----------------------------------------------------------------------------|   0.10%  [Elapsed: 0:00:00 ETA: 0:09:26] 2024-01-30 22:54:08,593: yfinance: ERROR: Failed to get ticker 'SPY' reason: HTTPSConnectionPool(host='fc.yahoo.com', port=443): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x17f659540>: Failed to establish a new connection: [Errno 61] Connection refused'))
2024-01-30 22:54:08,593: yfinance: ERROR: SPY: No timezone found, symbol may be delisted
2024-01-30 22:54:08,597: root: ERROR: YAHOO did not return data for symbol SPY. Make sure this symbol is valid.
2024-01-30 22:54:08,597: root: ERROR: unsupported operand type(s) for /: 'float' and 'NoneType'
2024-01-30 22:54:08,598: root: ERROR: Traceback (most recent call last):
  File "/opt/homebrew/Caskroom/miniconda/base/envs/trader/lib/python3.10/site-packages/lumibot/strategies/strategy_executor.py", line 831, in run
    self._run_trading_session()
  File "/opt/homebrew/Caskroom/miniconda/base/envs/trader/lib/python3.10/site-packages/lumibot/strategies/strategy_executor.py", line 782, in _run_trading_session
    self._on_trading_iteration()
  File "/opt/homebrew/Caskroom/miniconda/base/envs/trader/lib/python3.10/site-packages/lumibot/strategies/strategy_executor.py", line 274, in func_output
    result = func_input(self, *args, **kwargs)
  File "/opt/homebrew/Caskroom/miniconda/base/envs/trader/lib/python3.10/site-packages/lumibot/strategies/strategy_executor.py", line 298, in func_output
    result = func_input(self, *args, **kwargs)
  File "/opt/homebrew/Caskroom/miniconda/base/envs/trader/lib/python3.10/site-packages/lumibot/strategies/strategy_executor.py", line 87, in func_output
    return func_input(self, *args, **kwargs)
  File "/opt/homebrew/Caskroom/miniconda/base/envs/trader/lib/python3.10/site-packages/lumibot/strategies/strategy_executor.py", line 378, in _on_trading_iteration
    on_trading_iteration()
  File "/opt/homebrew/Caskroom/miniconda/base/envs/trader/lib/python3.10/site-packages/lumibot/tools/decorators.py", line 62, in func_output
    frame, result = call_function_get_frame(func_input, *args, **kwargs)
  File "/opt/homebrew/Caskroom/miniconda/base/envs/trader/lib/python3.10/site-packages/lumibot/tools/decorators.py", line 30, in call_function_get_frame
    result = func(*args, **kwargs)
  File "/Users/ahmedalhendi/Dropbox/Mac/Desktop/trading_bot/tradingbot.py", line 49, in on_trading_iteration
    cash, last_price, quantity = self.position_sizing()
  File "/Users/ahmedalhendi/Dropbox/Mac/Desktop/trading_bot/tradingbot.py", line 31, in position_sizing
    quantity = round(cash * self.cash_at_risk / last_price,0)
TypeError: unsupported operand type(s) for /: 'float' and 'NoneType'

Exception in thread MLTrader:
Traceback (most recent call last):
  File "/opt/homebrew/Caskroom/miniconda/base/envs/trader/lib/python3.10/site-packages/lumibot/strategies/strategy_executor.py", line 831, in run
    self._run_trading_session()
  File "/opt/homebrew/Caskroom/miniconda/base/envs/trader/lib/python3.10/site-packages/lumibot/strategies/strategy_executor.py", line 782, in _run_trading_session
    self._on_trading_iteration()
  File "/opt/homebrew/Caskroom/miniconda/base/envs/trader/lib/python3.10/site-packages/lumibot/strategies/strategy_executor.py", line 274, in func_output
    result = func_input(self, *args, **kwargs)
  File "/opt/homebrew/Caskroom/miniconda/base/envs/trader/lib/python3.10/site-packages/lumibot/strategies/strategy_executor.py", line 298, in func_output
    result = func_input(self, *args, **kwargs)
  File "/opt/homebrew/Caskroom/miniconda/base/envs/trader/lib/python3.10/site-packages/lumibot/strategies/strategy_executor.py", line 87, in func_output
    return func_input(self, *args, **kwargs)
  File "/opt/homebrew/Caskroom/miniconda/base/envs/trader/lib/python3.10/site-packages/lumibot/strategies/strategy_executor.py", line 378, in _on_trading_iteration
    on_trading_iteration()
  File "/opt/homebrew/Caskroom/miniconda/base/envs/trader/lib/python3.10/site-packages/lumibot/tools/decorators.py", line 62, in func_output
    frame, result = call_function_get_frame(func_input, *args, **kwargs)
  File "/opt/homebrew/Caskroom/miniconda/base/envs/trader/lib/python3.10/site-packages/lumibot/tools/decorators.py", line 30, in call_function_get_frame
    result = func(*args, **kwargs)
  File "/Users/ahmedalhendi/Dropbox/Mac/Desktop/trading_bot/tradingbot.py", line 49, in on_trading_iteration
    cash, last_price, quantity = self.position_sizing() 
  File "/Users/ahmedalhendi/Dropbox/Mac/Desktop/trading_bot/tradingbot.py", line 31, in position_sizing
    quantity = round(cash * self.cash_at_risk / last_price,0)
TypeError: unsupported operand type(s) for /: 'float' and 'NoneType'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/opt/homebrew/Caskroom/miniconda/base/envs/trader/lib/python3.10/threading.py", line 1016, in _bootstrap_inner
    self.run()
  File "/opt/homebrew/Caskroom/miniconda/base/envs/trader/lib/python3.10/site-packages/lumibot/strategies/strategy_executor.py", line 844, in run
    raise RuntimeError("Exception encountered, stopping BackTest.") from e
RuntimeError: Exception encountered, stopping BackTest.

Creating indicators plot...

After that I tried to track down the issue so I started with example here on https://lumibot.lumiwealth.com/backtesting.yahoo.html

from datetime import datetime

from lumibot.backtesting import BacktestingBroker, YahooDataBacktesting
from lumibot.strategies import Strategy
from lumibot.traders import Trader

# A simple strategy that buys AAPL on the first day
class MyStrategy(Strategy):
    def on_trading_iteration(self):
        if self.first_iteration:
            aapl_price = self.get_last_price("AAPL")
            quantity = self.portfolio_value // aapl_price
            order = self.create_order("AAPL", quantity, "buy")
            self.submit_order(order)

# Pick the dates that you want to start and end your backtest
# and the allocated budget
backtesting_start = datetime(2020, 11, 1)
backtesting_end = datetime(2020, 12, 31)

# Run the backtest
trader = Trader(backtest=True)
data_source = YahooDataBacktesting(
    datetime_start=backtesting_start,
    datetime_end=backtesting_end,
)
broker = BacktestingBroker(data_source)
strat = MyStrategy(
    broker=broker,
)
trader.add_strategy(strat)
trader.run_all()

which also results in similar error shown below

(trader) (base) someuser@trading_bot  % /opt/homebrew/Caskroom/miniconda/base/envs/trader/bin/python /Users
/ahmedalhendi/Dropbox/Mac/Desktop/trading_bot/test.py
Failed to get ticker '^IRX' reason: HTTPSConnectionPool(host='fc.yahoo.com', port=443): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x16d3e4fd0>: Failed to establish a new connection: [Errno 61] Connection refused'))
^IRX: No price data found, symbol may be delisted (period=7d)
ERROR:root:Error getting the risk free rate: unsupported operand type(s) for /: 'NoneType' and 'int'
Progress |█----------------------------------------------------------------------------|   2.39%  [Elapsed: 0:00:00 ETA: 0:00:21] 2024-01-30 22:55:17,435: yfinance: ERROR: Failed to get ticker 'AAPL' reason: HTTPSConnectionPool(host='fc.yahoo.com', port=443): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x16f871e70>: Failed to establish a new connection: [Errno 61] Connection refused'))
2024-01-30 22:55:17,435: yfinance: ERROR: AAPL: No timezone found, symbol may be delisted
2024-01-30 22:55:17,439: root: ERROR: YAHOO did not return data for symbol AAPL. Make sure this symbol is valid.
2024-01-30 22:55:17,440: root: ERROR: unsupported operand type(s) for //: 'float' and 'NoneType'
2024-01-30 22:55:17,440: root: ERROR: Traceback (most recent call last):
  File "/opt/homebrew/Caskroom/miniconda/base/envs/trader/lib/python3.10/site-packages/lumibot/strategies/strategy_executor.py", line 831, in run
    self._run_trading_session()
  File "/opt/homebrew/Caskroom/miniconda/base/envs/trader/lib/python3.10/site-packages/lumibot/strategies/strategy_executor.py", line 782, in _run_trading_session
    self._on_trading_iteration()
  File "/opt/homebrew/Caskroom/miniconda/base/envs/trader/lib/python3.10/site-packages/lumibot/strategies/strategy_executor.py", line 274, in func_output
    result = func_input(self, *args, **kwargs)
  File "/opt/homebrew/Caskroom/miniconda/base/envs/trader/lib/python3.10/site-packages/lumibot/strategies/strategy_executor.py", line 298, in func_output
    result = func_input(self, *args, **kwargs)
  File "/opt/homebrew/Caskroom/miniconda/base/envs/trader/lib/python3.10/site-packages/lumibot/strategies/strategy_executor.py", line 87, in func_output
    return func_input(self, *args, **kwargs)
  File "/opt/homebrew/Caskroom/miniconda/base/envs/trader/lib/python3.10/site-packages/lumibot/strategies/strategy_executor.py", line 378, in _on_trading_iteration
    on_trading_iteration()
  File "/opt/homebrew/Caskroom/miniconda/base/envs/trader/lib/python3.10/site-packages/lumibot/tools/decorators.py", line 62, in func_output
    frame, result = call_function_get_frame(func_input, *args, **kwargs)
  File "/opt/homebrew/Caskroom/miniconda/base/envs/trader/lib/python3.10/site-packages/lumibot/tools/decorators.py", line 30, in call_function_get_frame
    result = func(*args, **kwargs)
  File "/Users/ahmedalhendi/Dropbox/Mac/Desktop/trading_bot/test.py", line 13, in on_trading_iteration
    quantity = self.portfolio_value // aapl_price
TypeError: unsupported operand type(s) for //: 'float' and 'NoneType'

Exception in thread MyStrategy:
Traceback (most recent call last):
  File "/opt/homebrew/Caskroom/miniconda/base/envs/trader/lib/python3.10/site-packages/lumibot/strategies/strategy_executor.py", line 831, in run
    self._run_trading_session()
  File "/opt/homebrew/Caskroom/miniconda/base/envs/trader/lib/python3.10/site-packages/lumibot/strategies/strategy_executor.py", line 782, in _run_trading_session
    self._on_trading_iteration()
  File "/opt/homebrew/Caskroom/miniconda/base/envs/trader/lib/python3.10/site-packages/lumibot/strategies/strategy_executor.py", line 274, in func_output
    result = func_input(self, *args, **kwargs)
  File "/opt/homebrew/Caskroom/miniconda/base/envs/trader/lib/python3.10/site-packages/lumibot/strategies/strategy_executor.py", line 298, in func_output
    result = func_input(self, *args, **kwargs)
  File "/opt/homebrew/Caskroom/miniconda/base/envs/trader/lib/python3.10/site-packages/lumibot/strategies/strategy_executor.py", line 87, in func_output
    return func_input(self, *args, **kwargs)
  File "/opt/homebrew/Caskroom/miniconda/base/envs/trader/lib/python3.10/site-packages/lumibot/strategies/strategy_executor.py", line 378, in _on_trading_iteration
    on_trading_iteration()
  File "/opt/homebrew/Caskroom/miniconda/base/envs/trader/lib/python3.10/site-packages/lumibot/tools/decorators.py", line 62, in func_output
    frame, result = call_function_get_frame(func_input, *args, **kwargs)
  File "/opt/homebrew/Caskroom/miniconda/base/envs/trader/lib/python3.10/site-packages/lumibot/tools/decorators.py", line 30, in call_function_get_frame
    result = func(*args, **kwargs)
  File "/Users/ahmedalhendi/Dropbox/Mac/Desktop/trading_bot/test.py", line 13, in on_trading_iteration
    quantity = self.portfolio_value // aapl_price
TypeError: unsupported operand type(s) for //: 'float' and 'NoneType'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/opt/homebrew/Caskroom/miniconda/base/envs/trader/lib/python3.10/threading.py", line 1016, in _bootstrap_inner
    self.run()
  File "/opt/homebrew/Caskroom/miniconda/base/envs/trader/lib/python3.10/site-packages/lumibot/strategies/strategy_executor.py", line 844, in run
    raise RuntimeError("Exception encountered, stopping BackTest.") from e
RuntimeError: Exception encountered, stopping BackTest.

Creating indicators plot...

So I think it is the issue with lumibot version I would suggest that update the installation section with requirements.txt so that we can that we can replicate your work.. e.g.

# from nicknochnack pc
pip freeze > requirements.txt

then on our to replicate your work we can run

# using pip
pip install -r requirements.txt

# using Conda
conda create --name trader --file requirements.txt

Looking forward to your reply.. thank you

ZSchweyk commented 3 months ago

I'm having this same issue. Basically, self.get_last_price is always returning None and I'm unable to move forward... I'd greatly appreciate any help on this as well.

oezgan commented 1 month ago

Hello, I have had the same issue... If you look at the following line in the error: reason: HTTPSConnectionPool(host='fc.yahoo.com', port=443): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x16d3e4fd0>: Failed to establish a new connection: [Errno 61] Connection refused')) ^IRX: No price data found, symbol may be delisted (period=7d) it seems that fc.yahoo.com does not have a DNS entry anymore. What I did was: GoTo: python/Lib/site-packages/yfinance/data.py and change the entry with url='https://fc.yahoo.com', to url='https://finance.yahoo.com', that resolved the issue for me.