StockBag 程序解析
这个程序是用 c# 写的,可以通过MFC 或者QT模仿一个出来
下载地址
https://www.codeproject.com/Articles/23043/StockBag
表格
子窗口
工具栏
工具栏提示
标题栏提示
状态栏
关键词
private void GetStockData(string stockCode, StockData stockData)
{
string serverUrl = @"http://in.finance.yahoo.com/d/quotes.csv?s="+stockCode+
"&f=sl1d1t1c1ohgvj1pp2owern&e=.csv";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serverUrl);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(),
Encoding.ASCII);
string stockDataString = reader.ReadLine();
string[] stockDataContents = stockDataString.Split(',');
11
private void UpdateStockHash()
//Update back in Hash table
lock (m_StockHash.SyncRoot)
{
foreach (StockData stockData in stockList)
{
if (m_StockHash.ContainsKey(stockData.Code))
{
m_StockHash[stockData.Code] = stockData;
}
}
}
这个程序的流程图是
private Hashtable m_StockHash = new Hashtable();
这个是一个全局变量,类似于python中的字段,有key 和value。
key是股票的code,value 是StockData,StockData如下所示:
class StockData
{
public string Code;
public string Last;
public string Date;
public string Time;
public string Change;
public string Open;
public string High;
public string Low;
public string Volume;
public string MarketCapital;
public string PreviousClose;
public string PctChange;
public string AnnRange;
public string Earnings;
public string PERatio;
public float BuyPrice;
public UInt32 BuyQuantity;
}
UpdateStockHashThread的作用的是 执行一个线程一次。
这个线程不是while(true)类型的。是执行完一次web操作,就退出。
调用 UpdateStockHashThread 的地方
m_DataRequestTimer_Tick
m_StartStopToolStripButton_Click
this.m_DataRequestTimer.Enabled = true;
this.m_DataRequestTimer.Interval = 60000;
this.m_DataRequestTimer.Tick += new System.EventHandler(this.m_DataRequestTimer_Tick);
private void m_DataRequestTimer_Tick(object sender, EventArgs e)
{
//Start the thread only if previous timer initiated thread is terminated
if (m_DataRequestThread != null && m_DataRequestThread.ThreadState !=
System.Threading.ThreadState.Stopped)
return;
UpdateStockHashThread();
}
void m_RefreshTimer_Elapsed(object sender, ElapsedEventArgs e)
{
DisplayStockList();
}
private void m_RefreshTimer_Tick(object sender, EventArgs e)
{
DisplayStockList();
}
this.m_RefreshTimer.Enabled = true;
this.m_RefreshTimer.Interval = 2000;
this.m_RefreshTimer.Tick += new System.EventHandler(this.m_RefreshTimer_Tick);
public StockBagMainForm()
{
InitializeComponent();
LoadStockData();
配置文件 StockBagData.xml
StockBagData.xml
<?xml version="1.0" encoding="utf-8"?>
<StockBagData>
<Portfolio>
<Stock Code="stock code" Name="stock name" BuyPrice="0.00" BuyQuantity="0" />
</Portfolio>
<WatchList />
</StockBagData>