Beispiel #1
0
        protected void BuildParametersFromAttributes()
        {
            this.ParametersByName.Clear();
            Type myChild = this.GetType();

            PropertyInfo[] lookingForIndicatorParameters = myChild.GetProperties();
            foreach (PropertyInfo property in lookingForIndicatorParameters)
            {
                IndicatorParameterAttribute attrFoundUnique = null;
                object[] attributes = property.GetCustomAttributes(typeof(IndicatorParameterAttribute), true);
                foreach (object attrObj in attributes)
                {
                    IndicatorParameterAttribute attr = attrObj as IndicatorParameterAttribute;
                    if (attrFoundUnique != null)
                    {
                        string msg = "ATTRIBUTE_INDICATOR_PARAMETER_SET_MULTIPLE_TIMES_MUST_BE_SINGLE attrFoundUnique[" + attrFoundUnique + "]";
                        Assembler.PopupException(msg);
                    }
                    attrFoundUnique = attr;
                }
                if (attrFoundUnique == null)
                {
                    continue;
                }
                IndicatorParameter param = new IndicatorParameter(attrFoundUnique);

                object valueCurrentCasted = param.ValueCurrent;
                if (property.PropertyType.Name == "Int32")
                {
                    valueCurrentCasted = (int)Math.Round(param.ValueCurrent);
                }
                property.SetValue(this, valueCurrentCasted, null);
                if (this.ParametersByName.ContainsKey(param.Name))
                {
                    string msg = "INDICATOR_PARAMETER_ALREADY_ADDED_MUST_BE_UNIQUE param[" + param + "]";
                    Assembler.PopupException(msg);
                }
                // MOVED_TO_LATER_STAGE
                //string validationError = param.ValidateSelf();
                //if (string.IsNullOrEmpty(validationError) == false) {
                //    string msg = "INDICATOR_SELF_VALIDATION_FAILED [" + validationError + "] for param[" + param + "]";
                //    Assembler.PopupException(msg);
                //}
                this.ParametersByName.Add(param.Name, param);
            }
            // resetting it for fair recalculation to include parameters into this.NameWithParameters; it isn't redundant!
            this.parametersAsStringShort = null;
        }
		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);
			}
		}
		protected void BuildParametersFromAttributes() {
			this.ParametersByName.Clear();
			Type myChild = this.GetType();
			PropertyInfo[] lookingForIndicatorParameters = myChild.GetProperties();
			foreach (PropertyInfo property in lookingForIndicatorParameters) {
				IndicatorParameterAttribute attrFoundUnique = null;
				object[] attributes = property.GetCustomAttributes(typeof(IndicatorParameterAttribute), true);
				foreach (object attrObj in attributes) {
					IndicatorParameterAttribute attr = attrObj as IndicatorParameterAttribute;
					if (attrFoundUnique != null) {
						string msg = "ATTRIBUTE_INDICATOR_PARAMETER_SET_MULTIPLE_TIMES_MUST_BE_SINGLE attrFoundUnique[" + attrFoundUnique + "]";
						Assembler.PopupException(msg);
					}
					attrFoundUnique = attr;
				}
				if (attrFoundUnique == null) continue;
				IndicatorParameter param = new IndicatorParameter(attrFoundUnique);
				
				object valueCurrentCasted = param.ValueCurrent;
				if (property.PropertyType.Name == "Int32") {
					valueCurrentCasted = (int)Math.Round(param.ValueCurrent);
				}
				property.SetValue(this, valueCurrentCasted, null);
				if (this.ParametersByName.ContainsKey(param.Name)) {
					string msg = "INDICATOR_PARAMETER_ALREADY_ADDED_MUST_BE_UNIQUE param[" + param + "]";
					Assembler.PopupException(msg);
				}
				// MOVED_TO_LATER_STAGE
				//string validationError = param.ValidateSelf();
				//if (string.IsNullOrEmpty(validationError) == false) {
				//    string msg = "INDICATOR_SELF_VALIDATION_FAILED [" + validationError + "] for param[" + param + "]";
				//    Assembler.PopupException(msg);
				//}
				this.ParametersByName.Add(param.Name, param);
			}
			// resetting it for fair recalculation to include parameters into this.NameWithParameters; it isn't redundant!
			this.parametersAsStringShort = null;
		}