-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
46 lines (37 loc) · 1.93 KB
/
api.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
import requests
import json
class Underlying:
def __init__(self, config, symbol):
self.chain_data = {}
self.symbol = symbol
self.fundamental_data = {}
self.get_options_chain(config)
#self.get_fundamentals(config)
def get_options_chain(self, config):
header = {"Authorization": config.access_token, "Accept-Language": "en-us",
"Accept-Encoding": "gzip"}
url = "{}?apikey={}&symbol={}&contractType=CALL&strikeCount=6&includeQuotes=FALSE&strategy=ANALYTICAL&range" \
"=OTM".format(config.settings["options_endpoint"], config.settings["apikey"], self.symbol)
config.scheduler.report_call()
r = requests.get(url, headers=header)
self.chain_data = json.loads(json.dumps(r.json()))
def get_fundamentals(self, config):
header = {"Authorization": config.access_token, "Accept-Language": "en-us", "Accept-Encoding": "gzip"}
url = '{}?apikey={}&symbol={}&projection=fundamental'.format(config.settings["fundamental_endpoint"],
config.settings["apikey"], self.symbol)
config.scheduler.report_call()
r = requests.get(url, headers=header)
if r.status_code == 200:
self.fundamental_data = json.loads(json.dumps(r.json()))
def get_watchlist(config):
url = 'https://api.tdameritrade.com/v1/accounts/{}/watchlists/{}'.format(config.settings['account_num'],
config.settings['watchlist_id'])
header = {"Authorization": config.access_token}
ticker_list = []
config.scheduler.report_call()
r = requests.get(url, headers=header)
if r.status_code == 200:
response = json.loads(json.dumps(r.json()))
for symbol in response['watchlistItems']:
ticker_list.append(symbol['instrument']['symbol'])
return ticker_list