更新:应用策略时出现 quantstrat 错误:chart_Series 中的错误

UPDATED: quantstrat error when applying a strategy: Error in chart_Series

编辑:这个问题的第一部分似乎已经解决了,但是我还有另一个问题。

加载数据后,我试图绘制 chart.Posn 但现在 运行 遇到此错误:

> chart.Posn(portfolio.st, 'GOOG')
Error in chart_Series(Prices, name = Symbol, TA = TA, ...) : 
  'x' must be a time-series object 

我不知道这对我最初使用 tidyquant 而不是从 quantmod

获取符号是否有副作用

代码(我在下面提供了新数据)

    GOOG <- xts(GOOG, order.by = GOOG$date)
GOOG$date <- NULL

GOOG$predictions <- as.numeric(as.character(GOOG$predictions))

currency('USD')
Sys.setenv(TZ="UTC")

stock("GOOG", currency="USD", multiplier=1)

symbols <- c("GOOG")

initDate="2007-01-01"
strategy.st <- portfolio.st <- account.st <- "SVMstrat"
rm.strat(strategy.st)
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(strategy.st, store=TRUE)

GOOG$predictions <- as.numeric(as.character(GOOG$predictions))
mean(GOOG$direction == GOOG$predictions)

add.signal(strategy.st, 
           name = sigThreshold, 
           arguments = list(column = "predictions", 
                            threshold = 0.5, 
                            relationship = "gte", 
                            cross = TRUE),
           label = "longEntry")

add.signal(strategy.st, 
           name = sigThreshold, 
           arguments = list(column = "predictions", 
                            threshold = 0.5, 
                            relationship = "lte", 
                            cross = TRUE),
           label = "longExit")

applySignals(strategy = strategy.st, mktdata=GOOG)

add.rule(strategy.st, 
         name="ruleSignal", 
         arguments=list(sigcol="longEntry", 
                        sigval=TRUE, 
                        ordertype="market", 
                        orderside="long", 
                        replace=FALSE, 
                        prefer="Open", 
                        orderqty = 1), 
         type="enter", path.dep=TRUE)

add.rule(strategy.st, 
         name="ruleSignal", 
         arguments=list(sigcol="longExit", 
                        sigval=TRUE, 
                        orderqty="all", 
                        ordertype="market", orderside="long", 
                        replace=FALSE, prefer="Open"), 
         type="exit", path.dep=TRUE)

applyStrategy(strategy=strategy.st,portfolios=portfolio.st)

updatePortf(portfolio.st)
dateRange <- time(getPortfolio(portfolio.st)$summary)[-1]
updateAcct(portfolio.st,dateRange)
updateEndEq(account.st)

chart.Posn(portfolio.st, 'GOOG')

数据:

