-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
134 lines (95 loc) · 5.4 KB
/
main.py
File metadata and controls
134 lines (95 loc) · 5.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# The main decision-making process takes place in this file. The score calculation
# functions are imported from quant.py, and used here. As a reminder, you need to
# have an AlphaVantage API key stored in an apikeys.py file, in two variables called
# "alphavantage_api_key". This file is in the .gitignore, so as long as you don't modify
# it you can be assured that your API keys won't get published online.
# Also remember, the AlphaVantage API has a 25 request per day limit in the free version,
# which can be easily exceeded by HedgeHog. I have implemented a check function that
# verifies every time the API is called if the limit is reached, and it will raise a
# ConnectionRefusedError if it is.
import json
from quant import *
from scorecalc import *
from tickers import *
from apikeys import *
from time import sleep
# Startup splash screen
print('╻ ╻ ┏━━━━━ ━━━━━┓ ┏━━━━━ ┏━━━━━ ╻ ╻ ┏━━━━┓ ┏━━━━━')
print('┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ')
print('┣━━━━┫ ┣━━━━ ┃ ┃ ┃ ━━┓ ┣━━━━━ ┣━━━━┫ ┃ ┃ ┃ ━━┓')
print('┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃')
print('╹ ╹ ┗━━━━━ ━━━━━┛ ┗━━━━┛ ┗━━━━━ ╹ ╹ ┗━━━━┛ ┗━━━━┛')
# Get total investment budget from user
print('')
print('Enter your total investment budget: ', end='')
total_budget = float(input())
print('')
test_set = ['AMZN'] # Will be replaced by variable 'sp500' from 'tickers' module in full version
# Will include the companies the algorithm has decided to buy
cart = {}
# Main decision-making loop. If you get errors in this part it is probably due to the two APIs used
for company in test_set:
print(f'■ Currently Analysing: {company}')
scores = []
company_income_statement = get_income_statement(company, alphavantage_api_key)
company_balance_sheet = get_balance_sheet(company, alphavantage_api_key)
scores.append(liabilities_to_equity_score(company_balance_sheet))
print(f'Debt-to-Equity Ratio: {liabilities_to_equity(company_balance_sheet)}')
print(f'Debt-to-Equity Atrributed Score: {scores[-1]}')
sleep(0.2)
scores.append(liabilities_to_capital_score(company_balance_sheet))
print(f'Debt-to-Capital Ratio: {liabilities_to_capital(company_balance_sheet)}')
print(f'Debt-to-Capital Atrributed Score: {scores[-1]}')
sleep(0.2)
scores.append(float(assets_to_equity_score(company_balance_sheet)))
print(f'Assets-to-Equity Ratio: {assets_to_equity(company_balance_sheet)}')
print(f'Assets-to-Equity Atrributed Score: {scores[-1]}')
sleep(0.2)
scores.append(float(debt_to_ebitda_score(company_balance_sheet, company_income_statement)))
print(f'Debt-to-EBITDA Ratio: {debt_to_ebitda(company_balance_sheet, company_income_statement)}')
print(f'Debt-to-EBITDA Atrributed Score: {scores[-1]}')
sleep(0.2)
scores.append(float(quick_ratio_score(company_balance_sheet)))
print(f'Quick Ratio: {quick_ratio(company_balance_sheet)}')
print(f'Quick Ratio Atrributed Score: {scores[-1]}')
sleep(0.2)
scores.append(float(current_ratio_score(company_balance_sheet)))
print(f'Current Ratio: {current_ratio(company_balance_sheet)}')
print(f'Current Ratio Atrributed Score: {scores[-1]}')
sleep(0.2)
status = ten_yr_operating_expenses_growth(company_income_statement)
if status != False:
scores.append(float(ten_yr_opex_growth_score(company_income_statement)))
print(f'10-Year Operating Expenses Growth: {status}')
print(f'10-Year Operating Expenses Growth Atrributed Score: {scores[-1]}')
# sleep(0.2)
status = ten_yr_assets_growth(company_balance_sheet)
if status != False:
scores.append(float(ten_yr_assets_growth_score(company_balance_sheet)))
print(f'10-Year Assets Growth: {status}')
print(f'10-Year Assets Growth Atrributed Score: {scores[-1]}')
# sleep(0.2)
status = ten_yr_liabilities_growth(company_balance_sheet)
if status != False:
scores.append(float(ten_yr_liabilities_growth_score(company_balance_sheet)))
print(f'10-Year Liabilities Growth: {status}')
print(f'10-Year Liabilities Growth Atrributed Score: {scores[-1]}')
sleep(0.2)
status = ten_yr_share_count_growth(company_balance_sheet)
if status != False:
scores.append(float(ten_yr_share_count_growth_score(company_balance_sheet)))
print(f'10-Year Share Count Growth: {status}')
print(f'10-Year Share Count Growth Atrributed Score: {scores[-1]}')
# Sorts the cart by score values
sorted_cart = {key: value for key,
value in sorted(cart.items(),
key=lambda item: item[1])}
sorted_cart_selection = dict(list(sorted_cart.items())[len(sorted_cart) // 2:])
# Writes to portfolio file
portfolio_file = open('portfolio.txt', 'w')
print('WRITING ANALYSIS RESULTS TO "portfolio.txt"')
for pick in sorted_cart_selection:
line = f"Buy {(sorted_cart_selection[pick]/sum(list(sorted_cart_selection.values()))) * (total_budget):.2f}$ of {pick} shares\n"
portfolio_file.write(line)
print('File write completed. Exiting program...')
portfolio_file.close()