Back to Community
Is there any "Algorithm Stopped" event, to gracefully exit?

Is there any "Algorithm Stopped" event, to gracefully exit? If not, can it be added?

16 responses

Are you taking about if an error is thrown? If so, use a try/except to ensure the error does not stop your code.

I am referring to the situation when a live algorithm which is active and trading real money, is stopped manually. This would be more like an OnExit event or a destructor event, which could be used to maybe close out positions, or to persist the state or context so that on a subsequent initialize, it could be reloaded. (I have that as a question in another post, on how to persist and reload state). I can see myself wanting to do this as I tweak my algorithm, and want to put out newer versions.

I was wondering this as well because one may want to stop the algo, however you may want to simultaneously firesale all the positions as well (clearly depends on why you're stopping the algo in the first place...)

The event would allow you to do such a thing.

I would also welcome the convenience to exit the algo "gracefully". I have had to stop both IB live and IB paper traded algos and then have to log into IB and make sure there are no open orders and/or close out positions manually.

There are several ways a live trading algorithm can be stopped.

  1. You manually press the Stop Algorithm button in the live trading dashboard
  2. Algorithm hits an error in the code
  3. You login in to TWS/Webtrader to check your IB account

In cases #1 and #2, the algorithm is stopped and its session is finished. It cannot be resumed. When the algorithm is stopped, this only prevents new orders from being submitted and no more calls are made to handle_data(). Your positions remain in the portfolio. If you have any open orders, these will remain open until filled by the broker, they are not automatically cancelled.

If your algorithm hit a code error, you can go back to the IDE, tweak the code, run another full backtest in minute mode and redeploy to live trading. When you redeploy, this is considered a new algorithm. It has no memory of the previous algo. This new algo will have a new history, logs, risk metrics, etc.

In case #3, IB allows only one connection per account. If your algo is live trading on Quantopian and then you login to TWS/Webtrader using the same IB username/password, then IB will terminate your Quantopian connection. This will "pause" your Q algorithm. The algo won't receive any market data or place any trades. When you return to your algorithm and re-enter your password, it will continue to run. The variables saved in "context' will pick up from their last-seen state. To simultaneously have your algo run on Quantopian and check your IB account, you should create a second login:



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.

As a hack, if you are willing to wait a day, you could upload a fetcher file overnight that had stop instructions embedded in it (the algo code would have been written to interpret the instructions). You'd exit all positions, based on the instructions in the file, and then go into a hold state. You'd then stop the algo manually (or as another hack, time-out a call to handle_data). --Grant

It would be neat if Quantopian could support IB orders with different time-in-force options (specifically the GAT order attribute). So you could, for example, schedule "fail safe" orders every day (for a pre-determined time, such as market close minus 5 minutes) to close out all of your positions, which your algorithm could then cancel if it were still running properly. So if your algo crashes, the orders are never cancelled, and you close out everything at end of day.

You can specify a time (or day) to submit your order using schedule_function:

And if you want to have multiple order conditions (ie buy a basket at 10AM and then buy a different basket at 2PM) you can have multiple schedule_function calls.

Also, in live trading with IB, all open orders will automatically get cancelled at the end of the day. This is to prevent your algorithm from any wild price swings overnight. Hope that helps!

I assume that if my algo crashes, none of my scheduled functions will execute?

That's true, if the algo crahses, no more calls will be made to the algorithm code.

So I realize my example was a little different from GAT orders, but I wanted to give some background about algos trading with IB.

Cant you just link a weighting to your order fuction or selling fuctions that would eliminate trades if the variable was activated? seems simple... multiply by boolean.

Grant Kiehne:

As a hack, if you are willing to wait a day, you could upload a fetcher file overnight

FunctionCalledOutsideOfInitialize: 0027 'fetch_csv' only permitted within initialize function  

As a workaround you could write a function to handle shutdown as you wish, include a context.shutdown=False flag in initialize() and then check the state of that flag on all your scheduled functions. Then when you want to shutdown your algo, stop it, change the flag to true, and relaunch your algo so that now it will only execute your shutdown/cleanup code.

Not ideal. It would be nice if a handle_shutdown() method would automatically be executed by the quantopian framework when it hits and error or is manually stopped.

atiredmachine , why not putting the cleanup code in a separate algorithm. This way I don't need to re-edit production code. When I stop the algorithm, I lose the context state anyway.

How is automatic persisting possible here? Anything in the Quantopian API? I think external libraries that do that kind of thing cannot be imported here.

I don't think atiredmachine means that persisting would be automatic without any manual intervention. The data still has to be entered into the file which fetch_csv loads. From what I understand, Q does not export files, they only import. So no automatic, look no hands, persistence. I do not think Q has any API for persistence through stop / starts. Although I do think they have discussed it from time to time.