楼主: supermanangel
2145 1

[源码分享] R实例分享二:API小实例互动交流,荐股API示例,股票Chart示例 [推广有奖]

  • 0关注
  • 1粉丝

初中生

52%

还不是VIP/贵宾

-

威望
0
论坛币
27 个
通用积分
0
学术水平
2 点
热心指数
2 点
信用等级
2 点
经验
260 点
帖子
7
精华
0
在线时间
13 小时
注册时间
2014-6-12
最后登录
2014-8-30

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
实例展示
http://180.153.251.80:16889/sample-apps/queryPortfolio/
http://180.153.251.80:16889/sample-apps/U-Chart/

  1. # This is the server logic for a Shiny web application.
  2. # You can find out more about building applications with Shiny here:
  3. #
  4. # http://www.rstudio.com/shiny/
  5. #

  6. library(shiny)
  7. library(TTR)
  8. library(quantmod)
  9. library(DTSR)


  10. Getk1d <- function(symbol,beginDate="20140101", endDate=Sys.Date()) {
  11.   d <- NULL
  12.   #print(nchar(symbol))
  13.   #need know the input issueCode is DZH style
  14.   if (mode(symbol) == "character" & length(symbol) == 1 & nchar(symbol) == 12) {
  15.     beginDate <- gsub("-","",beginDate)
  16.     endDate <- gsub("-","",endDate)
  17.     field.set <- "Date,OpenPrice,HighPrice,LowPrice,ClosePrice,Volume,Amount"
  18.     rs <- dts.GetKLine("k1d", symbol, beginDate, endDate, field.set)
  19.     if (rs$ErrorCode == 0){
  20.       d <- rs$Data
  21.       d <- data.frame(as.numeric(c(d[,1])), as.numeric(c(d[,2])), as.numeric(c(d[,3])), as.numeric(c(d[,4])), as.numeric(c(d[,5])), as.numeric(c(d[,6])), as.numeric(c(d[,7])))
  22.       colnames(d) <- c("TDate", "Open", "High", "Low", "Close", "Volume", "Amount")
  23.       rownames(d) <- as.Date(as.character(d$TDate),"%Y%m%d")
  24.       d$TDate <- NULL
  25.     }
  26.   }
  27.   return(d)
  28. }

  29. shinyServer(function(input, output) {
  30.   #download historical data from web
  31.   #log <- "download CHL Data from yahoo begin..."
  32.   #getSymbols("CHL",src="yahoo",from="2014-02-01",end=Sys.Date())
  33.   #log <- "download CHL Data from yahoo end"
  34.   
  35.   bColor <- NA
  36.   cStyle <- NA
  37.   indiConnCheckedString <- NULL
  38.   
  39.   k<-reactive({
  40.     if (input$issueCode != "" & length(input$issueCode) == 1 & nchar(input$issueCode) == 12) {
  41.       Getk1d(input$issueCode,"2014-02-01",Sys.Date())
  42.     }
  43.   })
  44.   
  45.   uStyleSI_reative <- reactive({
  46.    
  47.     #radioButton
  48.     if (mode(input$colorRdBtn) == "character")
  49.       bColor <- input$colorRdBtn
  50.    
  51.     #choose style
  52.     if (input$uStyleSI != "")
  53.       cStyle <- input$uStyleSI
  54.    
  55.     #add indicator
  56.     #checkboxInput
  57.     if (input$addMACD)
  58.       indiConnCheckedString <- c(indiConnCheckedString,'addMACD()')
  59.     if (input$addADX)
  60.       indiConnCheckedString <- c(indiConnCheckedString,addADX())
  61.    
  62.     if (input$addATR)
  63.       indiConnCheckedString <- c(indiConnCheckedString,addATR())
  64.    
  65.     if (input$addBBands)
  66.       indiConnCheckedString <- c(indiConnCheckedString,addBBands())
  67.    
  68.     if (input$addCCI)
  69.       indiConnCheckedString <- c(indiConnCheckedString,addCCI())
  70.    
  71.     if (input$addCMF)
  72.       indiConnCheckedString <- c(indiConnCheckedString,addCMF())
  73.    
  74.     if (input$addCMO)
  75.       indiConnCheckedString <- c(indiConnCheckedString,addCMO())
  76.    
  77.     if (input$addDEMA)
  78.       indiConnCheckedString <- c(indiConnCheckedString,addDEMA())
  79.    
  80.     if (input$addDPO)
  81.       indiConnCheckedString <- c(indiConnCheckedString,addDPO())
  82.    
  83.     if (input$addEMA)
  84.       indiConnCheckedString <- c(indiConnCheckedString,addEMA())
  85.    
  86.     if (input$addEVWMA)
  87.       indiConnCheckedString <- c(indiConnCheckedString,addEVWMA())
  88.    
  89.     if (input$addRSI)
  90.       indiConnCheckedString <- c(indiConnCheckedString,addRSI())
  91.    
  92.     if (input$addSAR)
  93.       indiConnCheckedString <- c(indiConnCheckedString,addSAR())
  94.    
  95.     if (input$addSMA)
  96.       indiConnCheckedString <- c(indiConnCheckedString,addSMA())
  97.    
  98.     if (input$addSMI)
  99.       indiConnCheckedString <- c(indiConnCheckedString,addSMI())
  100.    
  101.     if (input$addWMA)
  102.       indiConnCheckedString <- c(indiConnCheckedString,addWMA())
  103.    
  104.     if (input$addWPR)
  105.       indiConnCheckedString <- c(indiConnCheckedString,addWPR())
  106.    
  107.     if (input$addZLEMA)
  108.       indiConnCheckedString <- c(indiConnCheckedString,addZLEMA())
  109.    
  110.    
  111.    
  112.     print(indiConnCheckedString)
  113.    
  114.     isOriginalChart <- "0"
  115.     if (is.null(indiConnCheckedString))
  116.       isOriginalChart <- "1"
  117.    
  118.     print(isOriginalChart)
  119.     if (input$issueCode == "" | length(input$issueCode) != 1 | nchar(input$issueCode) != 12)
  120.       return (NULL)

  121.     data<-k()
  122.     if (isOriginalChart == "1") #set default chart
  123.       chartSeries(name=input$issueCode,data,theme=chartTheme(bColor),type=cStyle)
  124.     else
  125.       chartSeries(name=input$issueCode,data,theme=chartTheme(bColor),type=cStyle,TA=indiConnCheckedString)
  126.       #chartSeries(name=input$issueCode,data,theme=chartTheme(bColor),type=cStyle,TA=c(addMACD(),addEMA(),addSAR()))
  127.     #dev.off()
  128.   })
  129.   
  130.   
  131.   output$uchart <- renderPlot({
  132.     uStyleSI_reative()
  133.   })
  134.   
  135. })
