Hi, I'm new to this community. I'm starting to use Quantconnect with a simple trading bot, moving average based.
During this experience, I've seen that I haven't clear how close my open position. I use the following code to open the position:
# Open position BUY the quantity described in 'volume'
def open_position_buy(self):
self.Debug("BUY")
self.SetHoldings(self.__Symbol, 0.2, True)
# Open position SELL the quantity described in 'volume'
def open_position_sell(self):
self.Debug("SELL")
self.SetHoldings(self.__Symbol, -0.2, True)
and for close all position I use :
#Close all position open
def close_all_position(self):
orders = self.Transactions.GetOrders(None)
for order in orders:
if self.Portfolio[order.Symbol].Invested:
self.Liquidate(order.Symbol)
self.Debug("liquidate")
But, in the order, when liquidate operation was called, I see an operation inverse of my with the voice "liquidate" and my capital doesn't increase. With the same method with MT5, I get more than 300€.
Is this the correct method for the close position and give the money realized?
How can I close all positions wen bot or simulation was closed?
Regards, Edoardo
Rahul Chowdhury
Hey Edoardo,
1. You can liquidate all your securities in your portfolio with self.Liquidate(). It's difficult to understand without seeing the entire code why you aren't receiving cash when you liquidate . Could you please post a backtest that produces the problem?
2. The OnEndOfAlgorithm method executes at the end of the algorithm. You can use it to liquidate all your securities when the backtest is complete.
def OnEndOfAlgorithm(self): self.Liquidate()
Edoardo Zanon
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.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!