[R-SIG-Finance] Quantstrat stoplimit orders not triggered
Heiko Stegmann
h.stegmann at onlinehome.de
Thu Jul 21 22:47:46 CEST 2011
Too bad... I didn't know the stoplimit orders don't work correctly.
Anyway, here's what I tried to do:
#----------------------------------------------------------------------
require(quantstrat)
library(timeDate)
# Clean up
rm(list=ls(all=TRUE, pos=.blotter), pos=.blotter)
rm(list=ls(all=TRUE, pos=.instrument), pos=.instrument)
rm(list=ls(all=TRUE, pos=.strategy), pos=.strategy)
# Custom indicator, returns last bar's Close price
LastClose <- function(price) {
temp <- Cl(price)
colnames(temp) <- "LastClose"
temp
}
# Set initial values
initDate <- '1999-01-04 00:00'
initEq <- 10000
# Set up instruments
currency("USD")
symbols = c("EURUSD")
for(symbol in symbols){
stock("EURUSD","USD")
}
# Load data
EURUSD <- read.csv("EURUSD240.csv", stringsAsFactors=F, header=F)
EURUSD_datetimes <- timeDate(paste(EURUSD[,1], EURUSD[,2]), format =
"%Y.%m.%d %H:%M")
EURUSD <- xts(EURUSD[3:7], EURUSD_datetimes)
getSymbols(symbols, src="csv")
for(symbol in symbols) {
x <- get(symbol)
x <- to.hourly(x, indexAt='firstof', drop.time=FALSE)
indexFormat(x) <- '%Y-%m-%d %H:%M:%S'
colnames(x) <- gsub("x",symbol,colnames(x))
assign(symbol,x)
}
portfolio.st <- 'FXportf'
account.st <- 'FXacc'
# Initialize portfolio and account
initPortf(name=portfolio.st, symbols=symbols, initDate=initDate)
initAcct(name=account.st, portfolios=portfolio.st, initDate=initDate,
initEq=initEq)
initOrders(portfolio=portfolio.st, initDate=initDate)
# Initialize a strategy object
strat <- strategy("StoplossTest")
# Add an indicator
#strat <- add.indicator(strategy = strat, name = "LastClose", arguments
= list(price=quote(mktdata)), label= "LastClose")
strat <- add.indicator(strategy = strat, name = "LastClose", arguments
=list(price=quote(mktdata)), label= "LastClose")
# Signal: TRUE if last bar's Close > 1.41
strat <- add.signal(strategy = strat, name="sigThreshold", arguments =
list(column="LastClose", threshold=1.41, relationship="gt",
cross=FALSE), label="LastCloseThrExc")
#Rule: Buy
strat <- add.rule(strategy = strat, name='ruleSignal', arguments =
list(sigcol="LastCloseThrExc", sigval=TRUE, orderqty=1000,
ordertype='market', orderside='long'), type='enter')
# Rule: Add stop loss
strat <- add.rule(strategy = strat, name='ruleSignal', arguments =
list(sigcol="LastCloseThrExc", sigval=TRUE, orderqty=-1000,
ordertype='stoplimit', orderside='long', threshold=-0.02, tmult=FALSE,
replace=FALSE), type='risk')
# Process the indicators and generate trades
out <- applyStrategy(strategy=strat, portfolios=portfolio.st)
updatePortf(Portfolio=portfolio.st, Symbols=symbols)
# graphics
for(symbol in symbols){
dev.new()
chart.Posn(Portfolio=portfolio.st, Symbol=symbol, theme=themelist)
}
# Save results for inspection
ob <- getOrderBook("FXportf")
write.zoo(mktdata, quote=FALSE, file = "mktdata.csv")
write.zoo(getTxns("FXportf", "EURUSD"), quote=FALSE, file = "txns.csv")
write.zoo(ob$FXportf$EURUSD, quote=FALSE, file = "orderbook.csv")
#----------------------------------------------------------------------
The data EURUSD240.csv:
#----------------------------------------------------------------------
2010.11.01,00:00,1.3970,1.3996,1.3909,1.3979,4472
2010.11.01,04:00,1.3980,1.4010,1.3971,1.3979,3492
2010.11.01,08:00,1.3978,1.3992,1.3946,1.3966,6186
2010.11.01,12:00,1.3965,1.3969,1.3886,1.3896,6752
2010.11.01,16:00,1.3895,1.3906,1.3864,1.3887,5424
2010.11.01,20:00,1.3886,1.3901,1.3873,1.3897,3550
2010.11.02,00:00,1.3896,1.3915,1.3881,1.3899,3805
2010.11.02,04:00,1.3898,1.3942,1.3898,1.3935,4286
2010.11.02,08:00,1.3934,1.3992,1.3916,1.3966,6593
2010.11.02,12:00,1.3965,1.4049,1.3948,1.4007,7280
2010.11.02,16:00,1.4006,1.4057,1.4005,1.4035,5498
2010.11.02,20:00,1.4034,1.4041,1.4023,1.4030,3000
2010.11.03,00:00,1.4031,1.4045,1.4008,1.4014,3918
2010.11.03,04:00,1.4015,1.4022,1.3992,1.4002,3784
2010.11.03,08:00,1.4003,1.4058,1.4002,1.4052,5760
2010.11.03,12:00,1.4051,1.4053,1.4012,1.4026,6585
2010.11.03,16:00,1.4027,1.4172,1.4000,1.4103,9708
2010.11.03,20:00,1.4102,1.4144,1.4088,1.4123,4643
2010.11.04,00:00,1.4124,1.4137,1.4102,1.4123,5048
2010.11.04,04:00,1.4122,1.4142,1.4112,1.4135,4031
2010.11.04,08:00,1.4134,1.4263,1.4112,1.4225,7173
2010.11.04,12:00,1.4224,1.4281,1.4213,1.4252,7895
2010.11.04,16:00,1.4251,1.4261,1.4186,1.4208,5912
2010.11.04,20:00,1.4207,1.4220,1.4193,1.4212,3218
2010.11.05,00:00,1.4213,1.4247,1.4195,1.4209,3796
2010.11.05,04:00,1.4208,1.4217,1.4183,1.4185,4073
2010.11.05,08:00,1.4186,1.4228,1.4100,1.4101,5734
2010.11.05,12:00,1.4100,1.4125,1.4036,1.4100,4193
2010.11.05,16:00,1.4099,1.4106,1.4024,1.4043,3476
2010.11.05,20:00,1.4042,1.4049,1.4025,1.4031,1822
2010.11.07,23:00,1.4057,1.4061,1.4053,1.4054,348
2010.11.08,00:00,1.4053,1.4083,1.3919,1.3952,5118
2010.11.08,04:00,1.3951,1.3973,1.3931,1.3941,4295
2010.11.08,08:00,1.3940,1.3976,1.3899,1.3901,6779
2010.11.08,12:00,1.3900,1.3949,1.3892,1.3901,6713
2010.11.08,16:00,1.3902,1.3948,1.3888,1.3935,6576
2010.11.08,20:00,1.3936,1.3940,1.3911,1.3912,3647
2010.11.09,00:00,1.3913,1.3927,1.3847,1.3885,5510
2010.11.09,04:00,1.3886,1.3892,1.3839,1.3866,4817
2010.11.09,08:00,1.3865,1.3938,1.3824,1.3925,7614
2010.11.09,12:00,1.3924,1.3972,1.3908,1.3935,6446
2010.11.09,16:00,1.3936,1.3952,1.3813,1.3826,7277
2010.11.09,20:00,1.3827,1.3836,1.3748,1.3763,5346
2010.11.10,00:00,1.3764,1.3792,1.3736,1.3748,5173
2010.11.10,04:00,1.3747,1.3784,1.3738,1.3784,4982
2010.11.10,08:00,1.3783,1.3824,1.3754,1.3755,7301
2010.11.10,12:00,1.3754,1.3808,1.3688,1.3699,7625
2010.11.10,16:00,1.3698,1.3790,1.3671,1.3776,6470
2010.11.10,20:00,1.3777,1.3802,1.3765,1.3774,2830
2010.11.11,00:00,1.3773,1.3820,1.3754,1.3804,5449
2010.11.11,04:00,1.3805,1.3809,1.3771,1.3787,4960
2010.11.11,08:00,1.3786,1.3804,1.3718,1.3759,6311
2010.11.11,12:00,1.3760,1.3763,1.3664,1.3675,6846
2010.11.11,16:00,1.3674,1.3691,1.3637,1.3656,6376
2010.11.11,20:00,1.3657,1.3669,1.3644,1.3661,3677
2010.11.12,00:00,1.3662,1.3664,1.3601,1.3622,5697
2010.11.12,04:00,1.3621,1.3629,1.3592,1.3622,4606
2010.11.12,08:00,1.3621,1.3744,1.3574,1.3719,8334
2010.11.12,12:00,1.3720,1.3775,1.3660,1.3765,8074
2010.11.12,16:00,1.3764,1.3766,1.3660,1.3684,7500
2010.11.12,20:00,1.3685,1.3711,1.3678,1.3691,2911
2010.11.14,23:00,1.3707,1.3707,1.3682,1.3684,460
2010.11.15,00:00,1.3683,1.3749,1.3680,1.3720,4739
2010.11.15,04:00,1.3719,1.3720,1.3662,1.3673,4589
2010.11.15,08:00,1.3674,1.3678,1.3606,1.3614,6065
2010.11.15,12:00,1.3613,1.3667,1.3603,1.3629,6872
2010.11.15,16:00,1.3628,1.3629,1.3581,1.3610,5746
2010.11.15,20:00,1.3609,1.3621,1.3563,1.3578,4199
2010.11.16,00:00,1.3579,1.3623,1.3560,1.3617,4833
2010.11.16,04:00,1.3616,1.3654,1.3587,1.3589,4642
2010.11.16,08:00,1.3590,1.3619,1.3573,1.3595,6081
2010.11.16,12:00,1.3594,1.3641,1.3574,1.3583,7368
2010.11.16,16:00,1.3582,1.3589,1.3451,1.3489,6545
2010.11.16,20:00,1.3490,1.3496,1.3475,1.3483,3216
2010.11.17,00:00,1.3484,1.3512,1.3467,1.3499,2901
2010.11.17,04:00,1.3498,1.3516,1.3483,1.3499,2700
2010.11.17,08:00,1.3498,1.3525,1.3466,1.3506,4394
2010.11.17,12:00,1.3505,1.3542,1.3461,1.3542,4709
2010.11.17,16:00,1.3541,1.3563,1.3514,1.3530,4321
2010.11.17,20:00,1.3531,1.3534,1.3511,1.3529,2539
2010.11.18,00:00,1.3530,1.3569,1.3528,1.3561,3313
2010.11.18,04:00,1.3560,1.3607,1.3559,1.3586,3077
2010.11.18,08:00,1.3587,1.3653,1.3576,1.3634,4585
2010.11.18,12:00,1.3635,1.3666,1.3617,1.3625,4546
2010.11.18,16:00,1.3624,1.3645,1.3584,1.3620,4375
2010.11.18,20:00,1.3621,1.3647,1.3613,1.3643,2539
2010.11.19,00:00,1.3642,1.3660,1.3621,1.3635,2508
2010.11.19,04:00,1.3636,1.3682,1.3608,1.3681,2956
2010.11.19,08:00,1.3682,1.3730,1.3672,1.3720,4305
2010.11.19,12:00,1.3719,1.3730,1.3648,1.3653,4424
2010.11.19,16:00,1.3654,1.3681,1.3631,1.3663,3992
2010.11.19,20:00,1.3664,1.3695,1.3660,1.3675,1896
2010.11.21,23:00,1.3717,1.3743,1.3711,1.3736,582
2010.11.22,00:00,1.3735,1.3766,1.3713,1.3752,3862
2010.11.22,04:00,1.3751,1.3767,1.3741,1.3742,3488
2010.11.22,08:00,1.3743,1.3785,1.3723,1.3741,5686
2010.11.22,12:00,1.3742,1.3746,1.3618,1.3659,6150
2010.11.22,16:00,1.3660,1.3661,1.3576,1.3588,5910
2010.11.22,20:00,1.3587,1.3632,1.3587,1.3624,3504
2010.11.23,00:00,1.3623,1.3628,1.3548,1.3570,4024
2010.11.23,04:00,1.3569,1.3605,1.3526,1.3560,4353
2010.11.23,08:00,1.3559,1.3598,1.3543,1.3583,6509
2010.11.23,12:00,1.3582,1.3589,1.3437,1.3437,7152
2010.11.23,16:00,1.3436,1.3455,1.3370,1.3384,6131
2010.11.23,20:00,1.3385,1.3401,1.3361,1.3380,3989
2010.11.24,00:00,1.3379,1.3418,1.3361,1.3404,4666
2010.11.24,04:00,1.3405,1.3412,1.3381,1.3386,3550
2010.11.24,08:00,1.3385,1.3412,1.3285,1.3323,7124
2010.11.24,12:00,1.3322,1.3411,1.3320,1.3399,7327
2010.11.24,16:00,1.3398,1.3419,1.3340,1.3348,6352
2010.11.24,20:00,1.3347,1.3348,1.3310,1.3336,3067
2010.11.25,00:00,1.3337,1.3360,1.3307,1.3321,4172
2010.11.25,04:00,1.3322,1.3337,1.3308,1.3316,3563
2010.11.25,08:00,1.3315,1.3351,1.3287,1.3312,4810
2010.11.25,12:00,1.3311,1.3351,1.3305,1.3343,4181
2010.11.25,16:00,1.3344,1.3385,1.3338,1.3365,3232
2010.11.25,20:00,1.3364,1.3374,1.3345,1.3345,1531
2010.11.26,00:00,1.3346,1.3356,1.3326,1.3326,3527
2010.11.26,04:00,1.3327,1.3335,1.3265,1.3273,4409
2010.11.26,08:00,1.3272,1.3295,1.3218,1.3237,6853
2010.11.26,12:00,1.3238,1.3261,1.3200,1.3249,6254
2010.11.26,16:00,1.3250,1.3256,1.3212,1.3237,4652
2010.11.26,20:00,1.3236,1.3248,1.3232,1.3243,1701
2010.11.28,23:00,1.3280,1.3289,1.3265,1.3277,504
2010.11.29,00:00,1.3276,1.3286,1.3182,1.3230,4775
2010.11.29,04:00,1.3231,1.3250,1.3212,1.3244,4157
2010.11.29,08:00,1.3243,1.3300,1.3191,1.3196,6460
2010.11.29,12:00,1.3197,1.3197,1.3081,1.3082,7046
2010.11.29,16:00,1.3081,1.3109,1.3064,1.3096,5543
2010.11.29,20:00,1.3097,1.3137,1.3097,1.3106,3324
2010.11.30,00:00,1.3108,1.3149,1.3095,1.3128,4328
2010.11.30,04:00,1.3127,1.3129,1.3076,1.3112,4377
2010.11.30,08:00,1.3111,1.3135,1.2983,1.3033,7216
2010.11.30,12:00,1.3034,1.3045,1.2970,1.3017,6710
2010.11.30,16:00,1.3016,1.3046,1.2994,1.3003,6492
2010.11.30,20:00,1.3004,1.3017,1.2976,1.2980,4058
More information about the R-SIG-Finance
mailing list