[R-SIG-Finance] calculating beta-based variable dollar amounts to each leg of a spread backtest

Brian G. Peterson brian at braverock.com
Wed Aug 26 16:08:24 CEST 2015

On Wed, 2015-08-26 at 13:12 +0000, Tucker Sferro wrote:
> I am currently working on a pairs trading backtest R script and have hit a
> wall in my efforts to generate dollar based backtesting that adjusts for
> the hedgeRatio ("beta" in my script) to each leg in a stock pair backtest.
> My existing script is mostly taken from Joshua Ulrich's pairs quantstrat
> backtest example.

The pairs trading demo was originally written by Garrett See, for the

As is stated very clearly in the comments on the demo script, you would
never trade equity pairs this way.  

You should use a spreader.  Period.  

If you don't have a spreader, you shouldn't be trying to trade intra-day
stat arb pair trades.  

If you're trading long-cycle portfolio baskets, you likely don't need
quantstrat, and would be better off using Return.portfolio and a
timeseries of weights as a first approximation.

> The script as-is backtests equal dollar amounts per pair, but this value is
> different between sets of pairs because Joshua's script uses a ratio
> spread, which is used to adjust the amount to buy for stock B in the pair.
> Stock A could be any price and the share amount remains fixed, meaning the
> dollar investment amount will vary between pairs. This is nonetheless ok,
> as I am only testing for risk-adjusted returns - *most importantly I would
> simply like to adjust the dollar invest amount based on the hedge ratio*.
> I've tried unsuccessfully to use the IKTrading  osMaxDollar function, but
> have hit a wall since Joshua's script uses an order sizing function that
> makes the 2nd stock shares to trade the opposite of the first stock.
> I've also looked at Ilya Kipnis's example of modeling the spread itself(
> https://quantstrattrader.wordpress.com/category/quantstrat/) but this
> apparently doesn't work since the dollar investment amount needs to
> be adjusted for each leg on a per trade basis. I have tried to resolve this
> for literally more than 2 weeks (stack exchange, demo deconstructing, etc)
> without any luck and was wondering if anyone on this list would be so kind
> as to have a look at my script? Perhaps you could point me in the right
> direction with a couple lines of helper code? I really don't know where
> else to turn and could very much use the help. Below is the code with the
> working script (ie all versions which incorporate osMaxDollar have been
> left out because they don't work!).

Construct multiple spreads. Trade these as instruments in your backtest.

for symbols




you can construct spreads




(note different ratios for each pair)

presumably your ratios came from some beta calculation you've performed.

and you can now construct the spread time series using buildSpread or
your own function.  Your strategy works on the spreads.

If you want the ratio to change over time, that's actually a different

At one firm I was at, we would put the spread ratio into the primary_id
of the spread, e.g. ZF3.ZN2 ZF5.ZN3 though there are other approaches
that would be equally valid.



Brian G. Peterson
Ph: 773-459-4973
IM: bgpbraverock

More information about the R-SIG-Finance mailing list