Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Importing Transactions into Backtester

Wondering if someone could point me to an example of importing transactions into the Quantopian backtester? I have my own portfolio construction code that is running outside of Quantopian that can produce a CSV file with a list of buy/sell transactions. I would like to run my model portfolio through the backtester to see how it compares to my own performance measurements and take advantage of some of the other features.

I've tried importing the transactions as security data using fetch_csv but that seems to replicate the buy and sell transactions. For example the CSV file that I import looks like:
date, symbol, shares, action
1/6/2009, AAPL,100, buy
1/20/2009, AAPL, 100, sell
1/21/2009, C, 200, buy
...

When I import this into Quantopian, the buy in AAPL that occurs on 1/6/2009, is replicated on every trading day between 1/6/2009 and 1/19/2009. The sell is also replicated on every day between 1/20/2009 and the next day I trade AAPL again. I know this is happening because Quantopian is replicating the security data, but I'm wondering if there is some way of turning off this replication or perhaps a different framework for importing transactions.

Thanks

3 responses

Hi Chris,

By default, the data in Fetcher is forward-filled. So your algo will buy 100 shares of AAPL every day from 1/6/2009-1/19/2009. You can easily fix this behavior in your CSV setup. Something like this:

date, symbol, shares, action
1/6/2009, AAPL,100, buy
1/7/2009, AAPL,0, no action
1/8/2009, AAPL,0, no action
1/9/2009, AAPL,0, no action
1/10/2009, AAPL,0, no action
... 1/20/2009, AAPL, 100, sell

You can then code your strategy to buy when the signal says BUY, sell when it says SELL and otherwise take no action. Here is a sample skeleton:

def handle_data(context, data):  
  for stock in data:  
      if data[stock]['action'] == 'no action':  
         log.info("Not trading today")  
          return

      if data[stock]['action'] == 'SELL':  
          log.info("Selling today")  
          order(stock, shares)

      if data[stock]['action'] == 'BUY':  
          log.info("Buying today")  
          order(stock, shares)  
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.

Thanks Alisa.

I think your suggestion will work for the time period between my buy and sell, but I'm still left with an issue that the sell signal is filled forward after 1/20/2009 and I'm ending up with a growing short position after my sell. I think, however that your on the right track and that I'll need to modify my portfolio constructor to output a daily set of portfolio positions and target weights.

BTW, I made a temporary update to the code to work around this by using an order_target(stock, shares) call for the buys and order_target_percent(stock,0) for the sells. This way I only buy up to the target amount and always sell down to zero, however this will not work in the long run as my portfolio constructor does generate partial buy / sell orders.

thanks,
Chris

to keep from selling into short territory, why not always make the last line of your CSV indicate 'no action'?