[原]量化投資教程:用R語言打造量化分析Web平臺

概述

和Python計算環境中的tushare包同樣,在R中咱們使用quantmod包接入第三方數據源,實現自定義量化分析平臺的構建。html

本文打算以陌陌的股票分析爲背景,介紹如何經過quantmod包構建專屬的量化分析平臺。react

什麼是quantmod

quantmod就是提供給寬客們使用的專業模塊,Quantmod自己提供強大的數據接入能力,默認是雅虎財經的數據源,此外quantmod還以繪製專業的行情分析圖表以及各類技術指標計算等功能著稱,經常只要幾行函數就能完成從數據獲取和處理到畫圖的複雜功能,其工做效率之高讓行家裏手都以爲膛目結舌。git

利用API讀取在線行情

首先,咱們利用雅虎財經的默認接口直接體驗一下讀取多隻股票。github

原理

利用API讀取的方式,咱們須要設定一個讀取序列和對應的配置,獲取行情函數getSymbols相似於原生的assignget函數,用函數的方式將變量名傳入後完成變量的賦值。web

基於這個原理,我寫了一個Quote函數來優化參數配置的體驗。首先咱們須要定義一個股票池序列,而後調用Quote函數獲取某隻股票的行情返回數據。ajax

下面以美股的陌陌、360和A股的平安銀行爲例:express

代碼

# 加載quantmod包
if(!require(quantmod)){
    install.packages("quantmod")
}
# 股票行情匹配函數
Quote = function(code){
  index = match(code,universes)
  temp = lapply(universes,get)
  return(temp[[index]])
}
# 基本配置
universes <<- c("000001.SZ","QIHU","MOMO")
from = "2015-01-04"
to = Sys.Date() # 結束時間設爲當前日期
src= "yahoo" # 來源雅虎財經

# 行情加載 速度有點慢,耐心等待
quantmod::getSymbols(universes,from=from,to=to,src=src)

# 繪製行情
quantmod::chartSeries(Quote("MOMO"),up.col='red',dn.col='green',TA="addVo(); addADX();addMACD(); addSMA(n=10);addBBands(n=14,sd=2,draw=\"bands\")")

效果

momo

利用CSV讀取離線行情

接着,在離線模式或者網絡訪問緩慢的狀況下,咱們也能夠用一些實現準備好的CSV文件來讀取行情。segmentfault

原理

分析底層數據結構後,咱們知道quantmod包讀取後的數據格式是 xts 和 zoo,咱們只須要將csv文件按必定的格式讀取到內存後再進行相應變換,quantmod強大的分析和做圖能力就能夠爲咱們所用。微信

zoo自己是一種時間序列格式,而xts則是在這基礎上一種時間序列格式的增強版。在讀取csv的時候,咱們須要用首行肯定header。在轉化爲zoo時,咱們則須要首列來肯定時間序列對應的時間。最後經過xts轉化爲能夠被quantmod識別的xts時間序列對象。下面以平安銀行爲例:websocket

代碼

# 加載 zoo 時間序列包
library(zoo)
library(quantmod)
# 配置文件路徑
filePath = '/Users/harryzhu/temp.csv'
# 讀取CSV並轉化時間格式
csv <- read.csv(filePath,header=TRUE,sep=",")
csv$LZ_GPA_QUOTE_TCLOSE <- as.POSIXct(as.character(csv$LZ_GPA_QUOTE_TCLOSE),tz="",format="%Y%m%d")
# 轉化爲zoo類型
temp = read.zoo(csv) 
# 轉化我xts類型
payh =as.xts(temp[,1]);colnames(payh)="Close"
# 製圖
chartSeries(payh,name="000001.SZ") 
# 添加MACD曲線
addMACD()

效果

payh

指標計算

參考官方文檔,咱們知道,利用quantmodTTR包,咱們能夠快速計算常見指標,下面是對應的計算列表。

