Hi, guys.

My second algorithm with pairs tradings strategy. The idea is simple, find two correlated stocks. Long the running slower one, and short the running faster one. Make the profit from the gap.

This test is based on COCA and PEPSI, and result shows the return is not extremely high, but very stable.

Clone Algorithm

619

Loading...

There was an error loading this backtest.

Backtest from
to
with
initial capital

Cumulative performance:

Algorithm
Benchmark

Custom data:

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 |

import numpy as np import pandas as pd from collections import deque def initialize(context): context.nobs = 30 context.max_notional = 100000 context.min_notional = -100000 #currently we are running COCA& PEPSI context.stocks = [sid(4283), sid(5885)] context.ratio = deque([]) context.strategy = 0 def handle_data(context, data): price0 = data[context.stocks[0]].price price1 = data[context.stocks[1]].price shares0 = context.portfolio.positions[context.stocks[0]].amount shares1 = context.portfolio.positions[context.stocks[1]].amount if len(context.ratio)<context.nobs: temp = price0/price1 context.ratio.append(temp) else: beta=sum(context.ratio)/context.nobs if price0 > beta*1.02*price1 and context.strategy != 1: order(context.stocks[0], -shares0) order(context.stocks[1], -shares1) num_shares = int(context.max_notional / price1) order(context.stocks[1], num_shares) order(context.stocks[0], -1 * num_shares/beta ) context.strategy = 1 elif price0 < beta*0.98*price1 and context.strategy != 2 : order(context.stocks[0], -shares0) order(context.stocks[1], -shares1) num_shares = int(context.max_notional / price0) order(context.stocks[0], num_shares) order(context.stocks[1], -1 * num_shares*beta ) context.strategy = 2 context.ratio.popleft() temp=price0 / price1 context.ratio.append(temp) record(gap=price0-beta*price1) record(shares0=shares0, shares1=shares1)

This backtest was created using an older version of the backtester. Please re-run this backtest to see results using the latest backtester. Learn more about the recent changes.