Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
First algo - a question about portfolio history

Hello,

Thanks in advance for your help. Studying the API docs, and unable to find the answer I need. What historical portfolio data do I have access to? Can I access the last date a security was purchased or sold via my account?

Thanks!

10 responses

Hello Matthew,

What, specifically, do you need to accomplish?

The answer sorta depends on what you are needing to do. If you need to stop your live-trading algo and download a trading history through Quantopian, I don't know how to do it. As the algo runs, you can store data (e.g. context.my_data), but when the algo is stopped, it gets wiped out (and even if it didn't, there would be no way to download it). It is a different story if you are wanting to access the data as your algo runs. For example, maybe you want to wait N days after buying / selling, prior to buying / selling again?

If you are backtesting, a trick is to use the debugger for getting at stored data (e.g. context.my_data). If this is what you need to do, I can explain further.

If you are live trading (paper / real money) at Interactive Brokers, I'd expect that they'd offer downloadable account activity reports. Perhaps someone with experience can comment.

Grant

Thanks Grant. Essentially, yes, I'd like to be able to write logic such as:

  • If I hold onto a security for x days, regardless of performance, sell.

  • If I've sold a security within the past x days, skip it when purchasing new lots.

These are just examples, but hopefully illustrate my intended capabilities.

Thanks!

Yes, it is doable, but if you are just getting started on Quantopian, I'd recommend first writing some dirt-simple algos to get up the learning curve. Hopefully, you have some familiarity with Python?

Good to know that it's possible. And yes, I'm definitely starting with some simpler concepts (but at the same time fleshing out some more sophisticated ideas).

And while I haven't done much work in Python specifically, I've been a software engineer for decades, so I've spent the weekend getting used to the syntactical and philosophical idiosyncrasies. But I am very far from new to programming.

Thanks for your help. I suppose once I dig into the live trading features I'll unearth the specifics of portfolio history.

Given your background, it should be no problem. A few quick tips:

  • Add SPY to your universe of securities, if you want to ensure that handle_data gets called every trading minute. This can avoid confusion, if you end up adding thinly traded securities.
  • See get_datetime() in the help docs.
  • Note that when you place an order, an order object is created, with a unique ID. You can then use get_order() to view the order object.
  • To persist data, you can use context (e.g. context.my_data). I think globals also work, but probably not the best practice.

One way to approach it (I think) is to keep track of your orders by security, and positions associated with those orders. I'll have to think about it. Maybe someone has an example worked out?

Thanks for your wisdom. I'll be sure to follow all those tips.

Yes, being able to track past orders would definitely solve my problem.

@Matthew C., An alternative for you.

It's a bit involved, but offers extensions which you can search for here plugable code block. There's no order tracking, just state variables stored on the context.S dictionary. Uncomment the #'s in the symbols list to view the whole DOW.

Thanks very much. I'll go through this piece-by-piece tomorrow. Take care.

Matthew - welcome! To start learning the Quantopian API, take a look at the 3 part tutorial series. To see how long you've held a security, you can setup a custom variable. Something like:

initialize(context):  
  # stock the algo is trading  
  context.stock = symbol('SPY')

   schedule_function(day_counter, date_rules.daily())

handle_data(context,data):  
  # sample ordering logic  
  if price < 50:  
     order_target(context.stock, 100)  
    # mark when the stock was ordered  
     context.order_date = get_datetime()  
     # log the order  
     log.info('Ordered stock %s at %s' % (context.stock, get_datetime())


def day_counter (context,data):  
   context.order_date+=1  

Here is a thread to check the market time in the algo: https://www.quantopian.com/posts/how-to-determine-time-in-market#54c9c479efe1c672120001d6

In the API you can use schedule_function() to specify a day and/or time to make a trade and get_datetime() to check the market time. If you'd like to see a rebalance algorithm, here is an example.

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.

Very helpful. Thank you for taking the time to share.