[前言]
這個主題寫了快一個月,今天把他做一個總結囉。[程式使用]
1. 將「操盤人學C# (5) 週選擇樂透策略1」製作出來的「Result.txt」放到D槽底下。也可到https://drive.google.com/open?id=0BwqcpF06v4J4a3UwZk9GNUNQM1U直接下載。2. 將「操盤人學C# (6) 週選擇樂透策略2」本來在Multicharts輸出視窗中的文字,貼到新的文字檔,檔名設為「BuyPoint.txt」,放到D槽底下。也可到https://drive.google.com/open?id=0BwqcpF06v4J4TjFoWVU5YTVsZjg直接下載。
3. 執行下方程式碼。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO; //用到檔案讀寫功能
namespace WeeklyOptionLottery3
{
class Program
{
static void Main(string[] args)
{
//先把Result.txt的全部資料存進記憶體
List<string[]> ResultList = new List<string[]>(); //這是一個內容是文字陣列的串列
StreamReader sr_Result = new StreamReader(@"D:\Result.txt", Encoding.Default); //讀取Result.txt用的讀取器
sr_Result.ReadLine(); //第一行標題不要讀,BuyPoint.txt沒有標題所以不用寫這行
//把每一行分割後存進ResultList裡面
while (!sr_Result.EndOfStream)
{
string line = sr_Result.ReadLine();
string[] results = line.Split(',');
ResultList.Add(results); //把買賣點內容塞進去
}
sr_Result.Close(); //關閉讀取器
StreamReader sr_BuyPoint = new StreamReader(@"D:\BuyPoint.txt", Encoding.Default); //讀取BuyPoint.txt用的讀取器
double sum_profit = 0; //儲存損益的變數
while (!sr_BuyPoint.EndOfStream)
{
// 這邊處理買賣點資訊
string line = sr_BuyPoint.ReadLine();
string[] buypoint_array = line.Split(',');
string buy_date = buypoint_array[0];
string buy_corp = buypoint_array[1];
string buy_strike = buypoint_array[2];
// 比對買賣點資訊、損益文件
foreach (string[] result in ResultList)
{
// 處理損益文件
string pnl_date = result[0];
string pnl_month = result[1];
string pnl_strike = result[2];
string pnl_corp = result[3];
string pnl_entryprice = result[4];
string pnl_endPrice = result[5];
if (buy_date == pnl_date && buy_corp == pnl_corp && buy_strike == pnl_strike)
{
double pnl = Convert.ToDouble(pnl_endPrice) - Convert.ToDouble(pnl_entryprice);
sum_profit = sum_profit + pnl;
Console.WriteLine("日期:" + pnl_date + " 合約:" + pnl_month + " 履約價:" + pnl_strike + " 買賣權:" + pnl_corp);
Console.WriteLine("進場價:" + pnl_entryprice + " 出場價:" + pnl_endPrice + " 損益:" + pnl);
Console.WriteLine("============");
}
}
}
sr_BuyPoint.Close();
Console.WriteLine("總損益點數:" + sum_profit.ToString());
Console.ReadLine();
}
}
}
4. 輸出結果截圖[觀念解釋]
<其實就是一堆檔案處理>
這次的內容其實沒有任何新觀念,就是一堆的檔案處理,主要是如何把「BuyPoint.txt」結合「Result.txt」。在這邊使用的方法是先將買賣點全部存到記憶體(ResultList)中,再把BuyPoint一行一行讀出來,每一行都比對全部的ResultList內容,試圖找到日期、合約規格相同的內容。比對內容的程式碼:
if (buy_date == pnl_date && buy_corp == pnl_corp && buy_strike == pnl_strike)
Good job !
回覆刪除你好,請問有機會可以跟Line上請教嗎? 我的line id:aminwhite5168
回覆刪除