public override object Eval(Context evaluationContext) { if (Type == typeof(PredefinedDataSet)) return PredefinedDataSetEval(evaluationContext); return evaluationContext.ValuesTable[this.Name]; }
public override object Eval(Context evaluationContext) { if (LeftExpression.Type == typeof(bool) && RightExpression.Type == typeof(bool)) return (bool)LeftExpression.Eval(evaluationContext) || (bool)RightExpression.Eval(evaluationContext); else throw new Exception("Unknown Type."); }
public override object Eval(Context evaluationContext) { var set = (IEnumerable<object>) RightExpression.Eval(evaluationContext); var element = LeftExpression.Eval(evaluationContext); return set.Contains(element); }
public override object Eval(Context evaluationContext) { if (LeftExpression.Type == typeof(int) && RightExpression.Type == typeof(int)) return (int)LeftExpression.Eval(evaluationContext) - (int)RightExpression.Eval(evaluationContext); else if (LeftExpression.Type == typeof(decimal) || RightExpression.Type == typeof(decimal)) return (decimal)LeftExpression.Eval(evaluationContext) - (decimal)RightExpression.Eval(evaluationContext); else throw new Exception("Unknown Type."); }
public override object Eval(Context evaluationContext) { var set = (IEnumerable<object>)RightExpression.Eval(evaluationContext); var element = LeftExpression.Eval(evaluationContext); if (set.All(s => s == null)) return true; return !set.Contains(element); }
public override object Eval(Context evaluationContext) { object target = Variable.Eval(evaluationContext); // property access through reflection var propertyInfo = target.GetType().GetProperty(_propertyName); if (propertyInfo != null) return propertyInfo.GetValue(target, null); else throw new Exception(String.Format("The property ({0}) of this variable {1} is not valid: ", _propertyName, Variable.Name)); }
public override object Eval(Context evaluationContext) { object left = LeftExpression.Eval(evaluationContext); object right = RightExpression.Eval(evaluationContext); if (left.GetType() == typeof(decimal)) { if (right.GetType() == typeof(int)) return (decimal)left > (int)right; else return (decimal)left > (decimal)right; } else { if (right.GetType() == typeof(int)) return (int)left > (int)right; else return (int)left > (decimal)right; } }
public override void Initialize(DateTime startDate, DateTime endDate, Context evaluationContext) { _length = (int) _lengthExpr.Eval(evaluationContext); Initialize(startDate, endDate); }
public virtual void Initialize(DateTime startDate, DateTime endDate, Context evaluationContext){ Initialize(startDate,endDate); }
public override object Eval(Context evaluationContext) { return LeftExpression.Eval(evaluationContext) != RightExpression.Eval(evaluationContext); }
public abstract object Eval(Context evaluationContext);
/// <summary> /// for pre-defined set, they are Top3Currencies or Bottom3Currencies /// </summary> /// <param name="evaluationContext"></param> /// <returns></returns> public object PredefinedDataSetEval(Context evaluationContext) { var result = evaluationContext.PredefinedDataContainer.GetData(Name, evaluationContext.CurrentDate); return result; }
/// <summary> /// The method for executing the trading strategy. /// The result are the position records that located in the position sets /// </summary> /// <param name="tradingStrategy">the trading strategy</param> /// <param name="startDate">start date of back-testing</param> /// <param name="endDate">end date of back-testing</param> public void Execute(TradingStrategy tradingStrategy, DateTime startDate, DateTime endDate) { Currency baseCurrency = _currencyTable[tradingStrategy.Portfolio.HomeCurrency]; // initialize evaluation context var collectionDataContainer = new PredefinedDataContainer(); collectionDataContainer.Add(new Top3Currencies(_currencyDataSource, baseCurrency)); // predefined data set "Top3Currencies" collectionDataContainer.Add(new Bottom3Currencies(_currencyDataSource, baseCurrency)); // predefine data set "Bottom3Currencies" _evaluationContext = new Context(_currencyDataSource, collectionDataContainer, new ValuesTable(10), startDate); // Add the current date of execution into values table _evaluationContext.ValuesTable.Add(TODAY_STR, startDate); // Add base currency as a variable _evaluationContext.ValuesTable.Add(BASE_CURRENCY_STR, baseCurrency.Name); // put all the paramter definitions into the table foreach (var param in tradingStrategy.ConstantVariableDefinitions) { _evaluationContext.ValuesTable.Add(param.Variable.Name, param.Constant.Eval(_evaluationContext)); } // initialize position set foreach (var positionDef in tradingStrategy.Portfolio.PositionSets) { PositionType type = (positionDef.PositionType == FXStrategy.MetaModel.PositionType.Long)?PositionType.Long:PositionType.Short; CreatePositionSet(positionDef.Name, baseCurrency, (int)positionDef.Number.Eval(_evaluationContext), type); } // initialize execute frequency tradingStrategy.TradingRules.ForEach(t => t.ExecuteFrequency.Initialize(startDate, endDate,_evaluationContext)); // add predefined data set into collection data container for(DateTime currentDate = startDate; currentDate <= endDate; currentDate = currentDate.AddDays(1)) { // No activities on weekend if (DateTimeHelper.IsWeekEnd(currentDate)) continue; _evaluationContext.ValuesTable[TODAY_STR] = currentDate; _evaluationContext.CurrentDate = currentDate; foreach (TradingRule rule in tradingStrategy.TradingRules) { // evaluate the rule inner statement // only if current date fit to its execution frequency if (rule.ExecuteFrequency.CanExecute(currentDate)) EvaluateStatement(rule.InnerStatement); } } }
public override object Eval(Context evaluationContext) { return Value; }