Skip to content

Delayed order #149

@mike-mik

Description

@mike-mik

Description

The delay parameter in ruleSignal seems to have no effect on execution time.

Expected behavior

The sample data contains a long signal at 2021-01-25 15:30:00.

The following strategy I would expect to issue an order at the next bar (2021-01-26 09:30:00) but to delay execution by one day.
Although the order is being delayed in the order book index the trade is still executed at time of order issue (2021-01-26 09:30:00) without any delay.

Output of example code (one day delay):

[1] "delay=86400"
           ^^^^^
[1] "2021-01-26 09:30:00 SYM 100 @ 61.24"
             ^^^^^^^^^^^?
$str2
$str2$SYM
                    Order.Qty Order.Price Order.Type Order.Side Order.Threshold
2021-01-26 15:30:00 "100"     "60.58"     "market"   "long"     NA
        ^^^^^^^^^^^ok
                    Order.Status Order.StatusTime      Prefer Order.Set
2021-01-26 15:30:00 "closed"     "2021-01-26 09:30:00" "High" NA
                    Txn.Fees Rule        Time.In.Force
2021-01-26 15:30:00 "0"      "EnterLONG" ""

Minimal, reproducible example

library(quantstrat)

suppressWarnings(rm("account.str2","portfolio.str2",pos=.blotter))
suppressWarnings(rm("order_book.str2",pos=.strategy))

oldtz<-Sys.getenv("TZ")
if(oldtz=="") Sys.setenv(TZ="UTC")

sy <- 'SYM'
fn <- paste0(sy,".csv")
tmp <- as.xts (read.csv2.zoo (fn, header=T, stringsAsFactors=F,
        dec = ".", FUN=as.POSIXct))
assign(sy, tmp)

tradeSize <- 5000
initEq <- 100000
delay_seconds <- 1 * 24*60*60

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

initPortf('str2', symbols=sy)
initAcct('str2', portfolios='str2', initEq=initEq)
initOrders(portfolio='str2')

strategy.st <- 'str2'
strategy (strategy.st, store=TRUE)


add.rule(strategy.st, name='ruleSignal', arguments = list(
        sigcol = "Long",
        sigval = TRUE,
        ordertype = 'market',
        prefer = 'High',
        orderside = 'long',
        delay = delay_seconds,
        orderqty = 100),
  type='enter',
  label='EnterLONG')


print(paste0("delay=", delay_seconds))
out<-try(applyStrategy(strategy='str2' , portfolios='str2'))
print(getOrderBook('str2'))

updatePortf(Portfolio='str2')
updateAcct('str2')
updateEndEq('str2')

Sys.setenv(TZ=oldtz)

My sample data SYM.csv:

Index;Open;High;Low;Close;Volume;Long
2021-01-25 09:30:00;60.67;60.77;59.705;59.98;4841167;0
2021-01-25 10:00:00;59.98;60.31;59.955;59.992;2331090;0
2021-01-25 10:30:00;59.99;60.155;59.64;59.69;2011158;0
2021-01-25 11:00:00;59.6771;59.76;59.26;59.38;2717999;0
2021-01-25 11:30:00;59.38;59.79;59.17;59.67;1758438;0
2021-01-25 12:00:00;59.675;59.85;59.4601;59.4601;1536049;0
2021-01-25 12:30:00;59.475;59.93;59.47;59.9;1212432;0
2021-01-25 13:00:00;59.9;60.37;59.875;60.32;1461132;0
2021-01-25 13:30:00;60.32;60.42;60.185;60.23;970523;0
2021-01-25 14:00:00;60.24;60.29;59.94;60;971210;0
2021-01-25 14:30:00;60;60.24;59.88;60.06;1038065;0
2021-01-25 15:00:00;60.055;60.4;60.04;60.345;931919;0
2021-01-25 15:30:00;60.35;60.58;60.3;60.56;2135034;1
2021-01-26 09:30:00;61.2;61.24;60.087;60.335;2512210;0
2021-01-26 10:00:00;60.335;60.9;60.29;60.78;1768021;0
2021-01-26 10:30:00;60.78;60.87;60.408;60.445;1173861;0
2021-01-26 11:00:00;60.45;60.56;60.15;60.52;1085131;0
2021-01-26 11:30:00;60.51;60.53;60.19;60.33;919405;0
2021-01-26 12:00:00;60.33;60.68;60.31;60.65;1130399;0
2021-01-26 12:30:00;60.645;60.95;60.63;60.915;756772;0
2021-01-26 13:00:00;60.92;61.04;60.81;60.875;848270;0
2021-01-26 13:30:00;60.87;60.95;60.73;60.805;822825;0
2021-01-26 14:00:00;60.8;61.15;60.78;60.95;989872;0
2021-01-26 14:30:00;60.95;60.98;60.81;60.9;1001693;0
2021-01-26 15:00:00;60.9;60.93;60.73;60.86;854300;0
2021-01-26 15:30:00;60.85;60.93;60.67;60.91;3214855;0
2021-01-27 09:30:00;59.9;59.99;58.53;58.98;3972687;0
2021-01-27 10:00:00;58.97;59.43;58.74;59.37;2386966;0
2021-01-27 10:30:00;59.375;59.85;59.13;59.81;2147582;0
2021-01-27 11:00:00;59.82;59.86;59.44;59.69;1273143;0
2021-01-27 11:30:00;59.6998;59.76;59.4;59.45;1183463;0
2021-01-27 12:00:00;59.45;60;59.35;59.41;1359946;0
2021-01-27 12:30:00;59.41;59.62;59.31;59.565;570512;0
2021-01-27 13:00:00;59.565;59.868;59.44;59.76;761216;0
2021-01-27 13:30:00;59.76;59.765;59.45;59.58;623576;0
2021-01-27 14:00:00;59.585;59.7;59.37;59.42;897427;0
2021-01-27 14:30:00;59.425;59.625;59.21;59.3283;2123241;0
2021-01-27 15:00:00;59.33;59.83;59.2;59.67;1558001;0
2021-01-27 15:30:00;59.66;59.7;59.17;59.4;3507501;0

Session Info

R version 4.2.1 (2022-06-23)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Gentoo Linux

Matrix products: default
BLAS:   /usr/lib64/libblas.so.3.10.0
LAPACK: /usr/lib64/R/lib/libRlapack.so

locale:
[1] C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] quantstrat_0.16.9          foreach_1.5.2             
[3] blotter_0.16.0             PerformanceAnalytics_2.0.4
[5] FinancialInstrument_1.3.1  quantmod_0.4.20           
[7] TTR_0.24.3                 xts_0.12.1                
[9] zoo_1.8-11                

loaded via a namespace (and not attached):
[1] quadprog_1.5-8   lattice_0.20-45  codetools_0.2-18 MASS_7.3-58.1   
[5] grid_4.2.1       curl_4.3.2       boot_1.3-28      iterators_1.0.14
[9] compiler_4.2.1  

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions