-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathAttachStopLimitToPosition.py
145 lines (111 loc) · 4.67 KB
/
AttachStopLimitToPosition.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
# example code how to attach Stop or Limit to open position, each create_order_request() can attach one order, to attach stop and limit need to call this function 2 times
# this code shows how to attach Limit, for a Stop replace order_type='S'
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()
def main():
str_user_id = 'username'
str_password = 'password'
str_url = 'www.fxcorporate.com/Hosts.jsp'
str_connection = 'Demo'
str_session_id = None
str_pin = None
str_instrument = 'CAD/JPY'
str_buy_sell = 'B' # should be oposite direction than open position
str_rate = 104.610
str_lots = 10 # Quantity should be the same as open position
str_account = 'account_id'
trade_id = 'trade_id'
stop_ord = 'S'
limit_ord = 'L'
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 * str_lots
request = fx.create_order_request(
order_type=limit_ord,
OFFER_ID=offer.offer_id,
ACCOUNT_ID=str_account,
BUY_SELL=str_buy_sell,
AMOUNT=amount,
RATE=str_rate,
TRADE_ID=trade_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)
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")