Quantstrat: buy the next bar upon opening

How to implement “Buy at the next bar with an open price” in quantstrate?

Here is my experiment with the maCross.R sample.

  • Add prefer='Open' to ruleSignal

     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') 
  • The order was generated with the current Open price, but completed in the next line 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 
  • For example, an order was generated in 2011-05-25 03:00:00 with Open price 1.61523, but the transaction was at 03:30:00 with Close price 1.61437

    The following are market data.

     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 
+4
source share
1 answer

Using the maCross.R demo, if you change the applyStrategy line to include prefer=Open , like this

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

You will get execution when you open the next line.

 > 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 

And check that the signals are sent to the panel before the execution line

 > .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" 

Note that this is not the way prefer supposed to be used (at least not the way it is documented). In addition, I'm not sure what and how it will change when the signals light up.

+2
source

Source: https://habr.com/ru/post/1413702/


All Articles