Yahoo!ファナンスから日経平均株価の株価時系列データをダウンロードするサンプルプログラム
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// Yahoo!ファナンスから日経平均株価の株価時系列データをダウンロードする
string url = @"http://table.yahoo.co.jp/t?c=2008&a=1&b=1&f=2008&d=2&e=31&g=d&s=998407.o&y=0&z=998407.o";
WebRequest req = WebRequest.Create(url);
WebResponse res = req.GetResponse();
Stream st = res.GetResponseStream();
StreamReader sr = new StreamReader(st, Encoding.GetEncoding("EUC-JP"));
string html = sr.ReadToEnd(); // 取得したデータをコンソールに出力してみる
Console.Write(html);
sr.Close();
st.Close();
// ダウンロードしたデータはhtmlなのでそのままでは使えない。
// htmlを解析して株価時系列データのみを抽出する必要がある
Console.ReadKey(); // キー入力待ち
}
}
}
Split
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// Splitのテスト
string str = "4753.t,(株)ライブドア,マザーズ,1,2001/05/28[1:3]2003/06/25[1:10]2003/12/25[1:100]2004/06/25[1:10]";
string[] words = str.Split(',');
int len = words.Length;
Console.WriteLine(len.ToString());
for(int i = 0;i < len;i++)
{
Console.WriteLine(words[i]);
}
Console.ReadKey(); // キー入力待ち
}
}
}
日付をフォーマットして文字列にする
string str0 = String.Format("{0:yyyyMMdd}",DateTime.Now);
string str1 = String.Format("{0:yyyy/MM/dd hh:mm:ss}", DateTime.Now);
string str2 = DateTime.Now.ToString("yyyy/MM/dd");
小数点のフォーマット
double value = 123.456789;
string s = value.ToString("f2");
Console.WriteLine(s);