コード例 #1
0
        public CombinedStrategySettings GetActiveSettings()
        {
            var settings = new CombinedStrategySettings
            {
                ComponentSettings = ComponentSettings.Where(s => s.Enabled).ToArray()
            };

            return(settings);
        }
コード例 #2
0
        public static CombinedStrategySettings GenerateExampleSettings()
        {
            CombinedStrategy.ForceLoad();

            var settings = new CombinedStrategySettings();

            var allComponents = AppDomain.CurrentDomain.GetAssemblies()
                                .SelectMany(a => a.GetTypes()
                                            .Where(type => type.IsClass &&
                                                   !type.IsAbstract &&
                                                   typeof(ITradingStrategyComponent).IsAssignableFrom(type) &&
                                                   !typeof(ITradingStrategy).IsAssignableFrom(type) &&
                                                   !type.IsInterface &&
                                                   !Attribute.IsDefined(type, typeof(DeprecatedStrategyAttribute))));


            settings.ComponentSettings = allComponents
                                         .OrderBy(t => t, new TradingStrategyComponentComparer())
                                         .Select(c => TradingStrategyComponentSettings.GenerateExampleSettings(
                                                     (ITradingStrategyComponent)Activator.CreateInstance(c)))
                                         .ToArray();

            return(settings);
        }
コード例 #3
0
        public CombinedStrategyAssembler(CombinedStrategySettings settings, bool allowRemovingInstructionRandomly)
        {
            if (settings == null)
            {
                throw new ArgumentNullException();
            }

            _componentSettings = settings.ComponentSettings.Where(s => s.Enabled).ToArray();

            if (_componentSettings.Length == 0)
            {
                throw new ArgumentException("No trading strategy component is enabled in settings");
            }

            _allowRemovingInstructionRandomly = allowRemovingInstructionRandomly;

            // verify if component settings can be used for creating new combined strategy
            try
            {
                NewStrategy();
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException("settings can't be used to create a valid combined strategy", ex);
            }

            // verify if components' parameter settings are correct
            var components = CreateComponents().ToArray();

            Debug.Assert(components.Length == _componentSettings.Length);

            for (var i = 0; i < _componentSettings.Length; ++i)
            {
                var attributes           = ParameterHelper.GetParameterAttributes(components[i]).ToArray();
                var allParameterSettings = _componentSettings[i].ComponentParameterSettings;

                if (allParameterSettings == null || allParameterSettings.Length == 0)
                {
                    continue;
                }

                // detect duplicate names
                var duplicateNames = allParameterSettings
                                     .Select(p => p.Name)
                                     .GroupBy(s => s)
                                     .Where(g => g.Count() > 1)
                                     .ToArray();

                if (duplicateNames.Any())
                {
                    throw new InvalidOperationException(
                              string.Format(
                                  "duplicate parameter name {0} is found in component {0} settings",
                                  duplicateNames.First().Key,
                                  components[i].GetType().FullName));
                }

                // verify if name and valueType matches the component's parameter
                foreach (var parameterSettings in allParameterSettings)
                {
                    // varify if there is name not defined in class.
                    var attribute = attributes.Where(a => a.Name == parameterSettings.Name).ToArray();

                    if (!attribute.Any())
                    {
                        throw new InvalidOperationException(
                                  string.Format(
                                      "there is no parameter named {0} in component {1}",
                                      parameterSettings.Name,
                                      components[i].GetType().FullName));
                    }

                    // verify if ValueType is a correct type
                    var valueType = Type.GetType(parameterSettings.ValueType, false);
                    if (valueType == null)
                    {
                        throw new InvalidOperationException(
                                  string.Format("{0} is not a valid type", parameterSettings.ValueType));
                    }

                    // verify if ValueType match the true type in class
                    if (attribute.First().ParameterType != valueType)
                    {
                        throw new InvalidOperationException(
                                  string.Format(
                                      "value type of parameter {0}: {1} does not match the definition of component {2}",
                                      parameterSettings.Name,
                                      parameterSettings.ValueType,
                                      components[i].GetType().FullName));
                    }

                    // verify the values specified in parameter setting is correct
                    object[] values;
                    try
                    {
                        values = parameterSettings.GetParsedValues().ToArray();
                    }
                    catch (Exception ex)
                    {
                        throw new InvalidOperationException(
                                  string.Format(
                                      "Failed to parse value for component {0}, parameter {1}",
                                      components[i].GetType().FullName,
                                      parameterSettings.Name),
                                  ex);
                    }

                    // associate attribute with all possible values
                    if (values.Length > 0)
                    {
                        _parameterValueSelectors.Add(
                            new ParameterValueSelector
                        {
                            Attribute  = attribute.First(),
                            Values     = values,
                            ValueIndex = 0
                        });
                    }
                }
            }

            // calculate the number of parameter value combinations
            NumberOfParmeterValueCombinations = 1L;
            if (_parameterValueSelectors.Count > 0)
            {
                foreach (var selector in _parameterValueSelectors)
                {
                    checked
                    {
                        NumberOfParmeterValueCombinations *= selector.Values.Length;
                    }
                }
            }
        }