//+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ public override void OnOrderAdd(CIMTOrder order) { if (order != null) { string str = order.Print(); m_manager.LoggerOut(EnMTLogCode.MTLogOK, "{0} has been added", str); } }
//+------------------------------------------------------------------+ //| Login | //+------------------------------------------------------------------+ public bool Login(string server, UInt64 login, string password) { //--- connect MTRetCode res = m_manager.Connect(server, login, password, null, CIMTManagerAPI.EnPumpModes.PUMP_MODE_FULL, MT5_CONNECT_TIMEOUT); if (res != MTRetCode.MT_RET_OK) { m_manager.LoggerOut(EnMTLogCode.MTLogErr, "Connection failed ({0})", res); return(false); } return(true); }
//+------------------------------------------------------------------+ //| Initialize library | //+------------------------------------------------------------------+ public bool Initialize() { string message = string.Empty; MTRetCode res = MTRetCode.MT_RET_OK_NONE; //--- loading manager API if ((res = SMTManagerAPIFactory.Initialize(null)) != MTRetCode.MT_RET_OK) { message = string.Format("Loading manager API failed ({0})", res); System.Console.WriteLine(message); return(false); } //--- creating manager interface m_manager = SMTManagerAPIFactory.CreateManager(SMTManagerAPIFactory.ManagerAPIVersion, out res); if ((res != MTRetCode.MT_RET_OK) || (m_manager == null)) { SMTManagerAPIFactory.Shutdown(); message = string.Format("Creating manager interface failed ({0})", (res == MTRetCode.MT_RET_OK ? "Managed API is null" : res.ToString())); System.Console.WriteLine(message); return(false); } //--- create deal array if ((m_deal_array = m_manager.DealCreateArray()) == null) { m_manager.LoggerOut(EnMTLogCode.MTLogErr, "DealCreateArray fail"); System.Console.WriteLine("DealCreateArray fail"); return(false); } //--- create user interface if ((m_user = m_manager.UserCreate()) == null) { m_manager.LoggerOut(EnMTLogCode.MTLogErr, "UserCreate fail"); System.Console.WriteLine("UserCreate fail"); return(false); } //--- create account interface if ((m_account = m_manager.UserCreateAccount()) == null) { m_manager.LoggerOut(EnMTLogCode.MTLogErr, "UserCreateAccount fail"); System.Console.WriteLine("UserCreateAccount fail"); return(false); } //--- all right return(true); }
//+------------------------------------------------------------------+ //| Dealer starting | //+------------------------------------------------------------------+ public bool Start(string server, UInt64 login, string password) { //--- check manager if (m_manager == null) { return(false); } //--- check arguments if (server == null || login == 0 || password == null) { m_manager.LoggerOut(EnMTLogCode.MTLogErr, "Dealer: starting failed with bad arguments"); return(false); } //--- do not let exception get out here try { //--- check if dealer is started already if (m_thread_dealer != null) { //--- dealer thread is running if (m_thread_dealer.IsAlive) { return(false); } //--- m_thread_dealer = null; } //--- save authorize info m_server = server; m_login = login; m_password = password; //--- subscribe for notifications if (m_manager.Subscribe(this) != MTRetCode.MT_RET_OK) { return(false); } //--- subscribe for requests if (m_manager.RequestSubscribe(m_request_sink) != MTRetCode.MT_RET_OK) { return(false); } //--- subscribe for orders if (m_manager.OrderSubscribe(m_order_sink) != MTRetCode.MT_RET_OK) { return(false); } //--- start dealing thread m_stop_flag = 0; m_connected = 0; //--- start thread m_thread_dealer = new Thread(DealerFunc, STACK_SIZE_COMMON); m_thread_dealer.Start(); //--- done return(true); } catch (Exception ex) { if (m_manager != null) { m_manager.LoggerOut(EnMTLogCode.MTLogErr, "Dealer: starting failed - {0}", ex.Message); } //--- MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error); } //--- done with errors return(false); }
//+------------------------------------------------------------------+ //| Initialize library | //+------------------------------------------------------------------+ public bool Initialize(CDealerExampleDlg parent) { //--- string message = string.Empty; MTRetCode res = MTRetCode.MT_RET_OK_NONE; uint version = 0; //--- check if (parent == null) { return(false); } m_parent = parent; //--- try { //--- init CIMTManagerSink native link if ((res = RegisterSink()) != MTRetCode.MT_RET_OK) { message = string.Format("Register sink failed ({0})", res); MessageBox.Show(message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } //--- loading manager API if ((res = SMTManagerAPIFactory.Initialize(@"..\..\..\API\")) != MTRetCode.MT_RET_OK) { message = string.Format("Loading manager API failed ({0})", res); MessageBox.Show(message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } //--- check Manager API version if ((res = SMTManagerAPIFactory.GetVersion(out version)) != MTRetCode.MT_RET_OK) { message = string.Format("Dealer: getting version failed ({0})", res); MessageBox.Show(message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } if (version != SMTManagerAPIFactory.ManagerAPIVersion) { message = string.Format("Dealer: wrong Manager API version, version {0} required", SMTManagerAPIFactory.ManagerAPIVersion); MessageBox.Show(message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } //--- create manager interface if ((m_manager = SMTManagerAPIFactory.CreateManager(SMTManagerAPIFactory.ManagerAPIVersion, out res)) == null || res != MTRetCode.MT_RET_OK) { message = string.Format("Dealer: creating manager interface failed ({0})", res); MessageBox.Show(message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } //--- create request object if ((m_request = m_manager.RequestCreate()) == null) { m_manager.LoggerOut(EnMTLogCode.MTLogErr, "Dealer: creating request object failed"); MessageBox.Show("Dealer: creating request object failed", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } //--- create confirmation object if ((m_confirm = m_manager.DealerConfirmCreate()) == null) { m_manager.LoggerOut(EnMTLogCode.MTLogErr, "Dealer: creating confirmation object failed"); MessageBox.Show("Dealer: creating confirmation object failed", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } //--- create requests event m_event_request = new EventWaitHandle(false, EventResetMode.ManualReset); //--- create requests event m_event_answer = new EventWaitHandle(false, EventResetMode.AutoReset); //--- m_request_sink = new CRequestSink(); if ((res = m_request_sink.Initialize(m_manager, m_event_request)) != MTRetCode.MT_RET_OK) { m_manager.LoggerOut(EnMTLogCode.MTLogErr, "Dealer: creating request sink failed"); MessageBox.Show("Dealer: creating request sink failed", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } //--- m_order_sink = new COrderSink(); if ((res = m_order_sink.Initialize(m_manager)) != MTRetCode.MT_RET_OK) { m_manager.LoggerOut(EnMTLogCode.MTLogErr, "Dealer: creating request sink failed"); MessageBox.Show("Dealer: creating request sink failed", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } //--- done return(true); } catch (Exception ex) { if (m_manager != null) { m_manager.LoggerOut(EnMTLogCode.MTLogErr, "Dealer: initialization failed - {0}", ex.Message); } //--- MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error); } //--- done with errors return(false); }
public bool Start(string server, UInt64 login, string password) { //--- check manager if (m_manager == null) { return(false); } //--- check arguments if (server == null || login == 0 || password == null) { m_manager.LoggerOut(EnMTLogCode.MTLogErr, "Dealer: starting failed with bad arguments"); return(false); } //--- do not let exception get out here try { //--- check if dealer is started already if (m_thread_dealer != null) { //--- dealer thread is running if (m_thread_dealer.IsAlive) { return(false); } //--- m_thread_dealer = null; } //--- save authorize info m_server = server; m_login = login; m_password = password; //--- subscribe for notifications if (m_manager.Subscribe(this) != MTRetCode.MT_RET_OK) { return(false); } //--- subscribe for requests if (m_manager.DealSubscribe(_dealSink) != MTRetCode.MT_RET_OK) { return(false); } //--- start dealing thread m_stop_flag = 0; m_connected = 0; //--- start thread //m_thread_dealer = new Thread(DealerFunc, STACK_SIZE_COMMON); //m_thread_dealer.Start(); //--- done MTRetCode res = m_manager.Connect(m_server, m_login, m_password, null, CIMTManagerAPI.EnPumpModes.PUMP_MODE_FULL, MT5_CONNECT_TIMEOUT); if (m_manager.DealerStart() != MTRetCode.MT_RET_OK) { } return(true); } catch (Exception ex) { if (m_manager != null) { m_manager.LoggerOut(EnMTLogCode.MTLogErr, "Dealer: starting failed - {0}", ex.Message); } } //--- done with errors return(false); }