我想使用 quantstrat 检查策略。该策略将使用自定义指标作为信号。
在初始设置、信号和规则定义之后,我调用了返回以下错误的函数“applySignals”(“信号”部分的最后一行):
> tmp <- applySignals(strategy = strat.name, mktdata = Observed)
Error in do.call(opr, list(data[, colNums[1]] + offset1, data[, colNums[2]] + :
'what' must be a function or character string
我的合并数据(带有时间序列和指标的“xts”)看起来像:[编辑]添加一些“观察到”数据以重现代码
> Observed
Observed Premium Up Dwn Avg
2006-02-14 2.1380 -0.2138143378 2.4328959 -2.3214583 5.571882e-02
2006-02-15 2.1360 0.6598056094 2.2417689 -2.2373971 2.185893e-03
2006-02-16 2.1160 0.1235587811 2.0652530 -2.1795442 -5.714558e-02
2006-02-17 2.1175 0.0850390948 2.0046508 -2.1755479 -8.544855e-02
2006-02-20 2.1175 -0.0160784147 2.0137277 -2.1340597 -6.016602e-02
2006-02-21 2.1660 0.6159878284 2.0582033 -2.1064381 -2.411742e-02
2006-02-22 2.1410 -0.8271348627 1.4545134 -1.7907587 -1.681227e-01
2006-02-23 2.1335 0.0262028696 1.0410485 -1.5390151 -2.489833e-01
2006-02-24 2.1400 -0.4904482488 1.0289706 -1.5539016 -2.624655e-01
2006-02-27 2.1405 0.4989304104 1.0867895 -1.5531596 -2.331851e-01
2006-02-28 2.1235 -0.6107197420 0.9581728 -1.5304154 -2.861213e-01
2006-03-01 2.1155 -1.4004603382 0.9392118 -1.6285840 -3.446861e-01
2006-03-02 2.1040 -2.6372637453 1.0549332 -1.9471603 -4.461135e-01
2006-03-03 2.1100 -1.0637038669 0.8887004 -1.9220545 -5.166770e-01
2006-03-06 2.1475 0.3791451348 0.9526426 -1.9328469 -4.901021e-01
2006-03-07 2.1420 0.0994600858 0.9899282 -1.9269629 -4.685173e-01
2006-03-08 2.1800 0.4611403968 1.0646190 -1.9221621 -4.287716e-01
2006-03-09 2.1618 1.0147107466 1.2076224 -1.9442195 -3.682985e-01
2006-03-10 2.1373 -0.8174364392 1.2042206 -1.9265078 -3.611436e-01
2006-03-13 2.1340 0.0987534656 1.2307299 -1.8806798 -3.249749e-01
2006-03-14 2.1240 -0.2929120228 1.2326618 -1.8787714 -3.230548e-01
2006-03-15 2.1157 -0.4997230526 1.2270634 -1.8869319 -3.299342e-01
2006-03-16 2.1135 -0.5146394026 1.2285254 -1.8792732 -3.253739e-01
2006-03-17 2.1240 0.0930870020 1.2549022 -1.8626156 -3.038567e-01
2006-03-20 2.1470 0.9648610335 1.3018329 -1.7219147 -2.100409e-01
2006-03-21 2.1710 1.4051133095 1.4683589 -1.7418489 -1.367450e-01
2006-03-22 2.1539 0.5030668339 1.5122114 -1.7175344 -1.026615e-01
2006-03-23 2.1615 0.0620782901 1.5151902 -1.6538302 -6.932004e-02
2006-03-24 2.1580 0.7646728211 1.5654217 -1.6598359 -4.720713e-02
2006-03-27 2.1740 -0.1862124849 1.5550725 -1.6694014 -5.716444e-02
2006-03-28 2.2410 1.2165496046 1.6669060 -1.6858773 -9.485643e-03
2006-03-29 2.2142 0.7524244206 1.6754260 -1.6882227 -6.398350e-03
2006-03-30 2.1870 0.2004133841 1.6790407 -1.6867138 -3.836530e-03
2006-03-31 2.1640 -0.8532554584 1.6755762 -1.7458022 -3.511301e-02
2006-04-03 2.1407 0.3298628411 1.6922944 -1.7394577 -2.358164e-02
2006-04-04 2.1404 0.0027410226 1.6548519 -1.7428983 -4.402320e-02
2006-04-05 2.1378 0.3333440255 1.6724672 -1.6831484 -5.340570e-03
2006-04-06 2.1330 0.2162185482 1.6807273 -1.6787408 9.932858e-04
2006-04-07 2.1490 0.6500901548 1.7243383 -1.6463158 3.901123e-02
2006-04-10 2.1550 0.4640603473 1.7219111 -1.6462133 3.784890e-02
2006-04-11 2.1310 -0.2175871349 1.7201852 -1.6182785 5.095332e-02
2006-04-12 2.1364 -0.3162412370 1.6710643 -1.4968764 8.709395e-02
2006-04-13 2.1380 0.3416813780 1.3919460 -1.0191617 1.863921e-01
2006-04-14 2.1380 -0.2983895635 1.3377500 -0.9139448 2.119026e-01
2006-04-17 2.1374 -0.0593549713 1.3255321 -0.9309603 1.972859e-01
2006-04-18 2.1155 -0.0693459894 1.3236018 -0.9402837 1.916591e-01
2006-04-19 2.1147 0.1871931205 1.3098852 -0.9448303 1.825275e-01
2006-04-20 2.1200 -0.5828489096 1.2448382 -0.9862872 1.292755e-01
2006-04-21 2.1215 -0.1185495175 1.2142135 -0.9090700 1.525717e-01
2006-04-24 2.1140 -0.0880255759 1.2114785 -0.9187870 1.463458e-01
2006-04-25 2.1305 0.5806637272 1.2386695 -0.8877396 1.754650e-01
2006-04-26 2.1190 -0.3910853634 1.2334783 -0.8753059 1.790862e-01
2006-04-27 2.1015 -1.1674376720 1.2946432 -0.9799907 1.573263e-01
2006-04-28 2.0870 -0.3034668733 1.2936662 -1.0054506 1.441078e-01
2006-05-01 2.0800 -0.8048605709 1.2420018 -1.0717676 8.511708e-02
2006-05-02 2.0618 -1.1095621994 1.1263909 -1.1238018 1.294567e-03
2006-05-03 2.0761 -0.4368114737 1.0895754 -1.1496448 -3.003471e-02
2006-05-04 2.0703 -0.4462234758 1.0822075 -1.1761637 -4.697810e-02
2006-05-05 2.0555 -0.4487127302 1.0078774 -1.1827260 -8.742429e-02
2006-05-08 2.0740 -0.6709235501 1.0118653 -1.2190279 -1.035813e-01
2006-05-09 2.0590 -1.1803067029 0.8829838 -1.2499368 -1.834765e-01
2006-05-10 2.0743 -0.5322050544 0.7864753 -1.2390704 -2.262975e-01
2006-05-11 2.0981 -0.0435156323 0.7680328 -1.2368898 -2.344285e-01
2006-05-12 2.1445 -0.5110121449 0.7578575 -1.2038982 -2.230204e-01
2006-05-15 2.1665 -0.2902450972 0.7148572 -1.2022385 -2.436906e-01
2006-05-16 2.1360 -1.2365412420 0.7344813 -1.3044814 -2.850000e-01
2006-05-17 2.2095 0.5379929766 0.7609727 -1.3173295 -2.781784e-01
2006-05-18 2.1805 -0.3411384826 0.7256124 -1.3191264 -2.967570e-01
2006-05-19 2.2090 1.0986468456 0.8087126 -1.3723228 -2.818051e-01
2006-05-22 2.2980 1.6265834890 1.0252594 -1.5113680 -2.430543e-01
2006-05-23 2.3525 2.0402188490 1.3501563 -1.6857445 -1.677941e-01
2006-05-24 2.3275 -1.4177090411 1.3801176 -1.7891370 -2.045097e-01
2006-05-25 2.2780 -0.0870963070 1.3531238 -1.7907284 -2.188023e-01
2006-05-26 2.2415 -0.2835027035 1.3535255 -1.7901376 -2.183061e-01
2006-05-29 2.2930 0.1716678912 1.3667027 -1.7879134 -2.106053e-01
2006-05-30 2.3125 -0.1196208714 1.3645126 -1.7890749 -2.122811e-01
2006-05-31 2.3070 0.2882825278 1.3718419 -1.7896648 -2.089115e-01
2006-06-01 2.2451 -1.0989510027 1.3824667 -1.8346964 -2.261149e-01
2006-06-02 2.2766 0.0055882465 1.3883872 -1.8323412 -2.219770e-01
2006-06-05 2.2645 -0.5557504270 1.3764825 -1.8516181 -2.375678e-01
2006-06-06 2.2420 -1.3329314639 1.3300468 -1.9327554 -3.013543e-01
2006-06-07 2.2545 -0.0352070252 1.3443832 -1.9233665 -2.894917e-01
2006-06-08 2.2585 -0.2936561933 1.3395479 -1.8602792 -2.603656e-01
2006-06-09 2.2592 0.4478957281 1.3851928 -1.8558332 -2.353202e-01
2006-06-12 2.2890 0.8456773376 1.4719615 -1.8325661 -1.803023e-01
2006-06-13 2.3096 0.9717809858 1.5546168 -1.7764652 -1.109242e-01
2006-06-14 2.2810 0.2899942076 1.5803720 -1.7537666 -8.669734e-02
2006-06-15 2.2635 -0.2286549666 1.5830390 -1.7419291 -7.944505e-02
2006-06-16 2.2450 -0.0717508264 1.5897434 -1.7235027 -6.687965e-02
2006-06-19 2.2603 0.2742449464 1.6096233 -1.6803714 -3.537404e-02
2006-06-20 2.2355 -0.6414388111 1.5871241 -1.6219477 -1.741178e-02
2006-06-21 2.2268 -0.5064409161 1.5868700 -1.6199759 -1.655297e-02
2006-06-22 2.2390 -2.3248131512 1.7189894 -1.9041819 -9.259622e-02
2006-06-23 2.2315 -1.1410019258 1.7323395 -1.9595313 -1.135959e-01
2006-06-26 2.2410 0.5484043021 1.7745707 -1.9458525 -8.564090e-02
2006-06-27 2.2405 -0.0337650650 1.7631549 -1.8542516 -4.554836e-02
2006-06-28 2.2200 0.1935305114 1.7406841 -1.8547450 -5.703044e-02
2006-06-29 2.1775 -2.1585026281 1.8354877 -2.0707062 -1.176092e-01
2006-06-30 2.1650 -2.6391690794 1.8609648 -2.3453711 -2.422031e-01
2006-07-03 2.1685 -1.1970917295 1.6713395 -2.3439907 -3.363256e-01
2006-07-04 2.1700 0.2059642050 1.4127213 -2.2076562 -3.974674e-01
2006-07-05 2.2080 0.8738532996 1.5042947 -2.1464587 -3.210820e-01
2006-07-06 2.1770 -0.0202271819 1.5078688 -2.1455749 -3.188531e-01
2006-07-07 2.1820 0.0016987703 1.5211015 -2.1397941 -3.093463e-01
2006-07-10 2.1789 0.1375845478 1.5187719 -2.1397368 -3.104825e-01
2006-07-11 2.1830 0.3870690128 1.5522351 -2.1394207 -2.935928e-01
2006-07-12 2.2025 0.9155863509 1.6141776 -2.1595430 -2.726827e-01
2006-07-13 2.2244 0.6330507352 1.6732863 -2.1031848 -2.149493e-01
2006-07-14 2.2120 0.6295527065 1.7177347 -2.1060357 -1.941505e-01
2006-07-17 2.2016 -0.0573716060 1.7300020 -2.0850777 -1.775378e-01
2006-07-18 2.1910 -0.3056416658 1.7146586 -2.0012483 -1.432948e-01
2006-07-19 2.1765 -0.2081095322 1.7085804 -2.0066969 -1.490583e-01
2006-07-20 2.1911 0.4077025408 1.7420549 -1.9934141 -1.256796e-01
2006-07-21 2.1988 0.0284398985 1.7165502 -1.9958732 -1.396615e-01
2006-07-24 2.1896 0.5962358374 1.6921366 -1.9880891 -1.479762e-01
2006-07-25 2.2005 0.8263923462 1.6758173 -1.9814622 -1.528225e-01
2006-07-26 2.1950 0.4095204766 1.6843090 -1.9819856 -1.488383e-01
2006-07-27 2.1922 -0.1622955343 1.6862826 -1.9795352 -1.466263e-01
2006-07-28 2.1754 -0.6732493197 1.6759770 -2.0093295 -1.666763e-01
2006-07-31 2.1770 -0.5164965887 1.6461394 -2.0322080 -1.930343e-01
2006-08-01 2.1910 0.0413051889 1.6628245 -2.0033768 -1.702762e-01
2006-08-02 2.1821 -0.0947159464 1.6722944 -1.9853984 -1.565520e-01
2006-08-03 2.1760 -0.2983090398 1.5481012 -1.7261049 -8.900187e-02
2006-08-04 2.1814 0.2807295177 1.5511915 -1.6344132 -4.161082e-02
2006-08-07 2.1851 0.3553678940 1.5364320 -1.6325228 -4.804537e-02
2006-08-08 2.1714 -0.2639736357 1.5307009 -1.6421388 -5.571899e-02
2006-08-09 2.1663 -0.5767039543 1.5132442 -1.6760311 -8.139347e-02
2006-08-10 2.1565 -0.9907589862 1.3912586 -1.4761960 -4.246868e-02
2006-08-11 2.1660 -0.1543471589 1.0886258 -1.0079084 4.035871e-02
2006-08-14 2.1635 -0.2251709611 1.0177610 -0.8722488 7.275607e-02
2006-08-15 2.1434 -0.7643886602 1.0318348 -0.9510129 4.041098e-02
2006-08-16 2.1409 -0.5358704934 0.9545552 -0.9677149 -6.579815e-03
2006-08-17 2.1415 -0.2812502973 0.9510774 -0.9816385 -1.528059e-02
2006-08-18 2.1469 0.3482744966 0.9717135 -0.9791697 -3.728061e-03
2006-08-21 2.1286 -0.5699506702 0.9680025 -1.0226276 -2.731257e-02
2006-08-22 2.1341 -0.0904836098 0.9398609 -1.0263228 -4.323099e-02
2006-08-23 2.1515 0.3959559424 0.8695172 -0.9906212 -6.055200e-02
2006-08-24 2.1526 0.1034269804 0.8168310 -0.9732432 -7.820613e-02
2006-08-25 2.1560 0.4648748481 0.7952556 -0.9626464 -8.369539e-02
2006-08-28 2.1404 0.3488799739 0.8228807 -0.9631880 -7.015367e-02
2006-08-29 2.1369 0.3672191410 0.8545862 -0.9500361 -4.772498e-02
2006-08-30 2.1365 0.5854814439 0.9077196 -0.9502635 -2.127194e-02
2006-08-31 2.1450 0.3774003361 0.9048436 -0.9494076 -2.228202e-02
2006-09-01 2.1390 -0.2430959460 0.8990395 -0.9617059 -3.133321e-02
2006-09-04 2.1230 -0.5489319994 0.8482056 -0.9872165 -6.950547e-02
2006-09-05 2.1386 0.0377860224 0.7587333 -0.9503180 -9.579235e-02
2006-09-06 2.1500 0.5131697437 0.7714305 -0.9561052 -9.233738e-02
2006-09-07 2.1524 0.4537598583 0.8140923 -0.9576967 -7.180220e-02
2006-09-08 2.1580 0.3988486095 0.8358190 -0.9079502 -3.606560e-02
2006-09-11 2.1854 0.3306108049 0.8544892 -0.8701466 -7.828685e-03
2006-09-12 2.1683 -0.1197807818 0.8498595 -0.8762559 -1.319822e-02
2006-09-13 2.1602 -0.3318065847 0.8493556 -0.8915581 -2.110124e-02
2006-09-14 2.1600 -0.3557649307 0.8502127 -0.8962455 -2.301644e-02
2006-09-15 2.1510 -0.4660987665 0.8320426 -0.9278640 -4.791071e-02
2006-09-18 2.1467 -0.4940336591 0.8046960 -0.9571442 -7.622410e-02
2006-09-19 2.1591 -0.0597936941 0.8086501 -0.9474863 -6.941810e-02
2006-09-20 2.1759 0.3452778280 0.8304066 -0.9077773 -3.868537e-02
2006-09-21 2.2120 0.6612526293 0.8442226 -0.8114592 1.638168e-02
2006-09-22 2.2075 -0.0127523279 0.8465256 -0.8043226 2.110151e-02
2006-09-25 2.2203 0.2440675132 0.8606385 -0.7871529 3.674279e-02
2006-09-26 2.1871 -0.7581483887 0.8600123 -0.7861107 3.695080e-02
2006-09-27 2.1862 -0.6124047168 0.8652457 -0.7964464 3.439966e-02
2006-09-28 2.1700 -0.6424288992 0.8820978 -0.8373770 2.236037e-02
2006-09-29 2.1690 -0.5614706415 0.8682245 -0.8841534 -7.964465e-03
2006-10-02 2.1565 -0.5049280130 0.8649442 -0.8765383 -5.797043e-03
2006-10-03 2.1754 -0.1457955782 0.8640762 -0.8793577 -7.640775e-03
2006-10-04 2.1639 -0.1023417362 0.8345381 -0.8830395 -2.425070e-02
2006-10-05 2.1635 -0.0603438998 0.8278018 -0.8872213 -2.970973e-02
2006-10-06 2.1620 -0.1571580762 0.7874387 -0.8883270 -5.044416e-02
2006-10-09 2.1552 0.0985182935 0.7675432 -0.8851222 -5.878955e-02
2006-10-10 2.1553 0.2525632896 0.7565981 -0.8818209 -6.261141e-02
2006-10-11 2.1590 0.0505341675 0.7028960 -0.8637819 -8.044298e-02
2006-10-12 2.1581 0.2768083668 0.6922607 -0.8598528 -8.379605e-02
2006-10-13 2.1371 -0.0093993501 0.6981232 -0.8501355 -7.600616e-02
2006-10-16 2.1294 -0.2689905414 0.6904272 -0.8237768 -6.667478e-02
2006-10-17 2.1315 -0.3628060866 0.6835533 -0.8436090 -8.002785e-02
2006-10-18 2.1371 -0.3238164858 0.6265774 -0.8424322 -1.079274e-01
2006-10-19 2.1409 0.2074037707 0.5975953 -0.8298738 -1.161393e-01
2006-10-20 2.1402 0.5243536270 0.6156067 -0.8395182 -1.119558e-01
2006-10-23 2.1392 0.4216697999 0.6269939 -0.8448348 -1.089205e-01
2006-10-24 2.1502 0.9139503692 0.7507359 -0.8996615 -7.446276e-02
2006-10-25 2.1442 1.1575084452 0.9084364 -0.9580743 -2.481892e-02
2006-10-26 2.1402 0.2369656995 0.9242912 -0.9344136 -5.061235e-03
2006-10-27 2.1336 -1.3563684461 1.0057439 -1.0752177 -3.473689e-02
2006-10-30 2.1494 -0.6477569837 1.0114373 -1.0911593 -3.986100e-02
2006-10-31 2.1418 -0.0745236268 1.0109986 -1.0917026 -4.035200e-02
2006-11-01 2.1443 0.2614575082 1.0044025 -1.0906946 -4.314601e-02
2006-11-02 2.1443 0.0662477501 0.9513886 -1.0773476 -6.297951e-02
2006-11-03 2.1390 -0.0087592904 0.9515500 -1.0772428 -6.284641e-02
2006-11-06 2.1372 0.0210966633 0.9380619 -1.0786194 -7.027877e-02
2006-11-07 2.1379 0.0992758203 0.9340438 -1.0174397 -4.169796e-02
2006-11-08 2.1445 0.2959330302 0.9472650 -0.9701050 -1.142004e-02
2006-11-09 2.1415 0.1798804272 0.9466327 -0.9146522 1.599028e-02
2006-11-10 2.1514 0.0247330103 0.9402576 -0.8691968 3.553040e-02
2006-11-13 2.1688 0.3139413461 0.9493083 -0.8236562 6.282604e-02
2006-11-14 2.1480 -0.3729555871 0.9529200 -0.8424120 5.525404e-02
2006-11-15 2.1470 -0.2971281525 0.9539304 -0.8564081 4.876116e-02
2006-11-16 2.1528 0.0342754821 0.9561702 -0.8523400 5.191514e-02
2006-11-17 2.1605 0.2775509780 0.9707292 -0.8379184 6.640544e-02
2006-11-20 2.1600 0.2827843563 0.9802707 -0.8351754 7.254765e-02
按照下面的代码:
library(quantstrat)
library(PerformanceAnalytics)
library(tidyverse)
library(xts)
library(zoo)
################################# Initial Setup ##################################
initDate <- '2005-11-20'
from <- '2006-02-14'
to <- '2017-09-20'
currency('USD')
Sys.setenv(TZ = 'UTC')
symbols <- 'Observed'
stock(symbols, currency = 'USD', multiplier = 1)
Observed <- merge(DfDygraphPredictionObservedComps$Observed, DfDygraphPremium) %>% na.exclude(.)
################################# Initial Setup Backtest ###########################
strat.name <- portfolio.st <- account.st <- "FactorModelBT"
rm.strat(strat.name)
initPortf(portfolio.st, symbols = symbols, initDate = initDate, currency = 'USD')
initAcct(account.st, portfolios = portfolio.st, initDate = initDate, currency = 'USD')
initOrders(portfolio.st, initDate = initDate)
strategy(strat.name, store = TRUE)
################################ Signals #########################################
add.signal(strat.name,
name = "sigCrossover",
arguments = list(columns = c("Premium", "Up"), relationship = "gt"),
label = "Premium.gt.Upper.Band")
add.signal(strat.name,
name = "sigCrossover",
arguments = list(columns = c("Premium", "Dwn"), relationship = "lt"),
label = "Premium.lt.Lower.Band")
add.signal(strat.name,
name = "sigCrossover",
arguments = list(columns = c("Premium", "Avg"), relationship = "op"),
label = "Cross.Mid")
tmp <- applySignals(strategy = strat.name, mktdata = Observed)
########################### Trading Rules - Actions ##############################
add.rule(strat.name,
name = "ruleSignal",
arguments = list(sigcol = "Premium.gt.Upper.Band",
sigval = TRUE,
orderqty = -1000,
ordertype = "market",
orderside = NULL,
threshold = NULL),
type = "enter",
path.dep = TRUE)
add.rule(strat.name,
name = "ruleSignal",
arguments = list(sigcol = "Premium.lt.Lower.Band",
sigval = TRUE,
orderqty = 1000,
ordertype = "market",
orderside = NULL,
threshold = NULL),
type = "enter",
path.dep = TRUE)
add.rule(strat.name,
name = "ruleSignal",
arguments = list(sigcol = "Cross.Mid",
sigval = TRUE,
orderqty = "all",
ordertype = "market",
orderside = NULL,
threshold = NULL),
type = "exit",
label = "exitMid",
path.dep = TRUE)
################################ Apply Strategy ############################
out <- applyStrategy(strategy = strat.name, portfolios = portfolio.st)
#set up analytics
updatePortf(portfolio.st)
dateRange <- time(getPortfolio(portfolio.st)$summary)[-1]
updateAcct(portfolio.st,dateRange)
updateEndEq(account.st)
chart.Posn(portfolio.st, 'Observed')