wind的R接口、況客RSDK學習,雙冒號以及quantmod主題


本文主要參考:來自segmentfault上的FinanceR的專欄
並自己對生疏不解之處做了補充和注釋。
當然,況客的R SDK的強大之處在於,你注冊賬號,進入網站的控制台后,會直接在瀏覽器模擬出一個RStudio的頁面,可以直接運行操作,還可以設置。
而對於的qutke包,托管於github,所謂的API,應當就是指這個包的用法。
每個人會分配一個私鑰。
個人感覺,現在他僅僅是提供了一個瀏覽器平台,以及數據獲取方式的qutke。
恩,大家都吐槽quantmod的不的數據不准確(尤其雅虎財經現在不上心)

恩,那么我們嘗試下wind吧,wind的話,他的R插件其實也蠻奇怪的,首先,是不能在CRAN上找到的。
其次,一般的方法是登錄windPC端(需要賬號)。文件——修復插件——修復R插件——確定,然后就是命令行界面一陣滾屏,按任意鍵推出即可。
(這樣的安裝方式,應該是更利於其控制接入吧,他就可以收費啦)
輸入以下代碼:
   
   
   
           
  1. library(WindR)
  2. w.start()
即得到下圖。

按鈕和excel插件的按鈕作用相似,而且,點擊之后出來的選擇參數、時間等各種的界面和excel插件的作用完全一致。
點擊確定后,將出現一行文本

恩,我們只需要復制這行文本,作為代碼運行他,即可將滿足我們要求的數據提取到對應的w_wsi_data對象中了,恩,使用class/mode函數,我們可以指導其是一個list。
我們當然可以不借助GUI的幫助,如果我們熟悉其函數的話,我們也可以根據自己的喜歡,改變各種參數和變量名。


況客
第一步安裝包
   
   
   
           
  1. library(quantmod)
  2. library(plyr)
  3. library(TTR)
  4. library(scales)
  5. library(ggplot2)
  6. library(DT)
  7. library(qutke)

第二步,設置key
   
   
   
           
  1. key<-'faca4c8ff4dc1502d87944ea9cfd74f19918f0aef37fde6746b61d4d339bfcf3'
  2. init(key)
  3. # [1] "authorize success!"
authorize 授權。即初始化key之后,會返回一個“授權成功”
這個key應該是官方公用的?每個人自己也有一個私鑰的

第三步,設置日期
   
   
   
           
  1. sDate<- as.Date("2016-01-03") #開始日期
  2. eDate<- Sys.Date() # 截止今日日期
  3. tradingDay<-getDate(data="tradingDay",
  4.                     startdate=sDate,
  5.                     enddate=eDate,key=key)
  6. tradingDay
getDate中,我們指定了data的值為 tradingDay ,同時給定了起止日期,並告示key
此時的tradingDay中,全是交易日日期數據(非交易日不會被包含在里面哦)

第四步:獲取行情數據
   
   
   
           
  1. qtid<-c('000001.sz') #設置股票池
  2. dailyQuote<-getDailyQuote(data="mktFwdDaily",qtid=qtid,
  3.                           startdate=sDate,enddate=eDate,key=key)
  4. # 000001.sz
  5. # 查看列名
  6. colnames(dailyQuote)
  7. colnames(dailyQuote)


第五步ggplot2 簡單作圖
   
   
   
           
  1. DT::datatable(dailyQuote)
只見RStudio右下方的地方出現如下

(恩,看來是可視化的數據展示啊,而且還有好多頁哦,上面的10還有其他選項25/50,即每頁顯示多少行數據)
關於雙冒號,什么鬼?
於是使用
   
   
   
           
  1. ??"::"
