Back to Community
Multiple pairs trading algorithm


I wrote a paris trading algorithm with more than one pair. I manage to trade each pair once every day, but somehow the backtest results showed that sometimes I buy and sell the same equity on the same day, which should not happen. Can someone help me figure out why and fix it.


3 responses

I have the same question.
The only way I have found to manage this is to do something like this:
open_orders = get_open_orders(stock)
if context.portfolio.positions[stock].amount is 0 and len(open_orders) is 0:
# open position.

Not sure if this is what you need but maybe it helps a little. If you have some open orders (say stop orders) you need to create more complicated logic and parse open orders probably to find out what they are created for...Also it doesn't cover the case if position is already closed this day.

I know that one other platform I am using does have configuration parameter like 'one entry per stock' which makes it very easy to make sure that your strategy doesn't open lot's of similar positions because of some bug, etc. This would be very useful specially in idea fast prototyping.

@ Ryan Chen

I think I found the bug! So if I understand what you are doing right you are trading daily and when you trade it is within certain hours. What is happening in your code is that in your order logic you have a bunch of ifs and they return when one of them is completed. Now what happens is that if the algo enters one of those if statements and executes the containing logic if returns from the entire function, not just the loop. So if your first pair fits one of the ifs then the code will never reach the second pair. Now that we know how the code is exiting the function we can see how it is being called twice and you are executing multiple orders for the same security. Since you call pairs_trade in handle_data and you are running in minute mode, the code to decide to call pairs_tradegets executed every minute. Since you give it a range of time in the day what happens is that if it is in the allowed time period and your first pair makes a transaction then the function is returned back to handle_data and then when handle_data is ran on the next minute(and probably still in the allowed time period) the code re-enters pairs_trade's for loop and possibly executes another transaction for the same pair, or if not, than it might execute something for the second pair. Does that make sense?

Some tips:

  • Use schedule_function that way you don't have to worry about a special range of times, and writing all of the code to make it happen because as we just saw it can be buggy.
  • Use an if...elif...else structure to make sure only one if block gets executed.
  • Make sure to check if security in data so you don't get any KeyErrors

I think that is a good place to start, while I'm sure your algo will evolve over time these suggestions are generally good for any algo.


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.

@ James Christopher

Thanks! I got it! I think you are right. Thanks for your help!