-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathChangeOrder.py
More file actions
146 lines (111 loc) · 4.19 KB
/
ChangeOrder.py
File metadata and controls
146 lines (111 loc) · 4.19 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
# example code how to change order, it can be used for any waiting order, attached stop or limit
#the AMOUNT field is optional,
import argparse
from time import sleep
from threading import Event
from forexconnect import fxcorepy, ForexConnect, Common
import common_samples
def parse_args():
parser = argparse.ArgumentParser(description='Process command parameters.')
common_samples.add_main_arguments(parser)
common_samples.add_instrument_timeframe_arguments(parser, timeframe=False)
common_samples.add_direction_rate_lots_arguments(parser)
common_samples.add_account_arguments(parser)
args = parser.parse_args()
return args
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 = "username"
p = "password"
u = "http://www.fxcorporate.com/Hosts.jsp"
c = "Demo"
i = 'CAD/JPY'
session = None
pin = None
lots = 10
r = 104.568
stop_ord = 'S'
limit_ord = 'L'
account = 'account id'
order_id = 'order_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
str_rate = args.r
lots = args.lots
str_account = args.account
str_order_type = args.limit_ord
order_id = args.order_id
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)
amount = base_unit_size * lots
request = fx.create_order_request(
command=fxcorepy.Constants.Commands.EDIT_ORDER,
order_type=str_order_type,
OFFER_ID=offer.offer_id,
ACCOUNT_ID=str_account,
RATE=str_rate,
AMOUNT=lots,
ORDER_ID=order_id,
)
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)
except Exception as e:
common_samples.print_exception(e)
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")