/// <summary> /// Start the process of analysing the strategy from source code /// </summary> /// <param name="startDate">start date of back-testing</param> /// <param name="endDate">end date of back-testing</param> /// <param name="monthsInTerm">length of forward contract in months</param> /// <param name="fxCode">source code for a strategy</param> /// <returns>Profit Index over time</returns> public IEnumerable<DateIndex> Execute(DateTime startDate, DateTime endDate, int monthsInTerm, string fxCode) { // these objects are created for each execution // it is to support multi-threading var fxParser = new StrategyLanguageParser(); var modelTransformer = new ASTToModelTransform.AstTreeToModelTransformer(); var calculationEngine = new ProfitCalculationEngine(_currencyDataSource); var strategyRunTime = new StrategyInterpreter(_currencyDataSource); // parse the source code into abstract syntax tree var strategyAstNode = fxParser.Parse(fxCode); // transform the abstract syntax tree to the model var strategyModel = modelTransformer.Transform(strategyAstNode); // execute the model strategyRunTime.Clear(); strategyRunTime.Execute(strategyModel, startDate, endDate); // calculate profit based on the position records calculationEngine.Evaluate( strategyRunTime.PositionSetTable.Values.SelectMany(v => v.Positions).ToList(), startDate,endDate, new PeriodicTimeDefinition(monthsInTerm, PeriodicType.Month)); return calculationEngine.IndexOverTime.Select( i => new DateIndex(){ Date = i.Time, Index = i.Value }); }
public void CalculationTest() { FXEntities.FXEntities fxEntities = new FXEntities.FXEntities(); CurrencyDataSource currencyDataSource = new CurrencyDataSource(fxEntities); StrategyInterpreter target = new StrategyInterpreter(fxEntities, currencyDataSource); TradingStrategy tradingStrategy = ConstructTradingStrategy(); DateTime startDate = new DateTime(2000, 1, 4); DateTime endDate = new DateTime(2002, 1, 1); target.Execute(tradingStrategy, startDate, endDate); ProfitCalculationEngine calEngine = new ProfitCalculationEngine( currencyDataSource ); calEngine.Evaluate(target.PositionSetTable["ShortPositions"].Positions, startDate, endDate, new PeriodicTimeDefinition(3, PeriodicType.Month) ); //calEngine.Analyze(target.PositionSetTable["LongPositions"].Positions // .Union(target.PositionSetTable["ShortPositions"].Positions).ToList(), // startDate, // endDate, new PeriodicTimeDefinition(3, PeriodicType.Month) // ); List<TimeSeriesData> returnOverTime = calEngine.ReturnOverTime; string dest = @"C:\temp\ReturnOverTime.txt"; WriteToFile(returnOverTime, dest); WriteToFile(calEngine.IndexOverTime, @"C:\temp\IndexOverTime.txt"); }