Hello,

In my code I want to set these rules:

  • if today's price is higher than yesterday, open 2 short positions. One position without and one with trailing SL.
  • After 4 days, close only the 1st position

 

I implemented these rules but I receive this error: 

Trying to dynamically access a method that does not exist throws a TypeError exception. To prevent the exception, ensure each parameter type matches those required by the 'QuantConnect.Orders.OrderTicket'>) method. Please checkout the API documentation.
  at OnData
    self.Liquidate(self.ticketPair1)
   at Python.Runtime.PythonException.ThrowLastAsClrException()
   at Python.Runtime.PyObject.Invoke(PyTuple args in main.py: line 45

The code looks as follows:

# region imports
from AlgorithmImports import *
# endregion

class Strategy(QCAlgorithm):

    def Initialize(self):

        self.Pair_1 = "EURUSD"
        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 

    def OnData(self,data):
      
        self.quantity_1 = self.CalculateOrderQuantity(self.Pair_1, 1)
        
        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.ticketPair1 is not None and self.UtcTime < self.ticketPair1.Time + timedelta(days=(self.holdingDays)):
            return

        if self.ticketPair1 is None and self.Securities[self.Pair_1].Exchange.ExchangeOpen is True and Pair1_0D > Pair1_1D :
            self.ticketPair1 = self.MarketOrder(self.Pair_1, 1000 )
            stop_price = self.ticketPair1.AverageFillPrice * 0.99
            self.stop_loss_ticket = self.StopMarketOrder(self.Pair_1, -self.quantity_1, stop_price)
            self.price = self.ticketPair1.AverageFillPrice

        if self.ticketPair1 is not None and self.Securities[self.Pair_1].Exchange.ExchangeOpen is True and self.UtcTime >= self.ticketPair1.Time + timedelta(days = 4): 
            self.Liquidate(self.ticketPair1)
            self.ticketPair1 = None


Do you know what could be the reason of it ? If I delete the last 3 rows, so orders will never be closed, the strategy opens trades correctly, so there is not problem with implementing orders. 

 

I would appreciate any help/tips :)