-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathplaceOrder.py
114 lines (102 loc) · 2.85 KB
/
placeOrder.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
import requests
import json
import csv
import ast
import sys
import pandas as pd
import argparse
if sys.argv[1]=="":
print ("You forgot to specify order .csv file!\n")
else:
print ("Executing Order!\n")
#orderCSV = sys.argv[1]
# Will Execute the order contained in last row of .csv
# (Same format that getOrders.py outputs)
# PAUL. YOU CAN EDIT THESE ROW[] ARGS TO YOUR LIKING
with open(sys.argv[1]) as csvfile: #'sample_order.csv'
readCSV = csv.reader(csvfile, delimiter=',')
for row in readCSV:
assetType = row[2]
instruction = row[1]
duration = row[18]
price = row[26]
orderType = row[19]
quantity = row[21]
symbol = row[5]
# Same format that getOrders.py outputs
stock_marketOrder = {
"orderType": "MARKET",
"session": "NORMAL",
"duration": duration,
"orderStrategyType": "SINGLE",
"orderLegCollection": [
{
"instruction": instruction,
"quantity": quantity,
"instrument": {
"symbol": symbol,
"assetType": assetType
}
}
]
}
stock_limitOrder = {
"orderType": "LIMIT",
"session": "NORMAL",
"price": price,
"duration": duration,
"orderStrategyType": "SINGLE",
"orderLegCollection": [
{
"instruction": instruction,
"quantity": quantity,
"instrument": {
"symbol": symbol,
"assetType": assetType
}
}
]
}
option_single = {
"complexOrderStrategyType": "NONE",
"orderType": "LIMIT",
"session": "NORMAL",
"price": price,
"duration": duration,
"orderStrategyType": "SINGLE",
"orderLegCollection": [
{
"instruction": instruction,
"quantity": quantity,
"instrument": {
"symbol": symbol,
"assetType": assetType
}
}
]
}
# Grabbing Authorization code from file in local DIR
path = 'access_token'
access_token = open(path,'r')
code = access_token.read()
code = code.rstrip()
headers = {}
headers['Authorization'] = 'Bearer ' + code
headers['Content-Type'] = 'application/json'
#print (headers)
if orderType=='LIMIT' and assetType=='EQUITY':
print ("order Type is "+orderType)
params = json.dumps(stock_limitOrder).encode('utf8')
elif orderType=='MARKET':
print ('Order Type is (sigh) '+orderType)
params = json.dumps(stock_markerOrder).encode('utf8')
elif assetType=='OPTION' and orderType=='LIMIT':
print ('Asset Type is '+assetType)
params = json.dumps(option_single).encode('utf8')
#params = json.dumps(stock_limitOrder).encode('utf8')
print (params)
# HTTP Post Request
request = requests.post('https://api.tdameritrade.com/v1/accounts/493539792/orders', headers=headers, data=params)
request.status_code
request.json()
# Returns Error but works fine