指標名 TTR 函數名 quantmod 函數名
威爾斯懷爾德移動方向指標 ADX addADX
真實波幅 ATR addATR
布林通道 BBands addBBands
布林帶寬 N/A addBBands
百分比布林帶 N/A addBBands
順勢指標 CCI addCCI
資金流動 CMF addCMF
錢德動量指標 CMO addCMO
雙指數移動平均線 DEMA addDEMA
離勢價格偏離指數 DPO addDPO
指數平滑移動平均線 EMA addEMA
價格信封 N/A addEnvelope
指數量權移動平均線 EVWMA addEVWMA
期權期貨到期 N/A addExpiry
異同平均線 MACD addMACD
動量 momentum addMomentum
變更率 ROC addROC
相對強弱指數 RSI addRSI
轉折點信號 SAR addSAR
簡單移動平均線 SMA addSMA
隨機動量指數 SMI addSMI
三重平滑振盪指數 TRIX addTRIX
成交量 N/A addVo
加權移動平均法 WMA addWMA
零延遲指數移動平均線 ZLEMA addZLEMA

Web應用構建

參考 Rich Harken大神 製做的 shiny app以及源碼,咱們能夠將上述參數暴露爲網頁上的選項,利用shiny的ajax和websocket的實時連接特性定製咱們的chartSeries函數,達到經過選項實時做圖的功能。

效果圖

shiny

ui.R

library(shiny)

# 爲應用程序定義UI,演示R包quantmod做圖功能
shinyUI(
  navbarPage("R quantmod Demonstration for Data Products Class", inverse=FALSE,
             
   tabPanel("Documentation",
            fluidPage(
              verticalLayout(
                h2("How to use the R quantmod Demonstration App", align="center"),
                hr(),
                h3("Directions"),
                p("To get started using this application, you'll need to look at the banner line above titled \"R quantmod Demonstration for Data Products Class\" and click \"Demo\".  This will take you to the application itself.  You can click \"Documentation\" to return to this screen."),
                p("The application begins by displaying the stock symbol \"^DJI\" which represents the Dow Jones Industrial Average.  You can enter whatever stock symbol you would like (without the caret \"^\") and the graph will update accordingly.  If your stock symbol is not valid - no graph will display"),
                p("There are 3 other areas you can play with to change the current display for the selected stock:"),
                tags$ol(tags$li("Date Range - you can change the date range selected for the price display"),
                        tags$li("Chart Theme - you can change how the graph is diplayed by choosing a supported theme"),
                        tags$li("Add Optional Technical Analysis Overlays - you can add additional chart items that will calculate some industry analytics and overlay them on the existing chart.  Also a small warning here - some analytics require at least 3 months of data.  If you don't have at least 3 months of data selected in the date range - you may get an error in display of the graph.")),

                p("The graph will immediately respond to any changes you make.  Feel free to experiment and explore this application based on R, Shiny, and the quantmod package for R!"),
                br(),
                h3("About the App"),
                p("This application was written for the project in the Coursera course \"Developing Data Products\". It is an example of how to use the quantmod package within R, and what some of its capabilities are."),
                p("The app starts by providing a user interface that collects a stock symbol, date range, and optional analytics to perform on the provided stock symbol.  When anything changes in the user interface, the application reacts to the changes by updating the graphs calculated by the server code."),
                tags$ol(tags$li("Input"), tags$ul(tags$li("Stock Symbol - textInput()"), tags$li("Date Range - dateRangeInput()"), tags$li("Chart Theme - radioButtions()"), tags$li("Technical Analysis - checkBoxGroupInput()")),
                        tags$li("Operation/Calculation", tags$ul(tags$li("Call to getSymbols() to retrieve data"), tags$li("preparation of variables for graphic display"))), 
                        tags$li("Reactive Output"), tags$ul(tags$li("Call to render graph calculations within the quantmod libraries"), tags$li("Display of the rendered graph"))),
                hr(),
                h3("Application Source Code"),
                h4("ui.R"),
                pre(includeText("ui.R")),
                h4("server.R"),
                pre(includeText("server.R")),
                p("Note: Some code used in this demo is based on the Shiny quantmod tutorial but was not copied wholesale.  The work demonstrated above represents a significant departure from the tutorial.")
              )
            )
   ),
    tabPanel("Demo",
  
      fluidPage(
      
      # 應用程序標題
      #titlePanel("R quantmod Demonstration"),
      
      # 爲 quantmod 繪圖功能提供參數入口的輸入欄
      sidebarLayout(
        sidebarPanel(
          textInput("stock", "Enter a stock symbol", value = "^DJI", width = "30%"),
    #      submitButton(text="Get Stock Quote"),
          br(),
          dateRangeInput("dtRange", "Date Range", start = Sys.Date()-90, end = NULL, min = NULL, max = NULL, format = "yyyy-mm-dd", startview = "month", weekstart = 0, language = "en", separator = " to ", width = NULL),
          hr(),
          flowLayout(radioButtons("theme", "Chart Theme:",
                       c("White" = "white",
                         "White Mono" = "white.mono",
                         "Black" = "black",
                         "Black Mono" = "black.mono",
                         "Beige" = "beige",
                         "WSJ"= "wsj"
                         )),
                     checkboxGroupInput("ta", "Add Optional Technical Analysis Overlays:",
                                        c("Directional Movement Index" = "addADX()",
                                          "Average True Range" = "addATR()",
                                          "Bollenger Bands" = "addBBands()",
                                          "Commodity Channel Index" = "addCCI()",
                                          "Chaiken Money Flow" = "addCMF()",
                                          "Chande Momentum Oscillator" = "addCMO()",
                                          "Contract Expiration Bars" = "addExpiry()",
                                          "De-trended Price Oscillator" = "addDPO()",
                                          "Simple Moving Average" = "addSMA()",
                                          "Expotential Moving Average" = "addEMA()",
                                          "Weighted Moving Average" = "addWMA()",
                                          "Double Expotential Moving Average" = "addDEMA()",
                                          "Expotential Volume Weighted Moving Average" = "addEVWMA()",
                                          "ZLEMA" = "addZLEMA()",
                                          "Moving Average Convergence Divergence" = "addMACD()",
                                          "Price Envelope" = "addEnvelope()",
                                          "Relative Strength Index" = "addRSI()",
                                          "Parabolic Stop and Reversal Indicator" = "addSAR()",
                                          "Rate of Change" = "addROC()",
                                          "Stochastic Momemtum Indicator" = "addSMI()"
                                          ))
                     
            )
          ),
          # 做圖
          mainPanel(
            plotOutput("distPlot", height="885px")
      #      ,textOutput("dispPrint")
          )
        )
      )
    )
  )
)

