StockBag 程序解析

StockBag是一款用C#编写的股票数据获取程序,通过HTTP请求从Yahoo Finance获取股票实时数据,并更新内部数据结构。该程序使用了多线程来定期刷新股票信息,并显示在界面上。

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>
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值