IIndicatorBinding CreateBinding() { var indicator = this.indicatorType .GetInterfaces() .SingleOrDefault(o => o.IsGenericType && !o.IsGenericTypeDefinition && o.GetGenericTypeDefinition() == typeof(IIndicator <,>)); if (indicator == null) { throw new InvalidOperationException("Type must implement IIndicator<TInput, TResult> interface."); } var valueType = indicator.GenericTypeArguments[0]; var nameAttribute = this.indicatorType.GetCustomAttribute <DisplayNameAttribute>(inherit: false); var descriptionAttribute = this.indicatorType.GetCustomAttribute <DescriptionAttribute>(inherit: false); var companyAttribute = this.indicatorType.GetCustomAttribute <CompanyAttribute>(inherit: false); var categoryAttribute = this.indicatorType.GetCustomAttribute <CategoryAttribute>(inherit: false); var parameters = this.indicatorType .GetCustomAttributes <IndicatorParameterAttribute>() .Select(o => IndicatorParameter.Create(o.Name, o.Type)) .ToArray(); return(new CommonIndicatorBinding( nameAttribute != null ? nameAttribute.DisplayName : string.Empty, descriptionAttribute != null ? descriptionAttribute.Description : string.Empty, companyAttribute != null ? companyAttribute.Company : string.Empty, categoryAttribute != null ? categoryAttribute.Category : string.Empty, valueType, parameters, this.CreateIndicator )); }
public void IndicatorsInstantiateStoreInSnapshot() { this.Executor.ExecutionDataSnapshot.Indicators.Clear(); Type myChild = this.GetType(); PropertyInfo[] lookingForIndicators = myChild.GetProperties(); foreach (PropertyInfo propertyIndicator in lookingForIndicators) { Type hopefullyIndicatorChildType = propertyIndicator.PropertyType; bool isIndicatorChild = typeof(Indicator).IsAssignableFrom(hopefullyIndicatorChildType); if (isIndicatorChild == false) { continue; } Indicator indicatorInstance = null; var expectingNull = propertyIndicator.GetValue(this, null); if (expectingNull != null) { indicatorInstance = expectingNull as Indicator; //Debugger.Break(); //continue; } else { object indicatorInstanceUntyped = Activator.CreateInstance(hopefullyIndicatorChildType); indicatorInstance = indicatorInstanceUntyped as Indicator; if (indicatorInstance == null) { Debugger.Break(); continue; } } indicatorInstance.Name = propertyIndicator.Name; //MORE_EFFECTIVE_IN_Indicator.NotOnChartBarsKey_get() indicatorInstance.OwnValuesCalculated.ScaleInterval = this.Bars.ScaleInterval; //indicatorInstance.BuildParametersFromAttributes(); //indicatorInstance.Initialize() invokes BuildParametersFromAttributes(); HostPanelForIndicator priceOrItsOwnPanel = this.Executor.ChartShadow.GetHostPanelForIndicator(indicatorInstance); indicatorInstance.Initialize(priceOrItsOwnPanel); #region overwriting IndicatorParameters from Strategy attributes; similar to Indicator.BuildParametersFromAttributes() object[] attributes = propertyIndicator.GetCustomAttributes(typeof(IndicatorParameterAttribute), true); string attributesAllAvailable = ""; foreach (object attrObj in attributes) { IndicatorParameterAttribute attr = attrObj as IndicatorParameterAttribute; if (attributesAllAvailable.Length > 0) { attributesAllAvailable += ","; } attributesAllAvailable += attr; } foreach (object attrObj in attributes) { IndicatorParameterAttribute attr = attrObj as IndicatorParameterAttribute; IndicatorParameter param = new IndicatorParameter(attr); if (indicatorInstance.ParametersByName.ContainsKey(param.Name) == false) { string msg = "SCRIPT_SETS_ATTRIBUTE_FOR_INDICATOR_WHICH_IT_DOESNT_HAVE#1 param[" + param + "] attributesAllAvailable[" + attributesAllAvailable + "]"; Assembler.PopupException(msg); continue; } // strategy has an Indicator and an attribute for that indicator; find matching property in the indicator & overwrite PropertyInfo propertyIndicatorFoundUnique = null; PropertyInfo[] lookingForIndicatorParameters = indicatorInstance.GetType().GetProperties(); foreach (PropertyInfo propertyIndicatorInstance in lookingForIndicatorParameters) { if (propertyIndicatorInstance.Name != param.Name) { continue; } propertyIndicatorFoundUnique = propertyIndicatorInstance; break; } if (propertyIndicatorFoundUnique == null) { string msg = "SCRIPT_SETS_ATTRIBUTE_FOR_INDICATOR_WHICH_IT_DOESNT_HAVE#2 param[" + param + "] attributesAllAvailable[" + attributesAllAvailable + "]"; Assembler.PopupException(msg); continue; } object valueCurrentCasted = param.ValueCurrent; if (propertyIndicatorFoundUnique.PropertyType.Name == "Int32") { valueCurrentCasted = (int)Math.Round(param.ValueCurrent); } propertyIndicatorFoundUnique.SetValue(indicatorInstance, valueCurrentCasted, null); indicatorInstance.ParametersByName[param.Name] = param; } // resetting it for fair recalculation to include parameters into this.NameWithParameters; it isn't redundant! indicatorInstance.parametersAsStringShort = null; #endregion propertyIndicator.SetValue(this, indicatorInstance, null); this.Executor.ExecutionDataSnapshot.Indicators.Add(propertyIndicator.Name, indicatorInstance); } }