-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathGetSymbol.py
85 lines (72 loc) · 2.13 KB
/
GetSymbol.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
import datetime
import requests
import socket
import json
import re
def extract_ip():
st = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
st.connect(('10.255.255.255', 1))
IP = st.getsockname()[0]
except Exception:
IP = '127.0.0.1'
finally:
st.close()
return IP
host = 'http://' + extract_ip() + ':8080'
def search(term:str):
url = host + '/Search'
contract = {"term": term}
x = requests.post(url, data = contract)
jsn = {}
try:
jsn = json.loads(x.text)
except:
print("1 Error: {}".format(x.text))
for j in jsn:
try:
j2 = json.loads(j)
print("{}, {}, {}, {}, {}".format(
j2["symbol"],
j2["secType"],
j2["currency"],
j2["exchange"],
j2["primaryExchange"]
))
except:
print("1 parse error: {}".format(j))
return
def contractLookup(symbol, secType, exchange):
url = host + '/ContractDetails'
print("------- Contract details --------")
contract = {"symbol": symbol,
"secType": secType,
"exchange": exchange
}
x = requests.post(url, data = contract)
jsn = {}
try:
jsn = json.loads(x.text)
except:
print("2 Error: {}".format(x.text))
skip = 0
for j in jsn:
try:
j2 = json.loads(j)
print("{}, {}, {}, {}, {}, {}, {}".format(
j2["symbol"],
j2["localSymbol"],
j2["secType"],
j2["currency"],
j2["exchange"],
j2["primaryExchange"],
j2["lastTradeDateOrContractMonth"]
))
#print(j2)
except:
print("2 parse error: {}".format(j))
return
if __name__ == "__main__":
#search("LZB")
contractLookup("TLSA", "STK", "ISLAND")
#contractLookup("AMZN", "STK", "") # island is nasdaq...