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.SH'])
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, `:=`(
side = 'buy', prices = close_adj
)][sma_50 %x<% sma_200, `:=`(
side = 'sell', prices = close_adj
)][, (c('side', 'prices')) := lapply(.SD, shift), .SDcols = c('side', 'prices')]
orders = dtorders[!is.na(side)]
head(orders)
#> symbol name date close_adj sma_50 sma_200 side prices
#> 1: 601988.SH 中国银行 2021-04-20 5.75 5.7032 5.69960 buy 5.76
#> 2: 601988.SH 中国银行 2021-08-19 5.64 5.6758 5.67855 sell 5.67
#> 3: 601988.SH 中国银行 2021-11-18 5.69 5.6916 5.69070 buy 5.70
#> 4: 601988.SH 中国银行 2021-11-25 5.71 5.6922 5.69315 sell 5.71
#> 5: 601988.SH 中国银行 2022-01-18 5.75 5.7016 5.69960 buy 5.72
e = pq_plot(boc, y='close_adj', addti = list(sma=list(n=200), sma=list(n=50)), orders = orders)
e[[1]]
#> NULL