GOOG <- structure(list(date = structure(c(17375, 17378, 17379, 17380, 
17381, 17382, 17385, 17386, 17387, 17388, 17389, 17392, 17393, 
17394, 17395, 17396, 17399, 17400, 17401, 17402, 17403, 17406, 
17407, 17408, 17409, 17410, 17414, 17415, 17416, 17417, 17420, 
17421, 17422, 17423, 17424, 17427, 17428, 17429, 17430, 17431, 
17434, 17435, 17436, 17437, 17438, 17441, 17442, 17443, 17444, 
17445, 17448, 17449, 17450, 17451, 17452, 17455, 17456, 17457, 
17458, 17459, 17462, 17463, 17464, 17465, 17466, 17469, 17470, 
17471, 17472, 17473, 17476, 17477, 17478, 17479, 17480, 17483, 
17484, 17485, 17486, 17487, 17490, 17491, 17492, 17494, 17497, 
17498, 17499, 17500, 17501, 17504, 17505, 17506, 17507, 17508, 
17511, 17512, 17513, 17514, 17515, 17518), class = "Date"), open = c(929.400024, 
941.890015, 932.380005, 928.609985, 930.340027, 926.75, 929.059998, 
927.090027, 920.609985, 917.549988, 907.969971, 922.530029, 924.22998, 
925.289978, 925.780029, 910.309998, 910, 912.719971, 921.929993, 
928.659973, 923.48999, 916, 905.099976, 920.049988, 931.76001, 
941.130005, 933.080017, 930.150024, 931.72998, 936.48999, 934.25, 
932.590027, 930.659973, 931.25, 924.659973, 920.01001, 917.419983, 
922.97998, 933, 927.75, 925.450012, 923.719971, 927.73999, 941.359985, 
952, 959.97998, 954, 957, 955.48999, 966.700012, 980, 980, 973.719971, 
987.450012, 992, 992.099976, 990.289978, 991.77002, 986, 989.440002, 
989.52002, 970, 968.369995, 980, 1009.190002, 1014, 1015.219971, 
1017.210022, 1021.76001, 1022.109985, 1028.98999, 1027.27002, 
1030.52002, 1033.98999, 1026.459961, 1023.419983, 1022.590027, 
1019.210022, 1022.52002, 1034.01001, 1020.26001, 1023.309998, 
1035, 1035.869995, 1040, 1055.089966, 1042.680054, 1022.369995, 
1015.799988, 1012.659973, 995.940002, 1001.5, 1020.429993, 1037.48999, 
1035.5, 1039.630005, 1046.119995, 1045, 1054.609985, 1066.079956
), high = c(943.830017, 943.590027, 937.447021, 932.599976, 932.23999, 
930.307007, 931.700012, 935.814026, 925.97998, 919.26001, 917.780029, 
924.66803, 926.549988, 932.700012, 926.859985, 915.275024, 913, 
925.859985, 929.929993, 930.840027, 925.554993, 919.244995, 923.330017, 
930.81897, 941.97998, 942.47998, 937, 930.914978, 936.409973, 
936.98999, 938.380005, 933.47998, 937.25, 932.77002, 926.48999, 
922.080017, 922.419983, 933.880005, 936.530029, 934.72998, 926.400024, 
930.820007, 949.900024, 950.690002, 959.786011, 962.539978, 958, 
960.390015, 970.909973, 979.460022, 985.424988, 981.570007, 990.710022, 
994.119995, 997.210022, 993.906982, 996.440002, 996.719971, 988.880005, 
991, 989.52002, 972.22998, 976.090027, 987.599976, 1048.390015, 
1024.969971, 1024, 1029.670044, 1028.089966, 1032.650024, 1034.869995, 
1033.969971, 1043.521973, 1033.98999, 1030.76001, 1031.579956, 
1026.810059, 1024.089966, 1035.920044, 1034.420044, 1022.609985, 
1035.109985, 1039.706055, 1043.177979, 1055.459961, 1062.375, 
1044.079956, 1028.48999, 1022.48999, 1016.099976, 1020.609985, 
1024.969971, 1034.23999, 1042.050049, 1043.800049, 1050.310059, 
1046.665039, 1058.5, 1067.619995, 1078.48999), low = c(927.5, 
926.039978, 929.26001, 916.679993, 922.23999, 923.030029, 926.5, 
925.609985, 917.25, 906.130005, 905.580017, 918.190002, 919.820007, 
923.445007, 910.97998, 907.153992, 903.400024, 911.474976, 919.359985, 
915.5, 915.5, 911.869995, 905, 919.650024, 931.76001, 935.150024, 
921.960022, 919.27002, 923.619995, 924.880005, 926.919983, 923.861023, 
929.859985, 924, 916.359985, 910.599976, 912.549988, 922, 923.830017, 
926.47998, 909.700012, 921.140015, 927.73999, 940.549988, 951.51001, 
947.840027, 949.140015, 950.690002, 955.179993, 963.359985, 976.109985, 
966.080017, 972.25, 985, 989, 984, 988.590027, 986.974976, 978.390015, 
984.580017, 966.119995, 961, 960.52002, 972.200012, 1008.200012, 
1007.5, 1010.419983, 1016.950012, 1013.01001, 1020.309998, 1025, 
1025.130005, 1028.449951, 1019.666016, 1025.280029, 1022.570007, 
1014.150024, 1015.419983, 1022.52002, 1017.75, 1017.5, 1022.655029, 
1031.430054, 1035, 1038.439941, 1040, 1015.650024, 1015, 1002.02002, 
995.570007, 988.280029, 1001.140015, 1018.070984, 1032.521973, 
1032.050049, 1033.689941, 1038.380005, 1043.109985, 1049.5, 1062
), close = c(941.530029, 930.5, 930.830017, 930.390015, 923.650024, 
927.960022, 929.359985, 926.789978, 922.900024, 907.23999, 914.390015, 
922.669983, 922.219971, 926.960022, 910.97998, 910.669983, 906.659973, 
924.690002, 927, 921.280029, 915.890015, 913.809998, 921.289978, 
929.570007, 939.330017, 937.340027, 928.450012, 927.809998, 935.950012, 
926.5, 929.080017, 932.070007, 935.090027, 925.109985, 920.289978, 
915, 921.809998, 931.580017, 932.450012, 928.530029, 920.969971, 
924.859985, 944.48999, 949.5, 959.109985, 953.27002, 957.789978, 
951.679993, 969.960022, 978.890015, 977, 972.599976, 989.25, 
987.830017, 989.679993, 992, 992.179993, 992.809998, 984.450012, 
988.200012, 968.450012, 970.539978, 973.330017, 972.559998, 1019.27002, 
1017.109985, 1016.640015, 1025.5, 1025.579956, 1032.47998, 1025.900024, 
1033.329956, 1039.849976, 1031.26001, 1028.069946, 1025.75, 1026, 
1020.909973, 1032.5, 1019.090027, 1018.380005, 1034.48999, 1035.959961, 
1040.609985, 1054.209961, 1047.410034, 1021.659973, 1021.409973, 
1010.169983, 998.679993, 1005.150024, 1018.380005, 1030.930054, 
1037.050049, 1041.099976, 1040.47998, 1040.609985, 1049.150024, 
1064.189941, 1077.140015), volume = c(1846400, 1970100, 1277700, 
1824400, 1202500, 1082300, 1032200, 1061600, 1192100, 1824000, 
1206800, 1064500, 883400, 1006700, 1277200, 1342700, 943400, 
1166700, 1090200, 1270300, 1053400, 1086500, 1185600, 1301200, 
1582600, 947400, 1326400, 1527700, 1212700, 1011500, 1267000, 
1134400, 1102600, 1397600, 2505400, 1306900, 936700, 1669800, 
1290600, 1052700, 1856800, 1666900, 2239400, 1020300, 1581000, 
1283400, 888300, 952400, 1213800, 1173900, 891400, 968400, 1693300, 
1262400, 1169800, 910500, 1290200, 1057600, 1313600, 1183200, 
1478400, 1212200, 1211300, 2042100, 5167700, 2085100, 1330700, 
1373400, 1049000, 1076400, 1125200, 1112300, 1088700, 1245200, 
720000, 885800, 959200, 854000, 1129700, 1397100, 953500, 1097000, 
746300, 537000, 1307900, 1424400, 2459400, 1724000, 1909600, 
1906400, 2067300, 1272000, 1458200, 1290800, 1192800, 1279500, 
1282700, 1558700, 3275900, 1554600), adjusted = c(941.530029, 
930.5, 930.830017, 930.390015, 923.650024, 927.960022, 929.359985, 
926.789978, 922.900024, 907.23999, 914.390015, 922.669983, 922.219971, 
926.960022, 910.97998, 910.669983, 906.659973, 924.690002, 927, 
921.280029, 915.890015, 913.809998, 921.289978, 929.570007, 939.330017, 
937.340027, 928.450012, 927.809998, 935.950012, 926.5, 929.080017, 
932.070007, 935.090027, 925.109985, 920.289978, 915, 921.809998, 
931.580017, 932.450012, 928.530029, 920.969971, 924.859985, 944.48999, 
949.5, 959.109985, 953.27002, 957.789978, 951.679993, 969.960022, 
978.890015, 977, 972.599976, 989.25, 987.830017, 989.679993, 
992, 992.179993, 992.809998, 984.450012, 988.200012, 968.450012, 
970.539978, 973.330017, 972.559998, 1019.27002, 1017.109985, 
1016.640015, 1025.5, 1025.579956, 1032.47998, 1025.900024, 1033.329956, 
1039.849976, 1031.26001, 1028.069946, 1025.75, 1026, 1020.909973, 
1032.5, 1019.090027, 1018.380005, 1034.48999, 1035.959961, 1040.609985, 
1054.209961, 1047.410034, 1021.659973, 1021.409973, 1010.169983, 
998.679993, 1005.150024, 1018.380005, 1030.930054, 1037.050049, 
1041.099976, 1040.47998, 1040.609985, 1049.150024, 1064.189941, 
1077.140015), direction = c(1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 
1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 
1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 
1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 
1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 
1, 0, 1, 1, 1), daily.returns = c(0.00796497316633915, -0.0117150050027772, 
0.000354666308436391, -0.000472698550717299, -0.00724426411648449, 
0.00466626740433007, 0.00150864581103693, -0.00276535146926948, 
-0.00419723356136681, -0.0169682886474819, 0.00788107345224054, 
0.0090551819947422, -0.000487728015749256, 0.00513982688410031, 
-0.0172391922205249, -0.000340289585727183, -0.00440336244178141, 
0.0198862082113775, 0.00249813234165353, -0.00617041100323623, 
-0.00585057076060858, -0.00227103360221692, 0.00818548715419065, 
0.00898742979704914, 0.0104994889319832, -0.00211852060935469, 
-0.00948430104756415, -0.000689335981181594, 0.00877336309971533, 
-0.010096705891169, 0.00278469185105235, 0.00321822657391202, 
0.00324012142577179, -0.0106728140733341, -0.00521019887165097, 
-0.00574816430305625, 0.00744262076502733, 0.0105987340354277, 
0.000933891865565917, -0.00420396047997473, -0.00814196392564925, 
0.004223822841668, 0.0212248397793964, 0.00530446066453272, 0.0101211005792523, 
-0.00608894192671761, 0.00474152958256258, -0.00637925342751922, 
0.0192081678026828, 0.00920655779357471, -0.00193077360177174, 
-0.0045036069600819, 0.0171190874057765, -0.00143541369724542, 
0.00187276754923715, 0.0023441991516544, 0.000181444556451638, 
0.000634970473547991, -0.00842052962484363, 0.00380923353577045, 
-0.0199858325846691, 0.00215805253147128, 0.00287472856682269, 
-0.000791118106450051, 0.0480279078885169, -0.00211919801192617, 
-0.000462064090345216, 0.00871496780500025, 7.7967820575342e-05, 
0.00672792400010591, -0.00637296231157924, 0.00724235483593283, 
0.00630971739679231, -0.00826077434077854, -0.0030933653676728, 
-0.00225660326812049, 0.000243724104313836, -0.0049610399610136, 
0.0113526435303026, -0.0129878673123487, -0.00069672156648426, 
0.0158192275191027, 0.00142096203366848, 0.00448861362895858, 
0.0130692345797547, -0.00645025872602245, -0.0245845086108847, 
-0.000244699808749416, -0.0110043863846236, -0.0113743134258227, 
0.00647858277461255, 0.0131621953779111, 0.0123235422321553, 
0.00593638237264926, 0.00390523775000573, -0.000595520136675076, 
0.000124947142183363, 0.0082067624980553, 0.01433533494348, 0.0121689498284778
), rsi = c(55.4941894407854, 50.1632187721403, 44.2057435629332, 
42.3277780878597, 34.916071046742, 38.3146934968324, 31.6795222837921, 
26.8964608058531, 26.5762418841725, 19.4454931404427, 19.2426086807444, 
33.6749929164219, 34.664251589656, 45.2102647300278, 31.5897223352515, 
36.2768164306463, 34.0840071047493, 46.9525192134939, 51.8801058990984, 
46.3093986201844, 42.8722859382365, 43.0957642799655, 49.175098084696, 
62.3766664464771, 63.434587161957, 58.4768395755699, 53.2799994541443, 
50.4676873087737, 65.036737044046, 58.5873924866939, 62.3539876687387, 
54.8745033541646, 55.2938209354096, 52.3738367508463, 52.7465383120286, 
50.7142001647387, 50.3146292014814, 51.194583971026, 45.4279672713402, 
44.2918322359654, 45.068547928843, 48.135031282721, 54.7140455891114, 
63.3503388014154, 66.1156673174906, 61.0393644854218, 61.638611076124, 
64.1858085150276, 73.1864682789322, 78.8442513456682, 76.0748344982738, 
70.4160617369582, 74.4321939715647, 76.0682095017227, 81.8013716675612, 
81.5329893185474, 77.4049144059356, 76.2072058576538, 65.5689633500972, 
72.0267383603007, 55.6390349896244, 60.4198811003328, 52.2463634887349, 
45.2655097258959, 68.9263022050927, 70.3371957548591, 64.6863349631492, 
68.7059200902, 68.1459609632161, 69.5555458937198, 65.3412385589247, 
67.360752439441, 74.1163158004527, 67.9866489434534, 78.9024435671165, 
76.7050678391648, 76.1182375524639, 72.9909746088876, 59.4459498647349, 
51.2180399554398, 51.0672179692865, 55.0636478144878, 55.756441335519, 
54.6240547411303, 64.9109643846478, 57.4655825824172, 41.988898103196, 
45.3179785535323, 42.0964437376148, 38.9428915240427, 41.8953683621125, 
49.0751009808747, 49.4300638103628, 56.8843996184545, 58.4915483608461, 
52.531910623186, 51.9880401490032, 53.5336152483392, 54.0807930604593, 
61.574386151279), momentum = c(-2.29998799999998, -16.659973, 
-25.159973, -23.0299680000001, -41.75, -42.929993, -38.790039, 
-46.130005, -57.4400029999999, -43.460022, -33.409973, -11.420044, 
-19.310058, -3.53997800000002, -19.850037, -19.7200319999999, 
-16.990051, -3.27001999999993, -2.35998500000005, -5.50994900000001, 
-7.01000900000008, 6.57000799999992, 6.89996300000007, 6.90002400000003, 
17.110046, 10.380005, 17.4700320000001, 17.1400149999999, 29.290039, 
1.80999799999995, 2.080017, 10.789978, 19.200012, 11.2999870000001, 
-1, -14.570007, -17.520019, -5.76000999999997, 4, 0.720031000000063, 
-14.980041, -1.64001499999995, 15.409973, 17.429993, 24.0199580000001, 
28.160035, 37.5, 36.679993, 48.150024, 47.309998, 44.549988, 
44.069947, 68.280029, 62.9700319999999, 45.1900029999999, 42.5, 
33.0700079999999, 39.5399779999999, 26.660034, 36.520019, -1.51000999999997, 
-8.35003699999993, -3.669983, -0.0399780000000192, 30.02002, 
29.2799680000001, 26.960022, 33.5, 33.3999630000001, 39.6699820000001, 
41.450012, 45.129944, 71.399964, 60.7200319999999, 54.7399290000001, 
53.190002, 6.72997999999995, 3.79998799999998, 15.8599850000001, 
-6.40997300000004, -7.19995100000006, 2.01000999999997, 10.059937, 
7.28002900000001, 14.3599850000001, 16.150024, -6.40997300000004, 
-4.34002699999996, -15.830017, -22.2299800000001, -27.349976, 
-0.710021999999981, 12.5500489999999, 2.56005899999991, 5.14001499999995, 
-0.130004999999983, -13.599976, 1.73999000000003, 42.5299680000001, 
55.7300419999999), ROC = c(-0.032795598957029, -0.0521774017299466, 
-0.0211218884757489, -0.0185396276689724, -0.011239584246205, 
-0.0145175903203612, -0.00122591501722535, -0.00434970056376649, 
-0.00808295692710193, -0.0179262252987202, -0.0147314594246657, 
-0.00722453871018125, -0.00494320358659461, 0.00438952614578625, 
0.00411390847797843, -0.00407661902179068, -0.0175041344808058, 
0.00267477300869512, 4.31271435541447e-05, 0.011243117733982, 
0.00571571283393091, 0.00785518232019733, -0.0036837104211509, 
0.00276855559648226, 0.0194028343644312, 0.0231498189132653, 
0.0158938717770809, 0.00705213034020158, 0.00683994790619824, 
-0.0137528275407712, -0.00885123791251541, 0.00389138470011119, 
0.00781584192612872, -0.0116494359217736, -0.0067252318670965, 
-0.0152708022295656, -0.0110688002733577, -0.00376072262811622, 
0.00790291050001635, 0.00891390782234858, 0.00650336567082288, 
0.00330323208950656, 0.0137630010983862, 0.0181199866055621, 
0.0324030338008408, 0.0344707694066191, 0.0349861658038035, 0.00758374779102411, 
0.02131932596276, 0.0204135365091132, 0.0245884516898194, 0.0153443490021248, 
0.0387182437892122, 0.018255779160036, 0.0109623594909625, 0.0152364552420901, 
0.019931662088692, 0.00359222408181115, -0.00342751359774773, 
-0.00149653292664187, -0.0240262396056901, -0.0220519404222213, 
-0.0198161049507126, -0.0121513535862787, 0.0309568648450718, 
0.0490236690313068, 0.046405770875606, 0.0522123781158035, 0.0530817741025684, 
0.0128769514166818, 0.00860504177727073, 0.0162834675770531, 
0.0138961503712416, 0.00552310197477013, -0.00428045026022694, 
-0.000146247165464075, -0.00711880768197481, -0.0183820889249988, 
0.00120168056673897, -0.00877310685546551, -0.00721091801104201, 
0.00824079523600485, 0.0146341354305113, 0.00782401940069555, 
0.0338815355466462, 0.0281073484004102, -0.0124798129988672, 
-0.0141444961739703, -0.0296884485614344, -0.0541125132421794, 
-0.0411836747473417, -0.0032155946565986, 0.00927736062930951, 
0.0262615746498476, 0.0415987024226974, 0.0345453179346213, 0.0215939309246291, 
0.0175189755318357, 0.0258336996226562, 0.0340315709208765), 
    predictions = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 
    1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
    2L, 2L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 
    2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L), .Label = c("0", "1"), class = "factor")), row.names = c("1", 
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", 
"14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", 
"25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", 
"36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", 
"47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", 
"58", "59", "60", "61", "62", "63", "64", "65", "66", "67", "68", 
"69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", 
"80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", 
"91", "92", "93", "94", "95", "96", "97", "98", "99", "100"), class = "data.frame")

