add a simple MatLab example with Output Windows

This commit is contained in:
Christian_x7
2020-10-24 11:47:42 +02:00
parent 9eedf15e9e
commit 258a98f7d5
5 changed files with 300 additions and 0 deletions
@@ -0,0 +1,135 @@
classdef ConnectToMT5_Api < handle
%CONNECTMT5 Connects to the MtApi Client via .NET
properties
h;
hOutputWindow
end
methods
%xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
function obj = ConnectToMT5_Api
obj.h = MtApi5.MtApi5Client();
end
%xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
function startMT5 (self,addr,port)
%% Handle to the .NET client
fprintf('Connecting...\n');
self.h.BeginConnect(addr,port);
end
function stopMT5(self)
self.h.BeginDisconnect();
end
function setOutputWindowHandle(self,h)
self.hOutputWindow = h;
end
%%
function [copied_cnt, MqlRates] = getHistoryBars(self)
symbol = 'EURUSD';
period = 'M1';
start_cnt = 0;
end_cnt = 5;
[ copied_cnt,MqlRates ] = self.h.CopyRates(symbol,period,start_cnt,end_cnt);
end
% *************************************************************************
% **************** Api Function **********************************************
%**************************************************************************
%% double = AccountInfoDouble(enum MtApi5.ENUM_ACCOUNT_INFO_DOUBLE)
function res = AccountInfoDouble(self,enum)
res = self.h.AccountInfoDouble(enum);
end
%% uint32 = SymbolsTotal(bool selected)
function res = SymbolsTotal(self,selected)
res = self.h.SymbolsTotal(selected);
end
%% Market Info
%% public bool SymbolInfoTick(string symbol, out MqlTick tick)
function [mqltick] = SymbolInfoTick(self,symbol)
[mqltick] = self.h.SymbolInfoTick(symbol);
end
%% Trading functions
%% bool OrderSend(MqlTradeRequest request, out MqlTradeResult result)
%%
function [res,result] = OrderSend(self, request)
[res,result] = self.h.OrderSend(request);
end
%%
function r = setQuoteListener(self)
r = addlistener(self.h, 'QuoteUpdate', @self.quoteListener);
end
function r = setQuoteAddedListener(self)
r = addlistener(self.h, 'QuoteAdded', @self.quoteAddedListener);
end
function r = setConnectionListener(self)
r = addlistener(self.h, 'ConnectionStateChanged', @self.connectionListener);
end
function quoteAddedListener(event)
askString = num2str(event.Quote.Ask,'%01.5f');
% askString = sprintf(event.Quote.Ask,'%01.5f');
bidString = num2str(event.Quote.Bid,'%01.5f');
% bidString = num2str(event.Quote.Bid,'%01.5f');
transmitString = strcat(askString,',',bidString);
fprintf('%s: %s\n',char(event.Quote.Instrument),transmitString);
end
function quoteListener(self,~,event)
askString = num2str(event.Quote.Ask,'%01.5f');
bidString = num2str(event.Quote.Bid,'%01.5f');
transmitString = strcat(askString,',',bidString);
text_to = sprintf('%s: %s\n',char(event.Quote.Instrument),transmitString);
NT_TextToOutputWindow(self.hOutputWindow,text_to);
end
function connectionListener(self, ~, event)
connectState = event.Status.ToString;
text_to = sprintf('Connection changed: %s\n', char(connectState));
NT_TextToOutputWindow(self.hOutputWindow,text_to);
if (connectState.eq('Failed') == true)
text_to = sprintf('Reason: %s\n', char(event.ConnectionMessage));
NT_TextToOutputWindow(self.hOutputWindow,text_to);
end
end
end
end
@@ -0,0 +1,24 @@
function [FigH,ListH] = NT_CreateOutputWindow(windowname,position)
FigH = figure('Name',windowname, ...
'menubar', 'none', ...
'Position', position,...
'NumberTitle','off');
ListH = uicontrol('Style', 'listbox', ...
'Units', 'normalized', ...
'Position', [0,0,1,1], ...
'String', {}, ...
'Min', 0, 'Max', 2, ...
'Value', []);
end
% for i = 1:10
% pause(0.5);
% Str = datestr(now, 0);
% newString = cat(1, get(ListH, 'String'), {Str});
% set(ListH, 'String', newString);
% drawnow;
% end
@@ -0,0 +1,32 @@
function [] = NT_TextToOutputWindow(ListH,Text)
if isempty(Text)
error(' Text for Outputwindow is empty ');
Text = 'error';
end
% FigH = figure('Name',windowname, ...
% 'menubar', 'none', ...
% 'NumberTitle','off');
%
% ListH = uicontrol('Style', 'listbox', ...
% 'Units', 'normalized', ...
% 'Position', [0,0,1,1], ...
% 'String', {}, ...
% 'Min', 0, 'Max', 2, ...
% 'Value', []);
newString = cat(1, get(ListH, 'String'), {Text});
set(ListH, 'String', newString);
drawnow;
end
+107
View File
@@ -0,0 +1,107 @@
function TestClient
% Main program
% interfaces to the MT5 client
% receives price data from MT5 via .NET interface
% recieives history bars from MT5
ordersend_ok = false;
[output_figH,output_listH] = NT_CreateOutputWindow('Test OutputWindow ',[400,400,800,200]);
NT_TextToOutputWindow(output_listH,'Startup ...');
[Trades_figH,Trades_listH] = NT_CreateOutputWindow('Trades OutputWindow ',[400,800,800,200]);
NT_TextToOutputWindow(Trades_listH,'Startup Trades Window ...');
[Events_figH,Events_listH] = NT_CreateOutputWindow('Events OutputWindow ',[400,1200,800,200]);
NT_TextToOutputWindow(Events_listH,'Startup Events Window ...');
format short
MTApi5_dll_Location = 'C:\Program Files\MtApi5\MtApi5.dll';
%% Connect to .NET
asm1 = NET.addAssembly (MTApi5_dll_Location);
NT_TextToOutputWindow(output_listH,'MtApi5.dll loaded ...');
%% Open the Metatrader Link via .NET
MT5 = ConnectToMT5_Api; % creates the MT5 object
MT5.setOutputWindowHandle(Events_listH) % copy output window pointer to listener
MT5.setConnectionListener; % create connection listener
MT5.setQuoteListener; % create quote listener
MT5.setQuoteAddedListener; % create quote added listener
port = 8301;
addr = '127.0.0.1';
MT5.startMT5(addr,port); % establishes .NET connection
NT_TextToOutputWindow(output_listH,'Setup Mt5Api ...');
while MT5.h.ConnectionState ~= MtApi5.Mt5ConnectionState.Connected
pause(0.01);
NT_TextToOutputWindow(output_listH,'Waiting for Connection....');
end
NT_TextToOutputWindow(output_listH,'Connected ...');
%% Test SymbolsTotal
text_to = sprintf('Symbols= %d ', MT5.SymbolsTotal(true));
NT_TextToOutputWindow(output_listH,text_to);
%% Test AccountInfo
text_to = sprintf('Balance = %d \n',MT5.AccountInfoDouble(MtApi5.ENUM_ACCOUNT_INFO_DOUBLE.ACCOUNT_BALANCE));
NT_TextToOutputWindow(output_listH,text_to);
%% Test OrderSend
request = MtApi5.MqlTradeRequest;
tick = MtApi5.MqlTick;
% [tick] = MT5.SymbolInfoTick('EURUSD');
request.Action = MtApi5.ENUM_TRADE_REQUEST_ACTIONS.TRADE_ACTION_DEAL;
request.Symbol = 'EURUSD';
request.Deviation = 30;
request.Volume = 0.01; %% Account MIN 10000 !
request.Type = MtApi5.ENUM_ORDER_TYPE.ORDER_TYPE_BUY;
request.Price = tick.ask;
try
[ordersend_ok,result] = MT5.OrderSend(request);
catch exception
text_to = sprintf('NET Error in OrderSend = %s\n ',exception.message);
NT_TextToOutputWindow(Trades_listH,text_to);
end
if (ordersend_ok)
text_to = sprintf('OrderSend OK \n');
NT_TextToOutputWindow(Trades_listH,text_to);
text_to = sprintf('Result: %s \n',char(result.ToString));
NT_TextToOutputWindow(Trades_listH,text_to);
else
text_to = sprintf('OrderSend ERROR \n');
NT_TextToOutputWindow(Trades_listH,text_to);
end
%% while not pressed 0
while true
pause(0.1);
prompt = 'Press 0 key to stop\n';
x = input(prompt);
if (x == 0)
break;
end
end
MT5.stopMT5;
fprintf('Connection Stopped \n');
end
+2
View File
@@ -0,0 +1,2 @@
TestClient();