Skip to content

Commit

Permalink
ord
Browse files Browse the repository at this point in the history
  • Loading branch information
ShichenXie committed Apr 4, 2024
1 parent 5273eb3 commit 7e1333c
Show file tree
Hide file tree
Showing 41 changed files with 125 additions and 70 deletions.
8 changes: 5 additions & 3 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
* fixed a bug in pq_plot function to support candle chart
* the md_stock_symbol supports to query the constituent symbols of Chinese securities index
* fixed a bug in md_moneycn function to query lpr
* fixed a bug in md_stock_symbol supports to query stock symbol constituent of index
* pq_plot supports order with multiple terms

# pedquant 0.2.4

* fixed mutual bugs
* fixed several bugs

# pedquant 0.2.3

Expand All @@ -21,7 +23,7 @@
# pedquant 0.2.2

* replace stock data source from the 163 to the Eastmoney
* fixed mutual bugs
* fixed several bugs

# pedquant 0.2.1

Expand All @@ -46,7 +48,7 @@

# pedquant 0.1.7

* fixed mutual bugs in the functions such as get_fromto, md_stock_divsplit1_163, md_stock_symbol etc.
* fixed several bugs in the functions such as get_fromto, md_stock_divsplit1_163, md_stock_symbol etc.
* modified stock adjust method for 163 data
* md_stock function supports to query listed company information, such as profile, IPO, structure of income, structure of employee

