A:Quantstrat TxnFees 乘数
R: Quantstrat TxnFees Multiplier
我正在尝试 运行 R 的 Quantstrat 包中的回测策略。该工具是小麦期货,以美分报价。合约规模为 5000 蒲式耳。因此,我添加了以下代码。
future(symbols,
currency = "USD",
tick_size = 0.25,
multiplier = 50)
然而,运行模型在利润太小的情况下似乎会亏损,这促使我查看 blotter 包中交易费用的计算方式 as shown in this code on github。
#' @param ConMult Contract/instrument multiplier for the Symbol if it is not defined in an instrument specification
这是否意味着当我指定.txnfees <- -10
时,税费为50*-10 = -500,在这种情况下我应该指定TxnFees为-0.2。如何为每个订单指定固定金额?
直接回答你的问题,在ruleSignal中设置.txnfees <- -10
将使交易的交易成本等于-10,而不管交易的数量。 FinancialInstrument
定义的合约中的乘数不直接影响交易成本的计算。这是您如何实现您的期望...
首先介绍一下背景知识:blotter
中 addTxn
的源代码是交易成本在 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
的参数中将其名称作为字符串给出),它将以您可能喜欢的方式对交易费用进行建模。
如果您查看已链接的相同 blotter 源文件,则有一个交易成本函数示例(查看 blotter 单元测试,您将看到如何使用此交易成本函数的示例):
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
我正在尝试 运行 R 的 Quantstrat 包中的回测策略。该工具是小麦期货,以美分报价。合约规模为 5000 蒲式耳。因此,我添加了以下代码。
future(symbols,
currency = "USD",
tick_size = 0.25,
multiplier = 50)
然而,运行模型在利润太小的情况下似乎会亏损,这促使我查看 blotter 包中交易费用的计算方式 as shown in this code on github。
#' @param ConMult Contract/instrument multiplier for the Symbol if it is not defined in an instrument specification
这是否意味着当我指定.txnfees <- -10
时,税费为50*-10 = -500,在这种情况下我应该指定TxnFees为-0.2。如何为每个订单指定固定金额?
直接回答你的问题,在ruleSignal中设置.txnfees <- -10
将使交易的交易成本等于-10,而不管交易的数量。 FinancialInstrument
定义的合约中的乘数不直接影响交易成本的计算。这是您如何实现您的期望...
首先介绍一下背景知识:blotter
中 addTxn
的源代码是交易成本在 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
的参数中将其名称作为字符串给出),它将以您可能喜欢的方式对交易费用进行建模。
如果您查看已链接的相同 blotter 源文件,则有一个交易成本函数示例(查看 blotter 单元测试,您将看到如何使用此交易成本函数的示例):
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