老问题(我离开这里是为了完整)

老问题(带数据)

我一直在使用 quantstrat 包,但我 运行 遇到了一个错误。

当我应用 applySignals(strategy = strategy.st, mktdata=GOOG) 行时出现错误。我收到此错误:

Error in `colnames<-`(`*tmp*`, value = label) : 
  attempt to set 'colnames' on an object with less than two dimensions

我使用 traceback() 并获得此报告:

5: stop("attempt to set 'colnames' on an object with less than two dimensions")
4: `colnames<-`(`*tmp*`, value = label)
3: (function (label, data = mktdata, column, threshold = 0, relationship = c("gt", 
       "lt", "eq", "gte", "lte"), cross = FALSE) 
   {
       relationship = relationship[1]
       ret_sig = NULL
       colNum <- match.names(column, colnames(data))
       switch(relationship, `>` = , gt = {
           ret_sig = data[, colNum] > threshold
       }, `<` = , lt = {
           ret_sig = data[, colNum] < threshold
       }, eq = {
           ret_sig = data[, colNum] == threshold
       }, gte = , gteq = , ge = {
           ret_sig = data[, colNum] >= threshold
       }, lte = , lteq = , le = {
           ret_sig = data[, colNum] <= threshold
       })
       if (isTRUE(cross)) 
           ret_sig <- diff(ret_sig) == 1
       if (!missing(label)) 
           colnames(ret_sig) <- label
       return(ret_sig)
   })(label = "longEntry", data = mktdata, column = "predictions", 
       threshold = 0.5, relationship = "gte", cross = TRUE)
