Skip to content

Commit

Permalink
0519
Browse files Browse the repository at this point in the history
  • Loading branch information
ShichenXie committed May 19, 2024
1 parent 7e1333c commit 040aa59
Show file tree
Hide file tree
Showing 11 changed files with 148 additions and 130 deletions.
Binary file modified .DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ License: GPL-3
URL: https://github.com/ShichenXie/pedquant, https://pedquant.com/
BugReports: https://github.com/ShichenXie/pedquant/issues
LazyData: true
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
Roxygen: list(markdown = TRUE, old_usage = TRUE)
Encoding: UTF-8
Binary file removed R/.DS_Store
Binary file not shown.
27 changes: 20 additions & 7 deletions R/md_future.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ future_symbols_sybnam = function(symbols) {
md_future1_info_sina = function(symbol, ...) {
. = name = NULL

dat = read_html(sprintf('https://finance.sina.com.cn/futures/quotes/%s.shtml', symbol), encoding = 'GBK')
dat = rvest::read_html(sprintf('https://finance.sina.com.cn/futures/quotes/%s.shtml', symbol), encoding = 'GBK')
dt = html_table(dat)[[7]]
setnames(setDT(dt), rep(c('variable','value'), 3))

Expand All @@ -67,17 +67,30 @@ md_future1_history_sina = function(symbol, name, freq, from, to, handle, ...) {

# url
# http://stock2.finance.sina.com.cn/futures/api/json.php/IndexService.getInnerFuturesDailyKLine?symbol=M0
url0 = 'https://stock2.finance.sina.com.cn/futures/api/jsonp.php/var=/InnerFuturesNewService.get%s?symbol=%s'
if (!(sybnam$exchange %in% c('DCE', 'ZCE', 'CFFEX', 'SHFE', 'GFEX'))) url0 = 'https://stock2.finance.sina.com.cn/futures/api/jsonp.php/var=/GlobalFuturesService.getGlobalFutures%s?symbol=%s'
if (sybnam$exchange %in% c('DCE', 'ZCE', 'SHFE', 'GFEX', 'CFFEX')) {
url0 = 'https://stock2.finance.sina.com.cn/futures/api/jsonp.php/var=/InnerFuturesNewService.get%s?symbol=%s'
} else if (FALSE) {
url0 = 'http://stock2.finance.sina.com.cn/futures/api/json.php/CffexFuturesService.getCffexFutures%s?symbol=%s'
} else {
url0 = 'https://stock2.finance.sina.com.cn/futures/api/jsonp.php/var=/GlobalFuturesService.getGlobalFutures%s?symbol=%s'
}

urli = sprintf(url0, freq, syb)
dat = try(read_lines(urli), silent = TRUE)

# data
cols_names = c("date", "open", "high", "low", "close", "volume", "position", "settle")
dt = setDT(fromJSON(sub('.+?(\\[.+\\]).+', '\\1', dat[2])))
dt = setnames(
dt, cols_names
)[, (cols_names[-1]) := lapply(.SD, as.numeric), .SDcols = cols_names[-1]]
if (FALSE) {
dt = setDT(as.data.frame(fromJSON(dat)))
} else {
dt = setDT(fromJSON(sub('.+?(\\[.+\\]).+', '\\1', dat[2])))

dt = setnames(
dt, cols_names
)[, (cols_names[-1]) := lapply(.SD, as.numeric), .SDcols = cols_names[-1]]
}



if (freq == 'DailyKLine') {
dt = dt[, date := as.Date(date)]
Expand Down
2 changes: 1 addition & 1 deletion R/pq_addti.R
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ pq_addti = function(dt, ...) {
arg_addti = function(addti, x) {
if (is.null(addti) || is.null(x)) return(addti)
lapply(addti, function(a) {
a$x = x
if (inherits(a, 'list')) a$x = x
return(a)
})
}
Expand Down
93 changes: 48 additions & 45 deletions R/pq_plot2.R
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
pp_yrng = function(dt, y, yb, yrng=NULL, ...) {
if (!is.null(yrng)) return(yrng)
yval = dt[[y]]
if (!is.null(yb)) yval = c(yval, dt[[yb]])

range(yval, na.rm = TRUE)
}

pp_dtpre = function(dt, x='date', y='close',
addti = NULL, markline = TRUE,
orders = NULL, order_y = 'prices', order_type = 'side_bs', order_term = 'side_term') {
sybnam = symbol = name = markline_value = NULL
addti = NULL,
orders = NULL, order_y = 'prices', order_type = 'side_bs', order_term = 'side_term', ...) {
symbol = name = markline_value = NULL

dt = setorderv(copy(dt), c('symbol', x))

if ('name' %in% names(dt)) dt = dt[, name := NULL]

dt = setorderv(copy(dt), c('symbol', x))[, sybnam := sprintf('%s %s', symbol, name[.N]), by = 'symbol']
if (!is.null(addti)) {
dt = suppressWarnings(do.call('pq_addti', args = c(list(dt=dt), addti)))
dt = rbindlist(dt)
Expand All @@ -21,7 +17,7 @@ pp_dtpre = function(dt, x='date', y='close',

if (length(rmcols) > 0) dt = dt[, (rmcols) := NULL]
}
if (isTRUE(markline)) dt = dt[, markline_value := get(y)[.N], by = 'symbol']
# if (isTRUE(markline)) dt = dt[, markline_value := get(y)[.N], by = 'symbol']


if (inherits(orders, 'data.frame') && nrow(orders) > 0) {
Expand All @@ -47,6 +43,7 @@ pp_title = function(dt, title=NULL, sybnam_N=TRUE, ...) {
if (sybnam_N == TRUE) {
title = setDT(dt)[.N, cols_sn, with=FALSE]
} else title = setDT(dt)[1, cols_sn, with=FALSE]

title = paste0(unlist(title), collapse = ' ')
}
return(title)
Expand All @@ -65,7 +62,7 @@ pp_xstart = function(dt, x = 'date', date_range = 'max') {
}


p_theme = function(e, xstart = 0, xend = 100, yaxis_log = FALSE, #yrng = NULL,
p_theme = function(e, xstart = 0, xend = 100, yaxis_log = FALSE,
title = 'none', theme = 'default') {
if (isTRUE(yaxis_log)) yaxis_type = 'log' else yaxis_type = 'value'

Expand All @@ -74,7 +71,7 @@ p_theme = function(e, xstart = 0, xend = 100, yaxis_log = FALSE, #yrng = NULL,
e_tooltip(trigger='axis', axisPointer = list(type = 'cross', show = TRUE)) |>
e_datazoom(x_index = 0, start = xstart, end = xend) |>
e_y_axis(
min='dataMin', # yrng[1], # max=yrng[2],
min='dataMin',
type = yaxis_type, position = 'right', axisLabel = list(rotate = 90) ) |>
e_toolbox(right='30') |>
e_toolbox_feature(c("restore", "dataZoom", "saveAsImage")) |>
Expand All @@ -83,16 +80,11 @@ p_theme = function(e, xstart = 0, xend = 100, yaxis_log = FALSE, #yrng = NULL,
e_theme(theme)

}
p_markline = function(e, dt, markline = TRUE) {
p_markline = function(e, markline_yvals, markline = TRUE) {
markline_value = NULL

if (isFALSE(markline)) return(e)

# e = e |>
# e_line_('markline_value', legend=FALSE, symbol='none',
# lineStyle = list(type = 'dashed', width = 1, color='grey'))

markline_yvals = dt[, unique(markline_value)]
for (yv in markline_yvals) {
e = e |>
e_mark_line(data = list(yAxis = yv), symbol = 'none',
Expand Down Expand Up @@ -189,7 +181,7 @@ fun_filter_overlays = function(addti) {
return(ti_overlays)
}

p_addti_overlay = function(e, dt, addti = NULL) {
p_addti_overlay = function(e, dtcols, addti = NULL) {
if (is.null(addti)) return(e)
# overlay: mm, sma, ema, smma, bb, sar
# overlay technical indicators
Expand All @@ -204,7 +196,7 @@ p_addti_overlay = function(e, dt, addti = NULL) {
serie_symbol = 'circle'
}

ti_cols = names(dt)[grep(sprintf('^%s', ti), names(dt))]
ti_cols = dtcols[grep(sprintf('^%s', ti), dtcols)]
for (ticol in ti_cols) {
if (ticol %in% c('bbands_dn', 'bbands_up', 'pbands_dn', 'pbands_up')) next
# tiname = sprintf('%s(%s)', gsub('[0-9._]+', '', ticol), gsub('_', ',', gsub('[a-z]+_', '', ticol)))
Expand Down Expand Up @@ -282,14 +274,13 @@ p_addti_indicator = function(e, dt, addti = NULL, x = 'date', theme = 'default')
}

pp_base = function(dt, x = 'date', h='100%', yb=NULL) {
symbol = name = NULL
symbol = name = syb_nam = NULL

if (dt[,length(unique(symbol))] > 1) {
e = dt |>
group_by(sybnam) |>
e = dt[, syb_nam := paste(symbol, name)] |>
group_by(syb_nam) |>
e_charts_(x)
} else {
sybnam = dt[.N, paste(symbol, name)]
e = dt |>
e_charts_(x)
}
Expand All @@ -303,19 +294,23 @@ pp_line = function(
markline = TRUE, nsd_lm = NULL, addti = NULL,
orders = NULL, order_y = 'prices', order_type = 'side_bs', ...
) {
dt = pp_dtpre(dt, x, y, addti, markline, orders, order_y, order_type) |>
pp_dtlm(x, y, yaxis_log, nsd_lm)
title = pp_title(dt, title, ...)

dt = dt[, intersect(names(dt), c('symbol', x, y, yb)), with=FALSE]
dt = pp_dtpre(dt, x, y, addti, orders, order_y, order_type) |>
pp_dtlm(x, y, yaxis_log, nsd_lm)

xstart = pp_xstart(dt, x, date_range)
# yrng = pp_yrng(dt=dt, y=y, yb=yb, ...)
markline_yvals = dt[, .SD[.N], by='symbol'][, unique(get(y))]
dtcols = names(dt)

e = pp_base(dt, x, yb=yb) |>
e_line_(serie = y, legend=TRUE, symbol='none') |>
p_orders(orders, color_up, color_down, ...) |>
p_markline(dt = dt, markline = markline) |>
p_markline(markline_yvals = markline_yvals, markline = markline) |>
p_lm(x=x, y=y, nsd_lm=nsd_lm) |>
p_addti_overlay(dt = dt, addti = addti) |>
p_theme(xstart = xstart, xend = 100, yaxis_log = yaxis_log, #yrng = yrng,
p_addti_overlay(dtcols = dtcols, addti = addti) |>
p_theme(xstart = xstart, xend = 100, yaxis_log = yaxis_log,
title = title, theme = theme) |>
p_addti_indicator(dt = dt, addti = addti, x = x, theme = theme)

Expand All @@ -328,19 +323,23 @@ pp_step = function(
markline = TRUE, nsd_lm = NULL, addti = NULL,
orders = NULL, order_y = 'prices', order_type = 'side_bs', ...
) {
dt = pp_dtpre(dt, x, y, addti, markline, orders, order_y, order_type) |>
pp_dtlm(x, y, yaxis_log, nsd_lm)
title = pp_title(dt, title, ...)

dt = dt[, intersect(names(dt), c('symbol', x, y, yb)), with=FALSE]
dt = pp_dtpre(dt, x, y, addti, orders, order_y, order_type) |>
pp_dtlm(x, y, yaxis_log, nsd_lm)

xstart = pp_xstart(dt, x, date_range)
# yrng = pp_yrng(dt=dt, y=y, yb=yb, ...)

markline_yvals = dt[, .SD[.N], by='symbol'][, unique(get(y))]
dtcols = names(dt)

e = pp_base(dt, x, yb=yb) |>
e_step_(serie = y, symbol='none') |>
p_orders(orders, color_up, color_down, ...) |>
p_markline(dt = dt, markline = markline) |>
p_markline(markline_yvals = markline_yvals, markline = markline) |>
p_lm(x=x, y=y, nsd_lm=nsd_lm) |>
p_addti_overlay(dt = dt, addti = addti) |>
p_theme(xstart = xstart, xend = 100, yaxis_log = yaxis_log, # yrng = yrng,
p_addti_overlay(dtcols = dtcols, addti = addti) |>
p_theme(xstart = xstart, xend = 100, yaxis_log = yaxis_log,
title = title, theme = theme) |>
p_addti_indicator(dt = dt, addti = addti, x = x, theme = theme)

Expand All @@ -353,22 +352,26 @@ pp_candle = function(
markline = TRUE, nsd_lm = NULL, addti = NULL,
orders = NULL, order_y = 'prices', order_type = 'side_bs', ...
) {
dt = pp_dtpre(dt, x, y, addti, markline, orders, order_y, order_type) |>
pp_dtlm(x, y, yaxis_log, nsd_lm)
title = pp_title(dt, title, ...)

dt = dt[, intersect(names(dt), c('symbol', x, y, yb, 'close', 'open', 'low', 'high')), with=FALSE]
dt = pp_dtpre(dt, x, y, addti, orders, order_y, order_type) |>
pp_dtlm(x, y, yaxis_log, nsd_lm)

xstart = pp_xstart(dt, x, date_range)
# yrng = pp_yrng(dt=dt, y=y, yb=yb, ...)
markline_yvals = dt[, .SD[.N], by='symbol'][, unique(get(y))]
dtcols = names(dt)

dt = copy(dt)[, date := as.factor(date)]
e = pp_base(dt, x, yb=yb) |>
e_candle_('close', 'open', 'low', 'high', name = title,
itemStyle = list(color = color_up, borderColor = color_up,
color0 = color_down, borderColor0 = color_down)) |>
p_orders(orders, color_up, color_down, ...) |>
p_markline(dt = dt, markline = markline) |>
p_markline(markline_yvals = markline_yvals, markline = markline) |>
p_lm(x=x, y=y, nsd_lm=nsd_lm) |>
p_addti_overlay(dt = dt, addti = addti) |>
p_theme(xstart = xstart, xend = 100, yaxis_log = yaxis_log, # yrng = yrng,
p_addti_overlay(dtcols = dtcols, addti = addti) |>
p_theme(xstart = xstart, xend = 100, yaxis_log = yaxis_log,
title = title, theme = theme) |>
p_addti_indicator(dt = dt, addti = addti, x = x, theme = theme)

Expand Down
Binary file modified R/sysdata.rda
Binary file not shown.
Loading

0 comments on commit 040aa59

Please sign in to comment.