R:Quantstrat TxnFees乘法器

R:Quantstrat TxnFees乘法器,r,quantstrat,financialinstrument,R,Quantstrat,Financialinstrument,我试图在R的Quantstrat包中运行一个回溯测试策略。该工具为小麦期货,以美分报价。合同尺寸为5000蒲式耳。因此,我添加了以下代码 future(symbols, currency = "USD", tick_size = 0.25, multiplier = 50) 然而,当运行该模型时,当利润太小时,它似乎会出现亏损,这促使我研究如何在blotter包中计算交易费用 这是否意味着当我指定.txnfees直接回答您的问题时,设置.txnfees@

我试图在R的Quantstrat包中运行一个回溯测试策略。该工具为小麦期货,以美分报价。合同尺寸为5000蒲式耳。因此,我添加了以下代码

future(symbols, 
      currency = "USD",
      tick_size = 0.25,
      multiplier = 50) 
然而,当运行该模型时,当利润太小时,它似乎会出现亏损,这促使我研究如何在blotter包中计算交易费用


这是否意味着当我指定
.txnfees直接回答您的问题时,设置
.txnfees@Stat这两个问题都应该通过以下方式解决:查看
blotter::addTxn
,您将看到
txnfees
是否作为函数提供,如果不清楚如何将参数传递到函数的环境,则会有一个硬编码的调用
txnfees,quantstrat::getOrderBook
给出了一个如何传递参数的示例,其中
.strategy
是存储外部参数/数据的环境,我想补充一点,自定义函数
pennyPerShare非常感谢。非常感谢。很抱歉我无意中删除了上面的问题,因为当我回到第二个问题时,我找到了我的答案。我还以为你还没有回复呢。很抱歉。为了完整性,我询问FXQuantRader Q1:是否可以将txnFUN的参数(例如,pct=0.015)添加到add.rule和Q2中,其中TxnQty、Txnprice在txnFUN中被提取。
#' @param ConMult Contract/instrument multiplier for the Symbol if it is not defined in an instrument specification
if (is.function(TxnFees)) {
      txnfees <- TxnFees(TxnQty, TxnPrice, Symbol) 
    } else {
      txnfees<- as.numeric(TxnFees)
    }
pennyPerShare <- function(TxnQty, ...) {
    return(abs(TxnQty) * -0.01)
}
#---------------------------------------------------------------
# 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