用R来调用QA的一个实例
发布于 3 个月前 作者 hiteck88 240 次浏览 来自 代码实现

思路是用R reticulate https://rstudio.github.io/reticulate/, 在R里面调用Python,下面是R部分的代码,实现了每5秒获取最新的数据和日线图。

library(shiny)
library(tidyverse)
library(quantmod)
library(timetk)
library(reticulate)
  
#use_condaenv( "py37")
# set the path for your Python
use_python("~/environments/my_env/bin/python")
# import python QUANTAXIS and QA_R library
QA <- import("QUANTAXIS")
QAR <- import("QA_R") 
today = Sys.Date()
#Sys.setlocale(, 'Chinese')
options(shiny.usecairo = FALSE)
myTheme<-chart_theme()
myTheme$col$up.col<-'darkgreen'
myTheme$col$dn.col<-'darkred'
myTheme$col$dn.border <- 'black'
myTheme$col$up.border <- 'black'
myTheme$rylab <- FALSE
myTheme$col$grid <- "lightgrey"

code = "000830"
ui <- fluidPage(
    # Define the sidebar with one input
    sidebarPanel(
      textInput("code", "Code", code),
      actionButton(inputId = "add", label = "Add"),
      selectInput("stock", "Stock:", choices=code),
      tableOutput("table")
    ),
    # plot for stock 
    mainPanel(
      plotOutput("stock")
    )     
)

server <- function(input, output, session) {
  autoUpdate <- reactiveTimer(5000)  
  observeEvent(input$add, {
    updateSelectInput(session, "stock", choices = c(input$code))
  })
  
  observe({
    autoUpdate()
    print(paste("Update price for ", isolate(input$stock)))
    price <<-  QAR$QA_fetch_latest(isolate(input$stock))
    print(price)
  })
  
  output$table <- renderTable(
    { autoUpdate()
      price}, bordered = FALSE, striped = FALSE, align = "c"
  )
 
  output$stock <- renderPlot({
    df1 = QAR$QA_fetch_data_day_adv(input$stock, '2019-6-1',today)
    #data = df1$data
    #data$date = df1$datetime$values
    df = tk_xts(df1)
    #colnames(df) <- c('Open', 'High', 'Low', 'Close', 'Volume', 'Amount')
    chart_Series(df,subset='2019-6::2019-9',TA="add_Vo();add_BBands();add_SMA(5)", theme = myTheme, name= paste("日线", input$stock))  
  })
}

shinyApp(ui=ui, server = server)
3 回复

这里是Python 部分,QA_R.py


import QUANTAXIS as QA
from QUANTAXIS.QAUtil import (DATABASE)


def QA_fetch_data_day_adv(code, start_date, end_date):
    """
    fetch the day data, support for stock, index and etf
    :param code:
    :param start:
    :param end:
    :return:
    """


    coll_stock_day = DATABASE.stock_day
    stock_list = DATABASE.stock_list
    code_list = coll_stock_day.distinct('code')
    index_day = DATABASE.index_day
    index_list = index_day.distinct('code')

    if code in code_list:
        df = QA.QA_fetch_stock_day_adv([code], start_date, end_date)

    if code in index_list:
        df = QA.QA_fetch_index_day_adv([code], start_date, end_date)

    data = df.data[['open','high','low','close','volume','amount']]
    data['date'] = df.datetime

    return data

def QA_fetch_latest(code):
    """
    fetch latest price 
    :param code:
    :return:
    """
    price=QA.QAFetch.QATdx.QA_fetch_get_stock_latest(code, ip ="61.152.249.56", port=7709)
    #return price.close.values[0]
    return price[['open','close','high','low','vol']]


@hiteck88 wow 酷炫 QUANTAXIS JUPYTER 支持R语言了 你可以使用QA_Community_R镜像 直接替换compose中的选项

Docker-Compose.yaml 中 直接替换 daocloud.io/quantaxis/qacommunity_r:latest

Dockerfile https://github.com/QUANTAXIS/QUANTAXIS/tree/master/docker/qa-community-r

回到顶部