public override double OptimizationFunction(StrategyParameter sParam, string directory) { if (!IsValid(sParam)) { return(0); } if (previosResult.ContainsKey(sParam.ToString())) { return(previosResult[sParam.ToString()]); } using (Log4Smart.Logger logger = new Log4Smart.Logger(false)) { Engine engine = new Engine(logger, logger); MartinGaleStrategy strategy = new MartinGaleStrategy(sParam, logger); int initialMoney = 10000000; Account acc = new Account(initialMoney); engine.StartTest(SampleData, strategy, acc); if (!engine.IsTestSuccessfull) { logger.SaveLogToFile(directory + "/errors", SampleData.Symbol + "_" + StartTime.ToString("yyyyMMdd")); previosResult[sParam.ToString()] = 0; return(0); } if (directory != string.Empty) { logger.SaveLogToFile(directory + "/" + this.StartTime.ToString("yyyyMMdd") + "_" + (acc.Balance.ToString()) + "$", "allData"); } if (acc.Balance > initialMoney && this.EstimationFunctionType == EstimationFunctionType.MostMoney8Deviation) { previosResult[sParam.ToString()] = initialMoney + (acc.Balance - initialMoney) * (1 - acc.Statistics.GetDeviation); } else { previosResult[sParam.ToString()] = acc.Balance; } return(previosResult[sParam.ToString()]); } }
public IEnumerable <Order> Init(DateTime currentDateTime, DateTime startSimulate, out List <string> logRecords) { logRecords = new List <string>(); string swapFilePath = Path.Combine(rootPath, "swap.txt"); string symbolPath = Path.Combine(rootPath, Symbol); InputData inData = new InputData(); inData.LoadFromDirectory(symbolPath, new SwapCollection().LoadSwap(swapFilePath), /*currentDateTime.AddMinutes( - CustomParam.InitHistoryMinutes)*/ startSimulate, currentDateTime); DateTime dateTemp = inData.Data.Count == 0 ? startSimulate : inData.Data.Last().DateTime; int shift = Meta.iBarShift(Symbol, CurrentGraphPeriod, dateTemp, false); for (int i = shift; i > 0; i--) { GroupTick gt = new GroupTick(); gt.DateTime = currentDateTime.AddMinutes(-i); gt.OpenBid = gt.OpenAsk = Meta.iOpen(CustomParam.Symbol, CurrentGraphPeriod, i); gt.MaxBid = gt.MaxAsk = Meta.iHigh(CustomParam.Symbol, CurrentGraphPeriod, i); gt.MinBid = gt.MinAsk = Meta.iLow(CustomParam.Symbol, CurrentGraphPeriod, i); if (gt.OpenBid == 0 || gt.MaxBid == 0 || gt.MinBid == 0) { continue; } inData.AddGroupTick(gt); } logRecords.Add(string.Format("{0} {1} were loaded.", Symbol, inData.Data.Count)); CustomParam.InitHistoryMinutes = 0; InputData inputData = inData; Logger = new Log4Smart.Logger(false); Engine = new Engine(Logger, Logger); //save state of CurrentStrategyParam int temp = CurrentStrategyParam["InitHistoryMinutes"]; CurrentStrategyParam["InitHistoryMinutes"] = 0; string firstRawParam = CurrentStrategyParam.GetStringParameter("P1String"); CurrentStrategyParam.SetStringParameter("P1String", CustomParam.RawStrategyParam); Advisor.TesterInit(CurrentStrategyParam, Logger); Account acc = new Account(10000000, 4); Engine.StartTest(inputData, Advisor, acc, false); //restor state of CurrentStrategyParam CurrentStrategyParam["InitHistoryMinutes"] = temp; CurrentStrategyParam.SetStringParameter("P1String", firstRawParam); IsSuccessful = Engine.IsTestSuccessfull; if (!Engine.IsTestSuccessfull) { string directory = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location); string fullDir = directory + string.Format("/errors/{0}/{1}/{2}/", inputData.Symbol, Guid.NewGuid().ToString(), inputData.Data.First().DateTime.ToString("yyyyMMdd")); Logger.SaveLogToFile(fullDir, "error"); } return(Engine.GetLimitOrders().Union(Engine.GetMarketOrders())); }
public double StandardOptimizationFunction(StrategyParameter sParam, string directory, FxAdvisorCore.SimpleAdvisor advisor, ConfigReader configReader, out StrategyResultStatistics srResult) { srResult = new StrategyResultStatistics(); if (!IsValid(sParam)) { return(Double.MinValue); } string cacheKey = CreateKey(SampleData.Symbol, sParam, SampleData.Data.First().DateTime, SampleData.Data.Last().DateTime); if (cacheResult.ContainsKey(cacheKey)) { return(cacheResult[cacheKey]); } using (Log4Smart.Logger logger = new Log4Smart.Logger(false)) { string strategyName = System.Configuration.ConfigurationManager.AppSettings["ExportAdvisorName"]; try { Engine engine = new Engine(logger, logger); advisor.TesterInit(sParam, logger); Account acc = new Account(DefaultAccount.Balance, DefaultAccount.Commission); engine.StartTest(SampleData, advisor, acc); srResult = acc.Statistics; if (!engine.IsTestSuccessfull) { if (string.IsNullOrEmpty(directory)) { directory = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location); } string fullDir = directory + string.Format("/errors/{0}/{1}/{2}/", SampleData.Symbol, Guid.NewGuid().ToString(), StartTime.ToString("yyyyMMdd")); configReader.SaveCurrentParameters(strategyName, sParam, fullDir); logger.SaveLogToFile(fullDir, "error"); cacheResult[cacheKey] = Double.MinValue; return(Double.MinValue); } if (sParam.Contains("MinAmountOrdersFLP") && acc.Statistics.NumberOrders < sParam["MinAmountOrdersFLP"] && string.IsNullOrEmpty(directory)) { cacheResult[cacheKey] = Double.MinValue; return(Double.MinValue); } if (!string.IsNullOrEmpty(directory)) { string fullDir = directory + "/" + this.StartTime.ToString("yyyyMMdd") + "_" + (acc.Balance.ToString()) + "$"; configReader.SaveCurrentParameters(strategyName, sParam, fullDir); logger.SaveLogToFile(fullDir, "allData"); } if (DefaultAccount.Commission > 0) { cacheResult[cacheKey] = acc.Statistics.CalculateConfidenceIntervalLow; } else { cacheResult[cacheKey] = 10 - acc.Statistics.CalculateConfidenceIntervalHigh; } return(cacheResult[cacheKey]); } catch (Exception exc) { (logger as Log4Smart.IStrategyLogger).AddMessage("{0}", exc.ToString()); if (string.IsNullOrEmpty(directory)) { directory = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location); } string fullDir = directory + string.Format("/errors/{0}/{1}/", SampleData.Symbol, StartTime.ToString("yyyyMMdd")); configReader.SaveCurrentParameters(strategyName, sParam, fullDir); logger.SaveLogToFile(fullDir, "error"); cacheResult[cacheKey] = Double.MinValue; return(Double.MinValue); } } }