Esempio n. 1
0
        public void InitExpert(int expertHandle, int port, string symbol, double bid, double ask, IMetaTraderHandler mtHandler)
        {
            Debug.WriteLine("MtApiServerInstance::InitExpert: symbol = {0}, expertHandle = {1}, port = {2}", symbol, expertHandle, port);

            MtServer server = null;
            lock (mServersDictionary)
            {
                if (mServersDictionary.ContainsKey(port))
                {
                    server = mServersDictionary[port];
                }
                else
                {
                    server = new MtServer(port);
                    server.Stopped += new EventHandler(server_Stopped);
                    mServersDictionary[port] = server;

                    server.Start();
                }
            }

            var expert = new MtExpert(expertHandle, new MtQuote(symbol, bid, ask), mtHandler);

            lock (mExpertsDictionary)
            {
                mExpertsDictionary[expert.Handle] = expert;
            }

            server.AddExpert(expert);
        }
Esempio n. 2
0
        public void AddCommandExecutor(MtExpert commandExecutor)
        {
            if (commandExecutor == null)
            {
                return;
            }

            var notify = false;

            lock (_locker)
            {
                if (_commandExecutors.Contains(commandExecutor))
                {
                    return;
                }

                _commandExecutors.Add(commandExecutor);
                if (_commandTasks.Count > 0)
                {
                    notify = true;
                }
            }

            commandExecutor.CommandExecuted += CommandExecutor_CommandExecuted;
            commandExecutor.CommandManager   = this;

            if (notify)
            {
                NotifyCommandReady();
            }
        }
Esempio n. 3
0
        public void RemoveCommandExecutor(MtExpert commandExecutor)
        {
            if (commandExecutor == null)
            {
                return;
            }

            var notify = false;

            lock (_locker)
            {
                if (_commandExecutors.Contains(commandExecutor) == false)
                {
                    return;
                }

                _commandExecutors.Remove(commandExecutor);
                if (_commandTasks.Count > 0)
                {
                    notify = true;
                }
            }

            commandExecutor.CommandExecuted -= CommandExecutor_CommandExecuted;
            commandExecutor.CommandManager   = null;

            if (notify)
            {
                NotifyCommandReady();
            }
        }
Esempio n. 4
0
        public void AddCommandExecutor(MtExpert commandExecutor)
        {
            if (commandExecutor == null)
            {
                return;
            }

            lock (_locker)
            {
                if (mCommandExecutors.Contains(commandExecutor) == true)
                {
                    return;
                }

                mCommandExecutors.Add(commandExecutor);

                commandExecutor.CommandManager = this;

                if (mCurrentExecutor == null)
                {
                    mCurrentExecutor = commandExecutor;
                    mCurrentExecutor.IsCommandExecutor = true;

                    if (mCommands.Count > 0)
                    {
                        mCurrentExecutor.NotifyCommandReady();
                    }
                }
            }
        }
Esempio n. 5
0
        public void RemoveCommandExecutor(MtExpert commandExecutor)
        {
            if (commandExecutor == null)
            {
                return;
            }

            lock (_locker)
            {
                if (mCommandExecutors.Contains(commandExecutor) == false)
                {
                    return;
                }

                mCommandExecutors.Remove(commandExecutor);

                if (mCurrentExecutor == commandExecutor)
                {
                    mCurrentExecutor.IsCommandExecutor = false;
                    mCurrentExecutor = mCommandExecutors.Count > 0 ? mCommandExecutors[0] : null;

                    if (mCommands.Count > 0)
                    {
                        mCurrentExecutor.NotifyCommandReady();
                    }
                }
            }
        }
Esempio n. 6
0
        public void InitExpert(int expertHandle, int port, string symbol, double bid, double ask, IMetaTraderHandler mtHandler)
        {
            Debug.WriteLine("MtApiServerInstance::InitExpert: symbol = {0}, expertHandle = {1}, port = {2}", symbol, expertHandle, port);

            MtServer server = null;

            lock (mServersDictionary)
            {
                if (mServersDictionary.ContainsKey(port))
                {
                    server = mServersDictionary[port];
                }
                else
                {
                    server                   = new MtServer(port);
                    server.Stopped          += new EventHandler(server_Stopped);
                    mServersDictionary[port] = server;

                    server.Start();
                }
            }

            var expert = new MtExpert(expertHandle, new MtQuote(symbol, bid, ask), mtHandler);

            lock (mExpertsDictionary)
            {
                mExpertsDictionary[expert.Handle] = expert;
            }

            server.AddExpert(expert);
        }
