-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharbitrage.py
executable file
·190 lines (142 loc) · 5.83 KB
/
arbitrage.py
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
from urllib.request import urlopen
import json
from os.path import exists
import datetime as dt
import pathlib
import PySimpleGUI as sg
def updateJSON(API_KEY):
"""Updates the relevant equity data csv files"""
for i in ["NYSE", "NASDAQ", "LSE"]:
url = (f"https://financialmodelingprep.com/api/v3/stock-screener?exchange={i}&apikey={API_KEY}")
data = get_jsonparsed_data(url)
with open(f"{i}.json", 'w', encoding="UTF-8") as f:
json.dump(data, f)
def get_jsonparsed_data(url):
"""
Receive the content of ``url``, parse it as JSON and return the object.
Parameters
----------
url : str
Returns
-------
dict
"""
response = urlopen(url)
data = response.read().decode("utf-8")
return json.loads(data)
def GB_US_Arbitrage(API_KEY):
#* Checks if there are already tables to avoid unnecessary request for the API
#* Also checks if the json files are up to date
if exists("NYSE.json") and exists("NASDAQ.json") and exists("LSE.json"):
for i in ["NYSE.json", "NASDAQ.json", "LSE.json"]:
# create a file path
path = pathlib.Path(i)
# get modification time
timestamp = path.stat().st_mtime
# convert time to dd-mm-yyyy hh:mm:ss
modifDate = dt.date.fromtimestamp(timestamp)
today = dt.date.today()
if today == modifDate:
pass
else:
updateJSON(API_KEY)
else:
updateJSON(API_KEY)
#* ================================================
#* Grabs info from csv into dataframes
NY, NAS, GB = "", "", ""
with open("NYSE.json", encoding="UTF-8") as f_ny:
NY = json.load(f_ny)
with open("NASDAQ.json", encoding="UTF-8") as f_nas:
NAS = json.load(f_nas)
with open("LSE.json", encoding="UTF-8") as f_ny:
GB = json.load(f_ny)
#* ===================================
#* Removes not actively trading equities
for exchange in [NY, NAS, GB]:
for stock in exchange:
if stock.get("isActivelyTrading") == "false" or int(stock.get("marketCap")) == 0 or float(stock.get("price")) == 0.0:
exchange.remove(stock)
#* ========================================
NY_companies = [stock.get("companyName") for stock in NY]
GB_companies = [stock.get("companyName") for stock in GB]
NAS_companies = [stock.get("companyName") for stock in NAS]
commonNYGB = []
commonNASGB = []
#* Grabs Multi-Listed Companies
for stock in GB_companies:
if stock in NY_companies:
commonNYGB.append(stock)
elif stock in NAS_companies:
commonNASGB.append(stock)
#* ============================
#* Gets the price of the stock
arbiNYGB = []
for stock in commonNYGB:
no_error = False
try:
usd = [float(i.get("price")) for i in NY if i.get("companyName") == stock][0]
gbp = [float(i.get("price")) for i in GB if i.get("companyName") == stock][0]
no_error = True
except IndexError:
continue
if no_error:
if float(usd) != 0.0 and float(gbp) != 0.0:
arbiNYGB.append({stock: [usd, gbp]})
print(arbiNYGB)
arbiNASGB = []
for i in commonNASGB:
no_error = False
try:
usd = [float(i.get("price")) for i in NAS if i.get("companyName") == stock][0]
gbp = [float(i.get("price")) for i in GB if i.get("companyName") == stock][0]
no_error = True
except IndexError:
continue
if no_error:
if float(usd) != 0.0 and float(gbp) != 0.0:
arbiNASGB.append({stock: [usd, gbp]})
arbitrageList_prov = arbiNYGB + arbiNASGB
arbitrageList = []
duplicate_remove = [arbitrageList.append(x) for x in arbitrageList_prov if x not in arbitrageList]
#* ===================================================================
#* Grabs the stocks with near equivalent prices in both exchanges
arbitrage = []
for i in arbitrageList:
prices = list(i.values())[0]
if abs((prices[0]/prices[1]) - 1) < 0.10:
if prices[0] > prices[1]:
arbitrage.append([list(i.keys())[0], "GB", "US", round((prices[0]-prices[1]),7)])
elif prices[1] > prices[0]:
arbitrage.append([list(i.keys())[0], "US", "GB", round((prices[1]-prices[0]),7)])
#* ==============================================================
return arbitrage
if __name__=="__main__":
def open_window(key):
table = GB_US_Arbitrage(key)
headings = ["Company Name", "Buy Side", "Sell Side", "Arbitrage per Share (in USD)"]
data = table
print(data)
layout = [[sg.Table(data, headings=headings, justification="left", key="-TABLE-", max_col_width=30, font=('Any 13'))]]
window = sg.Window("US-GB Arbitrage Opportunity Finder", layout, modal=False)
choice = None
while True:
event, values = window.read()
if event == "Exit" or event == sg.WIN_CLOSED:
break
window.close()
def main():
layout = [[sg.Text('Enter your API Key Here: ', font=('Any 13'))],
[sg.InputText(key='key', font=('Any 13'))],
[sg.Submit(key="submit", font=('Any 13')), sg.Cancel(font=('Any 13'))]]
window = sg.Window("US-GB Arbitrage Opportunity Finder", layout,)
while True:
event, values = window.read()
if event == "Exit" or event == sg.WIN_CLOSED:
break
if event == "submit":
key = str(values["key"])
open_window(key)
window.close()
window.close()
main()