I know that this question has already been asked in this post (xts error – order.by requires an appropriate time-based object) but the problem seems to remain still unsolved.
I’m working on the same code as here : R: Error in xts – order.by, everything was working and computing perfectly until i restarted my computer and I’m now facing this problem :
I didn’t produce the code, I found it on this book : http://www.amazon.com/Data-Mining-Learning-Knowledge-Discovery/dp/1439810184/ref=sr_1_1?ie=UTF8&qid=1344349381&sr=8-1&keywords=data+mining+with+r
Here is the reproducible example :
# Packages needed :
library(xts)
library(TTR)
library(randomForest)
library(DMwR)
# Time Series :
myTimeSeries <-
structure(c(2787, 2800, 2788, 2803, 2815, 2815, 2812, 2807, 2810,
2829, 2830, 2837, 2841, 2840, 2843, 2839, 2835, 2841, 2834, 2838,
2827, 2821, 2831, 2811, 2796, 2808, 2814, 2811, 2815, 2803, 2788,
2778, 2772, 2777, 2776, 2760, 2732, 2711, 2709, 2707, 2700, 2706,
2706, 2690, 2684, 2654, 2637, 2656, 2655, 2670, 2652, 2649, 2621,
2622, 2599, 2612, 2632, 2653, 2668, 2654, 2637, 2633, 2639, 2648,
2641, 2663, 2657, 2655, 2639, 2639, 2645, 2658, 2658, 2669, 2664,
2658, 2667, 2671, 2679, 2670, 2693, 2708, 2715, 2704, 2695, 2697,
2688, 2706, 2716, 2719, 2707, 2705, 2706, 2698, 2693, 2699, 2701,
2699, 2713, 2716, 2721, 2714, 2710, 2706, 2701, 2691, 2663, 2658,
2692, 2694, 2704, 2706, 2714, 2702, 2698, 2688, 2677, 2682, 2677,
2688, 2669, 2667, 2674, 2684, 2673, 2682, 2694, 2690, 2691, 2695,
2703, 2697, 2699, 2694, 2696, 2679, 2688, 2687, 2688, 2691, 2799,
2801, 2804, 2822, 2820, 2819, 2814, 2816, 2836, 2836, 2838, 2846,
2842, 2847, 2847, 2842, 2844, 2842, 2840, 2840, 2833, 2834, 2832,
2815, 2810, 2819, 2814, 2818, 2815, 2815, 2789, 2779, 2782, 2781,
2778, 2764, 2740, 2720, 2713, 2709, 2709, 2707, 2706, 2697, 2688,
2661, 2657, 2660, 2672, 2682, 2662, 2651, 2629, 2623, 2614, 2650,
2657, 2670, 2671, 2658, 2640, 2640, 2650, 2654, 2664, 2674, 2660,
2656, 2646, 2641, 2663, 2663, 2678, 2676, 2664, 2672, 2677, 2684,
2689, 2698, 2727, 2723, 2716, 2717, 2703, 2708, 2707, 2726, 2722,
2727, 2711, 2713, 2707, 2704, 2705, 2708, 2705, 2704, 2718, 2729,
2727, 2719, 2715, 2713, 2704, 2691, 2680, 2698, 2699, 2712, 2711,
2710, 2714, 2714, 2705, 2689, 2685, 2685, 2694, 2690, 2677, 2680,
2693, 2685, 2689, 2690, 2694, 2704, 2700, 2706, 2704, 2702, 2702,
2698, 2697, 2690, 2690, 2690, 2693, 2695, 2785, 2777, 2784, 2802,
2808, 2811, 2806, 2805, 2802, 2823, 2828, 2832, 2835, 2837, 2838,
2824, 2834, 2826, 2830, 2827, 2816, 2814, 2798, 2796, 2796, 2807,
2806, 2808, 2800, 2787, 2768, 2763, 2770, 2772, 2756, 2729, 2709,
2694, 2698, 2699, 2697, 2689, 2690, 2653, 2651, 2635, 2634, 2649,
2633, 2648, 2632, 2615, 2618, 2597, 2599, 2612, 2632, 2645, 2647,
2631, 2627, 2616, 2636, 2630, 2640, 2655, 2652, 2635, 2634, 2622,
2643, 2641, 2658, 2658, 2652, 2656, 2664, 2666, 2667, 2652, 2689,
2707, 2704, 2703, 2694, 2685, 2686, 2699, 2711, 2704, 2693, 2697,
2681, 2689, 2690, 2697, 2696, 2695, 2709, 2707, 2705, 2707, 2683,
2689, 2687, 2652, 2655, 2657, 2690, 2691, 2703, 2702, 2700, 2693,
2687, 2668, 2669, 2675, 2675, 2658, 2655, 2664, 2674, 2667, 2673,
2676, 2677, 2689, 2691, 2694, 2696, 2697, 2693, 2692, 2676, 2678,
2686, 2686, 2688, 2690, 2799, 2787, 2803, 2814, 2816, 2812, 2808,
2810, 2830, 2830, 2836, 2841, 2840, 2840, 2839, 2836, 2841, 2834,
2838, 2827, 2821, 2831, 2811, 2797, 2808, 2813, 2810, 2813, 2803,
2788, 2778, 2772, 2777, 2776, 2759, 2732, 2712, 2709, 2706, 2699,
2705, 2691, 2690, 2684, 2654, 2637, 2655, 2654, 2671, 2652, 2649,
2622, 2622, 2599, 2611, 2646, 2653, 2668, 2654, 2636, 2634, 2639,
2648, 2642, 2664, 2658, 2655, 2638, 2640, 2624, 2658, 2659, 2669,
2665, 2659, 2667, 2671, 2678, 2670, 2693, 2707, 2715, 2704, 2706,
2696, 2688, 2706, 2716, 2720, 2708, 2705, 2705, 2697, 2693, 2699,
2701, 2699, 2702, 2716, 2721, 2714, 2710, 2706, 2701, 2690, 2663,
2658, 2692, 2694, 2705, 2706, 2708, 2702, 2697, 2688, 2677, 2681,
2677, 2687, 2670, 2667, 2674, 2685, 2674, 2682, 2689, 2690, 2691,
2696, 2703, 2698, 2699, 2694, 2697, 2680, 2687, 2687, 2690, 2691,
2695), .Dim = c(140L, 4L), .Dimnames = list(NULL, c("price.Open",
"price.High", "price.Low", "price.Close")), index = structure(c(1265097599,
1265101199, 1265104796, 1265108399, 1265111999, 1265115597, 1265119198,
1265122799, 1265126399, 1265129992, 1265133591, 1265137199, 1265140797,
1265144399, 1265183999, 1265187599, 1265191199, 1265194799, 1265198398,
1265201999, 1265205599, 1265209199, 1265212799, 1265216399, 1265219999,
1265223580, 1265227194, 1265230799, 1265270399, 1265273999, 1265277595,
1265281198, 1265284798, 1265288397, 1265291998, 1265295599, 1265299199,
1265302799, 1265306399, 1265309997, 1265313586, 1265317199, 1265356799,
1265360399, 1265363999, 1265367599, 1265371199, 1265374799, 1265378399,
1265381999, 1265385599, 1265389199, 1265392798, 1265396399, 1265399984,
1265403599, 1265615999, 1265619593, 1265623195, 1265626785, 1265630396,
1265633999, 1265637599, 1265641199, 1265644799, 1265648398, 1265651994,
1265655599, 1265659198, 1265662799, 1265702399, 1265705999, 1265709599,
1265713199, 1265716798, 1265720398, 1265723995, 1265727598, 1265731199,
1265734799, 1265738399, 1265741975, 1265745594, 1265749199, 1265788799,
1265792399, 1265795999, 1265799599, 1265803197, 1265806799, 1265810399,
1265813999, 1265817599, 1265821196, 1265824795, 1265828380, 1265831990,
1265835598, 1265875199, 1265878799, 1265882399, 1265885999, 1265889599,
1265893199, 1265896797, 1265900399, 1265903998, 1265907586, 1265911187,
1265914799, 1265918356, 1265921999, 1265961596, 1265965199, 1265968798,
1265972399, 1265975998, 1265979599, 1265983196, 1265986798, 1265990399,
1265993997, 1265997598, 1266001197, 1266004796, 1266008399, 1266220799,
1266224399, 1266227999, 1266231598, 1266235187, 1266238799, 1266242399,
1266245996, 1266249598, 1266253195, 1266256799, 1266260385, 1266263956,
1266267599), tzone = "", tclass = c("POSIXct", "POSIXt")),
tclass = c("POSIXct", "POSIXt"), tzone = "", class = c("xts", "zoo"),
.indexCLASS = c("POSIXct", "POSIXt"), .indexTZ = "")
T.ind <- function(quotes,tgt.margin=0.005,n.days=5){
v <- apply(HLC(quotes),1,mean)
r <- matrix(NA,ncol=n.days,nrow=NROW(quotes))
for(x in 1:n.days) r[,x] <- Next(Delt(v,k=x),x)
x <- apply(r,1,function(x) sum(x[x > tgt.margin | x < -tgt.margin]))
if (is.xts(quotes)) xts(x,time(quotes)) else x
}
# Candle Chart and new indicator created above :
candleChart(last(myTimeSeries,"2 days"))
avgPrice<-function(p) apply(HLC(p),1,mean)
addAvgPrice<-newTA(FUN=avgPrice,col=1,legend="Average Price")
addT.indicator<-newTA(FUN=T.indicator,col="red",legend="Target")
addAvgPrice(on=1)
addT.indicator()
and the R sessionInfo() :
R version 2.15.0 (2012-03-30)
Platform: i386-pc-mingw32/i386 (32-bit)
locale:
[1] LC_COLLATE=French_France.1252 LC_CTYPE=French_France.1252 LC_MONETARY=French_France.1252 LC_NUMERIC=C LC_TIME=French_France.1252
attached base packages:
[1] splines grid stats graphics grDevices utils datasets methods base
other attached packages:
[1] boot_1.3-4 car_2.0-12 nnet_7.3-4 dynamo_0.1.3 fda_2.2.8 Matrix_1.0-6 Formula_1.1-0 gstat_1.0-13 lmtest_0.9-30 spatial_7.3-3 spacetime_0.7-1 sp_0.9-99 sm_2.2-4.1 tseries_0.10-28
[15] quadprog_1.5-4 DMwR_0.2.3 lattice_0.20-6 cluster_1.14.2 abind_1.4-0 rpart_3.1-52 class_7.3-3 ROCR_1.0-4 gplots_2.11.0 KernSmooth_2.23-7 caTools_1.13 bitops_1.0-4.1 gdata_2.11.0 gtools_2.7.0
[29] quantmod_0.3-17 Defaults_1.1-1 randomForest_4.6-6 fExtremes_2100.77 fTrading_2100.76 fGarch_2110.80.1 fBasics_2160.81 timeSeries_2160.94 TTR_0.21-1 fractal_1.1-1 scatterplot3d_0.3-33 akima_0.5-7 wmtsa_1.1-1 sapa_1.1-0
[43] ifultools_1.1-2 MASS_7.3-17 splus2R_1.1-1 chron_2.3-42 RTAQ_0.2 timeDate_2160.95 xts_0.8-6 zoo_1.7-7 rattle_2.6.20
loaded via a namespace (and not attached):
[1] stabledist_0.6-4 tools_2.15.0
Your
forloop makes no sense:xis the iterator, and the object you’re assigning to, and the argument to the anonymous function in yourapplycall, and the object you’re subsetting in yoursumcall, and the object you’re comparing totarget. I don’t even want to think about how this possibly worked.In short, you need better variable names/definitions.
If you were trying to change the value of
x(the iterator), you can’t. See the third paragraph in the Details section of?"for".UPDATE:
Here’s the function from Luis’ code you said you’re using. You added extra braces around the for loop, which then included the second-to-last line of the function in the for loop…