我有一个基于订单不平衡的信号,我想针对历史库存数据进行测试。
我还有每个信号的价格,然后我计算价格趋势以查看最近 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