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

Division by zero error? #7

Closed
MatthewMacomber opened this issue Jan 10, 2020 · 1 comment
Closed

Division by zero error? #7

MatthewMacomber opened this issue Jan 10, 2020 · 1 comment

Comments

@MatthewMacomber
Copy link
Contributor

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
Copy link
Contributor Author

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.

@2018kguo 2018kguo reopened this Jan 25, 2020
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