quantstrat: compre la próxima barra al abrir

¿Cómo implementar "Comprar en la siguiente barra con precio de apertura" en quantstrat?

Aquí está mi experimento con la muestra maCross.R.

  1. Agregar prefer='Open' en señal de regla

    stratMACROSS <- add.rule(strategy = stratMACROSS, name='ruleSignal',
                             arguments = list(sigcol="ma50.gt.ma200", sigval=TRUE, orderqty=100000, ordertype='market', orderside='long', prefer='Open'), type='enter')
    stratMACROSS <- add.rule(strategy = stratMACROSS, name='ruleSignal', 
                             arguments = list(sigcol="ma50.lt.ma200", sigval=TRUE, orderqty=-100000, ordertype='market', orderside='long', prefer='Open'), type='exit')
    
  2. El pedido se generó en el momento actual Open precio, pero ejecutado en la siguiente barra Close.

    > orders <- getOrderBook(portfolio.st)
    > head(orders)
                    Order.Qty Order.Price Order.Type Order.Side Order.Threshold Order.Status Order.StatusTime     
    2011-05-22 00:00:00 "0"       NA          "init"     "long"     "0"             "closed"     "2011-05-22"         
    2011-05-24 04:30:00 "1e+05"   "1.61297"   "market"   "long"     NA              "closed"     "2011-05-24 05:00:00"
    2011-05-25 03:00:00 "-1e+05"  "1.61523"   "market"   "long"     NA              "closed"     "2011-05-25 03:30:00"
    2011-05-25 05:00:00 "1e+05"   "1.61537"   "market"   "long"     NA              "closed"     "2011-05-25 05:30:00"
    2011-05-30 09:30:00 "-1e+05"  "1.64679"   "market"   "long"     NA              "closed"     "2011-05-30 10:00:00"
    
    > txns <- getTxns(Portfolio=portfolio.st, Symbol=fx.st[1])
    > head(txns)
                    Txn.Qty Txn.Price Txn.Fees Txn.Value Txn.Avg.Cost Net.Txn.Realized.PL
    2011-05-22 00:00:00   0e+00   0.00000        0         0      0.00000                   0
    2011-05-24 05:00:00   1e+05   1.61227        0    161227      1.61227                   0
    2011-05-25 03:30:00  -1e+05   1.61437        0   -161437      1.61437                 210
    2011-05-25 05:30:00   1e+05   1.61929        0    161929      1.61929                   0
    2011-05-30 10:00:00  -1e+05   1.64584        0   -164584      1.64584                2655
    2011-05-30 19:30:00   1e+05   1.65046        0    165046      1.65046                   0
    
  3. Por ejemplo, el pedido se generó en 2011-05-25 03:00:00 con Open precio 1.61523, pero la transacción fue a las 03:30:00 con Close precio 1.61437

    Los datos de mercado se muestran a continuación.

    Date    Time    Open    High    Low     Close   Up  Down
    5/24/2011   430 1.61297 1.6153  1.61288 1.61421 1804    1700
    5/24/2011   500 1.61409 1.61445 1.61224 1.61227 1709    1662
    5/25/2011   300 1.61523 1.61628 1.61318 1.6139  1526    1465
    5/25/2011   330 1.61393 1.61541 1.61345 1.61437 1713    1583
    

preguntado el 22 de mayo de 12 a las 10:05

1 Respuestas

Usando la demostración de maCross.R, si cambia la línea applyStrategy para incluir prefer=Open como este

out<-try(applyStrategy(strategy=stratMACROSS , portfolios=portfolio.st, prefer='Open'))

Obtendrás ejecuciones en la apertura de la siguiente barra.

> head(txns)
           Txn.Qty Txn.Price Txn.Fees Txn.Value Txn.Avg.Cost Net.Txn.Realized.PL
1999-12-31   0e+00  0.000000        0       0.0     0.000000                   0
2001-06-27   1e+05 11.863950        0 1186395.0    11.863950                   0
2001-09-07  -1e+05  8.709491        0 -870949.1     8.709491             -315446
2002-01-07   1e+05 11.808210        0 1180821.0    11.808210                   0
2002-07-10  -1e+05  8.814099        0 -881409.9     8.814099             -299411
2003-05-16   1e+05  9.255447        0  925544.7     9.255447                   0

> head(AAPL['2001-06-26/'])
           AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume AAPL.Adjusted
2001-06-26  11.61595  11.82995 11.45171      11.82     9742200         11.82
2001-06-27  11.86395  11.94859 11.20180      11.62    13361800         11.62
2001-06-28  11.47604  11.90421 11.42127      11.72    12443200         11.72
2001-06-29  11.78421  12.50142 11.55510      11.58    18406800         11.58
2001-07-02  11.77054  12.06431 11.52159      11.90     8216000         11.90
2001-07-03  11.70569  12.03929 11.70071      11.87     4019400         11.87

Y verifique que las señales se enviaron en la barra antes de la barra de ejecución.

> .strategy$order_book.macross$macross$AAPL
           Order.Qty Order.Price        Order.Type Order.Side Order.Threshold Order.Status Order.StatusTime      Prefer Order.Set Txn.Fees Rule             
1999-12-31 "0"       NA                 "init"     "long"     "0"             "closed"     "1999-12-31"          ""     ""        "0"      ""               
2001-06-26 "1e+05"   "11.6159494736842" "market"   "long"     NA              "closed"     "2001-06-27 00:00:00" "Open" NA        "0"      "ruleSignal.rule"
2001-09-06 "-1e+05"  "9.15846501128668" "market"   "long"     NA              "closed"     "2001-09-07 00:00:00" "Open" NA        "0"      "ruleSignal.rule"
2002-01-04 "1e+05"   "11.6158125791473" "market"   "long"     NA              "closed"     "2002-01-07 00:00:00" "Open" NA        "0"      "ruleSignal.rule"
2002-07-09 "-1e+05"  "9.0088819167142"  "market"   "long"     NA              "closed"     "2002-07-10 00:00:00" "Open" NA        "0"      "ruleSignal.rule"
2003-05-15 "1e+05"   "9.25531233315537" "market"   "long"     NA              "closed"     "2003-05-16 00:00:00" "Open" NA        "0"      "ruleSignal.rule"
2006-06-21 "-1e+05"  "57.4905184929139" "market"   "long"     NA              "closed"     "2006-06-22 00:00:00" "Open" NA        "0"      "ruleSignal.rule"
2006-09-25 "1e+05"   "73.498195379538"  "market"   "long"     NA              "closed"     "2006-09-26 00:00:00" "Open" NA        "0"      "ruleSignal.rule"
2008-03-06 "-1e+05"  "124.074175969569" "market"   "long"     NA              "closed"     "2008-03-07 00:00:00" "Open" NA        "0"      "ruleSignal.rule"
2008-05-16 "1e+05"   "189.299382795011" "market"   "long"     NA              "closed"     "2008-05-19 00:00:00" "Open" NA        "0"      "ruleSignal.rule"
2008-09-23 "-1e+05"  "131.28867076632"  "market"   "long"     NA              "closed"     "2008-09-24 00:00:00" "Open" NA        "0"      "ruleSignal.rule"
2009-05-13 "1e+05"   "122.684122520713" "market"   "long"     NA              "closed"     "2009-05-14 00:00:00" "Open" NA        "0"      "ruleSignal.rule"

Tenga en cuenta que esto no es así prefer se supone que debe usarse (al menos no cómo está documentado). Además, no estoy seguro de si esto cambiará o cómo cambiará el lugar donde se activan las señales.

Respondido 13 ago 12, 15:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.