Binary operators which create the upwards or downwards crossover signals.
x %x>% y
x %x<% y
numeric vectors
library(data.table)
library(pedquant)
data("dt_banks")
boc = md_stock_adjust(setDT(dt_banks)[symbol=='601988.SS'])
bocti = pq_addti(boc, x='close_adj', sma=list(n=200), sma=list(n=50))
dtorders = copy(bocti[[1]])[,.(symbol, name, date, close_adj, sma_50, sma_200)
][sma_50 %x>% sma_200, `:=`(
type = 'buy', prices = close_adj
)][sma_50 %x<% sma_200, `:=`(
type = 'sell', prices = close_adj
)][, (c('type', 'prices')) := lapply(.SD, shift), .SDcols = c('type', 'prices')]
orders = dtorders[!is.na(type)]
head(orders)
#> symbol name date close_adj sma_50 sma_200 type prices
#> 1: 601988.SS 中国银行 2015-09-28 3.283584 3.662233 3.692576 sell 3.309507
#> 2: 601988.SS 中国银行 2016-09-05 3.148622 3.054345 3.041040 buy 3.157722
#> 3: 601988.SS 中国银行 2018-05-03 3.638575 3.846793 3.863116 sell 3.629050
#> 4: 601988.SS 中国银行 2018-12-26 3.630000 3.624000 3.617244 buy 3.610000
e = pq_plot(boc, y='close_adj', addti = list(sma=list(n=200), sma=list(n=50)), orders = orders)
e[[1]]
#> NULL