Hi,
Despite going through problems posted, I can't find a solution on how to use close a position, without implementing a reverse market order. As you can see, at the end of my code I open 2 positions, each with separate ticket-name. Then, I want to close only one of them.
I tried changing the liquidate function to self.Liquidate(self.ticketPair1.Symbol) as it has been suggested previously, but unfortunately it doesn't solve the problem.
# region imports
from AlgorithmImports import *
# endregion
class CustomIndexStrategy(QCAlgorithm):
def Initialize(self):
self.Pair_1_Multiplier = 1
self.Pair_1 = "USDDKK"
self.holdingDays = 1
self.SetStartDate (2010, 1, 1)
self.SetEndDate(2022,7,1)
self.SetCash(10000)
self.SetBrokerageModel(BrokerageName.OandaBrokerage)
self.EURSEK = self.AddForex(self.Pair_1, Resolution.Daily, Market.Oanda)
self.symbols = [self.Pair_1]
self.prevPrices = { symbol : RollingWindow[QuoteBar](7) for symbol in self.symbols }
self.ticketPair1 = None
self.ticketPair2 = None
self.maximumholdingdays = 4
self.volume = 10
def OnData(self,data):
self.quantity_1 = self.CalculateOrderQuantity(self.Pair_1 , self.volume)
for symbol in self.symbols:
if data.ContainsKey(symbol):
self.prevPrices[symbol].Add( data[symbol] )
if not all([ window.IsReady for window in self.prevPrices.values() ]):
return
Pair1_window = self.prevPrices[self.Pair_1]
Pair1_1D = Pair1_window[1].Close
Pair1_0D = Pair1_window[0].Close
if self.ticketPair2 is None and self.Securities[self.Pair_1].Exchange.ExchangeOpen is True and Pair1_0D < Pair1_1D :
self.ticketPair2 = self.MarketOrder(self.Pair_1, self.quantity_1 )
self.priceLong= self.ticketPair2.AverageFillPrice
self.ticketPair1 = self.MarketOrder(self.Pair_1, self.quantity_1)
self.openingTime = self.ticketPair2.Time
if self.ticketPair2 is not None and self.Securities[self.Pair_1].Exchange.ExchangeOpen is True and Pair1_0D > self.priceLong and self.UtcTime >= self.openingTime + timedelta(days = 1):
self.Liquidate()
Nico Xenox
Hey Sebastian Wozniczka,
the problem here is that both self.ticketPair2 and self.ticketPair1 have the same Symbol Value. Thats why it isnt possible to liquidate it trough the symbol. Currently a market order with the filled amount is the only solution... To be sure that everything is closed how it should you can do it like this:
This way the other ticket doesnt get liquidated.
Liquidating symbols by tickets works just if you have different tickers for each ticket.
Hope it helps ;)
Sebastian Wozniczka
Nico Xenox
I understand now, thank you for this clear explanation :)
Sebastian Wozniczka
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!