-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrun.py
More file actions
162 lines (137 loc) · 6.29 KB
/
Copy pathrun.py
File metadata and controls
162 lines (137 loc) · 6.29 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
from srv import pricedata, portfolio, inputs, format
import finnhub
from decouple import config
FINNHUB_CLIENT_KEY = config("FINNHUB_KEY")
FINNHUB_CLIENT = finnhub.Client(api_key=FINNHUB_CLIENT_KEY)
def start_program():
"""
Provide opening option to user and runs appropriate
function based on the response given.
"""
expected_responses = ["L", "P"]
message = "If you would like to check Live Stock or "\
f"Crypto prices press {expected_responses[0]}. "\
"If you would like to View or Edit your personal positions "\
f"press {expected_responses[1]}.\n"\
"Enter your response "\
f"({expected_responses[0]}/{expected_responses[1]}):\n"
response = inputs.basic_input_request(message, expected_responses)
if response == expected_responses[0]:
live_search()
elif response == expected_responses[1]:
format.clear()
print("Obtaining position data...\n")
portfolio_search()
def live_search():
"""
Requests user input and confirms user inputs are valid.
Formats ticker for crypto if c is chosen.
Takes user's inputs and provides market data for the given ticker.
"""
expected_search_types = ["C", "S"]
search = f"Enter {expected_search_types[0]} if you wish to search a "\
f"cryptocurrency or {expected_search_types[1]} for "\
"a stock\nfollowed by the ticker/crypto symbol "\
"combo you wish to view, separated with "\
"a space (eg S AMC, C BTC): \n"
search_query = inputs.complex_query(search, expected_search_types)
search_type = search_query[0]
requested_ticker = search_query[1]
live_price = pricedata.get_live_data(
search_type, requested_ticker, FINNHUB_CLIENT)
requested_ticker = format.remove_crypto_format(requested_ticker)
print(f"The current price for {requested_ticker.pop()} "
f"is ${live_price.pop()}\n")
navigate()
def portfolio_search():
"""
Searches portfolio worksheets and uses data to display live
values for all assets held within portfolio. Passes total value
of portfolio currently to next option chain function.
"""
all_portfolio_amounts = portfolio.retrieve_portfolio_amounts()
stock_portfolio_tickers = all_portfolio_amounts[0][0]
stock_amounts = all_portfolio_amounts[0][1:]
stock_live_prices = pricedata.get_live_data(
"S", stock_portfolio_tickers, FINNHUB_CLIENT)
stock_values = portfolio.calculate_values(stock_amounts, stock_live_prices)
print("Your current portfolio contains: \n")
crypto_portfolio_tickers = all_portfolio_amounts[1][0]
format.format_crypto(crypto_portfolio_tickers)
crypto_amounts = all_portfolio_amounts[1][1:]
crypto_live_prices = pricedata.get_live_data(
"C", crypto_portfolio_tickers, FINNHUB_CLIENT)
crypto_values = portfolio.calculate_values(
crypto_amounts, crypto_live_prices)
crypto_tickers = format.remove_crypto_format(crypto_portfolio_tickers)
print("Stock:")
for (ticker, value) in zip(stock_portfolio_tickers, stock_values):
print(f"{ticker} worth ${value}")
print("\n")
print("Crypto:")
for (ticker, value) in zip(crypto_tickers, crypto_values):
print(f"{ticker} worth ${value}")
total_value = portfolio.calculate_total_value(stock_values, crypto_values)
portfolio_options(total_value, stock_amounts, crypto_amounts)
def portfolio_options(total_value, stock_amounts, crypto_amounts):
"""
Give user options for actions they can take to make changes
to their portfolio. Runs correct function according to choice given.
"""
while True:
expected_responses = ["1", "2", "3", "4"]
message = f"\nDo you want to: \n"\
f"{expected_responses[0]} Create/Remove a position?\n"\
f"{expected_responses[1]} Edit an existing position?\n"\
f"{expected_responses[2]} Calculate current P/L?\n"\
f"{expected_responses[3]} Return to start?\n"\
"\nInput the number of your desired option.\n"
response = inputs.basic_input_request(message, expected_responses)
if response == expected_responses[0]:
expected_types = ["C", "S"]
search = f"\nEnter {expected_types[0]} if you wish to add/remove "\
f"a cryptocurrency position or {expected_types[1]} for a "\
"stock\nfollowed by the ticker/crypto symbol combo you wish "\
"to add/remove, separated with a space (eg S AMC, C BTC): \n"
complex_response = inputs.complex_query(search, expected_types)
response_type = complex_response[0]
response_ticker = format.remove_crypto_format(complex_response[1])
portfolio.create_position(response_type, response_ticker)
elif response == expected_responses[1]:
expected_types = ["C", "S"]
search = f"\nEnter {expected_types[0]} if you wish to edit a "\
f"cryptocurrency or {expected_types[1]} for a stock\n"\
"followed by the ticker/crypto symbol combo you wish "\
"to edit, separated with a space (eg S AMC, C BTC): \n"
complex_response = inputs.complex_query(search, expected_types)
response_type = complex_response[0]
response_ticker = format.remove_crypto_format(complex_response[1])
portfolio.edit_positions(response_type, response_ticker)
elif response == expected_responses[2]:
portfolio.calculate_pl(total_value, stock_amounts, crypto_amounts)
elif response == expected_responses[3]:
format.clear()
start_program()
def navigate():
"""
Give user option of searching another ticker or
return to start menu options.
"""
expected_responses = ["A", "H"]
message = "If you wish to search another item "\
f"press {expected_responses[0]} \n"\
"If you wish to return to home "\
f"press {expected_responses[1]}\n"
response = inputs.basic_input_request(message, expected_responses)
if response == expected_responses[0]:
live_search()
elif response == expected_responses[1]:
format.clear()
start_program()
def main():
"""
Welcome user and start the program
"""
print("Welcome to your own personal Portfolio Tracker!\n")
start_program()
main()