Esempio n. 7
0
        public void AddCommandExecutor(MtExpert commandExecutor)
        {
            if (commandExecutor == null)
                return;

            lock (_locker)
            {
                if (mCommandExecutors.Contains(commandExecutor) == true)
                    return;

                mCommandExecutors.Add(commandExecutor);

                commandExecutor.CommandManager = this;

                if (mCurrentExecutor == null)
                {
                    mCurrentExecutor = commandExecutor;
                    mCurrentExecutor.IsCommandExecutor = true;

                    if (mCommands.Count > 0)
                    {
                        mCurrentExecutor.NotifyCommandReady();
                    }
                }
            }
        }
Esempio n. 8
0
        public void AddExpert(int port, MtExpert expert)
        {
            if (expert == null)
                throw new ArgumentNullException(nameof(expert));

            Log.InfoFormat("AddExpert: begin. expert = {0}", expert);

            MtServer server;
            lock (_servers)
            {
                if (_servers.ContainsKey(port))
                {
                    server = _servers[port];
                }
                else
                {
                    server = new MtServer(port);
                    server.Stopped += server_Stopped;
                    _servers[port] = server;

                    server.Start();
                }
            }

            lock (_experts)
            {
                _experts[expert.Handle] = expert;
            }

            server.AddExpert(expert);
            expert.Deinited += ExpertOnDeinited;

            Log.Info("AddExpert: end");
        }
Esempio n. 9
0
        public void RemoveExpert(int expertHandle)
        {
            Log.InfoFormat("RemoveExpert: begin. expertHandle = {0}", expertHandle);

            MtExpert expert = null;

            lock (_experts)
            {
                if (_experts.ContainsKey(expertHandle))
                {
                    expert = _experts[expertHandle];
                }
            }

            if (expert != null)
            {
                expert.Deinit();
            }
            else
            {
                Log.WarnFormat("RemoveExpert: expert with id {0} has not been found.", expertHandle);
            }

            Log.Info("RemoveExpert: end");
        }
Esempio n. 10
0
        private void expert_Deinited(object sender, EventArgs e)
        {
            MtExpert expert       = (MtExpert)sender;
            int      expertsCount = 0;

            lock (mExpertsLocker)
            {
                mExperts.Remove(expert);

                expertsCount = mExperts.Count();
            }

            mExecutorManager.RemoveCommandExecutor(expert);

            if (expert != null)
            {
                expert.Deinited     -= expert_Deinited;
                expert.QuoteChanged -= expert_QuoteChanged;

                mService.OnQuoteRemoved(expert.Quote);
            }

            if (expertsCount == 0)
            {
                var stopTimer = new System.Timers.Timer();
                stopTimer.Elapsed += stopTimer_Elapsed;
                stopTimer.Interval = STOP_EXPERT_INTERVAL;
                stopTimer.Start();
            }
        }
Esempio n. 11
0
        public void AddExpert(MtExpert expert)
        {
            Log.DebugFormat("AddExpert: begin. expert {0}", expert);

            if (expert == null)
            {
                Log.Warn("AddExpert: end. expert is not defined");
                return;
            }

            expert.Deinited     += expert_Deinited;
            expert.QuoteChanged += expert_QuoteChanged;
            expert.OnMtEvent    += Expert_OnMtEvent;

            lock (_experts)
            {
                _experts.Add(expert);
            }

            _executorManager.AddExecutor(expert);

            _service.OnQuoteAdded(expert.Quote);

            Log.Debug("AddExpert: end.");
        }
Esempio n. 12
0
        private void Expert_OnMtEvent(MtExpert expert, MtEvent e)
        {
            Log.DebugFormat("Expert_OnMtEvent: begin. expert = {0}, event = {1}", expert, e);

            _service.OnMtEvent(e);

            Log.Debug("Expert_OnMtEvent: end.");
        }
Esempio n. 13
0
        private void expert_QuoteChanged(MtExpert expert, MtQuote quote)
        {
            Log.DebugFormat("expert_QuoteChanged: begin. expert = {0}, quote = {1}", expert, quote);

            _service.QuoteUpdate(quote);

            Log.Debug("expert_QuoteChanged: end.");
        }
Esempio n. 14
0
        public int GetCommandType(int expertHandle)
        {
            Debug.WriteLine("MtApiServerInstance::GetCommandType: expertHandle = {0}", expertHandle);

            MtExpert expert = null;

            lock (mExpertsDictionary)
            {
                expert = mExpertsDictionary[expertHandle];
            }

            return((expert != null) ? expert.GetCommandType() : 0);
        }