复制代码

  1. # This is the user-interface definition of a Shiny web application.
  2. # You can find out more about building applications with Shiny here:
  3. #
  4. # http://www.rstudio.com/shiny/
  5. #

  6. library(shiny)

  7. shinyUI(pageWithSidebar(
  8.   
  9.   # Application title
  10.   headerPanel("U CHART"),
  11.   
  12.   # Sidebar with a slider input for number of observations
  13.   sidebarPanel(
  14.     textInput("issueCode","issueCode","SH600000.stk"),
  15.    
  16.     selectInput("uStyleSI",
  17.                 "Please select chart line style",
  18.                 c("bars","line","candlesticks","matchsticks")
  19.     ),
  20.    
  21.     radioButtons("colorRdBtn",
  22.                  "U can change backcolor depends on what you like!",
  23.                  c("white"="white","black"="black")),
  24.    
  25.     helpText("U can add indicators depends on what you need!"),
  26.     checkboxInput("addMACD","MACD",TRUE),
  27.     checkboxInput("addADX","ADX",FALSE),
  28.     checkboxInput("addATR","ATR",FALSE),
  29.     checkboxInput("addBBands","BBands",FALSE),
  30.     checkboxInput("addCCI","CCI",FALSE),
  31.     checkboxInput("addCMF","CMF",FALSE),
  32.     checkboxInput("addCMO","CMO",FALSE),
  33.     checkboxInput("addDEMA","DEMA",FALSE),
  34.     checkboxInput("addDPO","DPO",FALSE),
  35.     checkboxInput("addEMA","EMA",FALSE),
  36.     checkboxInput("addEVWMA","EVWMA",FALSE),
  37.     checkboxInput("addRSI","RSI",FALSE),
  38.     checkboxInput("addSAR","SAR",FALSE),
  39.     checkboxInput("addSMA","SMA",FALSE),
  40.     checkboxInput("addSMI","SMI",FALSE),
  41.     checkboxInput("addWMA","WMA",FALSE),
  42.     checkboxInput("addWPR","WPR",FALSE),
  43.     checkboxInput("addZLEMA","ZLEMA",FALSE)
  44.   ),
  45.   
  46.   # Show a plot of the generated distribution
  47.   mainPanel(
  48.     plotOutput("uchart",height="500px")
  49.     #textOutput("log")
  50.   )
  51. ))
复制代码


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Chart Hart API ART Applications building library server about

已有 1 人评分经验 论坛币 学术水平 热心指数 信用等级 收起 理由
fantuanxiaot + 10 + 9 + 1 + 1 + 1 精彩帖子

总评分: 经验 + 10  论坛币 + 9  学术水平 + 1  热心指数 + 1  信用等级 + 1   查看全部评分

沙发
fantuanxiaot 发表于 2015-2-11 22:28:00 |只看作者 |坛友微信交流群

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jr
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-24 07:56