Example #1
0
        /// <summary>
        /// Provides new unique Application ID in response to inquiry request
        /// </summary>
        /// <param name="msg">IMessage containg Inquiry request</param>
        private void SendApplictionId(IMessage <InquiryMessage> msg)
        {
            try
            {
                // Create the Inquiry Respons to be sent
                InquiryResponse inquiryResponse = new InquiryResponse();

                string id = ApplicationIdGenerator.NextId();
                inquiryResponse.Type  = Constants.InquiryTags.AppID;
                inquiryResponse.AppId = id;

                // Create EasyNetQ message to be published
                Message <InquiryResponse> message = new Message <InquiryResponse>(inquiryResponse);

                if (Logger.IsDebugEnabled)
                {
                    Logger.Debug("Publishing Inquiry Response for: " + msg.Body.Type, _type.FullName, "OnInquiryRequestReceived");
                }

                // Publish Messages on the exchange
                PublishMessages(msg.Properties.ReplyTo, message);
            }
            catch (Exception exception)
            {
                Logger.Error(exception, _type.FullName, "SendApplictionId");
            }
        }
        /// <summary>
        /// Sets up the strategy to be executed
        /// </summary>
        /// <param name="initializeStrategy">Holds info to initialize the given strategy</param>
        private void InitializeUserStrategy(InitializeStrategy initializeStrategy)
        {
            try
            {
                if (_asyncClassLogger.IsInfoEnabled)
                {
                    _asyncClassLogger.Info("Setting up user strategy to run: " + initializeStrategy.StrategyType.FullName,
                                           _type.FullName, "InitializeUserStrategy");
                }

                // Get new Key.
                string key = ApplicationIdGenerator.NextId();

                // Save Strategy details in new Strategy Executor object
                StrategyExecutor strategyExecutor = new StrategyExecutor(key, initializeStrategy.StrategyType, initializeStrategy.CtorArguments);

                // Add to local map
                _strategiesCollection.AddOrUpdate(key, strategyExecutor, (ky, value) => strategyExecutor);

                //Register Event
                strategyExecutor.StatusChanged     += OnStatusChanged;
                strategyExecutor.ExecutionReceived += OnExecutionArrived;

                // Save Brief info of constructor parameters
                StringBuilder briefInfo = new StringBuilder();

                // Add Strategy Description
                briefInfo.Append(LoadCustomStrategy.GetCustomClassSummary(initializeStrategy.StrategyType));
                briefInfo.Append(" :: ");

                // Add Parameters Description
                foreach (object ctorArgument in initializeStrategy.CtorArguments)
                {
                    briefInfo.Append(ctorArgument.ToString());
                    briefInfo.Append("|");
                }

                // Create object to add to AddStrattegy.cs object
                SelectedStrategy selectedStrategy = new SelectedStrategy
                {
                    Key       = key,
                    Symbol    = initializeStrategy.CtorArguments[3].ToString(),
                    BriefInfo = briefInfo.ToString()
                };

                // Create object to pass to event aggregator.
                AddStrategy addStrategy = new AddStrategy(selectedStrategy);

                // Publish event to notify listeners.
                EventSystem.Publish <AddStrategy>(addStrategy);
            }
            catch (Exception exception)
            {
                _asyncClassLogger.Error(exception, _type.FullName, "InitializeUserStrategy");
            }
        }
Example #3
0
        public void IdGeneratorTestCase()
        {
            var idOne   = ApplicationIdGenerator.NextId();
            var idTwo   = ApplicationIdGenerator.NextId();
            var idThree = ApplicationIdGenerator.NextId();

            Assert.AreEqual("A00", idOne, "Uniquely generated ID One");
            Assert.AreEqual("A01", idTwo, "Uniquely generated ID Two");
            Assert.AreEqual("A02", idThree, "Uniquely generated ID Three");
        }
        /// <summary>
        /// Start Strategy Optimization
        /// </summary>
        /// <param name="optimizationParameters">Contains info for the parameters to be used for optimization</param>
        private void StartOptimization(OptimizeStrategyBruteForce optimizationParameters)
        {
            try
            {
                if (_asyncClassLogger.IsInfoEnabled)
                {
                    _asyncClassLogger.Info("Getting argument combinations", _type.FullName, "StartOptimization");
                }

                // Clear all previous information
                _strategiesCollection.Clear();

                // Save Parameter Details
                _parmatersDetails = optimizationParameters.ParmatersDetails;

                // Get all ctor arguments to be used for optimization
                CreateCtorCombinations(optimizationParameters.CtorArgs, optimizationParameters.ConditionalParameters);

                // Initialize Stratgey for each set of arguments
                foreach (object[] ctorArgument in _ctorArguments)
                {
                    // Get new Key.
                    string key = ApplicationIdGenerator.NextId();

                    // Save Strategy details in new Strategy Executor object
                    StrategyExecutor strategyExecutor = new StrategyExecutor(key, optimizationParameters.StrategyType, ctorArgument);

                    // Register Event
                    strategyExecutor.StatusChanged += OnStrategyExecutorStatusChanged;

                    // Add to local map
                    _strategiesCollection.AddOrUpdate(key, strategyExecutor, (ky, value) => strategyExecutor);
                }

                // Start executing each instance
                StartStrategyExecution();
            }
            catch (Exception exception)
            {
                _asyncClassLogger.Error(exception, _type.FullName, "StartOptimization");
            }
        }
        /// <summary>
        /// Called when Inquiry request is received from MQ Server
        /// </summary>
        private void OnInquiryRequestReceived(IMessage <InquiryMessage> inquiryMessage)
        {
            try
            {
                if (Logger.IsDebugEnabled)
                {
                    Logger.Debug("Application Info received from MQ Server: " + inquiryMessage.Properties.AppId, _type.FullName, "OnInquiryRequestReceived");
                }

                // Create the Inquiry Respons to be sent
                InquiryResponse inquiryResponse = new InquiryResponse();

                if (inquiryMessage.Body.Type.Equals(Constants.InquiryTags.AppID))
                {
                    string id = ApplicationIdGenerator.NextId();
                    inquiryResponse.Type  = Constants.InquiryTags.AppID;
                    inquiryResponse.AppId = id;
                }
                else if (inquiryMessage.Body.Type.Equals(Constants.InquiryTags.DisconnectClient))
                {
                    OnDisconnectApplicationReceived(inquiryMessage.Properties.AppId);
                    return;
                }

                // Create EasyNetQ message to be published
                Message <InquiryResponse> message = new Message <InquiryResponse>(inquiryResponse);

                if (Logger.IsDebugEnabled)
                {
                    Logger.Debug("Publishing Inquiry Response for: " + inquiryMessage.Body.Type, _type.FullName, "OnInquiryRequestReceived");
                }

                // Publish Messages on the exchange
                PublishMessages(inquiryMessage.Properties.ReplyTo, message);
            }
            catch (Exception exception)
            {
                Logger.Error(exception, _type.FullName, "OnInquiryRequestReceived");
            }
        }
