Insight to Liquidate all position

Back

At 15.59 Liquidate() is schedulled to close all the opening positions.
Which should be the right Insight to work with this Liquidate()?
I mean, which should be the right XXX?

    def ClosePositions(self):
        self.Liquidate()
        insight = Insight.Price(XXX,Expiry.EndOfDay, InsightDirection.Flat)
        self.EmitInsights(insight)

Update Backtest







 
0

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.


I could have several differents Option contracts for one underlying, each one of them with its own name, so that I would like to close all the position automatically and not one by one, to be sure that the portfolio is empty before the market close.

0

You can do something like:

def ClosePositions(self):
    openPositions = [ x.Symbol for x in self.Portfolio.Values if x.Invested ]

    insights=[]
    for x in openPositions:
        insight = Insight.Price(x, Expiry.EndOfDay, InsightDirection.Flat)
        insights.append(insight)
    self.EmitInsights(insights)

    # maybe add a self.modelReady=False to prevent emitting insights after this event,
    # turn it back on when market open in another scheduled function

No need to use self.Liquidate() here as the PortfolioModel should be liquidating all the Flat insights anyways

1

Thank you very much Adam. Very usefull!!

0

Update Backtest





0

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.


Loading...

This discussion is closed