Hosting the .NET Bridge

If you'll like to integrate MetaTrader with your custom .NET application then best way is host .NET Bridge in your application rather than in .NET Shell because ,NET Bridge functionality not limited to run only isolated experts. You can interact with attached trading terminals directly from your code.
Before using benefits of connection with MetaTrader you need to add reference to TradePlatform.MT4.Core.dll and introduce configuration section in your application. for more details regarding this see .NET Bridge Configuration section. After that you have initialize hosts mentioned for .NET Bridge at start point of your application. It should be done once per application run.

Bridge.InitializeHosts(true);

InitializeHosts function accepts single parameter - isBackground which indicates how thread pool should handled. If your application is sort of UI then probably you will need to pass 'true' to prevent _.NET Bridge_ to block application disposing. Otherwise you should pass 'false' (also by default) to keep application running after main thread was exited.

To attach _MetaTrader_ to your custom application you need to add an Expert_NET to desired chart and use QuoteListener as System_NET_HandlerName. After this done, you can interact with _MetaTrader_ by listening QuoteRecieved event of particular instance of trading terminal or by sending commands:

using TradePlatform.MT4.Core;
using TradePlatform.MT4.SDK.API;

....

        protected MetaTrader4 terminalA;

        public Form1()
        {
            Bridge.InitializeHots(true);

            terminalA = Bridge.GetTerminal(727272, "EURUSD");
            terminalA.QuoteRecieved += terminalA_QuoteRecieved;
        }

        private void terminalA_QuoteRecieved(MqlHandler mql)
        {
            double bid = mql.Bid();
            double ask = mql.Ask();

            this.BeginInvoke((MethodInvoker) (() => labelBid.Text = bid.ToString()));
            this.BeginInvoke((MethodInvoker) (() => lableAsk.Text = ask.ToString()));
        }

        private void buttonBuy_Click(object sender, EventArgs e)
        {
            terminalA
               .MqlScope(mql => mql.OrderSend("EURUSD", ORDER_TYPE.OP_BUY, 1, mql.Ask(), 3, 0, 0));
        }

Integration with WPF and Windows Forms application

One important subject here about UI application integration. All communication aspects are handled in dedicated thread pool in a name of performance and as a result QuoteRecieved event raised in another thread as your UI thread. So to be able to access UI thread from QuoteRecieved event handler you should UI thread invoker for Windows Forms and Dispatcher for WPF applications.

Accessing UI thread in Windows Forms application

private void terminalA_QuoteRecieved(MqlHandler mql)
        {
            double bid = mql.Bid();

            formReference.BeginInvoke((MethodInvoker) (() => labelBid.Text = bid.ToString()));
        }

Accessing UI thread in WPF application.

private void terminalA_QuoteRecieved(MqlHandler mql)
        {
            double bid = mql.Bid();
            Dispatcher.CurrentDispatcher.Invoke(() => this.bidLabel.Content = bid);
        }

Last edited Nov 5, 2012 at 1:55 PM by vladkalos, version 1

Comments

deanfred4741 Jul 8, 2016 at 7:09 PM 
I am receiving a NullReferenceExcepton when I invoke Bridge.InitializeHosts(true); statement. I select it through Intellisense so I know I am selecting the correct method. please let me know how to fix

xc Sep 16, 2013 at 2:49 PM 
s/Recieved/Received :)