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.

  1. # region imports
  2. from AlgorithmImports import *
  3. # endregion
  4. class CustomIndexStrategy(QCAlgorithm):
  5. def Initialize(self):
  6. self.Pair_1_Multiplier = 1
  7. self.Pair_1 = "USDDKK"
  8. self.holdingDays = 1
  9. self.SetStartDate (2010, 1, 1)
  10. self.SetEndDate(2022,7,1)
  11. self.SetCash(10000)
  12. self.SetBrokerageModel(BrokerageName.OandaBrokerage)
  13. self.EURSEK = self.AddForex(self.Pair_1, Resolution.Daily, Market.Oanda)
  14. self.symbols = [self.Pair_1]
  15. self.prevPrices = { symbol : RollingWindow[QuoteBar](7) for symbol in self.symbols }
  16. self.ticketPair1 = None
  17. self.ticketPair2 = None
  18. self.maximumholdingdays = 4
  19. self.volume = 10
  20. def OnData(self,data):
  21. self.quantity_1 = self.CalculateOrderQuantity(self.Pair_1 , self.volume)
  22. for symbol in self.symbols:
  23. if data.ContainsKey(symbol):
  24. self.prevPrices[symbol].Add( data[symbol] )
  25. if not all([ window.IsReady for window in self.prevPrices.values() ]):
  26. return
  27. Pair1_window = self.prevPrices[self.Pair_1]
  28. Pair1_1D = Pair1_window[1].Close
  29. Pair1_0D = Pair1_window[0].Close
  30. if self.ticketPair2 is None and self.Securities[self.Pair_1].Exchange.ExchangeOpen is True and Pair1_0D < Pair1_1D :
  31. self.ticketPair2 = self.MarketOrder(self.Pair_1, self.quantity_1 )
  32. self.priceLong= self.ticketPair2.AverageFillPrice
  33. self.ticketPair1 = self.MarketOrder(self.Pair_1, self.quantity_1)
  34. self.openingTime = self.ticketPair2.Time
  35. 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):
  36. self.Liquidate()
+ Expand

Author

Sebastian Wozniczka

September 2022