我真的很难为移动平均分频器重复计算。这是我的尝试:
ma.crossover = function(x){
for(i in seq(1, length(MergedData))) {
ma20 <- SMA(Cl(MergedData[[i]]), n=20)
ma50 <- SMA(Cl(MergedData[[i]]), n=50)
merge_20_50 = na.omit(merge(ma20[50:length(ma20)], ma50))
sma.sig <- ifelse(merge20_50[,1]>merge20_50[,2], 1, ifelse(merge20_50[,1]==merge20_50[,2], 0, -1))
}
}
然后我最终得到了:
>ma <- ma.crossover(MergedData)
NULL
我的数据如下:
[[1]]
Date Eur.high Eur.low Eur.close
2016-08-30 1.1192 1.1153 1.1161
2016-08-29 1.1208 1.1158 1.1189
2016-08-28 1.1341 1.1181 1.1198
2016-08-27 1.1341 1.1181 1.1198
2016-08-26 1.1341 1.1181 1.1198
[[2]]
Date GBP.high GBP.low GBP.close
2016-08-30 1.3120 1.3060 1.3105
2016-08-29 1.3172 1.3060 1.3106
2016-08-28 1.3279 1.3122 1.3137
2016-08-27 1.3279 1.3122 1.3137
2016-08-26 1.3279 1.3122 1.3137
2016-08-25 1.3264 1.3169 1.3192
这一直持续到[[29]]。我的输入看起来像:
structure(list(Date = structure(c(1472533200, 1472446800, 1472360400,
1472274000, 1472187600, 1472101200, 1472014800, 1471928400, 1471842000,
1471755600, 1471669200, 1471582800, 1471496400, 1471410000, 1471323600,
1471237200, 1471150800, 1471064400, 1470978000, 1470891600, 1470805200,
1470718800, 1470632400, 1470546000, 1470459600, 1470373200, 1470286800,
1470200400, 1470114000, 1470027600, 1469941200, 1469854800, 1469768400,
1469682000, 1469595600, 1469509200, 1469422800, 1469336400, 1469250000,
1469163600, 1469077200, 1468990800, 1468904400, 1468818000, 1468731600,
1468645200, 1468558800, 1468472400, 1468386000, 1468299600, 1468213200,
1468126800, 1468040400, 1467954000, 1467867600, 1467781200, 1467694800,
1467608400, 1467522000, 1467435600, 1467349200, 1467262800, 1467176400,
1467090000, 1467003600, 1466917200, 1466830800, 1466744400, 1466658000,
1466571600, 1466485200, 1466398800, 1466312400, 1466226000, 1466139600,
1466053200, 1465966800, 1465880400, 1465794000, 1465707600, 1465621200,
1465534800, 1465448400, 1465362000, 1465275600, 1465189200, 1465102800,
1465016400, 1464930000, 1464843600, 1464757200, 1464670800, 1464584400,
1464498000, 1464411600, 1464325200, 1464238800, 1464152400, 1464066000,
1463979600, 1463893200, 1463806800, 1463720400, 1463634000, 1463547600,
1463461200, 1463374800, 1463288400, 1463202000, 1463115600, 1463029200,
1462942800, 1462856400, 1462770000, 1462683600, 1462597200, 1462510800,
1462424400, 1462338000, 1462251600, 1462165200, 1462078800, 1461992400,
1461906000, 1461819600, 1461733200, 1461646800, 1461560400, 1461474000,
1461387600, 1461301200, 1461214800, 1461128400, 1461042000, 1460955600,
1460869200, 1460782800, 1460696400, 1460610000, 1460523600, 1460437200,
1460350800, 1460264400, 1460178000, 1460091600, 1460005200, 1459918800,
1459832400, 1459746000, 1459659600, 1459573200, 1459486800, 1459400400,
1459314000, 1459227600, 1459141200, 1459054800, 1458968400, 1458882000,
1458795600, 1458709200, 1458622800, 1458536400, 1458450000, 1458363600,
1458277200, 1458190800, 1458104400, 1458018000, 1457931600, 1457848800,
1457762400, 1457676000, 1457589600, 1457503200, 1457416800, 1457330400,
1457244000, 1457157600, 1457071200, 1456984800, 1456898400, 1456812000,
1456725600, 1456639200, 1456552800, 1456466400, 1456380000, 1456293600,
1456207200, 1456120800, 1456034400, 1455948000, 1455861600, 1455775200,
1455688800, 1455602400, 1455516000, 1455429600, 1455343200, 1455256800,
1455170400, 1455084000, 1454997600, 1454911200, 1454824800, 1454738400,
1454652000, 1454565600, 1454479200, 1454392800, 1454306400, 1454220000,
1454133600, 1454047200, 1453960800, 1453874400, 1453788000, 1453701600,
1453615200, 1453528800, 1453442400, 1453356000, 1453269600, 1453183200,
1453096800, 1453010400, 1452924000, 1452837600, 1452751200, 1452664800,
1452578400, 1452492000, 1452405600, 1452319200, 1452232800, 1452146400,
1452060000, 1451973600, 1451887200, 1451800800, 1451714400, 1451628000
), tzone = "", class = c("POSIXct", "POSIXt")), EUR.HIGH = c(1.1192,
1.1208, 1.1341, 1.1341, 1.1341, 1.1298, 1.1312, 1.1355, 1.1334,
1.136, 1.136, 1.136, 1.1366, 1.1316, 1.1323, 1.1204, 1.1221,
1.1221, 1.1221, 1.1192, 1.119, 1.1123, 1.1105, 1.1161, 1.1161,
1.1161, 1.1156, 1.1227, 1.1234, 1.1184, 1.1197, 1.1197, 1.1197,
1.1119, 1.1065, 1.103, 1.0999, 1.1041, 1.1041, 1.1041, 1.106,
1.103, 1.1081, 1.1084, 1.1149, 1.1149, 1.1149, 1.1165, 1.112,
1.1126, 1.1075, 1.112, 1.112, 1.112, 1.1107, 1.1112, 1.1186,
1.116, 1.1169, 1.1169, 1.1169, 1.1155, 1.113, 1.1112, 1.1084,
1.1428, 1.1428, 1.1428, 1.1421, 1.1338, 1.135, 1.1382, 1.1296,
1.1296, 1.1296, 1.1295, 1.1298, 1.1298, 1.1303, 1.1321, 1.1321,
1.1321, 1.1416, 1.1411, 1.1381, 1.1393, 1.1374, 1.1374, 1.1374,
1.122, 1.1194, 1.1173, 1.1145, 1.1201, 1.1201, 1.1201, 1.1217,
1.1167, 1.1227, 1.1243, 1.1237, 1.1237, 1.1237, 1.123, 1.1316,
1.1349, 1.1342, 1.138, 1.138, 1.138, 1.1429, 1.1447, 1.141, 1.142,
1.1479, 1.1479, 1.1479, 1.1494, 1.1529, 1.1616, 1.1536, 1.1459,
1.1459, 1.1459, 1.1368, 1.1362, 1.134, 1.1278, 1.1309, 1.1309,
1.1309, 1.1398, 1.1388, 1.1385, 1.1332, 1.1317, 1.1317, 1.1317,
1.1295, 1.1391, 1.1465, 1.1447, 1.1419, 1.1419, 1.1419, 1.1454,
1.1432, 1.1405, 1.1413, 1.1438, 1.1438, 1.1438, 1.1412, 1.1365,
1.1303, 1.122, 1.1181, 1.1181, 1.1181, 1.1188, 1.1224, 1.126,
1.1288, 1.1337, 1.1337, 1.1337, 1.1342, 1.1242, 1.1125, 1.1176,
1.121, 1.121, 1.121, 1.1218, 1.1035, 1.1058, 1.1026, 1.1043,
1.1043, 1.1043, 1.0973, 1.0881, 1.0894, 1.0963, 1.1068, 1.1068,
1.1068, 1.105, 1.1046, 1.1053, 1.1135, 1.1139, 1.1139, 1.1139,
1.115, 1.1179, 1.1193, 1.1261, 1.1334, 1.1334, 1.1334, 1.1376,
1.1311, 1.1338, 1.1216, 1.1246, 1.1246, 1.1246, 1.1239, 1.1146,
1.094, 1.0913, 1.0949, 1.0949, 1.0949, 1.0968, 1.0917, 1.0874,
1.0857, 1.0877, 1.0877, 1.0877, 1.0921, 1.0976, 1.0939, 1.0942,
1.0985, 1.0985, 1.0985, 1.0943, 1.0888, 1.09, 1.097, 1.0934,
1.0934, 1.0934, 1.094, 1.0799, 1.0839, 1.0946, 1.0867, 1.0867,
1.0867), EUR.LOW = c(1.1153, 1.1158, 1.1181, 1.1181, 1.1181,
1.1259, 1.1245, 1.1303, 1.1271, 1.1304, 1.1304, 1.1304, 1.1286,
1.1242, 1.1178, 1.1154, 1.1131, 1.1131, 1.1131, 1.1136, 1.1113,
1.1071, 1.1072, 1.1046, 1.1046, 1.1046, 1.1114, 1.1141, 1.1159,
1.1155, 1.1073, 1.1073, 1.1073, 1.1053, 1.0961, 1.0978, 1.0952,
1.0956, 1.0956, 1.0956, 1.098, 1.0982, 1.1, 1.1038, 1.1025, 1.1025,
1.1025, 1.1088, 1.1042, 1.1052, 1.1016, 1.1002, 1.1002, 1.1002,
1.1053, 1.1029, 1.1062, 1.1098, 1.1072, 1.1072, 1.1072, 1.1024,
1.1049, 1.101, 1.0971, 1.0913, 1.0913, 1.0913, 1.1296, 1.1237,
1.1242, 1.1281, 1.1223, 1.1223, 1.1223, 1.1131, 1.119, 1.1189,
1.1232, 1.1246, 1.1246, 1.1246, 1.1306, 1.1355, 1.1339, 1.1326,
1.1137, 1.1137, 1.1137, 1.1145, 1.1114, 1.1122, 1.1098, 1.1111,
1.1111, 1.1111, 1.115, 1.1129, 1.1133, 1.1188, 1.1196, 1.1196,
1.1196, 1.118, 1.1214, 1.1302, 1.1302, 1.1283, 1.1283, 1.1283,
1.1371, 1.1369, 1.1359, 1.1376, 1.1387, 1.1387, 1.1387, 1.1386,
1.1467, 1.1496, 1.1431, 1.1348, 1.1348, 1.1348, 1.1296, 1.1272,
1.1256, 1.1217, 1.1218, 1.1218, 1.1218, 1.127, 1.1291, 1.1304,
1.1274, 1.1246, 1.1246, 1.1246, 1.1234, 1.1268, 1.1346, 1.1372,
1.1349, 1.1349, 1.1349, 1.1338, 1.1327, 1.1336, 1.1358, 1.1335,
1.1335, 1.1335, 1.131, 1.1284, 1.1169, 1.1153, 1.1154, 1.1154,
1.1154, 1.1144, 1.116, 1.1189, 1.1235, 1.1256, 1.1256, 1.1256,
1.1206, 1.1058, 1.1072, 1.1078, 1.108, 1.108, 1.108, 1.0822,
1.0946, 1.0994, 1.094, 1.0904, 1.0904, 1.0904, 1.0854, 1.0826,
1.0834, 1.0859, 1.0912, 1.0912, 1.0912, 1.0987, 1.0957, 1.099,
1.1003, 1.1067, 1.1067, 1.1067, 1.1071, 1.1106, 1.1124, 1.1128,
1.1214, 1.1214, 1.1214, 1.1274, 1.1161, 1.1162, 1.1087, 1.1109,
1.1109, 1.1109, 1.107, 1.0904, 1.0886, 1.0815, 1.081, 1.081,
1.081, 1.087, 1.0851, 1.0819, 1.079, 1.0789, 1.0789, 1.0789,
1.0778, 1.0877, 1.086, 1.0869, 1.0855, 1.0855, 1.0855, 1.0835,
1.0805, 1.082, 1.0848, 1.0803, 1.0803, 1.0803, 1.0771, 1.0716,
1.0711, 1.0781, 1.0854, 1.0854, 1.0854), EUR.CLOSE = c(1.1161,
1.1189, 1.1198, 1.1198, 1.1198, 1.1285, 1.1264, 1.1305, 1.132,
1.1325, 1.1325, 1.1325, 1.1354, 1.1289, 1.1279, 1.1184, 1.1162,
1.1162, 1.1162, 1.1137, 1.1176, 1.1117, 1.1088, 1.1086, 1.1086,
1.1086, 1.113, 1.1149, 1.1224, 1.1163, 1.1174, 1.1174, 1.1174,
1.1077, 1.1058, 1.0986, 1.0995, 1.0977, 1.0977, 1.0977, 1.1026,
1.1015, 1.1021, 1.1075, 1.1035, 1.1035, 1.1035, 1.112, 1.109,
1.1061, 1.1058, 1.1051, 1.1051, 1.1051, 1.1063, 1.11, 1.1076,
1.1154, 1.1136, 1.1136, 1.1136, 1.1106, 1.1125, 1.1065, 1.1025,
1.1117, 1.1117, 1.1117, 1.1385, 1.1296, 1.1242, 1.1314, 1.1277,
1.1277, 1.1277, 1.1225, 1.126, 1.1207, 1.1292, 1.1251, 1.1251,
1.1251, 1.1316, 1.1395, 1.1358, 1.1355, 1.1367, 1.1367, 1.1367,
1.1151, 1.1188, 1.1132, 1.1138, 1.1115, 1.1115, 1.1115, 1.1194,
1.1155, 1.1141, 1.122, 1.1224, 1.1224, 1.1224, 1.1203, 1.1216,
1.1313, 1.1321, 1.1309, 1.1309, 1.1309, 1.1377, 1.1426, 1.1372,
1.1383, 1.1404, 1.1404, 1.1404, 1.1405, 1.1487, 1.1496, 1.1534,
1.1451, 1.1451, 1.1451, 1.1352, 1.1322, 1.1297, 1.1268, 1.1222,
1.1222, 1.1222, 1.1288, 1.1297, 1.1358, 1.1313, 1.1284, 1.1284,
1.1284, 1.1268, 1.1274, 1.1386, 1.1408, 1.1399, 1.1399, 1.1399,
1.1378, 1.1399, 1.1384, 1.1391, 1.1391, 1.1391, 1.1391, 1.138,
1.1338, 1.1291, 1.1196, 1.1167, 1.1167, 1.1167, 1.1174, 1.1181,
1.1217, 1.1241, 1.127, 1.127, 1.127, 1.1318, 1.1224, 1.1109,
1.1103, 1.1156, 1.1156, 1.1156, 1.1177, 1.0999, 1.1011, 1.1014,
1.1005, 1.1005, 1.1005, 1.0957, 1.0868, 1.0868, 1.0873, 1.0934,
1.0934, 1.0934, 1.1018, 1.1013, 1.102, 1.103, 1.113, 1.113, 1.113,
1.1107, 1.1128, 1.1144, 1.1156, 1.1256, 1.1256, 1.1256, 1.1323,
1.1292, 1.1293, 1.1193, 1.1158, 1.1158, 1.1158, 1.1209, 1.1105,
1.0919, 1.0888, 1.0831, 1.0831, 1.0831, 1.094, 1.0893, 1.087,
1.0849, 1.0796, 1.0796, 1.0796, 1.0874, 1.089, 1.0908, 1.0892,
1.0916, 1.0916, 1.0916, 1.0865, 1.0877, 1.0858, 1.0859, 1.0922,
1.0922, 1.0922, 1.0932, 1.0781, 1.0748, 1.0831, 1.0856, 1.0856,
1.0856)), .Names = c("Date", "EUR.HIGH", "EUR.LOW", "EUR.CLOSE"
), row.names = c(NA, -243L), class = "data.frame")
structure(list(Date = structure(c(1472533200, 1472446800, 1472360400,
1472274000, 1472187600, 1472101200, 1472014800, 1471928400, 1471842000,
1471755600, 1471669200, 1471582800, 1471496400, 1471410000, 1471323600,
1471237200, 1471150800, 1471064400, 1470978000, 1470891600, 1470805200,
1470718800, 1470632400, 1470546000, 1470459600, 1470373200, 1470286800,
1470200400, 1470114000, 1470027600, 1469941200, 1469854800, 1469768400,
1469682000, 1469595600, 1469509200, 1469422800, 1469336400, 1469250000,
1469163600, 1469077200, 1468990800, 1468904400, 1468818000, 1468731600,
1468645200, 1468558800, 1468472400, 1468386000, 1468299600, 1468213200,
1468126800, 1468040400, 1467954000, 1467867600, 1467781200, 1467694800,
1467608400, 1467522000, 1467435600, 1467349200, 1467262800, 1467176400,
1467090000, 1467003600, 1466917200, 1466830800, 1466744400, 1466658000,
1466571600, 1466485200, 1466398800, 1466312400, 1466226000, 1466139600,
1466053200, 1465966800, 1465880400, 1465794000, 1465707600, 1465621200,
1465534800, 1465448400, 1465362000, 1465275600, 1465189200, 1465102800,
1465016400, 1464930000, 1464843600, 1464757200, 1464670800, 1464584400,
1464498000, 1464411600, 1464325200, 1464238800, 1464152400, 1464066000,
1463979600, 1463893200, 1463806800, 1463720400, 1463634000, 1463547600,
1463461200, 1463374800, 1463288400, 1463202000, 1463115600, 1463029200,
1462942800, 1462856400, 1462770000, 1462683600, 1462597200, 1462510800,
1462424400, 1462338000, 1462251600, 1462165200, 1462078800, 1461992400,
1461906000, 1461819600, 1461733200, 1461646800, 1461560400, 1461474000,
1461387600, 1461301200, 1461214800, 1461128400, 1461042000, 1460955600,
1460869200, 1460782800, 1460696400, 1460610000, 1460523600, 1460437200,
1460350800, 1460264400, 1460178000, 1460091600, 1460005200, 1459918800,
1459832400, 1459746000, 1459659600, 1459573200, 1459486800, 1459400400,
1459314000, 1459227600, 1459141200, 1459054800, 1458968400, 1458882000,
1458795600, 1458709200, 1458622800, 1458536400, 1458450000, 1458363600,
1458277200, 1458190800, 1458104400, 1458018000, 1457931600, 1457848800,
1457762400, 1457676000, 1457589600, 1457503200, 1457416800, 1457330400,
1457244000, 1457157600, 1457071200, 1456984800, 1456898400, 1456812000,
1456725600, 1456639200, 1456552800, 1456466400, 1456380000, 1456293600,
1456207200, 1456120800, 1456034400, 1455948000, 1455861600, 1455775200,
1455688800, 1455602400, 1455516000, 1455429600, 1455343200, 1455256800,
1455170400, 1455084000, 1454997600, 1454911200, 1454824800, 1454738400,
1454652000, 1454565600, 1454479200, 1454392800, 1454306400, 1454220000,
1454133600, 1454047200, 1453960800, 1453874400, 1453788000, 1453701600,
1453615200, 1453528800, 1453442400, 1453356000, 1453269600, 1453183200,
1453096800, 1453010400, 1452924000, 1452837600, 1452751200, 1452664800,
1452578400, 1452492000, 1452405600, 1452319200, 1452232800, 1452146400,
1452060000, 1451973600, 1451887200, 1451800800, 1451714400, 1451628000
), tzone = "", class = c("POSIXct", "POSIXt")), JPY.HIGH = c(102.56,
102.39, 101.94, 101.94, 101.94, 100.62, 100.61, 100.39, 100.93,
100.46, 100.46, 100.46, 100.5, 101.17, 101.29, 101.45, 102.27,
102.27, 102.27, 102.06, 101.96, 102.53, 102.66, 102.06, 102.06,
102.06, 101.67, 101.57, 102.83, 102.68, 105.63, 105.63, 105.63,
105.51, 106.54, 105.89, 106.72, 106.4, 106.4, 106.4, 107.49,
107.02, 106.53, 106.26, 106.32, 106.32, 106.32, 105.94, 104.88,
104.99, 102.89, 101.28, 101.28, 101.28, 101.41, 101.77, 102.59,
102.8, 103.39, 103.39, 103.39, 103.29, 102.94, 102.84, 102.48,
106.84, 106.84, 106.84, 106.17, 104.85, 105.06, 104.85, 104.83,
104.83, 104.83, 106.03, 106.4, 106.42, 106.98, 107.26, 107.26,
107.26, 107.18, 107.39, 107.9, 107.66, 109.14, 109.14, 109.14,
109.59, 110.83, 111.35, 111.45, 110.45, 110.45, 110.45, 110.23,
110.45, 110.13, 110.24, 110.59, 110.59, 110.59, 110.38, 110.26,
109.65, 109.1, 109.56, 109.56, 109.56, 109.4, 109.38, 109.35,
108.6, 107.42, 107.42, 107.42, 107.5, 107.46, 106.68, 106.82,
108.2, 108.2, 108.2, 111.88, 111.75, 111.47, 111.91, 111.81,
111.81, 111.81, 109.9, 109.88, 109.49, 108.99, 109.73, 109.73,
109.73, 109.55, 109.41, 108.79, 108.44, 109.1, 109.1, 109.1,
109.9, 110.64, 111.36, 111.8, 112.58, 112.58, 112.58, 112.66,
112.81, 113.8, 113.69, 113.32, 113.32, 113.32, 113.01, 112.91,
112.49, 111.98, 111.76, 111.76, 111.76, 112.96, 113.82, 114.14,
114.01, 113.92, 113.92, 113.92, 114.45, 113.45, 113.52, 114.09,
114.26, 114.26, 114.26, 114.27, 114.56, 114.19, 113.99, 114,
114, 114, 113.02, 112.27, 113.05, 113.39, 113.38, 113.38, 113.38,
114.33, 114.51, 114.87, 114.73, 113.54, 113.54, 113.54, 113.6,
115.26, 115.85, 117.53, 117.43, 117.43, 117.43, 118.24, 120.04,
121.04, 121.49, 121.69, 121.69, 121.69, 118.99, 119.07, 118.62,
118.86, 118.88, 118.88, 118.88, 117.81, 117.69, 118.11, 117.44,
118.27, 118.27, 118.27, 118.28, 118.38, 118.07, 118.02, 118.83,
118.83, 118.83, 118.76, 119.17, 119.7, 120.47, 120.55, 120.55,
120.55), JPY.LOW = c(101.76, 101.84, 100.06, 100.06, 100.06,
100.3, 100.1, 99.94, 100.21, 99.88, 99.88, 99.88, 99.65, 100.04,
99.54, 100.87, 100.83, 100.83, 100.83, 101.03, 100.97, 101.79,
101.72, 100.87, 100.87, 100.87, 100.86, 100.75, 100.68, 102.01,
101.97, 101.97, 101.97, 104.49, 104.63, 103.99, 105.74, 105.57,
105.57, 105.57, 105.42, 105.83, 105.65, 105.16, 104.64, 104.64,
104.64, 103.98, 103.91, 102.45, 100.31, 100, 100, 100, 100.6,
100.2, 101.45, 102.43, 102.44, 102.44, 102.44, 102.36, 102.18,
101.56, 101.41, 99.02, 99.02, 99.02, 104.04, 104.32, 103.58,
103.79, 104.08, 104.08, 104.08, 103.55, 105.44, 105.63, 105.74,
106.56, 106.56, 106.56, 106.26, 106.59, 107.15, 106.38, 106.51,
106.51, 106.51, 108.53, 109.05, 110.5, 110.32, 109.48, 109.48,
109.48, 109.42, 109.87, 109.17, 109.11, 109.85, 109.85, 109.85,
109.7, 108.72, 108.84, 108.47, 108.52, 108.52, 108.52, 108.23,
108.37, 108.28, 107.03, 106.44, 106.44, 106.44, 106.81, 106.25,
105.55, 106.14, 106.28, 106.28, 106.28, 107.87, 111.03, 110.67,
110.84, 109.26, 109.26, 109.26, 109.33, 108.76, 108.75, 107.77,
108.6, 108.6, 108.6, 108.9, 108.51, 107.87, 107.63, 108.04, 108.04,
108.04, 107.67, 109.34, 109.95, 111.1, 111.59, 111.59, 111.59,
112.11, 112.02, 112.61, 113.05, 112.76, 112.76, 112.76, 112.3,
112.14, 111.38, 111.22, 110.82, 110.82, 110.82, 110.67, 112.33,
112.63, 113.51, 112.76, 112.76, 112.76, 112.61, 112.23, 112.43,
113.23, 113.13, 113.13, 113.13, 113.3, 113.22, 112.16, 112.66,
112.56, 112.56, 112.56, 111.9, 111.04, 111.78, 112.36, 112.31,
112.31, 112.31, 113.14, 113.38, 113.6, 113.08, 111.67, 111.67,
111.67, 110.99, 113.12, 114.21, 115.18, 116.3, 116.3, 116.3,
116.53, 117.06, 119.84, 120.68, 118.5, 118.5, 118.5, 118.42,
118.04, 117.66, 118.17, 117.54, 117.54, 117.54, 116.47, 115.98,
117.23, 116.57, 116.51, 116.51, 116.51, 117.29, 117.64, 117.23,
116.7, 117.22, 117.22, 117.22, 117.33, 118.25, 118.79, 118.7,
120.2, 120.2, 120.2), JPY.CLOSE = c(102.5, 101.92, 101.84, 101.84,
101.84, 100.53, 100.45, 100.24, 100.33, 100.22, 100.22, 100.22,
99.89, 100.28, 100.31, 101.26, 101.3, 101.3, 101.3, 101.96, 101.29,
101.88, 102.45, 101.82, 101.82, 101.82, 101.22, 101.24, 100.89,
102.39, 102.06, 102.06, 102.06, 105.27, 105.4, 104.66, 105.81,
106.13, 106.13, 106.13, 105.82, 106.89, 106.12, 106.16, 104.88,
104.88, 104.88, 105.35, 104.49, 104.69, 102.8, 100.54, 100.54,
100.54, 100.77, 101.32, 101.74, 102.56, 102.52, 102.52, 102.52,
103.2, 102.83, 102.75, 102, 102.22, 102.22, 102.22, 106.16, 104.41,
104.75, 103.94, 104.16, 104.16, 104.16, 104.26, 106.01, 106.11,
106.26, 106.97, 106.97, 106.97, 107.1, 106.99, 107.37, 107.56,
106.53, 106.53, 106.53, 108.87, 109.54, 110.73, 111.12, 110.31,
110.31, 110.31, 109.76, 110.19, 109.99, 109.24, 110.15, 110.15,
110.15, 109.96, 110.19, 109.14, 109.03, 108.63, 108.63, 108.63,
109.02, 108.41, 109.27, 108.32, 107.12, 107.12, 107.12, 107.26,
107.01, 106.6, 106.41, 106.5, 106.5, 106.5, 108.11, 111.46, 111.31,
111.2, 111.79, 111.79, 111.79, 109.46, 109.84, 109.21, 108.82,
108.76, 108.76, 108.76, 109.4, 109.34, 108.54, 107.94, 108.07,
108.07, 108.07, 108.21, 109.79, 110.34, 111.34, 111.69, 111.69,
111.69, 112.57, 112.43, 112.7, 113.45, 113.08, 113.08, 113.08,
112.9, 112.38, 112.37, 111.95, 111.55, 111.55, 111.55, 111.39,
112.56, 113.18, 113.82, 113.86, 113.86, 113.86, 113.19, 113.35,
112.62, 113.46, 113.74, 113.74, 113.74, 113.69, 113.48, 114.01,
112.69, 114, 114, 114, 113, 112.18, 112.1, 112.92, 112.63, 112.63,
112.63, 113.24, 114.1, 114.07, 114.6, 113.25, 113.25, 113.25,
112.42, 113.35, 115.11, 115.85, 116.87, 116.87, 116.87, 116.78,
117.9, 119.97, 120.99, 121.14, 121.14, 121.14, 118.82, 118.68,
118.42, 118.3, 118.78, 118.78, 118.78, 117.7, 116.94, 117.64,
117.32, 116.98, 116.98, 116.98, 118.06, 117.68, 117.65, 117.76,
117.26, 117.26, 117.26, 117.67, 118.47, 119.06, 119.44, 120.55,
120.55, 120.55)), .Names = c("Date", "JPY.HIGH", "JPY.LOW", "JPY.CLOSE"
), row.names = c(NA, -243L), class = "data.frame")
我想计算所有货币的交叉移动平均线,但我想尝试使用带有for()循环的函数,所以我不必在R中写出相同的东西29次。不幸的是,我真的只对R有非常基本的了解。
这是我尝试为一种货币做的示例代码:
Eur = MergedData[[1]]
dataxts<-xts(Eur[2:4], order.by=Eur$Date)
ma20 <- SMA(Cl(dataxts), n=20)
ma50 <- SMA(Cl(dataxts), n=50)
merge20_50 <- na.omit(merge(ma20[50:length(ma20)], ma50))
##Signals (1 cross up, 0 no cross, -1 cross down)
sig <- ifelse(merge20_50[,1]>merge20_50[,2], 1, ifelse(merge10_20[,1]==merge10_20[,2], 0, -1))
然后返回:
SMA
2016-02-19 1
2016-02-20 -1
2016-02-21 0
2016-02-22 1
2016-02-23 1
我想知道是否有一种不那么繁琐的方法来为所有29种不同的货币编码,如果有办法将所有信号合并到一个表中,那么我有类似的东西:
Eur.SMA GBP.SMA
2016-02-19 1 0
2016-02-20 -1 0
2016-02-21 0 1
非常感谢帮助!
编辑:我已在此处上传了整个dput文件,以防有人发现有用https://drive.google.com/file/d/0ByfLu_yIza89Snk0VzA1N3AxTDQ/view?usp=sharing
答案 0 :(得分:1)
(提示:lapply
是一种在列表上编写for循环的简洁方法。)
试试这个:
ma.crossover = function(d) {
ma20 <- SMA(Cl(d), n=20)
ma50 <- SMA(Cl(d), n=50)
sma.sig <- na.omit(sign(ma20 - ma50))
}
# Convert data.frames to xts objects
MergedData = lapply(MergedData, function(d) xts(d[,-1], order.by=d[,1]))
Signals = lapply(MergedData, ma.crossover)
Signals = do.call(merge.xts, Signals)