Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
portfolio local to algorithm?

If I have already made purchases through Interactive Brokers, and then I kick off a new algorithm for trading which looks at my context.portfolio when determining what to sell, will the context be populated with shares already in my IB portfolio? Or is the context local to the algorithm running?

I guess The question also holds for the cash balance. If I add more cash to my IB account while the algo is running will the algo see the new influx of cash?

3 responses

We fetch you portfolio and cash balance at the start and update them when updates are sent to us by IB.

Note that you will only see U.S. equities, i.e., things we can map to Quantopian SIDs, in your portfolio. If you have anything else in your account, it will be invisible in Quantopian.

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.

That seems like it could be problematic if people want to run multiple algos. Or if I'm running a single intra-day algo which sells everything at the end of day, but I still have stocks I want to keep long.

what if I do something like set context.portfolio = {} during initialization?

And what do you mean

and update them when updates are sent to us by IB

When does that occur? sounds like it would impact a currently running algo (which may be a good thing, but I would think we should have control over that).

And is this behavior reflected in paper trading as well?

That seems like it could be problematic if people want to run multiple algos. Or if I'm running a single intra-day algo which sells everything at the end of day, but I still have stocks I want to keep long.

Our expectation is that you will set up a separate sub-account for your Quantopian algorithm to run in. If you have stocks you want to keep separate from your algorithm, you should not keep them in the sub-account that your algorithm is configured to use.

By design, you cannot have multiple Quantopian algorithms running against the same IB account / sub-account.

When does that occur? sounds like it would impact a currently running algo (which may be a good thing, but I would think we should have control over that).

We poll IB periodically to check if your cash balance has changed. IB sends pushes updates to us asynchronously whenever the equities in your account are changed, or because an order your algorithm placed was filled, or because you sold or bought stock through the IB web site while the algorithm was running, or because you exceeded your margin limit and IB put a margin call on your account and liquidated some of your holdings, etc.

When your algorithm needs to make buy/sell decisions based on what is in your account, it should always be consulting context.portfolio and treating whatever is recorded there as canonical; it should not keep its own, separate state about your holdings, or if it does, it should make sure to reconcile that state with what is in context.portfolio.

This is true of any algorithmic trading platform, not just Quantopian. Your holdings are what your brokerage says your holdings are, not what your algorithm thinks they should be.

And is this behavior reflected in paper trading as well?

In IB paper trading? Yes, to whatever extent as IB's paper trading implementation reflects it (which it seems to, by and large). As far as Quantopian is concerned, IB paper trading has no functional difference from IB real-money trading; the only difference is that the IB account number you give to Quantopian is a paper-trading account number rather than a real-money account number.