R:Quantstrat TxnFees乘数

you*_*his 2 r quantstrat financialinstrument

我正试图在R的Quantstrat包中运行回测策略.该工具为小麦期货,报价为美分.合约规模为5000蒲式耳.因此我添加了以下代码.

future(symbols, 
      currency = "USD",
      tick_size = 0.25,
      multiplier = 50) 
Run Code Online (Sandbox Code Playgroud)

然而,当运行模型时,当利润太小时,它似乎会产生损失,这促使我看看如何在印刷包中计算交易费用,如github上的此代码所示.

#' @param ConMult Contract/instrument multiplier for the Symbol if it is not defined in an instrument specification
Run Code Online (Sandbox Code Playgroud)

这是否意味着当我指定时.txnfees <- -10,税费是50*-10 = -500,在这种情况下我应该指定TxnFees为-0.2.如何为每个订单指定设定金额?

FXQ*_*der 6

要直接回答您的问题,.txnfees <- -10在ruleSignal中设置将使交易的交易成本等于-10,而不管交易的数量.由定义的合同中的乘数FinancialInstrument不直接影响交易成本计算.以下是您如何实现您期望的目标......

首先介绍一下背景:addTxnin 的源代码blotter是交易成本在后quantstrat验测试中发挥作用的地方,您已经正确识别了这些成本.您可以TxnFees作为(非正数)数值或字符串传递,该字符串是定义费用计算方式的函数名称.仔细查看,您会看到TxnQty, TxnPrice, Symbol提供给该TxnFee函数的所有参数.即在以下部分看到代码的这一部分addTxn:

if (is.function(TxnFees)) {
      txnfees <- TxnFees(TxnQty, TxnPrice, Symbol) 
    } else {
      txnfees<- as.numeric(TxnFees)
    }
Run Code Online (Sandbox Code Playgroud)

在quantstrat中,通过参数ruleSignal包含事务成本的TxnFees参数(并且ruleSignal是参数add.rule)但是您可以传入自定义函数(在参数ruleSignal中将其名称作为字符串),这将以您可能喜欢的方式模拟交易费用.

如果您查看已链接的相同印记源文件,则有一个事务成本函数的示例(查看印迹单元测试,您将看到如何使用此事务成本函数的示例):

pennyPerShare <- function(TxnQty, ...) {
    return(abs(TxnQty) * -0.01)
}
Run Code Online (Sandbox Code Playgroud)

下面是另一个完全可重复的例子,说明如何模拟作为交易数量函数的费用,仅用于演示我使用来自stock对象的合约乘数参数而不是future对象,但显然同一种逻辑适用于任何工具类型.在下面的示例中,对于每笔交易,相当于交易数量的1.5%的费用将作为交易成本收取.您也可以将费用作为其函数TxnPrice,这是函数的另一个参数.

#---------------------------------------------------------------
# Define the transaction cost function
txnFUN <- function(TxnQty, TxnPrice, Symbol, pct = 0.015) {
  multiStock <- getInstrument(Symbol)$multiplier
  # Do something with multiStock, here it is equal to 1, so it's effectively meaningless but shows how you could go about using it.

  fees <- abs(TxnQty) * pct * multiStock
  # Fees are a negative deduction for the trade:
  if (fees > 0) fees <- -fees

  fees
}

#-------------------------------------------------------------------------------------


library(quantstrat)


suppressWarnings(rm("order_book.RSI",pos=.strategy))
suppressWarnings(rm("account.RSI","portfolio.RSI",pos=.blotter))
suppressWarnings(rm("account.st","portfolio.st","stock.str","stratRSI","startDate","initEq",'start_t','end_t'))


strategy.st <- "RSI"

stratRSI <- strategy(strategy.st, store = TRUE)


add.indicator(strategy = strategy.st, name = "RSI", arguments = list(price = quote(getPrice(mktdata))), label="RSI")
add.signal(strategy = strategy.st, name="sigThreshold",arguments = list(threshold=70, column="RSI",relationship="gt", cross=TRUE),label="RSI.gt.70")

add.signal(strategy = strategy.st, name="sigThreshold",arguments = list(threshold=30, column="RSI",relationship="lt",cross=TRUE),label="RSI.lt.30")


add.rule(strategy = strategy.st, name='ruleSignal', arguments = list(sigcol="RSI.lt.30", sigval=TRUE, orderqty= 100, TxnFees="txnFUN", ordertype='market', orderside='long', pricemethod='market', replace=FALSE, osFUN=osMaxPos), type='enter', path.dep=TRUE)
add.rule(strategy = strategy.st, name='ruleSignal', arguments = list(sigcol="RSI.gt.70", sigval=TRUE, orderqty='all', TxnFees="txnFUN", ordertype='market', orderside='long', pricemethod='market', replace=FALSE), type='exit', path.dep=TRUE)


currency("USD")
symbols = c("SPY")
stock.str = symbols

    startDate <- "1987-01-01"
    getSymbols(stock.str,from=startDate, to= Sys.Date())

for(symbol in symbols){
    stock(symbol, currency="USD",multiplier=1)
}
SPY <- SPY["2015/"]


startDate='2005-12-31'
initEq=100000
port.st<-'RSI'

initPortf(port.st, symbols=symbols)
initAcct(port.st, portfolios=port.st, initEq=initEq)
initOrders(portfolio=port.st)
for(symbol in symbols){ addPosLimit(port.st, symbol, startDate, 300, 3 ) }

applyStrategy(strategy=strategy.st , portfolios=port.st, parameters=list(n=2) ) 

updatePortf(Portfolio=port.st,Dates=paste('::',as.Date(Sys.time()),sep=''))
Run Code Online (Sandbox Code Playgroud)

检查费用是否与交易数量相符:

tail(getTxns(port.st, "SPY"), 15)
#                     Txn.Qty Txn.Price Txn.Fees Txn.Value Txn.Avg.Cost Net.Txn.Realized.PL
# 2017-03-28 20:00:00    -100  234.3969     -1.5 -23439.69     234.3969            178.6209
# 2017-04-05 20:00:00     100  234.2974     -1.5  23429.74     234.2974             -1.5000
# 2017-04-11 20:00:00     100  232.8943     -1.5  23289.43     232.8943             -1.5000
# 2017-04-20 20:00:00    -200  233.4515     -3.0 -46690.31     233.4515            -31.8605
# 2017-05-14 20:00:00     100  239.1338     -1.5  23913.38     239.1338             -1.5000
# 2017-05-15 20:00:00    -100  238.9149     -1.5 -23891.49     238.9149            -23.3933
# 2017-05-17 20:00:00     100  235.6210     -1.5  23562.10     235.6210             -1.5000
# 2017-05-22 20:00:00    -100  238.8851     -1.5 -23888.51     238.8851            324.9084
# 2017-06-12 20:00:00     100  243.3632     -1.5  24336.32     243.3632             -1.5000
# 2017-06-13 20:00:00    -100  243.0547     -1.5 -24305.47     243.0547            -32.3502
# 2017-06-27 20:00:00     100  243.4900     -1.5  24349.00     243.4900             -1.5000
# 2017-06-29 20:00:00     100  241.8000     -1.5  24180.00     241.8000             -1.5000
# 2017-07-05 20:00:00    -200  240.5500     -3.0 -48110.00     240.5500           -422.0002
# 2017-07-06 20:00:00     100  242.1100     -1.5  24211.00     242.1100             -1.5000
# 2017-07-12 20:00:00    -100  244.4200     -1.5 -24442.00     244.4200            229.4997
Run Code Online (Sandbox Code Playgroud)