Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
pair trading BABA vs YHOO

I am trying to catch the moment when zscore > or <-4. Then stop all the trading, wait until these two stocks finally converged or profit reaches 10%, clear all the positions. Redo it again when zscore >4.
Could someone fixes the code for me please? Thanks a lot!

2 responses

You're headed in the right direction. To help you out:

I am trying to catch the moment when zscore > or <-4.

Then stop all the trading,

wait until these two stocks finally converged or profit reaches 10%, clear all the positions.

  • Check the profit using context.portfolio.pnl
  • Close the positions by doing order_target(stock, 0)

Redo it again when zscore >4.

  • this will get triggered in your trading logic, since len(context.portfolio.positions) is now 0
Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

Put any initialization logic here. The context object will be passed to

the other methods in your algorithm.

def initialize(context):
context.stock1 = sid(14848)
context.stock2 = sid(47740)

Will be called on every trade event for the securities you specify.

def handle_data(context, data):
deviation = data[context.stock1].stddev(30)

if(deviation != None):  
    mean = data[context.stock1].mavg(30)  
    zscore = (data[context.stock1].price - mean) / deviation  

# for cash in context.portfolio.cash:
if context.portfolio.positions_value 4.0):
order_percent(context.stock1, -.5)
order_percent(context.stock2, .5)

    elif(zscore < -4.0) and context.portfolio.positions_value<1.1*context.portfolio.starting_cash:  
        order_percent(context.stock1, .5)  
        order_percent(context.stock2, -.5)  

def rebalance(context, data):

    elif context.portfolio.returns>1.1:  
        order_target(context.stock1, 0)  
        order_target(context.stock2, 0)  

elif(zscore > -4.0 or zscore < 4.0):

reduce_position(context.stock1, context.portfolio, all)

# reduce_position(context.stock1, context.portfolio, all)
# log.info("Positions reduced")

def reduce_position(stock, portfolio, abs_quantity):
"""
decrease exposure, regardless of position long/short.
buy for a short position, sell for a long.
"""
pos_amount = portfolio.positions[stock].amount
if pos_amount > 0:
order(stock, -1 * abs_quantity)
elif pos_amount < 0:
order(stock, abs_quantity)

I just fix the code. But the algo does not close the position. Do you know why? Thank you!