Back to Community
VXX/XIV Pair Trade Algo

Alright, I had another thread where I was trying to find where ratios are deviating between pairs and when they reach an outlier 2.5*Standard Deviation to short one and go long the other. Here is the code that I came up with... again, I am terrible at Quant coding so go easy.

I still think it needs work to exit the pair trade before entering a new one. Also, the leverage is really bad and I don't know how to fix that. Any help is appreciated.

Clone Algorithm
19
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 577c1511ff0e910f8c2648b3
There was a runtime error.
2 responses

If you're inPosition, you're exiting if the ratioToday is <upper or >lower, which is always. That means you're only in for a minute.

You should distinguish between being long and short the pair, and incorporate this information into the conditions in lines 23 and 30.

Ok, I have modified the code a little further.

But I need it to only purchase once when it crosses above 2.5/-2.5 and then close once it crosses back below. The algo is still making multiple purchases as the ratio might stay outside the band for a couple minutes.

Any help is appreciated.

Clone Algorithm
19
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 577e6368250fc111f20a83ad
There was a runtime error.