/// <summary> /// 计算适应度 /// </summary> /// <param name="individual"></param> /// <returns></returns> private double computeFitness(int serinalno, Properties individual) { Properties testParam = context.BacktestParam.Clone(); testParam["serialno"] = serinalno; testParam["batchno"] = serinalno;; IStrategyInstance instance = meta.CreateInstance(serinalno.ToString(), individual, ""); logger.Info("启动回测:..."); instance.Initilization(); TotalStat stat = instance.DoTest(context, testParam); individual.Put("fitness", stat.TotalProfilt); return(stat.TotalProfilt); }
/// <summary> /// 查询指定参数 /// </summary> /// <typeparam name="K"></typeparam> /// <param name="name"></param> /// <returns></returns> public static K GetParameterValue <K>(this IStrategyInstance instance, String name) { KeyValuePair <PropertyDescriptor, Object> kp = instance.Parameters.FirstOrDefault(x => x.Key.hasName(name)); PropertyDescriptor pd = kp.Key; Object value = kp.Value; if (value == null) { return(default(K)); } if (value.GetType() == typeof(K)) { return((K)value); } return(ConvertUtils.ConvertTo <K>(value, pd.Format)); }
/// <summary> /// 收到的是回测序号和回测参数 /// </summary> /// <param name="args"></param> static void Main(string[] args) { init(); //取得参数 if (args == null || args.Length <= 1 || args[0] == null || args[1] == null || args[0] == "" || args[1] == "") { logger.Info("启动失败,参数错误"); return; } backtestxh = args[0]; String paramStr = args[1]; //生成策略参数 alpha = new AlphaStrategy4(); strategyProps = new Properties(); List <String> paramnames = alpha.GetParameterNames(); String[] paramValueArray = paramStr.Split(','); if (paramnames.Count != paramValueArray.Length) { logger.Info("启动失败,策略参数无效:" + paramStr); return; } for (int i = 0; i < paramnames.Count; i++) { strategyProps.Put(paramnames[i], paramValueArray[i]); } //读取回测参数 Properties fileprops = Properties.Load(FileUtils.GetDirectory() + "\\alpha.properties", Encoding.UTF8); Dictionary <String, Properties> propSet = fileprops.Spilt(); backtestProps = propSet["backtest"]; backtestProps["serialno"] = backtestxh.ToString(); //创建策略实例 IStrategyInstance instance = alpha.CreateInstance(backtestxh.ToString(), strategyProps); //执行策略实例的回测 instance.Initilization(); instance.DoTest(new StrategyContext(), backtestProps); }
public Task Go() { Task task = Task.Factory.StartNew(() => { for (int i = 0; i < param.Count; i++) { //准备执行参数 log4net.ILog logger = log4net.LogManager.GetLogger(param[i].backtestxh.ToString()); //启动回测程序并等待结束 IStrategyInstance instance = meta.CreateInstance(param[i].backtestxh, param[i].instanceParam, instanceVersion); logger.Info("启动回测:..."); instance.Initilization(); instance.DoTest(context, param[i].backtestProp); String batchno = param[i].backtestProp.Get <String>("batchno"); String resultPath = param[i].backtestProp.Get <String>("resultpath"); resultPath = FileUtils.GetDirectory(resultPath); String resultFile = resultPath + param[i].backtestxh + ".result"; String batchresultfile = resultPath + batchno + ".result"; if (System.IO.File.Exists(resultFile)) { String[] content = System.IO.File.ReadAllLines(resultFile); lock (batchResultFileLocker) { System.IO.File.AppendAllLines(batchresultfile, content); } logger.Info("合并回测结果:" + ((content == null || content.Length <= 0) ? "" : content[0])); } else { logger.Warn("没有找到执行结果"); } } }); return(task); }
/// <summary> /// 查询指定参数 /// </summary> /// <param name="name"></param> /// <returns></returns> public static Object GetParameterValue(this IStrategyInstance instance, String name) { KeyValuePair <PropertyDescriptor, Object> kp = instance.Parameters.FirstOrDefault(x => x.Key.hasName(name)); return(kp.Value); }
public void DoTest(String strategyName = "") { IndicatorRepository repository = new IndicatorRepository(backtestParam.Datapath); repository.Initilization(); backtestParam.Put("repository", repository); if (strategyName == null || strategyName == "") { strategyName = backtestParam.Get <String>("strategy"); } KeyValuePair <IStrategyMeta, Properties> kv = this.GetStrateMetaAndParam(strategyName); if (kv.Key == null) { return; } IStrategyMeta meta = kv.Key; Properties props = kv.Value; String version = props.Get <String>("version", ""); IStrategyInstance instance = meta.CreateInstance("1", props, version); if (instance == null) { return; } if (backtestParam.Optimization != null && backtestParam.Optimization.ToLower().Contains("ga")) { new EvolutionComputer(backtestParam.Optimization).Start(this); return; } List <Properties> instanceParamSet = spiltStrategyParams(props); int taskCount = this.backtestParam.TaskCount; List <Task> tasks = new List <Task>(); List <Executor> executors = new List <Executor>(); int instanceCountPerTask = instanceParamSet.Count / taskCount; List <ExecuteParam> execParams = new List <ExecuteParam>(); int batchno = backtestParam.BatchNo; String resultPath = backtestParam.Resultpath; for (int i = 0; i < instanceParamSet.Count; i++) { int backtestxh = batchno + i + 1; //回测序号 //结果文件已经有了,跳过 /*String resultfilename = resultPath + backtestxh + ".result"; * if (System.IO.File.Exists(resultfilename)) * continue; */ Properties instanceProp = instanceParamSet[i]; Properties backtestProp = backtestParam.Clone(); backtestProp["serialno"] = backtestxh.ToString(); backtestProp["batchno"] = batchno.ToString(); execParams.Add(new ExecuteParam(backtestxh.ToString(), instanceProp, backtestProp)); if (execParams.Count >= instanceCountPerTask) { Executor executor = new Executor(this, meta, "", execParams); executors.Add(executor); execParams.Clear(); } } if (execParams.Count > 0) { Executor executor = new Executor(this, meta, "", execParams); executors.Add(executor); execParams.Clear(); } System.IO.File.WriteAllText(resultPath + batchno + ".result", meta.GetBatchResultTitle() + System.Environment.NewLine); logger.Info("准备执行任务...,(任务数=" + executors.Count.ToString() + ")"); executors.ForEach(x => tasks.Add(x.Go())); Task.WaitAll(tasks.ToArray()); }