-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcloseall.py
More file actions
166 lines (152 loc) · 6.13 KB
/
Copy pathcloseall.py
File metadata and controls
166 lines (152 loc) · 6.13 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/env python
# -*- coding: ASCII
import requests
import logging
import json
import pprint
# LIVE values
hostName = "api.ig.com"
applicationKey = 'cf746c788888880d5e0339999925b78aaaab4155' # LIVE!
userName = 'liveflea'
password = "PASSWORD_NOT_REALLY"
#Demo values - Place this below the live. Comment out when golive happens
hostName = "demo-api.ig.com"
applicationKey = "a347395392345093945893490034abababbba893" # Demo
userName = 'demoflea'
password = 'DEMOPASSWORD'
logging.basicConfig(filename="closeall.log",
level=logging.DEBUG,
format="%(asctime)s %(message)s")
logger = logging.getLogger()
loggedIn = False #* Global var for our login status
accountId = ""
accessToken = ""
refreshToken = ""
position_list = "" #List of open positions
def main():
login()
# now that we have the client ID, get a list of open positions
if loggedIn:
logging.debug("we are logged in - accountId=%s" % accountId)
getPosList()
if len(position_list) > 0:
closePositions()
getPosList()
else:
logger.error("Login failure, so we can't continue")
def login():
# Login to server using REST API
global loggedIn
global accountId
global accessToken
global refreshToken
endpoint = "gateway/deal/session"
apiVersion = 3
logger.debug(80 * "=")
headers = {'Content-Type': 'application/json; charset=utf-8',
'Accept': 'application/json; charset=utf-8',
'X-IG-API-KEY': applicationKey,
'Version': str(apiVersion)
}
data = {"identifier": userName,
"password": password}
url = "https://" + hostName + "/" + endpoint
try:
r = requests.post(url, data=json.dumps(data), headers=headers)
except:
logger.error("login error of some unknown type")
else:
status = r.status_code
headers = r.headers
logger.debug("headers=%s" % headers)
logger.error("request status=%s" % status)
loggedIn = True
d = json.loads(r.text)
logger.error("request follows...")
logger.error(r)
logger.error(d)
logger.error("clientId=%s" % d["clientId"])
accountId = d["accountId"]
lightstreamerEndpoint = d["lightstreamerEndpoint"]
logger.error("lightstreamerEndpoint=%s" % d["lightstreamerEndpoint"])
p = "fdfdggf"
oauthToken = d["oauthToken"]
# logger.error("oauthToken=%s" % d["oauthToken"])
accessToken = oauthToken["access_token"]
refreshToken = oauthToken["refresh_token"]
finally:
logger.debug("End of login")
def getPosList():
""" get list of positions """
global accessToken
global position_list
endpoint = "gateway/deal/positions" # Might be just positions
url = "https://" + hostName + "/" + endpoint
logger.debug("accessToken=%s" % accessToken)
logger.debug("refreshToken=%s" % refreshToken)
global accountId
logging.debug("now to get a pos list")
apiVersion = 2 # 1,2,3 depending upon request
headersAuth = {'Content-Type': 'application/json; charset=utf-8',
'Accept': 'application/json; charset=utf-8',
'X-IG-API-KEY': applicationKey,
# 'CST': CST_token,
'Authorization': 'Bearer ' + accessToken,
'Version': str(apiVersion),
'IG-ACCOUNT-ID': accountId,
'X-SECURITY-TOKEN': accessToken}
url = "https://" + hostName + "/" + endpoint
r = requests.get(url, headers=headersAuth)
logger.debug("r=%s" % r)
#logger.debug(accountId)
text = r.text # The json string
positions = json.loads(text)
position_list = positions["positions"]
logger.debug("number of postions = %s" % len(position_list))
print("There are %s open positions for this account" % len(position_list))
def closePositions():
# Closes all positions in position_list
# Should be able to use a DELETE instead of the POST with " '_method':'DELETE'," header.
# look at the API companion sometime, as the JS appears to do a DELETE even though the Webpage Inspect
# trace shows a POST. Confusing
global position_list
endpoint = "gateway/deal/positions/otc"
for position in position_list:
logger.debug(position)
dealId = position["position"]["dealId"]
print("closing deal %s" % dealId)
direction = position["position"]["direction"]
size = position["position"]["size"]
epic = position["market"]["epic"]
logger.debug(">>>>>The epic is:%s" % epic)
expiry = position["market"]["expiry"]
if direction == "BUY": #Invert direction as we're closing the position
direction = "SELL"
else:
direction = "BUY"
logger.debug("dealId = %s" % dealId)
apiVersion = 1 # 1 has fewer constraints but V2 gives a page not found.
headersAuth = {'Content-Type': 'application/json; charset=utf-8',
'Accept': 'application/json; charset=utf-8',
'X-IG-API-KEY': applicationKey,
'Authorization': 'Bearer ' + accessToken,
'Version': str(apiVersion),
'IG-ACCOUNT-ID': accountId,
'_method':'DELETE',
'X-SECURITY-TOKEN': accessToken}
url = "https://" + hostName + "/" + endpoint
order_type = 'MARKET'
data = {"dealId":dealId,
"direction": direction,
"orderType": order_type,
"size": size}
logger.debug("attempting to close it")
logger.debug("request payload is:-")
logger.debug(json.dumps(data))
r = requests.post(url, headers=headersAuth, data=json.dumps(data))
logger.debug(r)
text = r.text # The json string
logger.debug(text)
if __name__ == '__main__':
main()
# end of code