darwinex / dwx-zeromq-connector

Wrapper library for algorithmic trading in Python 3, providing DMA/STP access to Darwinex liquidity via a ZeroMQ-enabled MetaTrader Bridge EA.
https://blog.darwinex.com/zeromq-interface-python-r-metatrader4/
BSD 3-Clause "New" or "Revised" License
342 stars 227 forks source link

Cannot save MT4 response as a variable in Python #67

Closed ghost closed 4 years ago

ghost commented 4 years ago

I connected Python and get responses from MT4, however, there is a small issue:

import zmq
from time import sleep
from pandas import DataFrame, Timestamp
from threading import Thread
from zmq.utils.monitor import recv_monitor_message
from DWX_ZeroMQ_Connector_v2_0_1_RC8 import DWX_ZeroMQ_Connector

_zmq = DWX_ZeroMQ_Connector()
_zmq._DWX_MTX_GET_ALL_OPEN_TRADES_()
_output = _zmq._get_response_()
print("output" = _output)

When I execute above code - all open trades are printed in console in JSON format, however I cannot save it as variable. _output returns None in this case. If I try _output = _zmq._thread_data_output _output also returns None

Can you plz help? Struggling with it over a week now.

jacojoubert12 commented 4 years ago

Same problem here. Would expect any function call to return a dict with the output. For example how should you get the ticket number programmatically after opening a trade? In the tutorial it was hardcoded by looking at the output, I assumed the function call opening the trade would return a dict which can be used in the next call to access the ticket number from there. I'm not sure how the workflow should be. Any advice will be much appreciated.

integracore2 commented 4 years ago

Hi @wisenlucky and @CornerStone12,

Thank you both very much for your interest in the project 🙂

The _get_response_() function is indeed coded to return the response (when it is received).

Executing the call to _DWX_MTX_GET_ALL_OPEN_TRADES_ followed immediately by a call to _get_response_ will likely result in nothing being read from the variable. What's happening here is that data has been requested by the first call, but not yet been received as quickly as Python has executed the read after the request.

It is therefore important to enclose any calls to _DWX_MTX_xxxxx methods inside a loop with an appropriate delay.

Please see an example implementation in the class method def _trader_(self, _symbol, _max_trades): in the following file: https://github.com/darwinex/dwx-zeromq-connector/blob/master/v2.0.1/python/examples/template/strategies/coin_flip_traders_v1.0.py

Hope this proves useful!