public void ExecuteTest() { StrategyInterpreter target = new StrategyInterpreter(); TradingStrategy tradingStrategy = ConstructTradingStrategy(); DateTime startDate = new DateTime(2000,1,7); DateTime endDate = new DateTime(2000,2,25); target.Execute(tradingStrategy, startDate, endDate); PositionSetRuntime longPositions = target.PositionSetTable["LongPositions"]; Assert.IsTrue(longPositions.Positions.Any( p => p.CurrencyInPosition.Name == "USD")); Assert.IsTrue(longPositions.Positions.Any( p => p.CurrencyInPosition.Name == "GBP")); Assert.IsTrue(longPositions.Positions.Any( p => p.CurrencyInPosition.Name == "NOK")); target.Clear(); target.Execute(tradingStrategy, startDate, new DateTime(2000,3,10)); longPositions = target.PositionSetTable["LongPositions"]; Assert.IsTrue(longPositions.Positions.Any(p => p.CurrencyInPosition.Name == "USD")); Assert.IsTrue(longPositions.Positions.Any(p => p.CurrencyInPosition.Name == "GBP")); Assert.IsTrue(longPositions.Positions.Any(p => p.CurrencyInPosition.Name == "NZD")); target.Clear(); target.Execute(tradingStrategy, startDate, new DateTime(2000, 4, 14)); longPositions = target.PositionSetTable["LongPositions"]; Assert.IsTrue(longPositions.Positions.Any(p => p.CurrencyInPosition.Name == "USD")); Assert.IsTrue(longPositions.Positions.Any(p => p.CurrencyInPosition.Name == "NOK")); Assert.IsTrue(longPositions.Positions.Any(p => p.CurrencyInPosition.Name == "NZD")); }
/// <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 ExchangeRateAccessorTest() { TradingStrategy tradingStrategy = new TradingStrategy() { ConstantVariableDefinitions = new System.Collections.Generic.List<GlobalIdentifier>(){ new GlobalIdentifier(){ Variable = new Variable("reEntryExRatePercent", typeof(int)), Constant = new Constant(typeof(decimal),0.05)}, new GlobalIdentifier(){ Variable = new Variable("noOfLongPosition",typeof(int)), Constant = new Constant(typeof(int),3)}, new GlobalIdentifier(){ Variable = new Variable("noOfShortPosition",typeof(int)), Constant = new Constant(typeof(int),3)} }, Portfolio = new Portfolio() { HomeCurrency = "EUR", PositionSets = new List<PositionSet>() { new PositionSet(){ Name = "LongPositions", Number = new Variable("noOfLongPosition",typeof(int)), PositionType = FXStrategy.MetaModel.PositionType.Long }, new PositionSet(){ Name = "ShortPositions", Number = new Variable("noOfShortPosition", typeof(int)), PositionType = FXStrategy.MetaModel.PositionType.Short } } }, TradingRules = new System.Collections.Generic.List<TradingRule>() { new TradingRule("test", new ConcreteTimeDefinition(){ ExecuteTime = new DateTime(2006,1,30) }, new Assignment( new Variable("exRateMVG", typeof(decimal)), new AtTime( new ExchangeRateAccessor( new Constant(typeof(string), "EUR"), new Constant(typeof(string), "USD")) , new Constant(typeof(DateTime), new DateTime(2006,1,30))))) } }; FXEntities.FXEntities fxEntities = new FXEntities.FXEntities(); CurrencyDataSource currencyDataSource = new CurrencyDataSource(fxEntities); StrategyInterpreter target = new StrategyInterpreter(fxEntities, currencyDataSource); DateTime startDate = new DateTime(2006, 1, 29); DateTime endDate = new DateTime(2006, 1, 31); target.Execute(tradingStrategy, startDate, endDate); }
public void ExecutionTimeTest() { TradingStrategy tradingStratgy = ConstructTradingStrategy(); FXEntities.FXEntities fxEntities = new FXEntities.FXEntities(); CurrencyDataSource currencyDataSource = new CurrencyDataSource(fxEntities); currencyDataSource.PreLoad(); StrategyInterpreter target = new StrategyInterpreter(fxEntities, currencyDataSource); DateTime startExecutionTime; DateTime endExecutionTime; DateTime startDate = new DateTime(2000, 1, 1); List<List<double>> results = new List<List<double>>(); StringBuilder output = new StringBuilder(); for (int j = 0; j < 10; j++) { results.Add(new List<double>()); output.AppendLine("Run no.: " + j); for (int i = 0; i < 11; i++) { startExecutionTime = DateTime.Now; target.Execute(tradingStratgy, startDate, startDate.AddYears(i + 1)); endExecutionTime = DateTime.Now; results[j].Add((endExecutionTime - startExecutionTime).TotalMilliseconds); output.AppendLine(String.Format("Execution time: {0}ms, Number of positionRecord: {1} ", (endExecutionTime - startExecutionTime).TotalMilliseconds, target.PositionSetTable.Sum(p => p.Value.Positions.SelectMany(pos => pos.PositionRecords).Count() ) )); target.Clear(); //results.Add(endExecutionTime - startExecutionTime); } output.AppendLine(); } output.AppendLine("Average: "); for (int i = 0; i < 10; i++) { output.AppendLine( (i+1) + " Year: \t" + results.Select(r => r.ElementAt(i)).Average()); } using (StreamWriter writer = new StreamWriter(@"D:\Documents\Study Materials\Master Thesis\Main Thesis\executionTime.txt")) { //for (int i = 1; i <= 10; i++) //{ // output += String.Format(results[i - 1].Milliseconds + " ms \n"); //} writer.WriteLine(output); } }
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"); }
public void TransformerTest2() { string exampleCode = @" global variables{ define reEntryExRatePercent as 0.05; define noOfLongPosition as 3; define noOfShortPosition as 3; } define Portfolio{ set BaseCurrency to 'EUR'; LongPositions: Position<Long>[3]; } rule reallocation executes on every Friday{ for all position in Portfolio.LongPositions{ if position.Currency is not in Top3Currencies: Close position; } for all currency in Top3Currencies{ if currency is not in Currency of Portfolio.LongPositions: Open Portfolio.LongPositions with currency; } }"; StrategyLanguageParser fxParser = new StrategyLanguageParser(); TradingStrategyAstNode strategyAst = fxParser.Parse(exampleCode); AstTreeToModelTransformer transformer = new AstTreeToModelTransformer(); TradingStrategy strategy = transformer.Transform(strategyAst); //Assert.IsTrue(strategy.TradingRules.Count == 1); StrategyInterpreter strategyRunTime = new StrategyInterpreter(); DateTime startDate = new DateTime(2000, 1, 7); DateTime endDate = new DateTime(2000, 2, 25); strategyRunTime.Execute(strategy, startDate, endDate); //PositionSetRunTime longPositions = strategyRunTime.PositionSetTable["LongPositions"]; //Assert.IsTrue(longPositions.Positions.Any(p => p.CurrencyInPosition.Name == "USD")); //Assert.IsTrue(longPositions.Positions.Any(p => p.CurrencyInPosition.Name == "GBP")); //Assert.IsTrue(longPositions.Positions.Any(p => p.CurrencyInPosition.Name == "NOK")); }
public void TransformerTest() { StrategyLanguageParser fxParser = new StrategyLanguageParser(); TradingStrategyAstNode strategyAst = fxParser.Parse(ExampleCode); AstTreeToModelTransformer transformer = new AstTreeToModelTransformer(); TradingStrategy strategy = transformer.Transform(strategyAst); Assert.IsTrue(strategy.TradingRules.Count > 0); StrategyInterpreter strategyRunTime = new StrategyInterpreter(); DateTime startDate = new DateTime(2000, 1, 7); DateTime endDate = new DateTime(2000, 2, 25); Assert.IsTrue(strategy.ConstantVariableDefinitions.Count == 3); strategyRunTime.Execute(strategy, startDate, endDate); PositionSetRuntime longPositions = strategyRunTime.PositionSetTable["LongPositions"]; Assert.IsTrue(longPositions.Positions.Any(p => p.CurrencyInPosition.Name == "USD")); Assert.IsTrue(longPositions.Positions.Any(p => p.CurrencyInPosition.Name == "GBP")); Assert.IsTrue(longPositions.Positions.Any(p => p.CurrencyInPosition.Name == "NOK")); strategyRunTime.Clear(); strategyRunTime.Execute(strategy, startDate, new DateTime(2000, 3, 10)); longPositions = strategyRunTime.PositionSetTable["LongPositions"]; Assert.IsTrue(longPositions.Positions.Any(p => p.CurrencyInPosition.Name == "USD")); Assert.IsTrue(longPositions.Positions.Any(p => p.CurrencyInPosition.Name == "GBP")); Assert.IsTrue(longPositions.Positions.Any(p => p.CurrencyInPosition.Name == "NZD")); strategyRunTime.Clear(); strategyRunTime.Execute(strategy, startDate, new DateTime(2000, 4, 14)); longPositions = strategyRunTime.PositionSetTable["LongPositions"]; Assert.IsTrue(longPositions.Positions.Any(p => p.CurrencyInPosition.Name == "USD")); Assert.IsTrue(longPositions.Positions.Any(p => p.CurrencyInPosition.Name == "NOK")); Assert.IsTrue(longPositions.Positions.Any(p => p.CurrencyInPosition.Name == "NZD")); }