2: do.call(sigFun, .formals)
1: applySignals(strategy = strategy.st, mktdata = GOOG)

根据我的研究和寻找解决方案 here,这是我的信号问题。

解决方案的 link 建议 head(mktdata) - 这是在 运行 applySignals() 行之后创建的。一种不同的策略(如果需要我可以添加代码)创建 mktdata 列名如下:

> colnames(mktdata)
[1] "SPY.Open"            "SPY.High"            "SPY.Low"            
[4] "SPY.Close"           "SPY.Volume"          "SPY.Adjusted"       
[7] "nonDerivedIndicator" "longEntry"           "longExit"

但是我使用 tidyquant 导入了数据,列名是;

> colnames(GOOG)
[1] "open"          "high"          "low"           "close"        
[5] "volume"        "adjusted"      "daily.returns" "predictions"

这是我面临的问题吗?只是一个列名问题?也就是说,quantmod 将数据导入为 SPY.Open 并且 quantstrat 是在考虑到这一点的情况下构建的,并期望这种格式而不是整洁的格式?还是我遇到了不同的错误?

量化代码:

currency('USD')
Sys.setenv(TZ="UTC")

stock("GOOG", currency="USD", multiplier=1)

symbols <- c("GOOG")


strategy.st <- portfolio.st <- account.st <- "SVMstrat"
rm.strat(strategy.st)
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(strategy.st, store=TRUE)

