Exemplo n.º 1
0
        /// <summary>
        /// Starts custom strategy execution
        /// </summary>
        public void ExecuteStrategy()
        {
            try
            {
                bool parameterChanged = true;

                // Check if any strategy parameter was changed
                if (_ctorArguments != null)
                {
                    parameterChanged = _strategyInstance.ParametersChanged(_ctorArguments);

                    // Get updated values to be used
                    if (parameterChanged)
                    {
                        // Get parameter values to be used
                        _ctorArguments = _strategyInstance.GetParameterValues();
                    }
                }

                // Verify Strategy Instance
                if (_tradeHubStrategy == null || parameterChanged)
                {
                    //create DB strategy
                    Strategy strategy = new Strategy();
                    strategy.Name          = _strategyType.Name;
                    strategy.StartDateTime = DateTime.Now;

                    // Get parameter values to be used
                    _ctorArguments = _strategyInstance.GetParameterValues();

                    // Get new strategy instance
                    var strategyInstance = StrategyHelper.CreateStrategyInstance(_strategyType, _ctorArguments);

                    if (strategyInstance != null)
                    {
                        // Cast to TradeHubStrategy Instance
                        _tradeHubStrategy = strategyInstance as TradeHubStrategy;
                    }

                    if (_tradeHubStrategy == null)
                    {
                        if (_asyncClassLogger.IsInfoEnabled)
                        {
                            _asyncClassLogger.Info("Unable to initialize Custom Strategy: " + _strategyType.FullName, _type.FullName, "ExecuteStrategy");
                        }

                        // Skip execution of further actions
                        return;
                    }

                    // Set Strategy Name
                    _tradeHubStrategy.StrategyName = StrategyHelper.GetCustomClassSummary(_strategyType);

                    // Set notificaitons preference
                    _tradeHubStrategy.SetNewOrderNotification(_newOrderNotification);
                    _tradeHubStrategy.SetAcceptedOrderNotification(_acceptedOrderNotification);
                    _tradeHubStrategy.SetExecutionNotification(_executionNotification);
                    _tradeHubStrategy.SetRejectionNotification(_rejectionNotification);

                    // Register Events
                    RegisterTradeHubStrategyEvent();
                }

                if (_asyncClassLogger.IsInfoEnabled)
                {
                    _asyncClassLogger.Info("Executing user strategy: " + _strategyType.FullName, _type.FullName, "ExecuteStrategy");
                }

                //Overriding if running on simulated exchange
                ManageBackTestingStrategy();

                _stopInstanceRequested = false;

                // Start Executing the strategy
                _tradeHubStrategy.Run();
            }
            catch (Exception exception)
            {
                _asyncClassLogger.Error(exception, _type.FullName, "ExecuteStrategy");
            }
        }