Binary operators which create the upwards or downwards crossover signals.

x %x>% y

x %x<% y

Arguments

x, y

numeric vectors

Examples

library(data.table)
library(pedquant)

data("dt_banks")
dtadj = md_stock_adjust(setDT(dt_banks)[symbol=='601988.SS'])
dtadjti = pq_addti(dtadj, x='close_adj', sma=list(n=200), sma=list(n=50))

dtorders = copy(dtadjti[[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
)]
orders = dtorders[!is.na(type)]
head(orders)
#>       symbol     name       date close_adj   sma_50  sma_200 type   prices
#> 1: 601988.SS 中国银行 2009-04-24  2.169398 2.156822 2.156319  buy 2.169398
#> 2: 601988.SS 中国银行 2010-03-22  2.689302 2.687621 2.688468 sell 2.689302
#> 3: 601988.SS 中国银行 2010-05-05  2.669908 2.696283 2.695734  buy 2.669908
#> 4: 601988.SS 中国银行 2010-05-19  2.521220 2.681156 2.681415 sell 2.521220
#> 5: 601988.SS 中国银行 2011-04-26  2.318449 2.301286 2.300055  buy 2.318449
#> 6: 601988.SS 中国银行 2011-07-20  2.239067 2.287715 2.288153 sell 2.239067