GOOG$predictions <- as.numeric(as.character(GOOG$predictions))
mean(GOOG$direction == GOOG$predictions)

add.signal(strategy.st, 
           name = sigThreshold, 
           arguments = list(column = "predictions", 
                            threshold = 0.5, 
                            relationship = "gte", 
                            cross = TRUE),
           label = "longEntry")

add.signal(strategy.st, 
           name = sigThreshold, 
           arguments = list(column = "predictions", 
                            threshold = 0.5, 
                            relationship = "lte", 
                            cross = TRUE),
           label = "longExit")

applySignals(strategy = strategy.st, mktdata=GOOG)

数据:

(Had to remove old data for new question)

编辑:

我已经重新运行模型,但仍然运行进入同样的错误:

colnames(GOOG) <- c("GOOG.Open", "GOOG.High", "GOOG.Low",
                    "GOOG.Close", "GOOG.Volume", "GOOG.Adjusted",
                    "daily.returns", "predictions")

回答我自己的问题。

看起来 quantstrat 接受 xts 个对象而 quantmod 产生 tibbles

我所要做的就是在 运行 实施策略之前将我的数据转换为 xts 对象。

GOOG <- xts(GOOG, order.by = GOOG$date)
GOOG$date <- NULL

编辑:

对于工作策略 (SPY),我得到以下 xts 结构输出。

An ‘xts’ object on 2003-01-02/2012-12-28 containing:
  Data: num [1:2516, 1:7] 72.9 74.6 74.8 76.2 75.6 ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:7] "SPY.Open" "SPY.High" "SPY.Low" "SPY.Close" ...
  Indexed by objects of class: [Date] TZ: UTC
  xts Attributes:  
List of 2
 $ src    : chr "yahoo"
 $ updated: POSIXct[1:1], format: "2018-12-09 12:43:49"

对于我的代码 - 使用 tidyquant 导入,添加一些指标,运行 SVM 模型,然后将其转换回 xts 我有以下结构输出。

An ‘xts’ object on 2017-07-28/2017-12-18 containing:
  Data: chr [1:100, 1:11] " 929.40" " 941.89" " 932.38" " 928.61" ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:11] "open" "high" "low" "close" ...
  Indexed by objects of class: [Date] TZ: UTC
  xts Attributes:  
 NULL

我错过了

List of 2
 $ src    : chr "yahoo"
 $ updated: POSIXct[1:1], format: "2018-12-09 12:43:49"

部分。