Esempio n. 15
0
        public object GetCommandParameter(int expertHandle, int index)
        {
            Debug.WriteLine("MtApiServerInstance::GetCommandParameter: expertHandle = {0}, index = {1}", expertHandle, index);

            MtExpert expert = null;

            lock (mExpertsDictionary)
            {
                expert = mExpertsDictionary[expertHandle];
            }

            return((expert != null) ? expert.GetCommandParameter(index) : null);
        }
Esempio n. 16
0
        public void AddExpert(MtExpert expert)
        {
            if (expert != null)
            {
                expert.Deinited     += new EventHandler(expert_Deinited);
                expert.QuoteChanged += new MtExpert.MtQuoteHandler(expert_QuoteChanged);

                lock (mExpertsLocker)
                {
                    mExperts.Add(expert);
                }

                mExecutorManager.AddCommandExecutor(expert);

                mService.OnQuoteAdded(expert.Quote);
            }
        }
Esempio n. 17
0
        public void AddExpert(MtExpert expert)
        {
            if (expert != null)
            {
                expert.Deinited     += expert_Deinited;
                expert.QuoteChanged += expert_QuoteChanged;

                lock (_experts)
                {
                    _experts.Add(expert);
                }

                _executorManager.AddCommandExecutor(expert);

                _service.OnQuoteAdded(expert.Quote);
            }
        }
Esempio n. 18
0
        public void SendQuote(int expertHandle, string symbol, double bid, double ask)
        {
            Debug.WriteLine("MtApiServerInstance::SendQuote: enter. symbol = {0}, bid = {1}, ask = {2}", symbol, bid, ask);

            MtExpert expert = null;

            lock (mExpertsDictionary)
            {
                expert = mExpertsDictionary[expertHandle];
            }

            if (expert != null)
            {
                expert.Quote = new MtQuote(symbol, bid, ask);
            }

            Debug.WriteLine("MtApiServerInstance::SendQuote: finish.");
        }
Esempio n. 19
0
        public void SendResponse(int expertHandle, MtResponse response)
        {
            Debug.WriteLine("MtApiServerInstance::SendResponse: id = {0}, response = {1}", expertHandle, response);

            MtExpert expert = null;

            lock (mExpertsDictionary)
            {
                expert = mExpertsDictionary[expertHandle];
            }

            if (expert != null)
            {
                expert.SendResponse(response);
            }

            Debug.WriteLine("MtApiServerInstance::SendResponse: finish");
        }
Esempio n. 20
0
        public void SendEvent(int expertHandle, int eventType, string payload)
        {
            Debug.WriteLine("MtApiServerInstance::SendEvent called. eventType = {0}, payload = {1}", eventType, payload);

            MtExpert expert = null;

            lock (mExpertsDictionary)
            {
                expert = mExpertsDictionary[expertHandle];
            }

            if (expert != null)
            {
                expert.SendEvent(new MtEvent(eventType, payload));
            }

            Debug.WriteLine("MtApiServerInstance::SendEvent: finished");
        }
Esempio n. 21
0
        public void DeinitExpert(int expertHandle)
        {
            Debug.WriteLine("MtApiServerInstance::DeinitExpert: expertHandle = {0}", expertHandle);

            MtExpert expert = null;

            lock (mExpertsDictionary)
            {
                if (mExpertsDictionary.ContainsKey(expertHandle) == true)
                {
                    expert = mExpertsDictionary[expertHandle];
                    mExpertsDictionary.Remove(expertHandle);
                }
            }

            if (expert != null)
            {
                expert.Deinit();
            }
        }
Esempio n. 22
0
        public void InitExpert(int expertHandle, int port, string symbol, double bid, double ask, IMetaTraderHandler mtHandler)
        {
            if (mtHandler == null)
            {
                throw new ArgumentNullException(nameof(mtHandler));
            }

            Log.InfoFormat("InitExpert: begin. symbol = {0}, expertHandle = {1}, port = {2}", symbol, expertHandle, port);

            MtServer server;

            lock (_servers)
            {
                if (_servers.ContainsKey(port))
                {
                    server = _servers[port];
                }
                else
                {
                    server          = new MtServer(port);
                    server.Stopped += server_Stopped;
                    _servers[port]  = server;

                    server.Start();
                }
            }

            var expert = new MtExpert(expertHandle, new MtQuote {
                Instrument = symbol, Bid = bid, Ask = ask, ExpertHandle = expertHandle
            }, mtHandler);

            lock (_experts)
            {
                _experts[expert.Handle] = expert;
            }

            server.AddExpert(expert);

            Log.Info("InitExpert: end");
        }
