skip to Main Content

I want an Expert Advisor to open an Trade triggerd by a Telegram-Message.

I succesfully set up an Hello-World application using MQ4 as Server and Python/Telegram-bot as Client.
When the Telegram-Bot recieves a Message, he will send a request to MQ4 and gets a simple response without executing a trade.

Running Code below.

#   Hello World client in Python
#   Connects REQ socket to tcp://localhost:5555

import zmq
context = zmq.Context()

#  Socket to talk to server
print("Connecting to trading server…")
socket = context.socket(zmq.REQ)
socket.connect("tcp://localhost:5555")
print("Connecting to trading server succeed")


#################################################################################
# Use your own values from my.telegram.org
api_id = ######
api_hash = '#####'
bot_token = '#####'
#################################################################################

from telethon import TelegramClient, events

client = TelegramClient('anon', api_id, api_hash)

@client.on(events.NewMessage)
async def my_event_handler(event):


    if "Ascending" in event.raw_text:

        if "AUDUSD" in event.raw_text:
            await event.reply("AUDUSD sell")

            #  Do 1 request, waiting for a response
            for request in range(1):
                print("Telegram: AUDUSD sell execution requested %s …" % request)
                socket.send(b"AUDUSD Sell execute")
                #Send 2 variables (Ordertype // Symbol)

                #  Get the reply. -> Not neccesary for final application
                #  Apülication just needs to send 2 Varianles to MQ4 and trigger the open_order()
                message = socket.recv()
                print("Received reply %s [ %s ]" % (request, message))



client.start()
client.run_until_disconnected()

// Hello World server in MQ4

#include <Zmq/Zmq.mqh>

//+------------------------------------------------------------------+
 void OnTick()
  {
   Context context("helloworld");
   Socket socket(context,ZMQ_REP);

   socket.bind("tcp://*:5555");

   while(!IsStopped())
     {
      ZmqMsg request;

      // Wait for next request from client

      // MetaTrader note: this will block the script thread
      // and if you try to terminate this script, MetaTrader
      // will hang (and crash if you force closing it)

      socket.recv(request);
      Print("Receive: AUDUSD Sell execute");

      Sleep(1000);

      ZmqMsg reply("Trade was executed");
      // Send reply back to client
      socket.send(reply);
      Print("Feedback: Trade was executed");
     }
  }
//+------------------------------------------------------------------+

Now I want to send 2 variables from Python to MQ4.
1. Ordertype: buy/sell
2. Symbol: EURUSD, AUDUSD,…

Send “Sell” if message contains “Ascending” –
Send “Buy” if message contains “Descending”

Send “AUDUSD” if message contains “AUDUSD”,…

To do so I found a Library from Darwinex and want to combine it (interpretation of message, sending value as an array) with my already functioning telegram-bot.


For testing I wanted to try the example-code from Darwinex by itself.

I found the Code v2.0.1:

Python:
https://github.com/darwinex/DarwinexLabs/blob/master/tools/dwx_zeromq_connector/v2.0.1/Python/DWX_ZeroMQ_Connector_v2_0_1_RC8.py

MQ4: (Note: This Library code may replace the whole MQ4 code above in final app.)
https://github.com/darwinex/DarwinexLabs/blob/master/tools/dwx_zeromq_connector/v2.0.1/MQL4/DWX_ZeroMQ_Server_v2.0.1_RC8.mq4

When I copy the Code without changing I get an error in Python:

NameError: name ‘_zmq’ is not defined

After running: _zmq._DWX_ZeroMQ_Connector() – in the Kernel of Spyder.

What can I do to fix that error?


In the final state I want to run the Python-Code and the Expert Advisor on the same Windows Server 2012 R2.

Is it enough if I run the .py file in the powershell from the server or should I host the file with the Webside?

I expect to get the whole system/examplecode running on my VPS or Webside-Host-Server and get an testing environment for further coding action, but currenty I cant get the Library Code in Python to run properly.

Also the MT4 ceeps crashing with the current code – but should be fixed if I combine my application with the Library-Codeexample.

(running everything on my local PC with WIN 10).

2

Answers


  1. Q : I think it is a connection-problem between MT4 and Python.

    Without a fully reproducible MCVE-code this is undecideable.

    Having used a ZeroMQ-based bidirectional signalling/messaging between a QuantFX in python and trading ecosystem MetaTrader 4 Terminal implemented in MQL4, there is a positive experience of using this architecture.

    Details decide.


    The Best Next Step :

    Start with a plain PUSH/PULL archetype python-PUSH-es, MQL4-script-PULL-s, preferably using tcp:// transport-class ( win platforms need not be ready to use an even simpler, protocol-less, ipc:// transport-class.

    Once you have posack’d this trivial step, move forwards.

    Q : How do I need to setup my Server to get a connection betwen those two – since it should be the same as on my local PC?

    It is normal to use ZeroMQ on the same localhost during prototyping, so you may test and debug the integration. For details on ZeroMQ, feel free to read all details in other posts.

    Q : Is it enough if I run the .py file in the powershell from the server or should I host the file with the Webside I already have and use that as “Python-Server”?

    Yes, in case the .py file was designed that way. No code, no advice. That simple.


    Possible issues :

    Versions – ZeroMQ, since 2.11.x till the recent 4.3.+, has made a lot of changes
    Installation DLL-details matter.

    MQL4 has similarly gone through many changes ( string ceased to be a string and become struct to name a biggest impacting one ), so start with simple scenarios and integrate the target architecture in steps / phases with due testing whether the completed phases work as expected.

    Login or Signup to reply.
  2. to fix that problem you need this:

    from DWX_ZeroMQ_Connector_v2_0_1_RC8 import DWX_ZeroMQ_Connector
    _zmq = DWX_ZeroMQ_Connector()
    

    (adjust your version of the connector as appropriate).
    should fix that problem.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search