前回、株価を保存するために、4本値のフィールドを持ったテーブルでは良くないことを説明しました。
では、少し視点を変えて、株価データ、信用残データ、ティックデータに共通することは何でしょうか。この観点から、テーブルのフィールドを考えてみます。
共通点は2つあります。まず、時系列のデータであること。もう一つは、株に関するデータであることです。つまり、言い換えると、「時系列(日時)」で「株に関する(銘柄コード)(市場)」「データ(値)」となるわけです。ここで( )でくくった言葉はフィールドとして抽出できるものです。

実際にどういったデータがどのように格納されるかは、以下のようなイメージになります(一部のフィールドを省略して表現しています)。
t_category
t_item
t_value
いかがだったでしょうか。拡張性が高く、管理もしやすい、テーブル設計が行えました。このようにテーブル設計をしておくことで、例えば、今後、テクニカル分析を行って、移動平均、RSI、一目均衡表の値などもテーブルを増やすことなく、データを保存することができるようになります。読者の皆さん、独自に拡張することに挑戦してみてください。
最後に、各テーブルをCreateするSQLと株価4本値を取得するビューのSQL例を掲載しておきます。参考にしてみてください。
- 日時
- 銘柄コード
- 市場
- 値
- 期間
- カテゴリー
- アイテム

実際にどういったデータがどのように格納されるかは、以下のようなイメージになります(一部のフィールドを省略して表現しています)。
t_category
category_id | category_name |
---|---|
1 | stockprice |
2 | creditbalance |
3 | tick |
t_item
category_id | item_id | item_name |
---|---|---|
1 | 1 | volume |
1 | 2 | open_price |
1 | 3 | high_price |
1 | 4 | low_price |
1 | 5 | close_price |
2 | 1 | buy |
2 | 2 | sell |
3 | 1 | price |
4 | 2 | volume |
t_value
category_id | item_id | datetime | value | term |
---|---|---|---|---|
1 | 1 | 2011/12/10 | 2000 | d |
1 | 2 | 2011/12/10 | 150 | d |
1 | 3 | 2011/12/10 | 170 | d |
1 | 4 | 2011/12/10 | 140 | d |
1 | 5 | 2011/12/10 | 160 | d |
1 | 1 | 2011/12/10 10:00 | 15 | 1m |
1 | 2 | 2011/12/10 10:00 | 205 | 1m |
1 | 3 | 2011/12/10 10:00 | 207 | 1m |
1 | 4 | 2011/12/10 10:00 | 204 | 1m |
1 | 5 | 2011/12/10 10:00 | 206 | 1m |
2 | 1 | 2011/12/10 | 15000 | w |
2 | 2 | 2011/12/10 | 20000 | w |
3 | 1 | 2011/12/10 10:00 | 205 | tc |
3 | 2 | 2011/12/10 10:00 | 10 | tc |
いかがだったでしょうか。拡張性が高く、管理もしやすい、テーブル設計が行えました。このようにテーブル設計をしておくことで、例えば、今後、テクニカル分析を行って、移動平均、RSI、一目均衡表の値などもテーブルを増やすことなく、データを保存することができるようになります。読者の皆さん、独自に拡張することに挑戦してみてください。
最後に、各テーブルをCreateするSQLと株価4本値を取得するビューのSQL例を掲載しておきます。参考にしてみてください。
CREATE TABLE t_category ( category_id smallint NOT NULL, category_name character varying(64) NOT NULL, CONSTRAINT t_category_pkey PRIMARY KEY (category_id) ) CREATE TABLE t_item ( category_id smallint NOT NULL, item_id smallint NOT NULL, item_name character varying(64) NOT NULL, CONSTRAINT t_item_pkey PRIMARY KEY (category_id, item_id), CONSTRAINT t_item_category_id FOREIGN KEY (category_id) ) CREATE TABLE t_value ( term character varying(2) NOT NULL, company_id integer NOT NULL, market_id smallint NOT NULL, category_id smallint NOT NULL, item_id smallint NOT NULL, "value" double precision NOT NULL, datetime timestamp without time zone NOT NULL, CONSTRAINT t_value_pkey PRIMARY KEY (term, company_id, market_id, datetime, category_id, item_id), CONSTRAINT t_value_company_id FOREIGN KEY (company_id), CONSTRAINT t_value_item_id FOREIGN KEY (category_id, item_id), CONSTRAINT t_value_market_id FOREIGN KEY (market_id) )
CREATE VIEW v_stockprice AS select a.term, a.company_id, a.market_id, a.datetime, a.value volume, b.value open_price, c.value high_price, d.value low_price, e.value close_price from t_value a, t_value b, t_value c, t_value d, t_value e where a.term=b.term and a.term=c.term and a.term=d.term and a.term=e.term and a.term=f.term and a.company_id=b.company_id and a.company_id=c.company_id and a.company_id=d.company_id and a.company_id=e.company_id and a.company_id=f.company_id and a.market_id=b.market_id and a.market_id=c.market_id and a.market_id=d.market_id and a.market_id=e.market_id and a.market_id=f.market_id and a.datetime=b.datetime and a.datetime=c.datetime and a.datetime=d.datetime and a.datetime=e.datetime and a.datetime=f.datetime and a.category_id=(select category_id from t_category where category_name='stockprice') and b.category_id=(select category_id from t_category where category_name='stockprice') and c.category_id=(select category_id from t_category where category_name='stockprice') and d.category_id=(select category_id from t_category where category_name='stockprice') and e.category_id=(select category_id from t_category where category_name='stockprice') and a.item_id=(select item_id from t_item where item_name='volume' and category_id=(select category_id from t_category where category_name='stockprice')) and b.item_id=(select item_id from t_item where item_name='open_price' and category_id=(select category_id from t_category where category_name='stockprice')) and c.item_id=(select item_id from t_item where item_name='high_price' and category_id=(select category_id from t_category where category_name='stockprice')) and d.item_id=(select item_id from t_item where item_name='low_price' and category_id=(select category_id from t_category where category_name='stockprice')) and e.item_id=(select item_id from t_item where item_name='close_price' and category_id=(select category_id from t_category where category_name='stockprice')) ;
スポンサーリンク