예제 #1
0
파일: Form1.cs 프로젝트: ifzz/AutoTrade
        private void Form1_Load(object sender, EventArgs e)
        {


            this.yuantaOrderAPI = new YuantaOrdClass();

            try
            {
                yuantaOrderAPI.OnLogonS += new _DYuantaOrdEvents_OnLogonSEventHandler(yuantaOrderAPI_OnLogonS);
            }
            catch (Exception ex)
            {
                throw ex;
            }

            timer1.Enabled = false;

            label_Version.Text = TradeUtility.TradeUtility.version;

            appDir = System.Windows.Forms.Application.StartupPath;

            trackFileDir = appDir + "\\" + Track_Dir + "\\";

            FileManager fm = new FileManager();

            trackFileList = fm.getConfigFileList(trackFileDir);

            configFilePath = appDir + "\\" + Config_Dir + "\\" + Config_File_Name;

            try
            {

                configFile = new ConfigFile(configFilePath);

            }
            catch (Exception ex)
            {
                MessageBox.Show("讀取  設定檔  失敗。原因 : " + ex.Message);

                return;
            }

            tradeCode = configFile.readConfig("Trade_Code");

            string tradeMonth = configFile.readConfig("Trade_Month");

            string tradeYear = configFile.readConfig("Trade_Year");

            string tradeMonthFilePath = appDir + "\\" + Config_Dir + "\\" + Month_File_Name;

            try
            {

                tradeCode = TradeUtility.TradeUtility.getInstance().dealTradeCode(tradeMonthFilePath, tradeCode, tradeMonth, tradeYear);

            }
            catch (Exception ex)
            {

                MessageBox.Show("讀取  月份代碼檔  失敗。原因 : " + ex.Message);

                return;
            }

            ipQuote = configFile.readConfig("IP_Quote");

            id = configFile.readConfig("ID");

            password = configFile.readConfig("Password");

            ipAPI = configFile.readConfig("IP_API");

            port = configFile.readConfig("Port");

            lots = configFile.readConfig("Lots");

            maxLoss = configFile.readConfig("Max_Loss");

            branchCode = configFile.readConfig("Branch_Code");

            account = configFile.readConfig("Account_Code");

            futuresCode = configFile.readConfig("Futures_Code");

            lotLimit = configFile.readConfig("Lot_Limit");

            try
            {

                master = new TradeMaster(this);

                master.setLotLimit(Convert.ToInt16(lotLimit));

                master.setFuturesCode(futuresCode);

                master.setMaxLoss(Convert.ToInt32(maxLoss));

                master.setID(id);

                master.setPassword(password);

                master.setBranchCode(branchCode);

                master.setAccount(account);

                master.setLots(lots);

                master.setIpAPI(ipAPI);

                master.setOrderAPI(yuantaOrderAPI);

                master.setTradeCode(tradeCode);

                master.prepareFirst();

                master.prepareTrackFile();

                readTrackFile(master);

                master.prepareDataFromLastTradeDay();

            }
            catch (Exception ez)
            {
                MessageBox.Show(ez.Message + "--" + ez.StackTrace);

                return;
            }

            textBox_loseLine.Text = Convert.ToString(master.getLoseLine()[1]);

            textBox1_winLine.Text = Convert.ToString(master.getWinLine()[1]);

            textBox_reverseLine.Text = Convert.ToString(master.getReverseLine()[1]);

            textBox_B_S.Text = master.OrderDircetion;

            textBox_MaxPrice.Text = Convert.ToString(master.MaxTradePointLastDay);

            textBox_MinPrice.Text = Convert.ToString(master.MinTradePointLastDay);

            textBox_OrderPrice.Text = Convert.ToString(master.OrderPrice);

            textBox_OrderStart.Text = Convert.ToString(master.IsStartOrder);

            textBox_NowTradeType.Text = master.NowTradeType;

            if (master.OrderNewPriceList != null)
            {
                for (int i = 0; i < master.OrderNewPriceList.Count; i++)
                {
                    textBox_OrderNewPriceList.Text += master.OrderNewPriceList[i] + Environment.NewLine;
                }
            }



            loginQuote();

            loginOrder();

        }
예제 #2
0
        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)