-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_fx.py
More file actions
227 lines (177 loc) · 6.61 KB
/
simple_fx.py
File metadata and controls
227 lines (177 loc) · 6.61 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import requests
from credentials import *
# create a rest client object
class Simple_FX_Client(object):
def __init__(self, api_key=None, api_secret=None,\
account=None, reality=None):
self.api_key = api_key
self.api_secret = api_secret
self.account = account
self.reality = reality
self.url = 'https://rest.simplefx.com'
self.headers = {
'accept': 'application/json',
'Content-Type': 'application/json',
}
# get a list of instruments
def list_instruments(self):
url = 'https://simplefx.com/utils/instruments.json'
response = requests.get(url, headers=self.headers)
data = response.json()
data = list(data.values())
return data
# get symbol information
def symbol_info(self, symbol):
symbol_list = self.list_instruments()
for i in symbol_list:
data = i.get('symbol')
if data == symbol:
data = i
return data
# get authentication token
def auth_key(self):
options = {'clientId':self.api_key, 'clientSecret':self.api_secret}
url = self.url + '/api/v3/auth/key'
response = requests.post(url, headers=self.headers, json=options)
json_response = response.json()
token = json_response['data']
token = token['token']
return token
# get overview of all accounts
def accounts_overview(self):
self.headers['Authorization'] = self.auth_key()
url = self.url + '/api/v3/accounts'
response = requests.get(url, headers=self.headers)
data = response.json()
return data
# get status of of individual account
def account_status(self):
self.headers['Authorization'] = self.auth_key()
url = self.url + '/api/v3/accounts/{}/{}'.format(
self.reality, self.account)
response = requests.get(url, headers=self.headers)
data = response.json()
return data
# place a market order
def market_order(self, symbol, side, vol, tp=None, sl=None):
self.headers['Authorization'] = self.auth_key()
url = self.url + '/api/v3/trading/orders/market'
options = {
"Reality":self.reality,
"Login":self.account,
"Symbol":symbol,
"Side":side,
"Volume":vol,
"RequestId":"string"
}
if tp is not None:
options['TakeProfit'] = tp
if sl is not None:
options['StopLoss'] = sl
response = requests.post(url, headers=self.headers, json=options)
data = response.json()
return data
# place a pending order
def pending_order(self, symbol, side, price, vol, tp=None, sl=None):
self.headers['Authorization'] = self.auth_key()
url = self.url + '/api/v3/trading/orders/pending'
options = {
"Reality":self.reality,
"Login":self.account,
"Symbol":symbol,
"ActivationPrice":price,
"Volume":vol,
"Side":side,
"RequestId":"string",
}
if tp is not None:
options['TakeProfit'] = tp
if sl is not None:
options['StopLoss'] = sl
response = requests.post(url, headers=self.headers, json=options)
data = response.json()
return data
# adjust a pending order
def adjust_order(self, order_id, price, vol, tp=None, sl=None):
self.headers['Authorization'] = self.auth_key()
url = self.url + '/api/v3/trading/orders/pending'
options = {
"Reality":self.reality,
"Login":self.account,
"Id":order_id,
"ActivationPrice":price,
"Volume":vol,
"RequestId":"string",
}
if tp is not None:
options['TakeProfit'] = tp
if sl is not None:
options['StopLoss'] = sl
response = requests.put(url, headers=self.headers, json=options)
data = response.json()
return data
# adjust take profit order
def adjust_tp(self, order_id, tp):
self.headers['Authorization'] = self.auth_key()
url = self.url + '/api/v3/trading/orders/market'
options = {
"Login":self.account,
"Reality":self.reality,
"Id":order_id,
"TakeProfit":tp
}
response = requests.put(url, headers=self.headers, json=options)
data = response.json()
return data
# adjust stop loss order
def adjust_sl(self, order_id, sl):
self.headers['Authorization'] = self.auth_key()
url = self.url + '/api/v3/trading/orders/market'
options = {
"Login":self.account,
"Reality":self.reality,
"Id":order_id,
"StopLoss":sl
}
response = requests.put(url, headers=self.headers, json=options)
data = response.json()
return data
# delete a pending order
def delete_order(self, order_id):
self.headers['Authorization'] = self.auth_key()
url = self.url + '/api/v3/trading/orders/pending'
options = {
"Login":self.account,
"Reality":self.reality,
"Id":order_id,
"RequestId":"string",
}
response = requests.delete(url, headers=self.headers, json=options)
data = response.json()
return data
# close an open position
def close_position(self, order_id, vol):
self.headers['Authorization'] = self.auth_key()
url = self.url + '/api/v3/trading/orders/market'
options = {
"Login":self.account,
"Reality":self.reality,
"Id":order_id,
"Volume":vol,
"RequestId":"string",
}
response = requests.delete(url, headers=self.headers, json=options)
data = response.json()
return data
# get positions and pending orders
def get_positions(self):
self.headers['Authorization'] = self.auth_key()
url = self.url + '/api/v3/trading/orders/active'
options = {
"Login":self.account,
"Reality":self.reality,
}
response = requests.post(url, headers=self.headers, json=options)
data = response.json()
return data
client = Simple_FX_Client(api, secret, account, reality)