-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhello.py
More file actions
61 lines (51 loc) · 2.27 KB
/
hello.py
File metadata and controls
61 lines (51 loc) · 2.27 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
from flask import Flask, render_template, request
import httplib
import pprint
import json
pp = pprint.PrettyPrinter(indent=4)
app = Flask(__name__)
@app.route('/')
def hello_world():
return getChain()
# variable route to quote a symbol
@app.route('/index/', methods=['GET','POST'])
def getChain():
if request.method == 'GET':
return render_template('index.html')
symbol = request.form['symbol']
# set up the https connection with Tradier API
connection = httplib.HTTPSConnection('sandbox.tradier.com', 443, timeout = 30)
# build headers to deliver to Tradier
headers = {"Accept":"application/json", "Authorization":"Bearer NrGhOFZHuGFdVtIzcwyRULgxjRFJ"}
# send request to connection
dates = getExpirations(symbol)
url = '/v1/markets/options/chains?symbol=%(symbol)s&expiration=%(date)s' % {"symbol": symbol, "date": dates[0]}
connection.request('GET', url, None, headers)
try:
response = connection.getresponse().read()
jsonQuote = json.loads(response)
contractsList = sorted(jsonQuote['options']['option'], key=lambda contract: contract['strike'])
putsChain = [x for x in contractsList if x['option_type'] == "put"]
callChain = [x for x in contractsList if x['option_type'] == "call"]
contractsMap = {}
for c in contractsList:
contractsMap[c['symbol']] = c
return render_template('index.html', putChain=putsChain, callChain=callChain, JSONData=json.dumps(contractsMap))
except httplib.HTTPException, e:
print('Exception during request')
def getExpirations(symbol):
# set up the https connection with Tradier API
connection = httplib.HTTPSConnection('sandbox.tradier.com', 443, timeout = 30)
# build headers to deliver to Tradier
headers = {"Accept":"application/json", "Authorization":"Bearer NrGhOFZHuGFdVtIzcwyRULgxjRFJ"}
# send request to connection
url = '/v1/markets/options/expirations?symbol=%s' % symbol
connection.request('GET', url, None, headers)
try:
response = connection.getresponse()
jsonQuote = json.loads(response.read())
return jsonQuote['expirations']['date']
except httplib.HTTPException, e:
print('Exception during request')
if __name__ == '__main__':
app.run(debug=True)