quantstrat:在开盘时买下一个酒吧

如何在quantstrat中实现“以开盘价在下一个柱子买”?

这是我对maCross.R样本的实验.

>在ruleSignal中添加prefer =’Open’

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')

>订单是以当前的开盘价生成的,但是在下一个柱关闭时执行.

> 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

>例如,订单生成于2011-05-25 03:00:00,营业价格为1.61523,但交易时间为03:30:00,收盘价1.61437

市场数据如下所示.

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

最佳答案 使用maCross.R演示,如果您将applyStrategy行更改为包含prefer = Open,就像这样

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

你将在下一个酒吧的开放处获得执行.

> 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

并检查信号是否在执行栏之前在条上发送

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

请注意,这不是应该如何使用(至少不是如何记录).此外,我不确定在信号触发的地方是否或如何改变.

点赞