Connect to Meta trader server using c# and console application in order to place orders automatically

 
Hi,

I want to send to meta trader orders automatically from an external API. For example we receive semnals from a API and we want to place orders automatically in metatrader4/5.
Is there a way to do this?
We think to code an personal application using c# ( console application) and connect to meta trader broker server using TCP/IP but we are not able to connect to the broker server.
We are receiving this error message:
Eroare: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 188.40.223.58:433

My code is:
using System;
using System.Net.Sockets;
using System.Text;

public class BrokerClient
{
    private const string ServerIpAddress = "broker-server.com"; // Broker server IP address
    private const int ServerPort = 1234; // Broker server port

    public static void Main()
    {
        try
        {
            // Create a TcpClient object and connect to the broker server
            TcpClient client = new TcpClient(ServerIpAddress, ServerPort);

            Console.WriteLine("Connected to the broker server.");

            // Get the data streams for reading and writing
            NetworkStream stream = client.GetStream();

            // Send a message to the broker server
            string message = "Hello, broker server!";
            byte[] data = Encoding.ASCII.GetBytes(message);
            stream.Write(data, 0, data.Length);

            Console.WriteLine("Message sent to the broker server.");

            // Receive the response from the broker server
            data = new byte[256];
            StringBuilder responseData = new StringBuilder();
            int bytes = stream.Read(data, 0, data.Length);
            responseData.Append(Encoding.ASCII.GetString(data, 0, bytes));
            Console.WriteLine("Response from the broker server: " + responseData.ToString());

            // Close the connection
            stream.Close();
            client.Close();
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error: " + ex.Message);
        }

        Console.ReadLine();
    }
}

Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Trade Operation Types
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Trade Operation Types
  • www.mql5.com
Trade Operation Types - Trade Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
When you post code please use the Code button (Alt+S) !

Please EDIT your post and use the CODE button (Alt-S) when inserting code.

Code button in editor

Hover your mouse over your post and select "edit" ... 

MQL5.community - User Memo
MQL5.community - User Memo
  • www.mql5.com
You have just registered and most likely you have questions such as, "How do I insert a picture to my a message?" "How do I format my MQL5 source code?" "Where are my personal messages kept?" You may have many other questions. In this article, we have prepared some hands-on tips that will help you get accustomed in MQL5.community and take full advantage of its available features.
 

You have shown C# code but what does that have to do with MQL and MetaTrader?

MetaQuotes trading servers use a propriety protocol and it's not possible to connect directly.

You have to go via MetaTrader but MetaTrader 5 does not offer any API for C#, only Python.

If you want to have MetaTrader receive instructions from an external source, then you will have to create a MQL program to do so.

 
Hi Fernando,

Thanks for your message. You have a project example for this to share with us? :)

Regards,
Ionel
 
ionel stefanuca #:
Hi Fernando,

Thanks for your message. You have a project example for this to share with us? :)

Regards,
Ionel

There are plenty of example using C# with MQL on this site. Please do a search before asking.

You need to go through MT5 Terminal, you can't connect directly to an MT5 server.