-
Notifications
You must be signed in to change notification settings - Fork 48
/
CreateOCOOrder.py
162 lines (127 loc) · 5.75 KB
/
CreateOCOOrder.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
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
import argparse
from distutils.cmd import Command
from time import sleep
from threading import Event
from tkinter import COMMAND
from forexconnect import fxcorepy, ForexConnect, Common
import common_samples
class OrdersMonitor:
def __init__(self):
self.__order_id = None
self.__orders = {}
self.__event = Event()
def on_added_order(self, _, __, order_row):
order_id = order_row.order_id
self.__orders[order_id] = order_row
if self.__order_id == order_id:
self.__event.set()
def wait(self, time, order_id):
self.__order_id = order_id
order_row = self.find_order(order_id)
if order_row is not None:
return order_row
self.__event.wait(time)
return self.find_order(order_id)
def find_order(self, order_id):
if order_id in self.__orders:
return self.__orders[order_id]
else:
return None
def reset(self):
self.__order_id = None
self.__orders.clear()
self.__event.clear()
class Args:
l = "YOUR USERNAME / LOGIN"
p = "PASSWORD"
u = "http://www.fxcorporate.com/Hosts.jsp"
c = "Demo"
i = 'EUR/USD'
session = None
pin = None
amount = 10000
r_up = 1.059
r_dn = 1.051
account = 'ACCOUNT ID'
def main():
args = Args
str_user_id = args.l
str_password = args.p
str_url = args.u
str_connection = args.c
str_session_id = args.session
str_pin = args.pin
str_instrument = args.i
rate_up = args.r_up
rate_dn = args.r_dn
str_amount = args.amount
str_account = args.account
with ForexConnect() as fx:
fx.login(str_user_id, str_password, str_url, str_connection, str_session_id, str_pin, common_samples.session_status_changed)
try:
account = Common.get_account(fx, str_account)
if not account:
raise Exception(
"The account '{0}' is not valid".format(str_account))
else:
str_account = account.account_id
print("AccountID='{0}'".format(str_account))
offer = Common.get_offer(fx, str_instrument)
if offer is None:
raise Exception(
"The instrument '{0}' is not valid".format(str_instrument))
login_rules = fx.login_rules
trading_settings_provider = login_rules.trading_settings_provider
base_unit_size = trading_settings_provider.get_base_unit_size(
str_instrument, account)
valuemap_oco = fx.session.request_factory.create_value_map()
valuemap_oco.set_string(fxcorepy.O2GRequestParamsEnum.COMMAND, fxcorepy.Constants.Commands.CREATE_OCO)
valuemap_up = fx.session.request_factory.create_value_map()
valuemap_up.set_string(fxcorepy.O2GRequestParamsEnum.COMMAND, fxcorepy.Constants.Commands.CREATE_ORDER)
valuemap_up.set_string(fxcorepy.O2GRequestParamsEnum.ORDER_TYPE, fxcorepy.Constants.Orders.STOP_ENTRY)
valuemap_up.set_string(fxcorepy.O2GRequestParamsEnum.ACCOUNT_ID, str_account)
valuemap_up.set_string(fxcorepy.O2GRequestParamsEnum.OFFER_ID, offer.offer_id)
valuemap_up.set_string(fxcorepy.O2GRequestParamsEnum.BUY_SELL, fxcorepy.Constants.BUY)
valuemap_up.set_int(fxcorepy.O2GRequestParamsEnum.AMOUNT, str_amount)
valuemap_up.set_double(fxcorepy.O2GRequestParamsEnum.RATE, rate_up)
valuemap_oco.append_child(valuemap_up)
valuemap_down = fx.session.request_factory.create_value_map()
valuemap_down.set_string(fxcorepy.O2GRequestParamsEnum.COMMAND, fxcorepy.Constants.Commands.CREATE_ORDER)
valuemap_down.set_string(fxcorepy.O2GRequestParamsEnum.ORDER_TYPE, fxcorepy.Constants.Orders.STOP_ENTRY)
valuemap_down.set_string(fxcorepy.O2GRequestParamsEnum.ACCOUNT_ID, str_account)
valuemap_down.set_string(fxcorepy.O2GRequestParamsEnum.OFFER_ID, offer.offer_id)
valuemap_down.set_string(fxcorepy.O2GRequestParamsEnum.BUY_SELL, fxcorepy.Constants.SELL)
valuemap_down.set_int(fxcorepy.O2GRequestParamsEnum.AMOUNT, str_amount)
valuemap_down.set_double(fxcorepy.O2GRequestParamsEnum.RATE, rate_dn)
valuemap_oco.append_child(valuemap_down)
request = fx.session.request_factory.create_order_request(valuemap_oco)
orders_monitor = OrdersMonitor()
orders_table = fx.get_table(ForexConnect.ORDERS)
orders_listener = Common.subscribe_table_updates(orders_table, on_add_callback=orders_monitor.on_added_order)
try:
resp = fx.send_request(request)
order_id = resp.order_id
except Exception as e:
common_samples.print_exception(e)
orders_listener.unsubscribe()
else:
# Waiting for an order to appear or timeout (default 30)
order_row = orders_monitor.wait(30, order_id)
if order_row is None:
print("Response waiting timeout expired.\n")
else:
print("The order has been added. OrderID={0:s}, "
"Type={1:s}, BuySell={2:s}, Rate={3:.5f}, TimeInForce={4:s}".format(
order_row.order_id, order_row.type, order_row.buy_sell, order_row.rate,
order_row.time_in_force))
sleep(1)
orders_listener.unsubscribe()
except Exception as e:
common_samples.print_exception(e)
try:
fx.logout()
except Exception as e:
common_samples.print_exception(e)
if __name__ == "__main__":
main()
input("Done! Press enter key to exit\n")