I am trying to run a backtesting strategy in R's Quantstrat package. The instrument is Wheat futures and is quoted in US cents. The contract size is 5000 bushels. I have therefore added the following code.
future(symbols,
currency = "USD",
tick_size = 0.25,
multiplier = 50)
However, when running the model it seems to draw a loss when the profit is too small, which prompted me to look at how transaction fees are calculated in the blotter package as shown in this code on github.
#' @param ConMult Contract/instrument multiplier for the Symbol if it is not defined in an instrument specification
Does this mean that when I specify .txnfees <- -10
, the taxation fee is 50*-10 = -500, in which case I should specify TxnFees to be -0.2. How do I specify a set amount per order?
To directly answer your question, setting
.txnfees <- -10
in ruleSignal will make the transaction cost for the trade equal to -10, regardless of the quantity of the trade. The multipliers in the contracts defined byFinancialInstrument
do not directly affect the transaction cost calculation. Here is how you could go about achieving what you expect though...First a bit of background: The source code for
addTxn
inblotter
is where transaction costs come into play inquantstrat
backtests, which you have correctly identified. You can pass inTxnFees
as a (non positive) numeric value, or a character string that is the name of a function that defines how the fees are calculated. Look carefully and you'll see thatTxnQty, TxnPrice, Symbol
are all arguments that are supplied to theTxnFee
function. i.e. See this part of the code inaddTxn
:In quantstrat, the arguments to
ruleSignal
include transaction costs via theTxnFees
argument (andruleSignal
is an argument toadd.rule
) But you can pass in a custom function (giving its name as a string in the argument toruleSignal
) which will model transaction fees in a way you might like.If you look in the same blotter source file you have linked, there is an example of a transaction cost function (look at the blotter unit tests and you'll see examples of how this transaction cost function is used):
Below is another fully reproducible example of how you could model fees that are a function of the quantity traded, and just for demonstration I use the contract multiplier argument from the
stock
object, instead offuture
object, but obviously the same kind of logic applies for any instrument type. In the example below, for each transaction, a fee equal to 1.5% of the quantity traded is charged as a transaction cost. You could also possibly make the fees a function of theTxnPrice
too, which is another argument to the function.Check that the fees are as expected in relation to quantity traded: