想写个股票的量化交易程序。想了下,太遥远了。
先从最简单的地方开始,提取沪深股市的数据。
我用的是阿里云的股票数据。(非广告 https://market.aliyun.com/products/57000002/cmapi010845.html?spm=5176.730005.0.0.35vS5E#sku=yuncode484500003)
数据种类有:
股指实时K线数据
股票列表查询
沪深及港股历史行情
名称编码拼音查询股票信息
股票实时行情_批量
股票实时分时线数据
股票实时K线数据
股指实时行情_批量
股指实时分时线
股指列表查询
沪深股票最新50条逐笔交易
股票实时行情
里面有很多例子,是curl,Java ,C#,PHP,Python,ObjectC的 ,根据这些例子可以改成了clojure的。
这需要用到两个库 clj-http 和 org.clojure
(require '[clj-http.client :as http])
(require '[clojure.data.json :as json])
用http/get调用阿里云的api借口就可以得到数据。以“某只股票沪深及港股历史行情”为例,下面两种写法都是可以的:
(http/get "https://ali-stock.showapi.com/sz-sh-stock-history?begin=2017-02-07&code=600887&end=2017-02-10" {:headers {"Authorization" "APPCODE 楼主的appcode"}})
或
(http/get "https://ali-stock.showapi.com/sz-sh-stock-history" {:query-params {:begin "2017-02-07" :code "600887" :end "2017-02-10"} :headers {"Authorization" "APPCODE 楼主的appcode"}})
上面是要取得某只股票在设定的开始时间和结束时间内的股票数据。
得到的是json数据,把它们处理一下,就可以得到相应的数据,例如改写成:
(:list (:showapi_res_body (json/read-str (:body (http/get "https://ali-stock.showapi.com/sz-sh-stock-history" {:query-params {:begin "2017-02-07" :code "600887" :end "2017-02-10"} :headers {"Authorization" "APPCODE 楼主的appcode"}})) :key-fn keyword)))
可以得到
[{:date "2017-02-10", :turnover "0.48", :min_price "18.10", :trade_money "520788200", :diff_rate "0.33", :diff_money "0.06", :open_price "18.12", :swing "0.829", :max_price "18.25", :code "600887", :close_price "18.16", :market "sh", :stockName "伊利股份", :trade_num "286670"} {:date "2017-02-09", :turnover "0.52", :min_price "18.04", :trade_money "563931200", :diff_rate "0.00", :diff_money "0.00", :open_price "18.13", :swing "1.05", :max_price "18.23", :code "600887", :close_price "18.10", :market "sh", :stockName "伊利股份", :trade_num "311167"} {:date "2017-02-08", :turnover "0.39", :min_price "17.91", :trade_money "422875000", :diff_rate "0.06", :diff_money "0.01", :open_price "18.12", :swing "1.216", :max_price "18.13", :code "600887", :close_price "18.10", :market "sh", :stockName "伊利股份", :trade_num "234740"} {:date "2017-02-07", :turnover "0.30", :min_price "18.07", :trade_money "322468300", :diff_rate "-0.33", :diff_money "-0.06", :open_price "18.17", :swing "0.606", :max_price "18.18", :code "600887", :close_price "18.09", :market "sh", :stockName "伊利股份", :trade_num "177991"}]
未完待续~~