Esempio n. 23
0
        public void OnCommandExecuted(MtExpert expert, MtCommand command, MtResponse response)
        {
            if (expert == null)
            {
                return;
            }

            if (CommandExecuted != null)
            {
                CommandExecuted(this, new MtCommandExecuteEventArgs(command, response));
            }

            lock (_locker)
            {
                if (expert == mCurrentExecutor)
                {
                    if (mCommands.Count > 0)
                    {
                        mCurrentExecutor.NotifyCommandReady();
                    }
                }
            }
        }
Esempio n. 24
0
        public void RemoveCommandExecutor(MtExpert commandExecutor)
        {
            if (commandExecutor == null)
            {
                throw new ArgumentNullException(nameof(commandExecutor));
            }

            Log.DebugFormat("RemoveCommandExecutor: begin. commandExecutor = {0}", commandExecutor);

            var notify = false;

            lock (_locker)
            {
                if (_commandExecutors.Contains(commandExecutor) == false)
                {
                    Log.Warn("RemoveCommandExecutor: end. Command executor is not exist in collection.");
                    return;
                }

                _commandExecutors.Remove(commandExecutor);
                if (_commandTasks.Count > 0)
                {
                    notify = true;
                }
            }

            commandExecutor.CommandExecuted -= CommandExecutor_CommandExecuted;
            commandExecutor.CommandManager   = null;

            if (notify)
            {
                NotifyCommandReady();
            }

            Log.Debug("RemoveCommandExecutor: end.");
        }
Esempio n. 25
0
        public void AddCommandExecutor(MtExpert commandExecutor)
        {
            if (commandExecutor == null)
            {
                throw new ArgumentNullException(nameof(commandExecutor));
            }

            Log.DebugFormat("AddCommandExecutor: begin. commandExecutor = {0}", commandExecutor);

            var notify = false;

            lock (_locker)
            {
                if (_commandExecutors.Contains(commandExecutor))
                {
                    Log.Warn("AddCommandExecutor: end. Command executor already exist.");
                    return;
                }

                _commandExecutors.Add(commandExecutor);
                if (_commandTasks.Count > 0)
                {
                    notify = true;
                }
            }

            commandExecutor.CommandExecuted += CommandExecutor_CommandExecuted;
            commandExecutor.CommandManager   = this;

            if (notify)
            {
                NotifyCommandReady();
            }

            Log.Debug("AddCommandExecutor: end.");
        }
Esempio n. 26
0
        public void RemoveCommandExecutor(MtExpert commandExecutor)
        {
            if (commandExecutor == null)
                return;

            lock (_locker)
            {
                if (mCommandExecutors.Contains(commandExecutor) == false)
                    return;

                mCommandExecutors.Remove(commandExecutor);

                if (mCurrentExecutor == commandExecutor)
                {
                    mCurrentExecutor.IsCommandExecutor = false;
                    mCurrentExecutor = mCommandExecutors.Count > 0 ? mCommandExecutors[0] : null;

                    if (mCommands.Count > 0)
                    {
                        mCurrentExecutor.NotifyCommandReady();
                    }
                }
            }
        }
Esempio n. 27
0
 private void expert_QuoteChanged(MtExpert expert, MtQuote quote)
 {
     mService.QuoteUpdate(quote);
 }
Esempio n. 28
0
        public void OnCommandExecuted(MtExpert expert, MtCommand command, MtResponse response)
        {
            if (expert == null)
                return;

            if (CommandExecuted != null)
            {
                CommandExecuted(this, new MtCommandExecuteEventArgs(command, response));
            }

            lock (_locker)
            {
                if (expert == mCurrentExecutor)
                {
                    if (mCommands.Count > 0)
                    {
                        mCurrentExecutor.NotifyCommandReady();
                    }
                }
            }
        }
Esempio n. 29
0
        public void AddExpert(MtExpert expert)
        {
            if (expert != null)
            {
                expert.Deinited += new EventHandler(expert_Deinited);
                expert.QuoteChanged += new MtExpert.MtQuoteHandler(expert_QuoteChanged);

                lock (mExpertsLocker)
                {
                    mExperts.Add(expert);
                }

                mExecutorManager.AddCommandExecutor(expert);

                mService.OnQuoteAdded(expert.Quote);
            }
        }
Esempio n. 30
0
 private void expert_QuoteChanged(MtExpert expert, MtQuote quote)
 {
     mService.QuoteUpdate(quote);
 }