-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOrders.py
85 lines (67 loc) · 3.33 KB
/
Orders.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 json
import requests
from ConfigParser import SafeConfigParser
#initialize parser to get secret data, end points, etc.
#put you API Key and Endpoints in file called config.ini
parser = SafeConfigParser()
parser.read('config.ini')
def Buy_Stock_Limit(ticker, quantity, limit_price, duration):
"""sends limit buy order, returns response from brokerage"""
#set query args
endpoint = parser.get('endpoint', 'brokerage') + 'accounts/' + parser.get('account', 'AccountNumber') + '/orders'
headers = {'Authorization': parser.get('account', 'Auth'), 'Accept': parser.get('message_format', 'accept_format')}
payload = {'class' : 'equity',
'symbol' : ticker,
'duration' : duration,
'side' : 'buy',
'quantity' : quantity,
'type' : 'limit',
'price' : limit_price,
'preview' : parser.get('message_format', 'preview')}
#send query
r = requests.post(endpoint, headers = headers , params = payload)
#get parse response
content = json.loads(r.text)
return content
def Sell_Stock_Limit(ticker, quantity, limit_price, duration):
"""sends limit sell order for ticker security, returns response from brokerage"""
#set query args
endpoint = parser.get('endpoint', 'brokerage') + 'accounts/' + parser.get('account', 'AccountNumber') + '/orders'
headers = {'Authorization': parser.get('account', 'Auth'), 'Accept': parser.get('message_format', 'accept_format')}
payload = {'class' : 'equity',
'symbol' : ticker,
'duration' : duration,
'side' : 'sell',
'quantity' : quantity,
'type' : 'limit',
'price' : limit_price,
'preview' : parser.get('message_format', 'preview')}
#send query
r = requests.post(endpoint, headers = headers , params = payload)
#get parse response
content = json.loads(r.text)
return content
def Cancel_Order(Order_Number):
"""cancels ticker order number, prints results of order cancel request"""
endpoint = parser.get('endpoint', 'brokerage') + 'accounts/' + parser.get('account', 'AccountNumber') + '/orders' + Order_Number
headers = {'Authorization': parser.get('account', 'Auth'), 'Accept': parser.get('message_format', 'accept_format')}
#send query
r = requests.delete(endpoint, headers = headers)
#get parse response
content = json.loads(r.text)
if (content['order']['id'] == Order_Number and
content['order']['status'] == 'ok'):
print 'Order ' + Order_Number + ' Canceled'
else:
print 'Order Failed to Cancel. Status: ' + content['order']['status']
def Check_Order_Status(order_id):
"""returns status of order of given order_id"""
#set query args
endpoint = parser.get('endpoint', 'brokerage') + 'accounts/' + parser.get('account', 'AccountNumber') + '/orders/' + order_id
headers = {'Authorization': parser.get('account', 'Auth'), 'Accept': parser.get('message_format', 'accept_format')}
payload = {'account_id' : parser.get('account', 'AccountNumber'), 'id' : order_id}
#send query
r = requests.get(endpoint, headers = headers , params = payload)
#get parse response
content = json.loads(r.text)
return content