5

quantstrat で「始値で次のバーで購入」を実装する方法は?

これは、maCross.R サンプルを使用した私の実験です。

  1. prefer='Open'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')
    
  2. 注文は現在のOpen価格で生成されましたが、次のバーで実行されました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. たとえば、注文は 2011-05-25 03:00:00 にOpen価格 1.61523 で生成されましたが、トランザクションは 03:30:00 に価格 1.61437 でClose生成されました

    市場データを以下に示します。

    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

1 に答える 1

2

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"

これは の使用方法ではないことに注意してくださいprefer(少なくとも文書化されている方法ではありません)。また、信号が発火する場所が変わるかどうか、またはどのように変わるかはわかりません。

于 2012-08-13T14:21:58.303 に答える