0

当我有一个做多和做空的策略并且我将addPosLimit()功能设置为 havemaxpos=1 and minpos=-1时,它​​仍然需要一个多头和一个空头头寸。但如果我只做多或做空策略,它会按预期工作。

我为此示例创建了一个基本策略。它有 3 个移动平均线,一个长期 SMA 作为多头/空头偏差过滤器,两个短期 SMA 作为一个交叉向上做多,当有做多倾向时,一个交叉向下做空当有做空倾向。交易开始后,我有一个固定距离的利润目标和止损。

这是策略。

    require(quantstrat)
    require(IKTrading)

    symbols <- "mySymbol"

    options("getSymbols.warning4.0"=FALSE)
    rm(list=ls(.blotter), envir=.blotter)
    currency('USD')
    Sys.setenv(TZ="UTC")

    stock(symbols, currency="USD", multiplier=1)
    initDate="1980-01-01"

    tradeSize  <- 1000
    initEq  <- tradeSize*length(symbols)

    account.st  <- 0
    strategy.st  <- portfolio.st <- account.st  <- "smatest"
    rm.strat(portfolio.st)
    rm.strat(strategy.st)
    initPortf(portfolio.st, symbols=symbols, initDate=initDate, currency='USD')
    initAcct(account.st, portfolios=portfolio.st, initDate=initDate, currency='USD',initEq=initEq)
    initOrders(portfolio.st, initDate=initDate)
    strategy(strategy.st, store=TRUE)

    addPosLimit(portfolio.st, symbols, timestamp=initDate, maxpos=1, minpos=-1)


    nSMAquick = 15
    nSMAslow = 30
    nSMAbias= 300

    add.indicator(strategy.st, name = "SMA", arguments=list(x=quote(mktdata$Close), n=nSMAquick), label="quickMA")
    add.indicator(strategy.st, name = "SMA", arguments=list(x=quote(mktdata$Close), n=nSMAslow), label="slowMA")
    add.indicator(strategy.st, name = "SMA", arguments=list(x=quote(mktdata$Close), n=nSMAbias), label="bias")


    add.signal(strategy.st, name = "sigCrossover", arguments = list(columns=c("quickMA", "slowMA"), 
                                                                     relationship="gt"), label="smaup")

    add.signal(strategy.st, name = "sigCrossover", arguments = list(columns=c("quickMA", "slowMA"),
                                                                    relationship="lt"), label="smadown")

    add.signal(strategy.st, name = "sigComparison", arguments = list(columns=c("Close", "bias"), 
                                                                     relationship="gt"), label="biasup")

    # 
    add.signal(strategy.st, name = "sigComparison", arguments = list(columns=c("Close", "bias"), 
                                                                     relationship="lt"), label="biasdown")


    add.signal(strategy.st, name="sigAND", arguments=list(columns=c("smaup", "biasup"), cross=F),
               label="longentry")


    add.signal(strategy.st, name="sigAND", arguments=list(columns=c("smadown", "biasdown"), cross=F),
               label="shortEntry")


    #enter rule
    add.rule(strategy.st, name = "ruleSignal", arguments = list(sigcol="longentry",
                                                                sigval=TRUE, 
                                                                ordertype="market",
                                                                orderside="long",
                                                                replace=FALSE,
                                                                prefer="Open",
                                                                orderqty=1,
                                                                orderset="orderslong",
                                                                osFUN=osMaxPos),
             type="enter",path.dep=TRUE,label="long")
    # 


    #take profit
    add.rule(strategy.st, name="ruleSignal", arguments=list(sigcol="longentry", 
                                                            sigval=TRUE, 
                                                            ordertype="limit", 
                                                            orderside="long", 
                                                            replace=FALSE, 
                                                            orderqty=-1,
                                                            threshold=quote(.75),
                                                            orderset="orderslong"), 
             type="chain", 
             parent="long",
             label="takeProfitLong",
             path.dep=TRUE)



    #stop loss
    add.rule(strategy.st, name="ruleSignal", arguments=list(sigcol="longentry", 
                                                            sigval=TRUE, 
                                                            ordertype="stoplimit", 
                                                            orderside="long", 
                                                            replace=FALSE, 
                                                            orderqty=-1,
                                                            threshold=quote(.25),
                                                            orderset="orderslong"), 
             type="chain", 
             parent="long",
             label="stopLossLong",
             path.dep=TRUE)


    # 
    # ###########
    # #short rule
    add.rule(strategy.st, name = "ruleSignal", arguments = list(sigcol="shortEntry",
                                                                sigval=TRUE, 
                                                                ordertype="market",
                                                                orderside="short",
                                                                replace=FALSE,
                                                                prefer="Open",
                                                                orderqty=-1,
                                                                orderset="ordersshort",
                                                                osFUN=osMaxPos),
             type="enter",path.dep=TRUE,label="short")
    # 


    #take profit
    add.rule(strategy.st, name="ruleSignal", arguments=list(sigcol="shortEntry", 
                                                            sigval=TRUE, 
                                                            ordertype="limit", 
                                                            orderside="short", 
                                                            replace=FALSE, 
                                                            orderqty=1,
                                                            threshold=quote(.75),
                                                            orderset="ordersshort"), 
             type="chain", 
             parent="short",
             label="takeProfitShort",
             path.dep=TRUE)



    #stop loss
    add.rule(strategy.st, name="ruleSignal", arguments=list(sigcol="shortEntry", 
                                                            sigval=TRUE, 
                                                            ordertype="stoplimit", 
                                                            orderside="short", 
                                                            replace=FALSE, 
                                                            orderqty=1,
                                                            threshold=quote(.25),
                                                            orderset="ordersshort"), 
             type="chain", 
             parent="short",
             label="stopLossShort",
             path.dep=TRUE)






    #apply strategy
    t1 <- Sys.time()
    out2 <- applyStrategy(strategy=strategy.st,portfolios=portfolio.st )

