public void startTest(int guid) { this.guid = guid; try { testReportFileName = reportDir + now.Year + "_" + now.Month + "_" + now.Day + "_" + now.Hour + "_" + now.Minute + "_" + now.Second + "_"; if (loseLine != null) { testReportFileName += loseLine[1] + "_"; } if (winLine != null) { testReportFileName += winLine[1] + "_"; } if (reverseLine != null) { for (int i = 1; i <= reverseLine.Count; i++) { testReportFileName += reverseLine[i] + "_"; } } testReportFileName += guid + ".rpt"; testReportFile = new TradeFile(testReportFileName); testReportFile.prepareWriter(); } catch (Exception ex) { Console.WriteLine(ex.Message); Console.WriteLine(ex.Source); Console.WriteLine(ex.StackTrace); reportMsg(ex.Source + ex.Message + ex.StackTrace); } double totalProfit = 0; //所有測試日期,各自跑了XX次之後的總利潤 double oneDayProfit = 0; //某日的單日利潤 int winDayCount = 0; //獲利日數 int loseDayCount = 0; //賠錢日數 int winCountInOneDayTradeRunManyTimes = 0; //一日交易中有幾次獲利,執行數次測試之後的結果 int loseCountInOneDayTradeRunManyTimes = 0; //一日交易中有幾次賠錢,執行數次測試之後的結果 int totalWinCountRumManyTimes = 0; //總獲利次數 int totalLoseCountRunManyTimes = 0; //總賠錢次數 int[] profitRange = new int[45]; //獲利的範圍 double maxWinPureProfit = 0; //單日最大獲利 double maxLosePureProfit = 0; //單日最大賠錢 for (int i = 0; i < 45; i++) { profitRange[i] = 0; } FileManager fm = new FileManager(); List <TradeFile> oFileList = fm.getTradeFileList(sourceFileDir); testDayCount = oFileList.Count; if (oFileList == null || oFileList.Count <= 0) { reportMsg("目錄內無檔案!"); return; } //for (int j = 0; j < oFileList.Count; j++) { double oneDayRunManyTimesTotalProfit = 0; double oneDayMaxLossSetting = -9999999;//單日最大停損設定 TradeManager manager = new TradeManager(); //List<TradeFile> fileList = new List<TradeFile>(); //fileList.Add(oFileList[j]); //manager.setSourceFileList(fileList); manager.setSourceFileList(oFileList); recordList = manager.prepareRecordList(); for (int i = 0; i < runCount; i++) { oneDayRunManyTimesTotalProfit = 0; //某日跑了XX次之後的總利潤 winCountInOneDayTradeRunManyTimes = 0; //一日交易中有幾次獲利 loseCountInOneDayTradeRunManyTimes = 0; //一日交易中有幾次賠錢 manager = new TradeManager(); manager.RecordList = recordList; manager.ReportFile = testReportFile; if (maxLoss != null && !maxLoss.Equals("")) { manager.setMaxProfitLoss(Convert.ToDouble(maxLoss)); } if (checkCount > 0) { manager.setCheckCount(checkCount); } manager.LotLimit = lotLimit; manager.setDebugEnabled(DEBUG); manager.setStopRatio(stopRatio); manager.setRatio(ratio); manager.setCore(coreMethod); manager.setLots(lotArray); manager.setWinLine(winLine); manager.setLoseLine(loseLine); manager.setReverseLine(reverseLine); //manager.setSourceFile(oFileList[j]);//當沖 oneDayProfit = manager.startTrade(); //recordList.Clear();//當沖 int tmpMaxLot = manager.getMaxLot(); if (tmpMaxLot > maxLot) { maxLot = tmpMaxLot; } maxContinueLossMoney = manager.MaxContinueLossMoney; maxContinueLossMoneyTime = manager.MaxContinueLossMoneyTime; maxContinueWinMoney = manager.MaxContinueWinMoney; maxContinueWinMoneyTime = manager.MaxContinueWinMoneyTime; winCountInOneDayTradeRunManyTimes += manager.WinVolume; loseCountInOneDayTradeRunManyTimes += manager.LoseVolume; oneDayPureProfit = oneDayProfit * valuePerPoint - (manager.WinVolume + manager.LoseVolume) * cost; if (oneDayPureProfit > 0) { winDayCount++; } else { loseDayCount++; } if (oneDayPureProfit > maxWinPureProfit) { maxWinPureProfit = oneDayPureProfit; //maxWinPureProfitFileName = oFileList[j].getFileName(); } if (oneDayPureProfit < maxLosePureProfit) { maxLosePureProfit = oneDayPureProfit; //maxLosePureProfitFileName = oFileList[j].getFileName(); } if (oneDayPureProfit > 0 && oneDayPureProfit < 2000) { profitRange[0]++; } else if (oneDayPureProfit > 100000) { profitRange[38]++; } else if (oneDayPureProfit > 90000) { profitRange[37]++; } else if (oneDayPureProfit > 80000) { profitRange[36]++; } else if (oneDayPureProfit > 70000) { profitRange[35]++; } else if (oneDayPureProfit > 60000) { profitRange[34]++; } else if (oneDayPureProfit > 50000) { profitRange[33]++; } else if (oneDayPureProfit > 40000) { profitRange[32]++; } else if (oneDayPureProfit > 30000) { profitRange[31]++; } else if (oneDayPureProfit > 20000) { profitRange[10]++; } else if (oneDayPureProfit > 10000) { profitRange[9]++; } else if (oneDayPureProfit > 9000) { profitRange[8]++; } else if (oneDayPureProfit > 8000) { profitRange[7]++; } else if (oneDayPureProfit > 7000) { profitRange[6]++; } else if (oneDayPureProfit > 6000) { profitRange[5]++; } else if (oneDayPureProfit > 5000) { profitRange[4]++; } else if (oneDayPureProfit > 4000) { profitRange[3]++; } else if (oneDayPureProfit > 3000) { profitRange[2]++; } else if (oneDayPureProfit > 2000) { profitRange[1]++; } else if (oneDayPureProfit < -50000) { profitRange[41]++; } else if (oneDayPureProfit < -40000) { profitRange[40]++; } else if (oneDayPureProfit < -30000) { profitRange[39]++; } else if (oneDayPureProfit < -20000) { profitRange[30]++; } else if (oneDayPureProfit < -10000) { profitRange[29]++; } else if (oneDayPureProfit < -9000) { profitRange[28]++; } else if (oneDayPureProfit < -8000) { profitRange[27]++; } else if (oneDayPureProfit < -7000) { profitRange[26]++; } else if (oneDayPureProfit < -6000) { profitRange[25]++; } else if (oneDayPureProfit < -5000) { profitRange[24]++; } else if (oneDayPureProfit < -4000) { profitRange[23]++; } else if (oneDayPureProfit < -3000) { profitRange[22]++; } else if (oneDayPureProfit < -2000) { profitRange[21]++; } if (oneDayPureProfit < 0 && oneDayPureProfit > -2000) { profitRange[20]++; } totalProfit += oneDayProfit; oneDayRunManyTimesTotalProfit += oneDayProfit; oneDayMaxLossSetting = manager.getMaxProfitLoss(); Console.WriteLine("交易結束,單日交易總利潤 : " + oneDayPureProfit); } totalWinCountRumManyTimes += winCountInOneDayTradeRunManyTimes; totalLoseCountRunManyTimes += loseCountInOneDayTradeRunManyTimes; reportMsg(//oFileList[j].getFullPath() + "交易結束,單日交易平均利潤 : " + ((oneDayRunManyTimesTotalProfit * valuePerPoint) - (winCountInOneDayTradeRunManyTimes + loseCountInOneDayTradeRunManyTimes) * cost) / runCount); reportMsg(//oFileList[j].getFullPath() + "交易結束,單日獲利口數 : " + winCountInOneDayTradeRunManyTimes); reportMsg(//oFileList[j].getFullPath() + "交易結束,單日賠錢口數 : " + loseCountInOneDayTradeRunManyTimes); reportMsg(//oFileList[j].getFullPath() + "交易結束,單日獲利口數的總比率 : " + Convert.ToDouble(winCountInOneDayTradeRunManyTimes) / ((Convert.ToDouble(winCountInOneDayTradeRunManyTimes) + Convert.ToDouble(loseCountInOneDayTradeRunManyTimes))) * 100 + " %"); reportMsg("最大連續贏錢" + maxContinueWinMoney); reportMsg("最大連續贏錢日" + maxContinueWinMoneyTime); reportMsg("最大連續賠錢" + maxContinueLossMoney); reportMsg("最大連續賠錢日" + maxContinueLossMoneyTime); reportMsg("----------------------------------------------------------------------------------------------"); reportMsg("----------------------------------------------------------------------------------------------"); reportMsg("----------------------------------------------------------------------------------------------"); }//end for fileList reportMsg("程式版本 :" + Version); reportMsg("說明 :" + Comment); reportMsg(" 測試編號 : " + guid); reportMsg(" 每個交易日的測試次數 : " + runCount); reportMsg("獲利口數 : " + totalWinCountRumManyTimes); reportMsg("賠錢口數 : " + totalLoseCountRunManyTimes); reportMsg("交易結束,獲利口數的總比率 : " + Convert.ToDouble(totalWinCountRumManyTimes) / ((Convert.ToDouble(totalWinCountRumManyTimes) + Convert.ToDouble(totalLoseCountRunManyTimes))) * 100 + " %"); reportMsg("獲利日數 : " + winDayCount); reportMsg("賠錢日數" + loseDayCount); winDayRate = Convert.ToDouble(winDayCount) / ((Convert.ToDouble(winDayCount) + Convert.ToDouble(loseDayCount))); reportMsg("交易結束,獲利日數的總比率 : " + winDayRate * 100 + " %"); reportMsg("單日最大獲利 : " + maxWinPureProfit); reportMsg("最大獲利 是哪一天: " + maxWinPureProfitFileName); reportMsg("單日最大賠錢 : " + maxLosePureProfit); reportMsg("最大賠錢 是哪一天: " + maxLosePureProfitFileName); reportMsg("單日設定最大停損" + maxLoss); reportMsg("曾經最大交易口數 : " + maxLot); reportMsg("總獲利口數 : " + totalWinCountRumManyTimes); reportMsg("總賠錢口數 : " + totalLoseCountRunManyTimes); reportMsg("總手續費 : " + (totalWinCountRumManyTimes + totalLoseCountRunManyTimes) * cost); reportMsg("平均手續費 : " + ((totalWinCountRumManyTimes + totalLoseCountRunManyTimes) * cost) / (runCount * testDayCount)); double pureProfit = ((totalProfit * valuePerPoint - (totalWinCountRumManyTimes + totalLoseCountRunManyTimes) * cost)) / (runCount * testDayCount); reportMsg(runCount * oFileList.Count + "次,總利潤 : " + totalProfit * valuePerPoint); reportMsg(runCount * oFileList.Count + "次,扣除手續費後,總平均利潤 : " + pureProfit); reportMsg("獲利兩千以下次數 : " + profitRange[0]); reportMsg("獲利兩千以上次數 : " + profitRange[1]); reportMsg("獲利三千以上次數 : " + profitRange[2]); reportMsg("獲利四千以上次數 : " + profitRange[3]); reportMsg("獲利五千以上次數 : " + profitRange[4]); reportMsg("獲利六千以上次數 : " + profitRange[5]); reportMsg("獲利七千以上次數 : " + profitRange[6]); reportMsg("獲利八千以上次數 : " + profitRange[7]); reportMsg("獲利九千以上次數 : " + profitRange[8]); reportMsg("獲利一萬以上次數 : " + profitRange[9]); reportMsg("獲利兩萬以上次數 : " + profitRange[10]); reportMsg("獲利三萬以上次數 : " + profitRange[31]); reportMsg("獲利四萬以上次數 : " + profitRange[32]); reportMsg("獲利五萬以上次數 : " + profitRange[33]); reportMsg("獲利六萬以上次數 : " + profitRange[34]); reportMsg("獲利七萬以上次數 : " + profitRange[35]); reportMsg("獲利八萬以上次數 : " + profitRange[36]); reportMsg("獲利九萬以上次數 : " + profitRange[37]); reportMsg("獲利十萬以上次數 : " + profitRange[38]); reportMsg("----------------------------------------------------------------------------------------------"); reportMsg("賠錢兩千以下次數 : " + profitRange[20]); reportMsg("賠錢兩千以上次數 : " + profitRange[21]); reportMsg("賠錢三千以上次數 : " + profitRange[22]); reportMsg("賠錢四千以上次數 : " + profitRange[23]); reportMsg("賠錢五千以上次數 : " + profitRange[24]); reportMsg("賠錢六千以上次數 : " + profitRange[25]); reportMsg("賠錢七千以上次數 : " + profitRange[26]); reportMsg("賠錢八千以上次數 : " + profitRange[27]); reportMsg("賠錢九千以上次數 : " + profitRange[28]); reportMsg("賠錢一萬以上次數 : " + profitRange[29]); reportMsg("賠錢兩萬以上次數 : " + profitRange[30]); reportMsg("賠錢三萬以上次數 : " + profitRange[39]); reportMsg("賠錢四萬以上次數 : " + profitRange[40]); reportMsg("賠錢五萬以上次數 : " + profitRange[41]); reportMsg("----------------------------------------------------------------------------------------------"); reportMsg("----------------------------------------------------------------------------------------------"); if (pureProfit > 500 || winDayRate > 0.5) { conclusionMsg("----------------------------------------------------------------------------------------------"); conclusionMsg("交易結束,獲利口數的總比率 : " + Convert.ToDouble(totalWinCountRumManyTimes) / ((Convert.ToDouble(totalWinCountRumManyTimes) + Convert.ToDouble(totalLoseCountRunManyTimes))) * 100 + " %"); conclusionMsg("交易結束,獲利日數的總比率 : " + Convert.ToDouble(winDayCount) / ((Convert.ToDouble(winDayCount) + Convert.ToDouble(loseDayCount))) * 100 + " %"); if (loseLine != null) { for (int i = 1; i <= winLine.Count; i++) { conclusionMsg("測試規則LOSE 00" + i + ":" + loseLine[i]); } } if (winLine != null) { for (int i = 1; i <= winLine.Count; i++) { conclusionMsg("測試規則WIN 00" + i + ":" + winLine[i]); } } if (reverseLine != null) { for (int i = 1; i <= winLine.Count; i++) { conclusionMsg("測試規則REVERSE 00" + i + ":" + reverseLine[i]); } } conclusionMsg("曾經最大交易口數 : " + maxLot); conclusionMsg(runCount * oFileList.Count + "次,扣除手續費後,總平均利潤 : " + pureProfit); conclusionMsg("----------------------------------------------------------------------------------------------"); } }//end startTrade(guid)
public Boolean prepareTest() { appDir = System.IO.Directory.GetCurrentDirectory(); //主程式所在目錄 appDir = System.Windows.Forms.Application.StartupPath; string configFilePath = appDir + "\\" + Config_Dir + "\\" + Config_File_Name; ConfigFile configFile = new ConfigFile(configFilePath); try { configFile.prepareReader(); coreMethod = configFile.readConfig("Core_Method"); ruleCountWin = Convert.ToInt32(configFile.readConfig("Rule_Count_Win")); ruleCountLose = Convert.ToInt32(configFile.readConfig("Rule_Count_Lose")); string strRuleCountReverse = "Rule_Count_Reverse_"; for (int i = 1; i <= 10; i++) { ruleCountReverse[i - 1] = Convert.ToInt32(configFile.readConfig(strRuleCountReverse + Convert.ToString(i))); } runCount = Convert.ToInt32(configFile.readConfig("Run_Count")); rulePeriod = Convert.ToInt32(configFile.readConfig("Rule_Period")); maxLoss = configFile.readConfig("Max_Loss"); sourceDir = configFile.readConfig("Source_Dir"); ratio = Convert.ToDouble(configFile.readConfig("Ratio")); lotLimit = Convert.ToInt32(configFile.readConfig("Lot_Limit")); checkCount = Convert.ToInt32(configFile.readConfig("Check_Count")); reversePeriod = Convert.ToDouble(configFile.readConfig("Reverse_Period")); if (null == sourceDir) { sourceDir = Source_Dir; } } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine(e.Source); Console.WriteLine(e.StackTrace); return(false); } List <int> lotList = new List <int>(); try { lots = configFile.readConfig("Lots"); lotArray = lots.Split(','); } catch (Exception ee) { Console.WriteLine(ee.StackTrace); } //----------------------------------------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------------------------------------- StrategyFile strategyInstance = StrategyFile.getInstance(); Boolean isRuleReady = false; if (TradeManager.Core_Method_1.Equals(coreMethod) || TradeManager.Core_Method_2.Equals(coreMethod)) { isRuleReady = strategyInstance.dealStrategyRule(appDir, "TestStrategy.txt"); this.winLine = strategyInstance.getWinLine(); this.loseLine = strategyInstance.getLoseLine(); this.reverseLine = strategyInstance.getReverseLine(); } else if (TradeManager.Core_Method_3.Equals(coreMethod)) { isRuleReady = strategyInstance.dealStopRatioRule(appDir, "TestStrategy.txt"); this.stopRatio = strategyInstance.getStopRatio(); } if (!isRuleReady) { return(false); } //----------------------------------------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------------------------------------- reportDir = appDir + "\\" + Report_Dir + "\\"; conclusionDir = appDir + "\\" + Conclusion_Dir + "\\"; sourceFileDir = appDir + "\\" + sourceDir + "\\"; conclusionReportFileName = conclusionDir + now.Year + "_" + now.Month + "_" + now.Day + "_" + now.Hour + "_" + now.Minute + "_" + now.Second + "_Conclusion.rpt"; conclusionReport = new TradeFile(conclusionReportFileName); conclusionReport.prepareWriter(); conclusionMsg("程式版本 :" + Version); conclusionMsg("說明 :" + Comment); conclusionMsg("使用核心:" + coreMethod); conclusionMsg("單日設定最大停損" + maxLoss); conclusionMsg("動態停利反轉比率:" + ratio); conclusionMsg("下單口數:" + lots); conclusionMsg("測試次數:" + runCount); conclusionMsg("確定買賣方向的檢查機制,時間間隔次數:" + checkCount); conclusionMsg("----------------------------------------------------------------------------------------------"); if (stopRatio != null) { conclusionMsg("逆勢動態停利規則 : " + stopRatio.ToString()); } return(true); }