Пример #1
0
        //+------------------------------------------------------------------+
        //| 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();
        }