Live Trading Restarts?

Say you have an algo trading live and it is holding active positions that have not yet met the criteria to sell.  Something happens and the algo crashes.  You redeploy it to get it started back on track and it goes through it's initialization process.  Does it see the positions existing in the account and sell them when the criteria are met?  I think I read somewhere that algos will only close positions that they opened to avoid selling off manual trades that were made in the same account directly through the broker.  While the algo did indeed open the position, it was during the previous deployment.  Does it recognize that and sell the stock, or do you have to liquidate the position manually?

Also, if the algo is written to not buy a symbol if there is an existing position, will it see the open position and not rebuy it again until it is liquidated?

Thanks for the help.

Update Backtest

phu very difficult question

in the algo you develop there is a portofolio class where you can see assets you hold when starting for example (init).
...............when it crashes i don't know what happens...a guy from the staff has to answer that but you would need to develop the criteria when it starts with already some assets in the when you a have to check over the portfolio if you already have it........ :) !
if you earned littlebit of money with that stock over 1 day.....the order would execute with for example setholdings(spy,1)
and buy you one more stock. maybe it is what you want..who nows.

string.Join(";", Portfolio.Select(x => x.Key + "=" + x.Value).ToArray());


Michael, you did NOT make my day! LOL  But somehow, I was expecting that the answer to my question was not going to be easy.  I appreciate your response.  Maybe the best solution is to manually liquidate everything in the account, and take what losses may come, before you redeploy. Then you would be back to scratch.  This is gonna require a bit more thought! :(

Thanks again ............


but if you start with some assets in the portfolio and you use a warmup to be back where you stopped....there would be no one can tell if you that is not what you want .... but wait maybe for an official answer from the staff....

but if that algo buy a stock at the first run you have to handle that runing it again taking a look in the portfolio. nothing can get a list without any problem...


Hi Christian,

I have been running lean locally.  Answer to you question is both "yes" and "no".  When algorithm crashes it loses all of its states.  Positions in you trading acount reamins open.

When you start your algorith again, during the inilization phase of lean it rebuilds the portfolio and securities that existings in your trading account.  Now to get back on the feet and start trading, you will have to utilize history or warmup during initialization (write some code that goes throught your algorithm and rebuild the states but does not actully perform order/trades).  Once warmup is done algorithm can start from same state.  Keep in mind that accuracy of state rebuild depends on how frequently your trades in algo and how long it took you first to know that alogrim has crashed and second to restart algo again.  

Personally I utilize history function to pump in bars I need into algorithm.  If you use technical indicators you have make sure that you feed tradebars in them as well make them capable of start trading right after initialization state.

I find that when algo crashes and if you are running your algo on QC live, time you get email saying your algo crashed is variable and incosistant (this may be fixed).  If you run locally then you can write some software to monitor your algo and trigger text or email.    Alternatively you can utilize scheduled events and send heartbeat somewhere in your algo every X minuties.  




Thanks guys, I appreciate the feedback.  I'm still trying to wrap my head around all of this, but Desi, as a sidebar you brought up something else I was wondering about but haven't yet started researching.  Is there a way to have your algorithm send emails or texts?  It sounds like the answer is "yes" if you are running locally. Is it also possible if your algo is running in the QC cloud?  I ran live for awhile (and stopped for multiple reasons), and I also found the notifications to be inconsistant.  This may have been fixed, but I had considered your "heartbeat" text idea as a means of keeping tabs on the algo without having to check on it.  If you don't get a text at a certain time, you need to check on it!  If you do, just delete it and know it is still running.  Learning how to do that is on my bucketlist and any suggestions you can make are welcome.

Thanks again to all!


Update Backtest


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 QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect 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. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


This discussion is closed