//+------------------------------------------------------------------+ //| 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); }
//+------------------------------------------------------------------+ //| Dealing thread function | //+------------------------------------------------------------------+ void DealerFunc() { //--- deal while (Interlocked.Add(ref m_stop_flag, 0) == 0) { //--- connect to the server if (Interlocked.Add(ref m_connected, 0) == 0) { //--- connect manager to the server MTRetCode res = m_manager.Connect(m_server, m_login, m_password, null, CIMTManagerAPI.EnPumpModes.PUMP_MODE_SYMBOLS | CIMTManagerAPI.EnPumpModes.PUMP_MODE_GROUPS | CIMTManagerAPI.EnPumpModes.PUMP_MODE_USERS | CIMTManagerAPI.EnPumpModes.PUMP_MODE_ORDERS, MT5_CONNECT_TIMEOUT); if (res != MTRetCode.MT_RET_OK) { Thread.Sleep(100); continue; } //--- start dealer if (m_manager.DealerStart() != MTRetCode.MT_RET_OK) { Thread.Sleep(100); continue; } Interlocked.Exchange(ref m_connected, 1); //--- reset answer event m_event_answer.Reset(); } //--- wait for request m_event_request.WaitOne(Timeout.Infinite); //--- check stop flag if (Interlocked.Add(ref m_stop_flag, 0) != 0) { break; } //--- get next request if (m_manager.DealerGet(m_request) == MTRetCode.MT_RET_OK) { MTTickShort tick; string str = ""; //--- clear confirmation m_confirm.Clear(); //--- print request str = m_request.Print(); //--- get last tick for request symbol if (m_manager.TickLast(m_request.Symbol(), m_request.Group(), out tick) != MTRetCode.MT_RET_OK) { //--- ticks not found //--- select symbol m_manager.SelectedAdd(m_request.Symbol()); //--- request id m_confirm.ID(m_request.ID()); //--- return request m_confirm.Retcode(MTRetCode.MT_RET_REQUEST_RETURN); if (m_manager.DealerAnswer(m_confirm) == MTRetCode.MT_RET_OK) { m_manager.LoggerOut(EnMTLogCode.MTLogOK, "'{0}': return {1} for '{2}'", m_login, str, m_request.Login()); } continue; } //--- set order price if (m_request.PriceOrder() == 0) { switch (m_request.Type()) { case CIMTOrder.EnOrderType.OP_BUY: m_request.PriceOrder(tick.ask); break; case CIMTOrder.EnOrderType.OP_SELL: m_request.PriceOrder(tick.bid); break; } } //--- request ready m_parent.FireAPIRequest(); //--- wait for answer m_event_answer.WaitOne(Timeout.Infinite); //--- check stop flag if (Interlocked.Add(ref m_stop_flag, 0) != 0) { break; } //--- send confirm if (m_manager.DealerAnswer(m_confirm) == MTRetCode.MT_RET_OK) { m_manager.LoggerOut(EnMTLogCode.MTLogOK, "'{0}': confirm {1} for '{2}'", m_login, str, m_request.Login()); } } else { Thread.Sleep(100); } } //--- stop dealer m_manager.DealerStop(); //--- disconnect manager m_manager.Disconnect(); }
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); }