-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrader.py
More file actions
108 lines (83 loc) · 3.47 KB
/
trader.py
File metadata and controls
108 lines (83 loc) · 3.47 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
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
import datetime
import requests
import json
import dotenv
import os
# Toggle between sending requests to the live or demo environment
# I would recommend using the demo environment to test your code before going live
# WARNING: DO NOT SWITCH TO MD - THIS WILL BREAK EVERYTHING
# MD IS MARKET DATA AND DOES NOT ALLOW FOR ORDER PLACEMENT OR ACCOUNT AUTHENTICATION
# version = "live"
version = "demo"
dotenv.load_dotenv()
# These are the environment variables that you need to set in your .env file
name = os.getenv('TRADOVATE_NAME')
password = os.getenv('TRADOVATE_PASSWORD')
app_id = os.getenv('TRADOVATE_APP_ID')
app_version = os.getenv('TRADOVATE_APP_VERSION')
cid = os.getenv('TRADOVATE_CID')
sec = os.getenv('TRADOVATE_SEC')
device_id = os.getenv('TRADOVATE_DEVICE_ID')
# This function will place an order for you
def place_order(symbol, order_action, quantity, account_spec, account_id):
with open('./accessToken.json') as f:
access_token = json.load(f)['accessToken']
initial = {
"accountSpec": account_spec,
"accountId": account_id,
"symbol": symbol,
"action": order_action,
"orderQty": quantity,
"orderType": "Market",
"isAutomated": True
}
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {access_token}"
}
response = requests.post(f"https://{version}.tradovateapi.com/v1/order/placeorder", headers=headers, data=json.dumps(initial))
print(f'Tradovate Status: {response.status_code}')
print(response.text)
# This function will cancel an order for you
def cancel_order(account_id, contract_id):
with open('./accessToken.json') as f:
access_token = json.load(f)['accessToken']
body = {
"accountId": account_id,
"contractId": contract_id,
"admin": False
}
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {access_token}"
}
response = requests.post(f'https://{version}.tradovateapi.com/v1/order/liquidateposition', headers=headers, data=json.dumps(body))
print(f'Tradovate Status: {response.status_code}')
print(response.text)
# This function will refresh your access token
# It is advisable to call this function in a loop to keep your access token fresh
# As of my present knowledge, the access token expires every hour
def refresh_access_token():
print(f"Refreshing your access token... [{datetime.datetime.now()}]")
# You must register an app on tradovate's website prior to these variables working properly
body = {
"name": f"{name}",
"password": f"{password}",
"appId": f"{app_id}",
"appVersion": f"{app_version}",
"cid": cid,
"sec": f"{sec}",
"deviceId": f"{device_id}"
}
response = requests.post(f'https://{version}.tradovateapi.com/v1/auth/accesstokenrequest', json=body)
if response.status_code == 200:
print(f"Access token successfully refreshed [{datetime.datetime.now().isoformat()}]")
try:
data = {"accessToken": response.json()['accessToken']}
except Exception as e:
# Most common exception is thrown because you forgot to register your app properly
print(f"Error: {e}")
print(f"Response: {response.text}")
return
with open("./accessToken.json", 'w') as f:
json.dump(data, f)