0

我有一个基于订单不平衡的信号,我想针对历史库存数据进行测试。

我还有每个信号的价格,然后我计算价格趋势以查看最近 4 个价格的回报是上涨还是下跌。从我收到的信号

nSignal <- pnorm(Signal, mean = mean(Signal), sd = sd(Signal)) 

现在我的想法是每次 nSignal 升至 0.70 以上时买入/做空。如果趋势是正面的,那么我会买入,如果趋势是负面的,我会做空。

Sell   if( nSignal > = 0.70 && Trend < 0 ) 
Buy    if( nSignal > = 0.70 && Trend > 0 ) 
Ignore if( nSignal > = 0.70 && Trend = 0 )

然后,当 nSignal 再次开始下降时,我将退出该位置。我不想在任何时候拥有一个以上的空缺职位。因此,如果在我有未平仓头寸时出现买入/卖出信号,我会忽略。

我的问题是关于 Sell 和 Buy 向量的编码以及这些向量的回报的计算。理想情况下,我希望有 1 个向量作为输出。

我可以产生买入/卖出信号,但我坚持告诉 R 忽略进一步的买入/卖出,直到 nSignal 下降并释放头寸。我附上了我想计算的东西。

   dd <- textConnection("PriceTao,nSignal,Trend,Sell ,Buy,,Return
79.35,0.799276057198847,0.00632873284203539, ,Buy,79.35,
79.5,0.800495492911607,0.00631674578326402, ,,,
79.55,0.849748126078447,0.00378111963884864, ,,,
79.7,0.781025021425822,0.00440489652262133, ,release,79.7,0.00100623484156181
79.7,0.850907051807651,0.00251453735437934, ,Buy,79.7,
79.85,0.835339437922026,0.00376766425320585, ,release,79.85,0.000429459362427442
80,0.829431322197511,0.00376057994561618, ,,,
79.75,0.721861766918789,0.000635579945616138, ,,,
79.8,0.749198554641736,-0.000619518523171436,Sell, ,79.8,
79.9,0.655121878771812,-0.00124490792027077,release, ,79.9,-0.000285955381947645
79.85,0.638399458172212,0.00125430985194441, , ,,
79.75,0.677237812176031,-0.00062499754849088, , ,,
79.8,0.77229131417357,-0.00125117113292239,Sell, ,79.8,
79.9,0.78060399324371,0.00062774392694287, ,,,
80,0.785209846277682,0.00313165653529857, ,,,
80,0.71354933296563,0.00250469728764968,release,,80,-0.000571553096032407
80,0.723175396790292,0.00125156445556929, ,,,
80.05,0.645047940052525,0.000624999999999876, , ,,
79.95,0.654754824370203,-0.000624219237976287, , ,,
79.95,0.66648952405407,-0.000624219237976287, , ,,
80.05,0.66246174072327,1.56250061034147E-06, , ,,
80.1,0.64268970941157,0.00187539135757464, , ,,
80.05,0.626534449371471,0.00125117163223132, , ,,
80.05,0.659757399947805,3.89893644814343E-07, , ,,
80.15,0.605440623800618,0.000624999512632951, , ,,
80.15,0.555063339554548,0.00124921923797627, , ,,
80.1,0.623048801370024,0.000625388919822667, , ,,
79.95,0.671863289394849,-0.00249648949418346, , ,,
80.05,0.629889151643382,-0.00124570775559696, , ,,
80.15,0.692044829948308,0.000627341800532921, , ,,
80.25,0.781503635407542,0.00374766161286955, ,Buy,80.25,
80.35,0.767181308420401,0.00374298579328602, ,release,80.35,0.000283988254209611
80.2,0.712321509444304,0.000626933947966979, ,,,
")

Data <- read.table(dd, header = TRUE, sep = ",")
close(dd) 
Data                         
4

1 回答 1

3

这是基本移动平均交叉系统的实现。调整它以适应您的需求应该是微不足道的。

library(quantmod)
library(tseries) # for maxdrawdown if you want it

x <- getSymbols("SPY", src="yahoo", from="2012-01-01", to="2012-12-31", auto.assign=FALSE)
n1 <- 20 # for short term moving average
n2 <- 50 # for long term moving average

x$ma1 <- SMA(Ad(x), n1) # add a short term MA
x$ma2 <- SMA(Ad(x), n2) # add a long term MA
x$spd <- x$ma1 - x$ma2 # difference between the two
x$spd[1:n2] <- 0 # flat until we have enough data to calculate both MAs
x$cross <- c(0, diff(x$spd > 0, na.pad=FALSE)) # short cross long
x$pos <- NA # Position
x$pos[x$cross == 1] <- 1 # long where it crosses from below
x$pos[x$cross == -1] <- -1 # short where it crosses from above
# fill forward your position until you get the next signal.  Also, you have to 
# lag your signal because today's return will be a result of yesterday's position
x$pos <- c(0, lag(na.locf(x$pos), na.pad=FALSE)) 
x$pos[is.na(x$pos)] <- 0
x$ret <- c(0, ROC(Ad(x), na.pad=FALSE))
x$cumret <- cumsum(x$pos * x$ret)
out <- data.frame(n1=n1, n2=n2, cumret=as.numeric(last(x$cumret)), 
                  mdd=maxdrawdown(x$cumret)$maxdrawdown, ntrades=sum(abs(x$cross)))
out
tail(x)

编辑

好的,然后使用您的数据

library(quantmod) # for Lag.  Also loads zoo which is needed for na.locf, and 
                  # TTR which is needed for ROC
x <- Data[, 1:3]
x$pos <- NA
x$pos[with(x, nSignal >= 0.7 & Trend < 0)] <- -1
x$pos[with(x, nSignal >= 0.7 & Trend > 0)] <- 1
x$pos <- na.locf(x$pos)
x$pos[is.na(x$pos)] <- 0 # first few rows are NA; replace with 0 meaning "no position"
x$ret <- ROC(x$PriceTao) * Lag(x$pos) # yesterdays position * return from yesterday to today
x

请注意,您使用的 adata.frame比 a 慢matrix。此外,您没有任何与您的数据相关的日期或时间。恕我直言xts,或者zoo如果您实际上有时间序列数据,这将是一个更好的数据结构。

于 2013-02-16T17:12:10.507 回答