为了直接回答您的问题,.txnfees <- -10
在 ruleSignal 中设置将使交易的交易成本等于-10,无论交易数量如何。定义的合约中的乘数FinancialInstrument
不直接影响交易成本的计算。这是您如何实现您的期望的方法...
首先是背景知识:in 的源代码addTxn
是blotter
交易成本在回测中发挥作用的地方quantstrat
,您已经正确识别了它。TxnFees
您可以作为(非正)数值或作为定义费用计算方式的函数名称的字符串传入。仔细看,你会发现这些TxnQty, TxnPrice, Symbol
都是提供给TxnFee
函数的所有参数。即见这部分代码addTxn
:
if (is.function(TxnFees)) {
txnfees <- TxnFees(TxnQty, TxnPrice, Symbol)
} else {
txnfees<- as.numeric(TxnFees)
}
在 quantstrat 中,通过参数ruleSignal
包含交易成本的TxnFees
参数(并且ruleSignal
是 的参数add.rule
)但是您可以传入一个自定义函数(在参数中将其名称作为字符串提供给ruleSignal
),它将以您可能喜欢的方式模拟交易费用.
如果您查看已链接的同一个记事本源文件,则有一个事务成本函数的示例(查看记事本单元测试,您将看到如何使用此事务成本函数的示例):
pennyPerShare <- function(TxnQty, ...) {
return(abs(TxnQty) * -0.01)
}
下面是另一个完全可重现的示例,说明如何对作为交易数量函数的费用进行建模,仅出于演示目的,我使用来自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=''))
检查费用是否符合交易数量的预期:
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