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
348 stars 230 forks source link

ZeroMQ Connector failed to read response from server when compiled into a program #75

Closed hoangson0409 closed 3 years ago

hoangson0409 commented 4 years ago

I was following the 7 video tutorial on how to interface python with metatrader 4 and got no problem running them on iPython kernel By no problem I mean a trade was sent to the server with ease and client (my laptop) received proper response after each action is executed. When I call zmq._get_response_() I can get access to server response.

The problem starts to arise when I want to compile the DWX_ZeroMQ_Connector class within a program. So I start with a simple program just like this :

def main():  
zmq = DWX_ZeroMQ_Connector()
my_trade = { '_action':       'OPEN',
             '_type':         0,
             '_symbol':       'USOIL',
             '_price':        0.0,
             '_SL':           0,
             '_TP':           0,
             '_comment':      'dwx-zeromq',
             '_lots':         0.01,
             '_magic':   123456,
             '_ticket':       0
             }

zmq._DWX_MTX_NEW_TRADE_(_order=my_trade)

reply = zmq._get_response_()  
all_trade = zmq._DWX_MTX_GET_ALL_OPEN_TRADES_()
print("reply" + str(reply))
print("----------------------")
print("alltrade"+ str(all_trade))

main() 

his script STILL SENDS ORDERS SUCCESSFULLY, BUT DID NOT PROMPT ANY RESPONSE BACK as the reply turned out to be null. As I could get response and send data back and forth using ipython console, I guess you could eliminate dependencies as source of problem. Already installed the VC++ libraries from 2017.

A more comprehensive description of my issues can be found here, where I also showed my printed version in the console, logs of both MT4 Terminal and Experts and etc.

Please do not hesisitate asking me to send any additional information as I was struggling with this for quite a while. Cheers

chand2020 commented 4 years ago

Hey, Refer this link for possible solution. You will not get response as soon as request send. Response will be delayed. So does your code should wait until you get response back. https://github.com/darwinex/dwx-zeromq-connector/issues/67#issuecomment-614401255

hoangson0409 commented 4 years ago

Hey, Refer this link for possible solution. You will not get response as soon as request send. Response will be delayed. So does your code should wait until you get response back. #67 (comment)

Hi @SpideyAntman, could you kindly point out how can I fix my code in this case ? The example given was probably from an older version and I could not find methods that was used in the example. So far I have tried adding

while zmq._ACTIVE == True:

            reply = zmq._get_response_()  
            all_trade = zmq._DWX_MTX_GET_ALL_OPEN_TRADES_()
            print("reply" + str(reply))
            print("----------------------")
            print("alltrade"+ str(all_trade))
            print(reply == None)
            print(all_trade == None)

This doesnt seem to work

And also tried using async function

async def main():  

   zmq = DWX_ZeroMQ_Connector()
   my_trade = { '_action':       'OPEN',
             '_type':         0,
             '_symbol':       'USOIL',
             '_price':        0.0,
             '_SL':           0,
             '_TP':           0,
             '_comment':      'dwx-zeromq',
             '_lots':         0.01,
             '_magic':   123456,
             '_ticket':       0
             }

   zmq._DWX_MTX_NEW_TRADE_(_order=my_trade)

   reply = await zmq._get_response_()  
   all_trade = zmq._DWX_MTX_GET_ALL_OPEN_TRADES_()
   print("reply" + str(reply))
   print("----------------------")
   print("alltrade"+ str(all_trade))

main() 

Ah and this would not work too

brenoperucchi commented 4 years ago

@hoangson0409 How you solved your problem?

elvinex commented 3 years ago

Hi, you have to give MT4 time to answer the request. You could add a for loop to check the result and if it is still None, use time.sleep(0.1) to check later again.

unesssaeed commented 3 years ago

@elvinex hey dude, may you share your code? I have the same problem

elvinex commented 3 years ago

Hi, one solution would be to sleep on the python side to give MT4 time to answer the request. You can see it right at the end of this example script: https://github.com/darwinex/dwx-zeromq-connector/blob/master/v2.0.1/python/examples/template/strategies/prices_subscriptions.py

unesssaeed commented 3 years ago

@elvinex I used this code but nothing happened info = zmq._DWX_MTX_GET_ALL_OPENTRADES() while info == None: time.sleep(1) if info != None: print(type(info)) break

elvinex commented 3 years ago

Most of the python function like _DWX_MTX_GET_ALL_OPEN_TRADES_() only send the request to MT4. They don't return anything because we don't know how long MT4 takes for an answer. Therefore it would always be None.

The answer is then stored and you could try to check if for example like this:

while True:
    time.sleep(1)
    print(zmq._get_response_())

Btw. we published the DWX Connect package which is easier to use and probably better suited for most of the use cases.

unesssaeed commented 3 years ago

@elvinex thank you very much take care of yourself cause you're the best

elvinex commented 3 years ago

Thank you, I'm glad we were able to solve this. :)