这是交易输出的样本。

[1] "2015-08-04 05:23:00 mySymbol 1 @ 45.51"
[1] "2015-08-04 14:43:00 mySymbol -1 @ 45.87"
[1] "2015-08-04 14:44:00 mySymbol 1 @ 45.96"
[1] "2015-08-04 15:00:00 mySymbol 1 @ 46.12"
[1] "2015-08-04 15:22:00 mySymbol -1 @ 46.15"
[1] "2015-08-04 16:41:00 mySymbol -1 @ 45.96"
[1] "2015-08-04 17:00:00 mySymbol 1 @ 46.03"
[1] "2015-08-04 17:12:00 mySymbol -1 @ 45.91"
[1] "2015-08-04 17:36:00 mySymbol -1 @ 45.86"
[1] "2015-08-04 17:42:00 mySymbol -1 @ 45.78"
[1] "2015-08-04 19:09:00 mySymbol 1 @ 45.79"
[1] "2015-08-04 20:33:00 mySymbol 1 @ 45.95"
[1] "2015-08-04 20:34:00 mySymbol 1 @ 46.04"


> transactions <- getTxns(Portfolio=portfolio.st, symbols)
> transactions
                    Txn.Qty Txn.Price Txn.Fees Txn.Value Txn.Avg.Cost Net.Txn.Realized.PL
1980-01-01 00:00:00       0      0.00        0      0.00         0.00                0.00
2015-08-04 05:23:00       1     45.51        0     45.51        45.51                0.00
2015-08-04 14:43:00      -1     45.87        0    -45.87        45.87                0.36
2015-08-04 14:44:00       1     45.96        0     45.96        45.96                0.00
2015-08-04 15:00:00       1     46.12        0     46.12        46.12                0.00
2015-08-04 15:22:00      -1     46.15        0    -46.15        46.15                0.11
2015-08-04 16:41:00      -1     45.96        0    -45.96        45.96               -0.08
2015-08-04 17:00:00       1     46.03        0     46.03        46.03                0.00
2015-08-04 17:12:00      -1     45.91        0    -45.91        45.91               -0.12
2015-08-04 17:36:00      -1     45.86        0    -45.86        45.86                0.00
2015-08-04 17:42:00      -1     45.78        0    -45.78        45.78                0.00
2015-08-04 19:09:00       1     45.79        0     45.79        45.79                0.03
2015-08-04 20:33:00       1     45.95        0     45.95        45.95               -0.13
2015-08-04 20:34:00       1     46.04        0     46.04        46.04                0.00

在此处输入图像描述

我希望策略做多,然后达到利润目标或被止损,或者在给出空头信号时关闭订单,反之亦然。

如果我注释掉所有短边代码,它似乎可以按预期工作。以下是空方被注释掉时的交易示例。

[1] "2015-08-04 05:23:00 mySymbol 1 @ 45.51"
[1] "2015-08-05 06:24:00 mySymbol -1 @ 46.26"
[1] "2015-08-05 07:35:00 mySymbol 1 @ 46.24"
[1] "2015-08-05 08:31:00 mySymbol -1 @ 45.99"
[1] "2015-08-05 12:01:00 mySymbol 1 @ 46.03"
[1] "2015-08-05 14:51:00 mySymbol -1 @ 45.78"
[1] "2015-08-05 21:00:00 mySymbol 1 @ 45.14"
[1] "2015-08-06 06:16:00 mySymbol -1 @ 44.89"
[1] "2015-08-06 18:29:00 mySymbol 1 @ 44.56"

我可以看到它在达到目标/停止之前不会再次交易。

4

1 回答 1

1

似乎从更改orderqty=-1orderqty="all"解决了我的问题。

我在想,因为我一开始只做长 1,那么 -1 就足够了。

这是 add.rule 现在的样子

add.rule(strategy.st, name="ruleSignal", arguments=list(sigcol="longEntry", 
                                                        sigval=TRUE, 
                                                        ordertype="stoplimit", 
                                                        orderside="long", 
                                                        replace=FALSE, 
                                                        orderqty="all",
                                                        #order.price=55,
                                                        threshold=quote(.25),
                                                        orderset="orderslong"), 
         type="chain", 
         parent="long",
         label="stopLossLong",
         path.dep=TRUE)

现在它在每个方向上只有 1 个位置。

于 2015-09-04T20:47:53.327 回答