2018kguo / RobinhoodBot

Trading bot for Robinhood accounts
MIT License
207 stars 105 forks source link

Division by zero error? #7

Closed MatthewMacomber closed 4 years ago

MatthewMacomber commented 4 years ago

When I run the script I get a division by zero error when calculating the ideal_position_size:

Current Portfolio: []

Current Watchlist: ['AAPL', 'TSLA', 'AMD', 'NVDA', 'PLUG', 'TEUM', 'BIIB', 'WBA', 'VSLR', 'NOG', 'FCEL', 'GLUU', 'APPS', 'WIFI', 'BABA', 'AMZN', 'SQ', 'WORK', 'INTC', 'TCEHY', 'CRM', 'GOOGL', 'TWLO', 'SHOP', 'DBX', 'BIDU', 'IBM', 'ADBE', 'INSG', 'BB', 'CTL', 'FTR', 'MGI', 'RKUNY', 'UPWK', 'TRVG', 'SHCAY', 'ACB', 'DIS', 'HEXO', 'CHK', 'NFLX', 'BYND', 'NTDOY', 'MU', 'COST', 'SPWR', 'INFY', 'NET', 'PBFS', 'NOVA', 'LVGO', 'PING', 'HCAT', 'BLU', 'OPRT', 'PHR', 'BNTX', 'IPHA', 'IFS', 'ENPH', 'SNE', 'QCOM', 'AMAT', 'TSM', 'XLNX', 'AVGO', 'FLEX', 'PLAB', 'FJTSY', 'AEIS', 'MPWR']

----- Scanning portfolio for stocks to sell -----

----- Scanning watchlist for stocks to buy -----

PBFS: Short SMA crossed ABOVE Long SMA at 2020-01-07 00:00:00+00:00, which was 3 days 07:05:54.254984 ago , price at cross: 14.58, current price: 15.08 Traceback (most recent call last): File "C:\Users\cryptic\Desktop\robinhoodbot\main.py", line 259, in scan_stocks() File "C:\Users\cryptic\Desktop\robinhoodbot\main.py", line 253, in scan_stocks buy_holdings(potential_buys, profile_data, holdings_data) File "C:\Users\cryptic\Desktop\robinhoodbot\main.py", line 205, in buy_holdings ideal_position_size = (portfolio_value/len(holdings_data)+cash/len(potential_buys))/(2 * len(potential_buys)) ZeroDivisionError: float division by zero

MatthewMacomber commented 4 years ago

Solved by replacing portfolio_value/len(holdings_data) with a division function that returns 0 if len(holdings_data) is equal to 0 instead of attempting a divide by zero. Simply an issue only if for whatever reason you have zero holdings at the time of running the script.