-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
112 lines (79 loc) · 2.79 KB
/
main.py
File metadata and controls
112 lines (79 loc) · 2.79 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
from urllib.request import urlopen
import json
import sys
from apikey import api #API KEY
import certifi
import tkinter as tk
from tkinter import *
import pandas as pd
import ssl
import math
from pandastable import Table, TableModel
# Create a SSL context
ssl_context = ssl.create_default_context()
ssl_context.load_verify_locations(cafile=certifi.where())
def get_data(url):
response = urlopen(url, context=ssl_context)
if response.getcode() == 200:
print("API Response Successful")
else:
print("API Response Failed")
sys.exit()
return json.load(response)
url = f"https://financialmodelingprep.com/api/v3/sp500_constituent?apikey={api}"
data = get_data(url)
with open("data.json", 'w') as f:
json.dump(data, f, indent=2)
symbol = []
for blob in data:
symbol.append(blob['symbol'])
quote = ','.join(symbol)
url = f"https://financialmodelingprep.com/api/v3/quote/{quote}?apikey={api}"
quote = get_data(url)
with open("quote.json", 'w') as f:
json.dump(quote, f, indent=2)
columns = ['Name', 'Symbol', 'Price', 'Market Capitalization', 'Weight', 'Number of Shares to Buy', 'Portfolio Allocation']
df = pd.DataFrame(columns=columns)
asset = 0
for ticker in quote:
asset += ticker['marketCap']
df = df._append(
pd.Series([
ticker['name'],
ticker['symbol'],
ticker['price'],
ticker['marketCap'],
'N/A',
'N/A',
'N/A'
], index = columns),
ignore_index = True
)
for i in range(0, len(df.index)):
df.loc[i, 'Weight'] = (df.loc[i, 'Market Capitalization'] / asset) * 100
money = float(input("Enter The Size of your Portfolio: "))
for i in range(0, len(df.index)):
df.loc[i, 'Number of Shares to Buy'] =math.floor(((df.loc[i, 'Weight'] / 100) * money) / df.loc[i, 'Price'])
df.loc[i, 'Portfolio Allocation'] = round(df.loc[i, 'Number of Shares to Buy'] * df.loc[i, 'Price'], 2)
df.to_csv('datasource.csv', index=False)
df.to_excel('datasource.xlsx', index=False)
print(df)
class TestApp(Frame):
"""Basic test frame for the table"""
def __init__(self, parent=None):
self.parent = parent
Frame.__init__(self)
self.main = self.master
self.main.geometry('1100x800+30+30')
self.main.title('Table app')
self.main.attributes('-topmost', 1)
#self.main.after_idle(self.main.attributes, '-topmost', 0)
f = Frame(self.main)
f.pack(fill=BOTH,expand=1)
df = pd.read_excel('datasource.xlsx')
self.table = pt = Table(f, dataframe=df,
showtoolbar=True, showstatusbar=True)
pt.show()
return
app = TestApp()
app.mainloop()