我有一个基于订单不平衡的信号,我想针对历史股票数据进行测试。
我也有这些信号的价格,然后我计算价格的趋势,看看回报是在上一个前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
答案 0 :(得分: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
请注意,您使用的data.frame
比matrix
慢。此外,您没有与您的数据相关的任何日期或时间。如果您确实有时间序列数据,恕我信息xts
或zoo
将是更好的数据结构。