-
Notifications
You must be signed in to change notification settings - Fork 89
/
dwx_client_example.py
137 lines (82 loc) · 4.17 KB
/
dwx_client_example.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
import json
from time import sleep
from threading import Thread
from os.path import join, exists
from traceback import print_exc
from random import random
from datetime import datetime, timezone, timedelta
from api.dwx_client import dwx_client
"""
Example dwxconnect client in python
This example client will subscribe to tick data and bar data. It will also request historic data.
!!! ----- IMPORTANT ----- !!!
If open_test_trades=True, it will open many trades.
Please only run this on a demo account!
!!! ----- IMPORTANT ----- !!!
"""
class tick_processor():
def __init__(self, MT4_directory_path,
sleep_delay=0.005, # 5 ms for time.sleep()
max_retry_command_seconds=10, # retry to send the commend for 10 seconds if not successful.
verbose=True
):
# if true, it will randomly try to open and close orders every few seconds.
self.open_test_trades = False
self.last_open_time = datetime.now(timezone.utc)
self.last_modification_time = datetime.now(timezone.utc)
self.dwx = dwx_client(self, MT4_directory_path, sleep_delay,
max_retry_command_seconds, verbose=verbose)
sleep(1)
self.dwx.start()
# account information is stored in self.dwx.account_info.
print("Account info:", self.dwx.account_info)
# subscribe to tick data:
self.dwx.subscribe_symbols(['EURUSD', 'GBPUSD'])
# subscribe to bar data:
self.dwx.subscribe_symbols_bar_data([['EURUSD', 'M15'], ['GBPJPY', 'M5'], ['AUDCAD', 'M1']])
# request historic data:
end = datetime.now(timezone.utc)
start = end - timedelta(days=30) # last 30 days
self.dwx.get_historic_data('EURUSD', 'D1', start.timestamp(), end.timestamp())
def on_tick(self, symbol, bid, ask):
now = datetime.now(timezone.utc)
print('on_tick:', now, symbol, bid, ask)
# to test trading.
# this will randomly try to open and close orders every few seconds.
if self.open_test_trades:
if now > self.last_open_time + timedelta(seconds=3):
self.last_open_time = now
order_type = 'buy'
price = ask
if random() > 0.5:
order_type = 'sell'
price = bid
self.dwx.open_order(symbol=symbol, order_type=order_type,
price=price, lots=0.5)
if now > self.last_modification_time + timedelta(seconds=10):
self.last_modification_time = now
for ticket in self.dwx.open_orders.keys():
self.dwx.close_order(ticket, lots=0.1)
if len(self.dwx.open_orders) >= 10:
self.dwx.close_all_orders()
# self.dwx.close_orders_by_symbol('GBPUSD')
# self.dwx.close_orders_by_magic(0)
def on_bar_data(self, symbol, time_frame, time, open_price, high, low, close_price, tick_volume):
print('on_bar_data:', symbol, time_frame, datetime.now(timezone.utc), time, open_price, high, low, close_price)
def on_historic_data(self, symbol, time_frame, data):
# you can also access the historic data via self.dwx.historic_data.
print('historic_data:', symbol, time_frame, f'{len(data)} bars')
def on_historic_trades(self):
print(f'historic_trades: {len(self.dwx.historic_trades)}')
def on_message(self, message):
if message['type'] == 'ERROR':
print(message['type'], '|', message['error_type'], '|', message['description'])
elif message['type'] == 'INFO':
print(message['type'], '|', message['message'])
# triggers when an order is added or removed, not when only modified.
def on_order_event(self):
print(f'on_order_event. open_orders: {len(self.dwx.open_orders)} open orders')
MT4_files_dir = 'C:/Users/Administrator/AppData/Roaming/MetaQuotes/Terminal/3B534B10135CFEDF8CD1AAB8BD994B13/MQL4/Files/'
processor = tick_processor(MT4_files_dir)
while processor.dwx.ACTIVE:
sleep(1)