Clojure China

用clojure 来提取沪深股票的数据

#1

想写个股票的量化交易程序。想了下,太遥远了。
先从最简单的地方开始,提取沪深股市的数据。
我用的是阿里云的股票数据。(非广告 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"}]

未完待续~~

#2

clj-http 请求时可以指定返回值的类型:

(let [ret (client/get "http://httpbin.org/user-agent" {:as :json})
      body (:body ret)]
  (println (type body) body))

clojure.lang.PersistentArrayMap {:user-agent Apache-HttpClient/4.5.2 (Java/1.8.0_112)}

为了让{:as :json}生效,需要在project.clj里面加上下面的依赖

[cheshire "5.6.1"]
#3

感谢jiacai2050的帮助,我修改了下代码如下
(let [ret (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 MYappCode”}
:as :json})
body (:list (:showapi_res_body (:body ret)))]
(println (type body) body)
;(println body)
)
结果如下
clojure.lang.PersistentVector [{: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}]

#4

json这边可以用Cheshire,或者edn其实也不错,不过我有个问题,提取了你要准备干吗?

#5

准备大数据分析一下

#6

量化交易?JoinQuant了解一下?(非广告,前几天自己搜到的)