quantstrat order execution
Hi Jan, Thanks for your answer. I have been searching all over the quantstrat code for the "switch" where I could change (that daily data would be processed in the same way as monthly, quarterly and yearly) default behaviour as you described it: "=> for monthly, quarterly and yearly data, quantstrat will use the current bar to get the price. => for anything with a higher frequency (eg. weekly, daily, intraday), quantstrat will use the next bar to get the price." The more I looked the more it seems to me there is no such "switch" since it seems to me that the whole logic of quantstrat::applyRules is based on nextIndex. I could not find a way to change quantstrat::applyRules in such a way that would solve my "problem". Any hint on what and how to change highly appreciated. Next, I still do not get the prefer option. For me it still does not work as I would like to. I have modified the script I attached with my first post (please see attachment) in such a way that I have lagged signals so that I now get transactions occurring on the correct date (on the same bar as true signal occurs). But, then looking at the transactions, they are still done at the close (quntstrat ignoring prefer option). I have used prefer='Open' when using addRule, but it is ignored and transaction happens on Close price.
print(getOrderBook(p))
$qs.test
$qs.test$VXX
Order.Qty Order.Price Order.Type Order.Side Order.Threshold
Order.Status Order.StatusTime Prefer Order.Set Txn.Fees
Rule
2011-02-01 "0" NA "init" "long" "0"
"closed" "2011-02-01" "" "" "0"
""
2011-02-08 "-1" "28.42" "market" "short" NA
"closed" "2011-02-09 00:00:00" "Close" NA "-1.9"
"EnterSHORT"
2011-02-10 "all" "29.14" "market" "short" NA
"closed" "2011-02-11 00:00:00" "Open" NA "-1.9"
"ExitShort2CASH"
attr(,"class")
[1] "order_book"
txns <- getTxns(p, "VXX") txns
Txn.Qty Txn.Price Txn.Fees Txn.Value Txn.Avg.Cost Net.Txn.Realized.PL 2011-02-01 0 0.00 0.0 0.00 0.00 0.00 2011-02-09 -1 28.51 -1.9 -28.51 28.51 -1.90 2011-02-11 1 28.02 -1.9 28.02 28.02 -1.41
mktdata["2011-02-08/2011-02-11"]
VXX.Open VXX.High VXX.Low VXX.Close VXX.Volume VXX.Adjusted PreviousC NextO signal.enter signal.exit cash short 2011-02-08 28.62 29.19 28.25 28.42 13997600 28.42 28.77 28.69 -1 0 0 1 2011-02-09 28.69 29.11 28.35 28.51 18603000 28.51 28.42 29.14 0 -1 0 0 2011-02-10 29.14 29.25 28.30 28.51 17734000 28.51 28.51 28.75 0 0 1 0 2011-02-11 28.75 28.80 27.93 28.02 14826100 28.02 28.51 28.00 0 0 0 0 I would appreciate any help to sort this prefer option thing. Thnx. Best regards, Samo __________________________________________________ Hi Samo, Currently, the following is hard-coded in the quantstrat software: => for monthly, quarterly and yearly data, quantstrat will use the current bar to get the price. => for anything with a higher frequency (eg. weekly, daily, intraday), quantstrat will use the next bar to get the price. The idea is that you once a bar is complete, there is no more trading possible within that bar, so you can only trade within the next bar. I suggest that you use prefer='Open' to use the opening rate of the next bar instead. Be aware that your results will be unrealistic though, because in practice you will usually not get the opening price of the next bar. Another option is to adjust your strategy a bit, and use higher frequency data, for example 15min rates, and decide on the on-but-last bar of the day, then trade on the last one. HTH, Jan. Quantstrat
On 19-07-12 21:48, Samo Pahor wrote:
Hi, I am trying to "persuade" quantstrat, working with daily data, to execute orders at the close on the day that signal is telling "short" and exit(cover short) a day after "move to cash" signal is generated. Unfortunately, quantstrat does is not easily presuaded to do this... I have searched all possible lists and e-mails but it seems to me that either quantstrat was not designed for delaying order execution on daily data with prefer option column or there is a bug in quantstrat or my understanding is limited(again)... I tried to play with all possible combinations of delay (-1, 0, 1) and prefer (Open, PreviousOpen) arguments but without success (following discussion here https://stat.ethz.ch/pipermail/r-sig-finance/2011q3/008356.html and here https://stat.ethz.ch/pipermail/r-sig-finance/2012q2/010224.html). <https://stat.ethz.ch/pipermail/r-sig-finance/2012q2/010224.html%29.>.. Am I missing something here or I do not understand something about quantstrat? I have prepared a reproducible example (based on Jan Humme excellent demos). It also contains expected result and actual result from quantstrat. Please see attached R script file. Looking at OrderBook, txns and maktdata it just does not make sense to me...
print(getOrderBook(p))
$qs.test
$qs.test$VXX
Order.Qty Order.Price Order.Type Order.Side Order.Threshold
Order.Status Order.StatusTime Prefer Order.Set Txn.Fees Rule
2009-01-30 "0" NA "init" "long" "0"
"closed" "2009-01-30" "" "" "0" ""
2009-02-10 "-1" "418.4" "market" "short" NA
"closed" "2009-02-11 00:00:00" "Close" NA "-1.9"
"EnterSHORT"
2009-02-12 "all" "421" "market" "short" NA
"closed" "2009-02-12 00:00:00" "NextO" NA "-1.9"
"ExitShort2CASH"
attr(,"class")
[1] "order_book"
txns <- getTxns(p, "VXX") txns
Txn.Qty Txn.Price Txn.Fees Txn.Value Txn.Avg.Cost Net.Txn.Realized.PL <http://Net.Txn.Realized.PL> 2009-01-30 0 0.00 0.0 0.00 0.00 0.00 2009-02-11 -1 412.28 -1.9 -412.28 412.28 -1.90 2009-02-12 1 411.04 -1.9 411.04 411.04 -0.66
mktdata["2009-02-10/2009-02-12"]
VXX.Open VXX.High VXX.Low VXX.Close VXX.Volume VXX.Adjusted PreviousC NextO signal cash long short 2009-02-10 404.0 422.64 395.12 418.40 64200 418.40 397.32 416.20 -1 0 0 1 2009-02-11 416.2 419.32 408.40 412.28 21500 412.28 418.40 421.00 0 1 0 0 2009-02-12 421.0 432.00 411.04 411.04 25500 411.04 412.28 412.96 0 0 0 0 I would appreciate any hints on how to accomplish what I want. Best, Samo
_______________________________________________ R-SIG-Finance at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.
Jan Humme - OpenTrades WWW: http://www.opentrades.nl Email: jan at opentrades.nl Twitter: @opentrades [[alternative HTML version deleted]] -------------- next part -------------- An HTML attachment was scrubbed... URL: <https://stat.ethz.ch/pipermail/r-sig-finance/attachments/20120722/f436f019/attachment.html> -------------- next part -------------- A non-text attachment was scrubbed... Name: quantstrat_test.R Type: application/octet-stream Size: 6992 bytes Desc: not available URL: <https://stat.ethz.ch/pipermail/r-sig-finance/attachments/20120722/f436f019/attachment.obj>