Example #6
0
        /// <summary>
        /// Start Strategy Optimization
        /// </summary>
        /// <param name="optimizationParameters">Contains info for the parameters to be used for optimization</param>
        private void StartOptimization(BruteForceParameters optimizationParameters)
        {
            try
            {
                // Save instance
                _optimizationParameters = optimizationParameters;

                if (_asyncClassLogger.IsInfoEnabled)
                {
                    _asyncClassLogger.Info("Getting argument combinations", _type.FullName, "StartOptimization");
                }

                // Change Status to indicate on UI
                _optimizationParameters.Status = OptimizationStatus.Working;

                // Clear all previous information
                _strategiesCollection.Clear();
                _ctorArguments.Clear();

                // Get Parameter values to be used in the Constructor
                object[] ctorArguments = optimizationParameters.GetParameterValues();

                // Get Conditional Parameter values to be used for creating Iterations
                Tuple <int, object, double>[] conditionalParameters = optimizationParameters.GetConditionalParameters();

                // Save Parameter Details
                _parmatersDetails = optimizationParameters.ParameterDetails;

                // Get all ctor arguments to be used for optimization
                CreateCtorCombinations(ctorArguments, conditionalParameters);

                // Initialize Stratgey for each set of arguments
                foreach (object[] ctorArgumentValues in _ctorArguments)
                {
                    // Get new Key.
                    string key = ApplicationIdGenerator.NextId();

                    var instanceParameterDetails = new Dictionary <string, ParameterDetail>();

                    for (int i = 0; i < ctorArgumentValues.Length; i++)
                    {
                        // Create new parameter details to be when creating Strategy Instance object
                        ParameterDetail tempParameterDetail = new ParameterDetail(_parmatersDetails[i].ParameterType, ctorArgumentValues[i]);

                        instanceParameterDetails.Add(_parmatersDetails[i].Description, tempParameterDetail);
                    }

                    // Create Strategy Instance object
                    var instance = new StrategyInstance(key, instanceParameterDetails, optimizationParameters.StrategyType);

                    // Save Strategy details in new Strategy Executor object
                    var strategyExecutor = new StrategyExecutor(instance, _currentDispatcher);

                    // Register Event
                    strategyExecutor.StatusChanged += OnStrategyExecutorStatusChanged;

                    // Add to local map
                    _strategiesCollection.AddOrUpdate(key, strategyExecutor, (ky, value) => strategyExecutor);

                    StringBuilder parametersInfo = new StringBuilder();
                    foreach (object ctorArgument in strategyExecutor.CtorArguments)
                    {
                        parametersInfo.Append(ctorArgument.ToString());
                        parametersInfo.Append(" | ");
                    }

                    // Create new object to be used with Event Aggregator
                    var optimizationStatistics = new OptimizationStatistics(strategyExecutor.StrategyKey);
                    optimizationStatistics.Description      = parametersInfo.ToString();
                    optimizationStatistics.ExecutionDetails = instance.ExecutionDetails;

                    // Raise event to Bind statistics to UI and will be updated as each instance is executed
                    EventSystem.Publish <OptimizationStatistics>(optimizationStatistics);
                }

                // Save total number of iterations count
                _optimizationParameters.TotalIterations = _strategiesCollection.Count;

                // Start executing each instance
                StartStrategyExecution();
            }
            catch (Exception exception)
            {
                _asyncClassLogger.Error(exception, _type.FullName, "StartOptimization");
            }
        }