server.R

library(shiny)
library(quantmod)
options("getSymbols.warning4.0"=FALSE)

# Define server logic required to draw a histogram
shinyServer(function(input, output) {
  
  # Expression that generates the stock chart plot. The expression is
  # wrapped in a call to renderPlot to indicate that:
  #
  #  1) It is "reactive" and therefore should re-execute automatically
  #     when inputs change
  #  2) Its output type is a plot
  sSymbol <- reactive({
    tryCatch({
      suppressWarnings(getSymbols(input$stock, from=input$dtRange[1], to=input$dtRange[2],
                                  auto.assign = FALSE))
    }, error = function(err) {
      return(NULL)
    })
  })

  output$distPlot <- renderPlot({
    taStr<-"addVo()"
    if (!is.null(input$ta)) {
      for (ta in input$ta) {
        taStr<-paste(taStr, paste(";", ta))
      }
    }
    
    if(!is.null(sSymbol())) {
      chartSeries(sSymbol(), name=input$stock, TA=taStr, theme=chartTheme(input$theme))
    }
  })
  
  output$dispPrint <- renderPrint({
    print(sSymbol())
  })
})

尾註

綜上所述,咱們能夠發現,利用quantmod、shiny包,咱們能夠快速實現各類姿式的行情獲取以及常見的關鍵指標的計算和繪製,而且轉化爲實時的Web應用,創建一個專屬的量化分析Web平臺。

參考資料

做爲分享主義者(sharism),本人全部互聯網發佈的圖文均聽從CC版權,轉載請保留做者信息並註明做者 Harry Zhu 的 FinanceR 專欄:https://segmentfault.com/blog/harryprince,若是涉及源代碼請註明GitHub地址:https://github.com/harryprince。微信號: harryzhustudio商業使用請聯繫做者。

相關文章
相關標籤/搜索