Search code examples
pythonzeromqalgorithmic-tradingmql4pyzmq

How to get a value from Custom Indicator with zmq?


I have an MQL4 Custom Indicators and I make the EA ( an Expert Advisor ) to get data into MetaTrader Terminal 4 ( mt4 ).

But, how can I import my custom indicators EA to client zmq to get the value ?


Solution

  • Q : But, how can I import my custom indicators EA to client zmq to get the value ?

    Your MQL4-side ExpertAdvisor code or Script ( not the CustomIndicator one) has to import a DLL first. Then your EA code becomes capable to either zmq_bind() and receive a remote python-code .connect()-s, or ( reciprocally ) zmq_connect() to a remote python-code .bind()-exposed ZeroMQ's AccessPoint address ( via any feasible transport-class { tcp:// | pgm:// | epgm:// | vmci:// | ... } ( depends on ZeroMQ API-version implemented in DLL-wrapper ( 've been using a v2.11 one ) and on the selected ZeroMQ Scalable Formal Communications Pattern Archetype(s) )

    Having done any form or shape of this above sketched interconnect meta-plane, your code can zmq_send() / zmq_receive() data in whatever scenario you opt to implement. Just serialise / deserialise data and zmq_send() them.

    extern string ZMQ_transport_protocol = "tcp";
    extern string ZMQ_address            = "192.168.0.386";
    extern string ZMQ_outbound_port      = "1985";
    
    // Include the libzmq.dll abstration wrapper.
    #include <mql4zmq.mqh>
    
    

    ...
    //+------------------------------------------------------------------+
    int init()
    {
       int major[1],
           minor[1],
           patch[1];
    
       zmq_version( major, minor, patch );
    
       Print( "Using ZeroMQ version " + major[0] + "." + minor[0] + "." + patch[0] );
       Print( ping( "Hello World" ) );
       
       Print( "NOTE: to use the precompiled libraries you will need to have the Microsoft Visual C++ 2010 Redistributable Package installed. To Download: http://www.microsoft.com/download/en/details.aspx?id=5555" );
       
       context = zmq_init( 1 );
       speaker = zmq_socket( context, ZMQ_PUB );
       
       outbound_connection_string =         ZMQ_transport_protocol
                                  + "://" + ZMQ_server_address
                                  + ":"   + ZMQ_outbound_port;
    
       if ( zmq_connect( speaker, outbound_connection_string ) == -1 )
       {
          Print( "Error connecting the speaker to the listener's address:port!" );
          return( -1 );
       }
       return( 0 );
    }
    

    //+------------------------------------------------------------------+
    //| Script start function -OR- re-use as an Expert Advisor int OnTick(...){...} template
    //+------------------------------------------------------------------+
    int start()
    {
       ...
    
    // Publish current tick value + any iCustom(...) data
    
       string current_tick = "tick|" + AccountName() + " " + Symbol() + " " + Bid + " " + Ask + " " + Time[0];
    
       if ( s_send( speaker, current_tick ) == -1 )
            Print( "Error sending message: " + current_tick );
       else
            Print( "Published message: " + current_tick );
    
       return(0);
      }
    

    //+------------------------------------------------------------------+
    //| expert deinitialization function                                 |
    //+------------------------------------------------------------------+
    int deinit()
    {
       Comment( "Going to tidy-up..." );
    
    // Protect against memory leaks on shutdown.
       zmq_close( speaker );
       zmq_term(  context );
    
       return(0);
      }