Back to Community
$ABGB and $FSLR Pair Trading

Hello everyone!

I am new to quantopian and this is my very first public backtest. It is a simple pair trading strategy using $ABGB and $FSLR. Any feedback is greatly appreciated.

Clone Algorithm
7
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
"""
This is a template algorithm on Quantopian for you to adapt and fill in.
"""
import quantopian.algorithm as algo
from quantopian.pipeline import Pipeline
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.filters import QTradableStocksUS
import numpy as np

def zscore(series):
    return (series - series.mean()) / np.std(series)

def initialize(context):
    """
    Called once at the start of the algorithm.
    """
    context.stock1 = sid(45676)
    context.stock2 = sid(32902)
    
    context.upper_threshold = 1.0
    context.lower_threshold = -1.0
    
    context.day = 0
    # Rebalance every day, 1 hour after market open.
    algo.schedule_function(
        rebalance,
        algo.date_rules.every_day(),
        algo.time_rules.market_open(hours=1),
    )

    # Record tracking variables at the end of each day.
    algo.schedule_function(
        record_vars,
        algo.date_rules.every_day(),
        algo.time_rules.market_close(),
    )


def before_trading_start(context, data):
    """
    Called every day before market open.
    """
    #context.output = algo.pipeline_output('pipeline')

    # These are the securities that we are interested in trading each day.
    #context.security_list = context.output.index


def rebalance(context, data):
    """
    Execute orders according to our schedule_function() timing.
    """
    context.stock1_price = data.history(context.stock1, 'price', 25, '1d')
    context.stock2_price = data.history(context.stock2, 'price', 25, '1d')
    
    diff_series = context.stock1_price - context.stock2_price
    if str(diff_series) != 'nan' and context.day > 10:
        z_score = zscore(diff_series)[-1]
        if z_score > context.lower_threshold and z_score < context.upper_threshold:
            order_target_percent(context.stock1, 0)
            order_target_percent(context.stock2, 0)
        elif z_score > context.upper_threshold and context.stock1 not in context.portfolio.positions and data.can_trade(context.stock1) and data.can_trade(context.stock2) and context.stock1 not in get_open_orders():
            order_target(context.stock1, -10)
            order_target(context.stock2, 10)
        elif z_score < context.lower_threshold and context.stock1 not in context.portfolio.positions and data.can_trade(context.stock1) and data.can_trade(context.stock2) and context.stock1 not in get_open_orders():
            order_target(context.stock1, 10)
            order_target(context.stock2, -10)
        
    context.day += 1
    pass


def record_vars(context, data):
    """
    Plot variables at the end of each day.
    """
    pass


def handle_data(context, data):
    """
    Called every minute.
    """
    pass
There was a runtime error.