Description 
Accessing exported and internal variables, i.e. R objects (including lazy loaded data sets) in a namespace.
即在名稱空間中方法R的對象/變量/數據集的
Usage
pkg::name
pkg:::name
Arguments
pkg   
package name: symbol or literal character string.(包名:字符串字面值?)
name   
variable name: symbol or literal character string.(變量名)
Details
For a package pkg, pkg::name returns the value of the exported variable name in namespace pkg, whereas pkg:::name returns the value of the internal variable name. The package namespace will be loaded if it was not loaded before the call, but the package will not be attached to the search path.
Specifying a variable or package that does not exist is an error.
Note that pkg::name does not access the objects in the environment package:pkg (which does not exist until the package's namespace is attached): the latter may contain objects not exported from the namespace. It can access datasets made available by lazy-loading.
Note
It is typically a design mistake to use ::: in your code since the corresponding object has probably been kept internal for a good reason. Consider contacting the package maintainer if you feel the need to access the object for anything but mere inspection.
其實在我看來,怎么更像是在調用一個包中的函數或變量(哈哈靜態變量么?)或數據集呢?
那么和正常的調用的區別在哪里呢?
請看我在知乎這篇問答中的評論
里面提到了這本書 http://r-pkgs.had.co.nz/

   
   
   
           
  1. qplot(dailyQuote$date,dailyQuote$close)

第六步格式轉換
   
   
   
           
  1. dailyQuote$date<-as.POSIXct(as.character(dailyQuote$date),
  2.                             tz="",format="%Y-%m-%d")
POSIXct應是一種日期格式
Functions to manipulate objects of classes "POSIXlt" and "POSIXct" representing calendar dates and times.
tz=""表示the current time zone
format是character string giving a date-time format as used by strptime.
此時dailyQuote$date中的每個元素的形式從"2016-01-04"變為"2016-01-04 CST"
其中CST可視為美國,澳大利亞或中國的標准時間(中國標准時間:China Standard Time UT+8:00)。
temp = read.zoo(dailyQuote[,-1][,1:2]) 
dailyQuote的第一列是qtid(證券代碼)
去除第一列后取此時的第一列和第二列,即為date和close
恩,不如優化為temp = read.zoo(dailyQuote[,2:3])

神奇吧?read.zoo的第一個參數是file,除了可以是文件名外, file can be a connection or a data.frame (e.g., resulting from a previous read.table call) that is subsequently processed to a "zoo" series.
自己寫個例子測試下就知道了:
   
   
   
           
  1. > testD<-data.frame(x=1:5,y=2:6)
  2. > read.zoo(testD)
  3. 1 2 3 4 5 
  4. 2 3 4 5 6 
轉化為xts格式
     
     
     
             
  1. > payh =as.xts(temp[,1])
  2. > head(payh)
  3.             [,1]
  4. 2016-01-04 11.33
  5. 2016-01-05 11.40
  6. 2016-01-06 11.53
  7. 2016-01-07 10.94
  8. 2016-01-08 11.12
  9. 2016-01-11 10.76
  10. > colnames(payh)="Close"
  11. > head(payh)
  12.            Close
  13. 2016-01-04 11.33
  14. 2016-01-05 11.40
  15. 2016-01-06 11.53
  16. 2016-01-07 10.94
  17. 2016-01-08 11.12
  18. 2016-01-11 10.76

第七步quantmod制圖
    
    
    
            
  1. chartSeries(payh,name="000001.SZ HarryZhu CopyRight") #走勢圖

    
    
    
            
  1. addMACD()   #MACD曲線,不用自己計算


   
   
   
           
  1. addBBands() #布林帶


quantmod包的主題
換種風格咯?
   
   
   
           
  1. chartSeries(payh,name=qtid,theme="white") 
  2. addMACD()   #MACD曲線,不用自己計算
  3. addBBands() #布林帶


theme="white.mono"


theme="black.mono"


theme="beige"


theme="wsj"


恩 quantmod到底有多少個主題呢?
請看以下鏈接:
On Thu, Apr 28, 2011 at 2:22 AM, Costas Vorlow <costas.vorlow at gmail.com> wrote:
I am having trouble with selecting themes in quantmod
How many themes (predefined) exist?
Use the source! ;-)
    
    
    
            
  1. > chartTheme
  2. function (theme = "black", ...)
  3. {
  4.     ctheme <- get(".chart.theme", as.environment("package:quantmod"))
  5.     attr(ctheme, ".Environment") <- NULL
  6.     current.theme <- ctheme[[theme]]
  7.     ll <- list(...)
  8.     for (i in names(ll)) {
  9.         current.theme[[i]] <- ll[[i]]
  10.     }
  11.     return(structure(current.theme, class = "chart.theme"))
  12. }
  13. <environment: namespace:quantmod>

  14. > names(quantmod:::.chart.theme)

  15. [1] "white"      "white.mono" "black"      "black.mono" "beige"
  16. [6] "wsj"
三冒號我倒是見識了,三冒號后面還加個點,恩,好吧.
當然,我們可以自動以theme的,使用chartTheme函數即可,不過既然不是專業的設計人員,我們覺得配圖配色上,自己搞起來肯定四不像,就沒動手。

附上源碼






附件列表

     


    免責聲明!

    本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



     
    粵ICP備18138465號   © 2018-2026 CODEPRJ.COM