Expand Down
4 changes: 3 additions & 1 deletion R/md_2chinamoney.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ md_moneycn_symbol = function() {
)
}
md_moneycn_widelong = function(dt) {
. = name = symbol = NULL
. = name = symbol = value = NULL

melt(dt, id.vars = 'date', variable.name = 'symbol', value.name = 'value')[
md_moneycn_symbol()[], on = 'symbol', nomatch = 0
Expand All @@ -83,6 +83,8 @@ md_cnshibor = function(date_range = '3y', from=NULL, to=Sys.Date(), ...) {

# loan prime rate, lpr
md_cnlpr = function(date_range = 'max', from=NULL, to=Sys.Date(), ...) {
cn1ylpr = cn5ylpr = value = symbol = NULL

## from/to
to = check_to(to)
from = check_from(date_range, from, to, default_from = "1997-01-01", default_date_range = '3y')
Expand Down
25 changes: 20 additions & 5 deletions R/pq_orders.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# current prices:
# current values
check_odr = function(orders) {
. = symbol = side = prices = values = quantity = NULL
. = symbol = side = prices = values = quantity = side_term = n = term = NULL

if (is.null(orders)) return(orders)
# symbol, date, side, prices, quantity, values
Expand All @@ -34,10 +34,25 @@ check_odr = function(orders) {
][, side := ifelse(values >= 0, 1, -1)]
}

orders = orders[
data.table(side=c(1,-1, 1.1,-1.1), side_bs = c('buy', 'sell', 'buy_short', 'sell_short')),
on='side', nomatch=0
][]
# side level
if (!('side_term' %in% names(orders))) {
if ('n' %in% names(orders)) {
orders = orders[, side_term := n]
} else if ('term' %in% names(orders)) {
orders = orders[, side_term := term]
} else {
orders = orders[, side_term := 10]
}
}

# side type
if (!('side_bs' %in% names(orders))) {
orders = orders[
data.table(side=c(1,-1, 1.1,-1.1), side_bs = c('buy', 'sell', 'buy_short', 'sell_short')),
on='side', nomatch=0
][]
}

}

return(orders)
Expand Down
41 changes: 31 additions & 10 deletions R/pq_plot2.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pp_yrng = function(dt, y, yb, yrng=NULL, ...) {

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

dt = setorderv(copy(dt), c('symbol', x))[, sybnam := sprintf('%s %s', symbol, name[.N]), by = 'symbol']
Expand All @@ -31,10 +31,11 @@ pp_dtpre = function(dt, x='date', y='close',
dt = merge(
dt,
dcast(orders,
sprintf('%s ~ %s', paste0(dtodr_cols,collapse='+'), order_type),
sprintf('%s ~ %s + %s', paste0(dtodr_cols,collapse='+'), order_type, order_term),
value.var = order_y),
by = dtodr_cols, all.x = TRUE
)

}

return(dt)
Expand Down Expand Up @@ -101,14 +102,31 @@ p_markline = function(e, dt, markline = TRUE) {
}
# orders
p_orders = function(e, orders, color_up = "#CF002F", color_down = "#000000", orders_ml=FALSE, ...) {
side_term = side_bs = NULL

if (is.null(orders)) return(e)

# long
if ('buy' %in% unique(orders$side_bs)) e = e_scatter_(e, 'buy', symbol = 'triangle', symbolSize = 12, color = color_up, legend = FALSE)
if ('sell' %in% unique(orders$side_bs)) e = e_scatter_(e, 'sell', symbol = 'triangle', symbolSize = 12, symbolRotate=180, color = color_down, legend = FALSE)
# short
if ('buy_short' %in% unique(orders$side_bs)) e = e_scatter_(e, 'buy_short', symbol = 'triangle', symbolSize = 12, color = color_up, legend = FALSE)
if ('sell_short' %in% unique(orders$side_bs)) e = e_scatter_(e, 'sell_short', symbol = 'triangle', symbolSize = 12, symbolRotate=180, color = color_down, legend = FALSE)
lvls = orders[order(-side_term)][,unique(side_term)]
sybs = rep_len(c('triangle', 'arrow', 'diamond'), length(lvls))
cols = orders[, unique(paste(side_bs, side_term, sep = '_'))]

for (i in seq_along(lvls) ) {
sybi = sybs[i]
sybsize = 12 - (i*i-1)

# long
col1 = sprintf('buy_%s', lvls[i])
if (col1 %in% cols) e = e_scatter_(e, col1, symbol = sybi, symbolSize = sybsize, color = color_up, legend = FALSE)
col2 = sprintf('sell_%s', lvls[i])
if (col2 %in% cols) e = e_scatter_(e, col2, symbol = sybi, symbolSize = sybsize, symbolRotate=180, color = color_down, legend = FALSE)

# short
col3 = sprintf('buy_short_%s', lvls[i])
if (col3 %in% cols) e = e_scatter_(e, col3, symbol = sybi, symbolSize = sybsize, color = color_up, legend = FALSE)
col4 = sprintf('sell_short_%s', lvls[i])
if (col4 %in% cols) e = e_scatter_(e, col4, symbol = sybi, symbolSize = sybsize, symbolRotate=180, color = color_down, legend = FALSE)
}


if (isFALSE(orders_ml)) return(e)
for (o in split(orders,by='side_bs')) {
Expand Down Expand Up @@ -414,8 +432,11 @@ pp_candle = function(
#'
#' # orders
#' b2 = dt_banks[symbol %in% c('601988.SH', '601398.SH')]
#' b2orders = b2[sample(.N, 10), .(symbol, date, prices=close,
#' side=sample(c(-1, 1), 10, replace=TRUE))]
#' b2orders = b2[sample(.N, 20), .(
#' symbol, date, prices=close,
#' side = sample(c(-1, 1), 20, replace=TRUE),
#' term = sample(c(10, 20), 20, replace=TRUE)
#' )]
#'
#' e5 = pq_plot(b2, orders=b2orders)
#' e5[[1]]
Expand Down
2 changes: 1 addition & 1 deletion docs/404.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/LICENSE.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions docs/authors.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/index.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 13 additions & 5 deletions docs/news/index.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ pandoc: 3.1.1
pkgdown: 2.0.7
pkgdown_sha: ~
articles: {}
last_built: 2024-01-10T12:48Z
last_built: 2024-04-04T02:09Z

2 changes: 1 addition & 1 deletion docs/reference/crossover.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/dt_banks.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/dt_ssec.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/ed_code.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/ed_fred.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/ed_fred_symbol.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 7e1333c

Please sign in to comment.