MT4跟单系统制作第一篇 MT4Api接口
作者:互联网
namespace DoFollowFx.Com
{
public class Mt4Manager
{
private MainServer _mainServer;
private QuoteClient _quoteClient;
private OrderClient _orderClient;
private Server[] _slaves;
public int Id { get; }
public int Type { get; }
public uint User { get; }
public string PlatformName { get; }
public string Password { get; set; }
public string PlatformPath { get; }
private Action<object, Mt4ManagerOrderUpdateEventArgs> _onMt4ManagerOrderUpdateHandler;
private Action<object, Mt4ManagerConnectEventArgs> _onMt4ManagerOnConnectHandler;
private Action<object, Mt4ManagerDisconnectEventArgs> _onMt4ManagerOnDisconnectHandler;
public Mt4Manager(int id,string platformName, string platformPath,uint user,string password, int type, Action<object, Mt4ManagerConnectEventArgs> onMt4ManagerOnConnectHandler,
Action<object, Mt4ManagerDisconnectEventArgs> onMt4ManagerOnDisconnectHandler,Action<object, Mt4ManagerOrderUpdateEventArgs> onMt4ManagerOrderUpdateHandler)
{
Id = id;
PlatformName = platformName;
User = user;
Type = type;
Password = password;
PlatformPath = platformPath;
_onMt4ManagerOrderUpdateHandler = onMt4ManagerOrderUpdateHandler;
_onMt4ManagerOnConnectHandler = onMt4ManagerOnConnectHandler;
_onMt4ManagerOnDisconnectHandler = onMt4ManagerOnDisconnectHandler;
_quoteClient = new QuoteClient();
_orderClient = new OrderClient();
_mainServer = QuoteClient.LoadSrv(PlatformPath,out _slaves);
if (_onMt4ManagerOrderUpdateHandler != null)
_quoteClient.OnOrderUpdate += new OrderUpdateEventHandler(OnOrderUpdate);
if (_onMt4ManagerOnConnectHandler != null)
_quoteClient.OnConnect += new ConnectEventHandler(OnConnect);
if (_onMt4ManagerOnDisconnectHandler != null)
_quoteClient.OnDisconnect += new DisconnectEventHandler(OnDisconnect);
}
public double GetBalance()
{
return _quoteClient.AccountBalance;
}
public double GetEquity()
{
return _quoteClient.AccountEquity;
}
public double GetProfit()
{
return _quoteClient.AccountProfit;
}
public double GetCredit()
{
return _quoteClient.AccountCredit;
}
public double GetFreeMargin()
{
return _quoteClient.AccountFreeMargin;
}
public double GetMargin()
{
return _quoteClient.AccountMargin;
}
public int GetLeverage()
{
return _quoteClient.AccountLeverage;
}
public int GetMode()
{
return _quoteClient.AccountMode;
}
public string GetName()
{
return _quoteClient.AccountName;
}
public DateTime GetServerTime()
{
return _quoteClient.ServerTime;
}
public bool IsDemoAccount()
{
return _quoteClient.IsDemoAccount;
}
public double GetMarginLevel()
{
return Math.Round(_quoteClient.AccountMargin != 0 ? _quoteClient.AccountEquity / _quoteClient.AccountMargin * 100 : 0,2, MidpointRounding.AwayFromZero);
}
public string[] GetSymbols()
{
return _quoteClient.Symbols;
}
public void Init()
{
}
public void Connect()
{
if (IsConnected())
return;
try
{
_quoteClient.Init(User, Password, _mainServer.Host, _mainServer.Port);
_orderClient.Init(_quoteClient);
_quoteClient.Connect();
}
catch (Exception ex)
{
bool isConnected = false;
foreach (var i in _slaves)
{
_quoteClient.Init(User, Password, i.Host, i.Port);
_orderClient.Init(_quoteClient);
try
{
_quoteClient.Connect();
isConnected = true;
break;
}
catch { }
}
if (!isConnected)
throw ex;
}
try
{
_orderClient.Connect();
}
catch (Exception ex)
{
_quoteClient.Disconnect();
throw ex;
}
}
public void Disconnect()
{
try
{
_quoteClient.Disconnect();
_orderClient.Disconnect();
}
catch (Exception ex)
{
throw ex;
}
}
public bool IsConnected()
{
return _quoteClient.Connected && _orderClient.Connected;
}
public QuoteEventArgs GetQuote(string symbol)
{
if (IsConnected())
return _quoteClient.GetQuote(symbol);
else
throw new Exception("Mt4未连接");
}
public Order OrderSend(string symbol, Op operation, double lots, double price, int slippage, double stoploss, double takeprofit, string comment, int magic, DateTime expiration)
{
if (IsConnected())
return _orderClient.OrderSend(symbol, operation, lots, price, slippage, stoploss, takeprofit, comment, magic, expiration);
else
throw new Exception("Mt4未连接");
}
public Order OrderClose(string symbol, int ticket, double lots, double price, int slippage)
{
if (IsConnected())
return _orderClient.OrderClose(symbol, ticket, lots, price, slippage);
else
throw new Exception("Mt4未连接");
}
public ConGroupSec GetSymbolGroupParams(string symbol)
{
if (IsConnected())
return _quoteClient.GetSymbolGroupParams(symbol);
else
throw new Exception("Mt4未连接");
}
public Order[] GetOpenOrders()
{
if (IsConnected())
return _quoteClient.GetOpenedOrders();
else
throw new Exception("Mt4未连接");
}
public Order GetOpenOrder(int ticket)
{
if (IsConnected())
return _quoteClient.GetOpenedOrder(ticket);
else
throw new Exception("Mt4未连接");
}
public Order[] DownloadOrderHistory(DateTime from,DateTime to)
{
if (IsConnected())
return _quoteClient.DownloadOrderHistory(from, to);
else
throw new Exception("Mt4未连接");
}
private void OnOrderUpdate(object sender, OrderUpdateEventArgs update)
{
_onMt4ManagerOrderUpdateHandler(this, new Mt4ManagerOrderUpdateEventArgs(Id,PlatformName,User,Type,update));
}
private void OnConnect(object sender, ConnectEventArgs args)
{
_onMt4ManagerOnConnectHandler(this, new Mt4ManagerConnectEventArgs(Id, PlatformName, User, Type, args));
}
private void OnDisconnect(object sender, DisconnectEventArgs args)
{
_onMt4ManagerOnDisconnectHandler(this, new Mt4ManagerDisconnectEventArgs(Id, PlatformName, User, Type, args));
}
}
}
标签:return,string,MT4Api,跟单,MT4,public,double,new,quoteClient 来源: https://blog.csdn.net/hahawei1234/article/details/121444975