Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

    Issuing multiple orders, how do I only issue one order at a time?

    General Code/Help
    4
    19
    110
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • cept0r
      cept0r last edited by

      I'm trying to only issue one limit buy if condition x, z is met. And if said limit buy executes I want to issue a sell limit.

          def __init__(self):
              self.dataclose = self.datas[0].close
              self.ind = AmaBands(plot=True, subplot=False)
              self.step = False
              self.order = None
              self.o = list()
      
          def next(self):
              self.log('Close %0.2f, Mean %0.2f, Buy1 %0.2f, Sell1 %0.2f, Buy2 %0.2f, Sell2 %0.2f, Stop %0.2f'
                       % (self.dataclose[0], self.ind[0], self.ind.bot[0],
                          self.ind.top[0], self.ind.bot2[0], self.ind.top2[0],
                          self.ind.bot5[0]))
      
              orders = self.broker.get_orders_open()
      
              if self.order:
                  return
      
              mean = self.ind[0]  # Current mean(moving average) value
      
              size1 = cerebro.broker.get_cash() * 0.035 / self.data
              size2 = size1 * 2
              size3 = size2 * 2
              size4 = size3 * 2
      
              pos = self.position.size
      
              stop = self.ind.bot5[0]
              close = self.dataclose[0]  # Current close
      
              # Buy levels
              buy1 = self.ind.bot[0]
              buy2 = self.ind.bot2[0]
              buy3 = self.ind.bot3[0]
              buy4 = self.ind.bot4[0]
      
              # Sell levels
              sell1 = self.ind.top[0]
              sell2 = self.ind.top2[0]
              sell3 = self.ind.top3[0]
              sell4 = self.ind.top4[0]
      
              """Initializing algorithm..."""
              if not self.position:
                  if close < mean:
                      self.buy(price=buy1, size=size1, exectype=bt.Order.Limit)
                      self.log('Init... %.2f' % close)
                      if pos > 0:
                          self.sell(price=sell1, size=-size1, exectype=bt.Order.Limit)
                          self.log('Buy order executed.-. creating sell order %.2f' % close)
      

      Here is my output:

      Starting Portfolio Value: 100000.00
      2020-02-01, Close 9356.33, Mean 9360.70, Buy1 9344.78, Sell1 9376.61, Buy2 9319.51, Sell2 9401.88, Stop 9203.98
      2020-02-01, Init... 9356.33
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Accepted
      2020-02-01, Status 2: Ref: 1, Size: 0.03740782977941138, Price: 9344.78348
      2020-02-01, Close 9357.21, Mean 9360.67, Buy1 9344.76, Sell1 9376.59, Buy2 9319.49, Sell2 9401.86, Stop 9203.98
      2020-02-01, Init... 9357.21
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Accepted
      2020-02-01, Status 2: Ref: 2, Size: 0.03740431175532024, Price: 9344.76025
      2020-02-01, Close 9358.41, Mean 9360.66, Buy1 9344.75, Sell1 9376.57, Buy2 9319.47, Sell2 9401.85, Stop 9203.98
      2020-02-01, Init... 9358.41
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Accepted
      2020-02-01, Status 2: Ref: 3, Size: 0.03739951551599044, Price: 9344.74762
      2020-02-01, Close 9358.30, Mean 9360.64, Buy1 9344.73, Sell1 9376.55, Buy2 9319.45, Sell2 9401.83, Stop 9203.98
      2020-02-01, Init... 9358.30
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Accepted
      2020-02-01, Status 2: Ref: 4, Size: 0.03739995512005387, Price: 9344.72616
      2020-02-01, Close 9355.65, Mean 9360.57, Buy1 9344.65, Sell1 9376.48, Buy2 9319.38, Sell2 9401.75, Stop 9203.98
      2020-02-01, Init... 9355.65
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Accepted
      2020-02-01, Status 2: Ref: 5, Size: 0.03741054870586224, Price: 9344.65439
      2020-02-01, Close 9355.65, Mean 9360.51, Buy1 9344.60, Sell1 9376.42, Buy2 9319.33, Sell2 9401.70, Stop 9203.98
      2020-02-01, Init... 9355.65
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Accepted
      2020-02-01, Status 2: Ref: 6, Size: 0.03741054870586224, Price: 9344.59873
      2020-02-01, Close 9359.29, Mean 9360.49, Buy1 9344.58, Sell1 9376.40, Buy2 9319.31, Sell2 9401.68, Stop 9203.98
      2020-02-01, Init... 9359.29
      --------------------------------  NOTIFY ORDER  --------------------------------
      AutoOrderedDict() Order Accepted
      2020-02-01, Status 2: Ref: 7, Size: 0.037395999055483915, Price: 9344.57896
      2020-02-01, Close 9363.69, Mean 9360.57, Buy1 9344.66, Sell1 9376.49, Buy2 9319.39, Sell2 9401.76, Stop 9203.98
      2020-02-01, Close 9374.21, Mean 9361.16, Buy1 9345.24, Sell1 9377.07, Buy2 9319.97, Sell2 9402.35, Stop 9203.98
      2020-02-01, Close 9370.20, Mean 9361.40, Buy1 9345.48, Sell1 9377.31, Buy2 9320.21, Sell2 9402.59, Stop 9203.98
      2020-02-01, Close 9373.75, Mean 9361.53, Buy1 9345.62, Sell1 9377.45, Buy2 9320.34, Sell2 9402.72, Stop 9203.98
      2020-02-01, Close 9381.68, Mean 9362.01, Buy1 9346.09, Sell1 9377.92, Buy2 9320.81, Sell2 9403.20, Stop 9203.98
      2020-02-01, Close 9383.44, Mean 9362.45, Buy1 9346.53, Sell1 9378.37, Buy2 9321.26, Sell2 9403.64, Stop 9203.98
      2020-02-01, Close 9387.57, Mean 9363.11, Buy1 9347.19, Sell1 9379.03, Buy2 9321.91, Sell2 9404.31, Stop 9203.98
      2020-02-01, Close 9383.22, Mean 9363.38, Buy1 9347.46, Sell1 9379.30, Buy2 9322.18, Sell2 9404.58, Stop 9203.98
      2020-02-01, Close 9388.64, Mean 9364.35, Buy1 9348.43, Sell1 9380.26, Buy2 9323.14, Sell2 9405.55, Stop 9203.98
      2020-02-01, Close 9374.66, Mean 9364.48, Buy1 9348.56, Sell1 9380.40, Buy2 9323.28, Sell2 9405.69, Stop 9203.98
      2020-02-01, Close 9372.53, Mean 9364.54, Buy1 9348.62, Sell1 9380.46, Buy2 9323.33, Sell2 9405.74, Stop 9203.98
      2020-02-01, Close 9375.25, Mean 9364.67, Buy1 9348.75, Sell1 9380.59, Buy2 9323.46, Sell2 9405.87, Stop 9203.98
      2020-02-01, Close 9375.22, Mean 9364.78, Buy1 9348.86, Sell1 9380.70, Buy2 9323.57, Sell2 9405.98, Stop 9203.98
      2020-02-01, Close 9378.71, Mean 9364.97, Buy1 9349.05, Sell1 9380.89, Buy2 9323.76, Sell2 9406.18, Stop 9203.98
      2020-02-01, Close 9376.21, Mean 9365.08, Buy1 9349.16, Sell1 9381.01, Buy2 9323.88, Sell2 9406.29, Stop 9203.98
      2020-02-01, Close 9383.29, Mean 9365.44, Buy1 9349.52, Sell1 9381.36, Buy2 9324.23, Sell2 9406.65, Stop 9203.98
      2020-02-01, Close 9385.85, Mean 9365.95, Buy1 9350.03, Sell1 9381.87, Buy2 9324.74, Sell2 9407.16, Stop 9203.98
      2020-02-01, Close 9387.54, Mean 9366.64, Buy1 9350.71, Sell1 9382.56, Buy2 9325.42, Sell2 9407.85, Stop 9203.98
      2020-02-01, Close 9384.05, Mean 9367.49, Buy1 9351.57, Sell1 9383.42, Buy2 9326.28, Sell2 9408.71, Stop 9203.98
      2020-02-01, Close 9383.29, Mean 9368.15, Buy1 9352.23, Sell1 9384.08, Buy2 9326.93, Sell2 9409.37, Stop 9203.98
      2020-02-01, Close 9387.60, Mean 9369.15, Buy1 9353.22, Sell1 9385.08, Buy2 9327.93, Sell2 9410.37, Stop 9203.98
      2020-02-01, Close 9379.23, Mean 9369.65, Buy1 9353.73, Sell1 9385.58, Buy2 9328.43, Sell2 9410.88, Stop 9203.98
      2020-02-01, Close 9383.07, Mean 9370.36, Buy1 9354.43, Sell1 9386.29, Buy2 9329.13, Sell2 9411.59, Stop 9203.98
      2020-02-01, Close 9382.12, Mean 9370.84, Buy1 9354.91, Sell1 9386.77, Buy2 9329.61, Sell2 9412.08, Stop 9203.98
      2020-02-01, Close 9382.09, Mean 9371.29, Buy1 9355.36, Sell1 9387.22, Buy2 9330.05, Sell2 9412.52, Stop 9203.98
      2020-02-01, Close 9388.36, Mean 9372.11, Buy1 9356.17, Sell1 9388.04, Buy2 9330.87, Sell2 9413.34, Stop 9203.98
      2020-02-01, Close 9397.93, Mean 9373.77, Buy1 9357.83, Sell1 9389.70, Buy2 9332.52, Sell2 9415.01, Stop 9203.98
      2020-02-01, Close 9384.26, Mean 9374.15, Buy1 9358.21, Sell1 9390.08, Buy2 9332.90, Sell2 9415.39, Stop 9203.98
      2020-02-01, Close 9392.70, Mean 9375.03, Buy1 9359.09, Sell1 9390.97, Buy2 9333.78, Sell2 9416.28, Stop 9203.98
      2020-02-01, Close 9379.89, Mean 9375.13, Buy1 9359.19, Sell1 9391.07, Buy2 9333.88, Sell2 9416.38, Stop 9203.98
      2020-02-01, Close 9387.01, Mean 9375.41, Buy1 9359.47, Sell1 9391.35, Buy2 9334.16, Sell2 9416.66, Stop 9203.98
      2020-02-01, Close 9384.69, Mean 9375.52, Buy1 9359.58, Sell1 9391.45, Buy2 9334.26, Sell2 9416.77, Stop 9203.98
      2020-02-01, Close 9388.39, Mean 9375.76, Buy1 9359.82, Sell1 9391.70, Buy2 9334.51, Sell2 9417.02, Stop 9203.98
      2020-02-01, Close 9395.97, Mean 9376.23, Buy1 9360.29, Sell1 9392.17, Buy2 9334.97, Sell2 9417.48, Stop 9203.98
      2020-02-01, Close 9393.64, Mean 9376.45, Buy1 9360.51, Sell1 9392.39, Buy2 9335.20, Sell2 9417.71, Stop 9203.98
      2020-02-01, Close 9391.20, Mean 9376.59, Buy1 9360.65, Sell1 9392.53, Buy2 9335.33, Sell2 9417.85, Stop 9203.98
      2020-02-01, Close 9405.99, Mean 9377.12, Buy1 9361.18, Sell1 9393.06, Buy2 9335.86, Sell2 9418.38, Stop 9203.98
      2020-02-01, Close 9405.56, Mean 9377.77, Buy1 9361.83, Sell1 9393.71, Buy2 9336.51, Sell2 9419.03, Stop 9203.98
      
      

      Looks like I'm issuing multiple orders when I only want to issue one order per "step" What am I doing wrong? I've been stuck on this part of the project for a week now.. I've browsed trough both the docs + forums for hours and I'm sure it's a simple solution but I can't find it.

      vladisld 1 Reply Last reply Reply Quote 0
      • vladisld
        vladisld @cept0r last edited by

        @cept0r said in Issuing multiple orders, how do I only issue one order at a time?:

        if self.order

        Where self.order got updated?

        @cept0r said in Issuing multiple orders, how do I only issue one order at a time?:

        if not self.position:

        The position will be created/updated upon order execution. However, the execution may or may not happen depending on various factors (like not enough available cache, or incorrect order size, etc ) . Probably, it is better to test for the existence of the order instead.

        cept0r 1 Reply Last reply Reply Quote 1
        • cept0r
          cept0r last edited by

          aha so I forgot to add a self.order = self.buy/self.sell, thank you.

          1 Reply Last reply Reply Quote 0
          • cept0r
            cept0r last edited by

            Now I have the opposite issue running this code @vladisld

                    """Initializing algorithm..."""
                    if not self.position and close < mean:
                        self.long = self.buy(price=buy1, size=size1, exectype=bt.Order.Limit)
                        self.log('Buying step 1... %.2f' % close)
                    elif self.position and close > mean:
                        self.short = self.sell(price=sell1, size=-pos, exectype=bt.Order.Limit)
                        self.log('Exiting position... %.2f' % close)
            

            Output:

            Starting Portfolio Value: 100000.00
            2020-06-08, Close 9766.21, Mean 9752.87, Buy1 9736.29, Sell1 9769.45, Buy2 9709.95, Sell2 9795.78, Stop 9583.57
            2020-06-08, Close 9764.69, Mean 9752.94, Buy1 9736.36, Sell1 9769.52, Buy2 9710.02, Sell2 9795.85, Stop 9583.57
            2020-06-08, Close 9765.99, Mean 9753.04, Buy1 9736.46, Sell1 9769.62, Buy2 9710.13, Sell2 9795.96, Stop 9583.57
            2020-06-08, Close 9748.40, Mean 9752.88, Buy1 9736.30, Sell1 9769.46, Buy2 9709.97, Sell2 9795.79, Stop 9583.57
            2020-06-08, Buying step 1... 9748.40
            
            
            2020-06-08, Close 9749.90, Mean 9752.54, Buy1 9735.96, Sell1 9769.12, Buy2 9709.63, Sell2 9795.45, Stop 9583.57
            2020-06-08, Close 9741.00, Mean 9752.39, Buy1 9735.82, Sell1 9768.97, Buy2 9709.48, Sell2 9795.31, Stop 9583.57
            2020-06-08, Close 9746.80, Mean 9752.34, Buy1 9735.77, Sell1 9768.92, Buy2 9709.43, Sell2 9795.25, Stop 9583.57
            2020-06-08, Close 9746.61, Mean 9752.28, Buy1 9735.71, Sell1 9768.86, Buy2 9709.37, Sell2 9795.19, Stop 9583.57
            --------------------------------  NOTIFY ORDER  --------------------------------
            AutoOrderedDict() Order Completed
            2020-06-08, Status 4: Ref: 1, Size: 0.03590332772557549, Price: 9736.29896
            Created: 2020-06-08 00:33:00 Price: 9736.298955328994 Size: 0.03590332772557549
            --------------------------------------------------------------------------------
            
            
            Right here it should issue one sell order:
            2020-06-08, Close 9740.72, Mean 9750.44, Buy1 9733.86, Sell1 9767.01, Buy2 9707.54, Sell2 9793.34, Stop 9583.57
            2020-06-08, Close 9745.95, Mean 9750.36, Buy1 9733.79, Sell1 9766.94, Buy2 9707.46, Sell2 9793.26, Stop 9583.57
            2020-06-08, Close 9750.83, Mean 9750.37, Buy1 9733.80, Sell1 9766.95, Buy2 9707.47, Sell2 9793.27, Stop 9583.57
            
            

            So now instead of issuing multiple orders for each step it only issues one buy but I can't get it to sell.

            Rest of my code:

                def __init__(self):
                    self.dataclose = self.datas[0].close
                    self.ind = AmaBands(plot=True, subplot=False)
                    self.step = False
                    self.order = None
                    self.long = None
                    self.short = None
                    self.o = list()
            
                def next(self):
                    self.log('Close %0.2f, Mean %0.2f, Buy1 %0.2f, Sell1 %0.2f, Buy2 %0.2f, Sell2 %0.2f, Stop %0.2f'
                             % (self.dataclose[0], self.ind[0], self.ind.bot[0],
                                self.ind.top[0], self.ind.bot2[0], self.ind.top2[0],
                                self.ind.bot5[0]))
            
                    orders = self.broker.get_orders_open()
            
                    if self.long:
                        return
            
                    if self.short:
                        return
            
                    if self.order:
                        return
            
                    mean = self.ind[0]  # Current mean(moving average) value
            
                    size1 = cerebro.broker.get_cash() * 0.035 / self.data
                    size2 = size1 * 2
                    size3 = size2 * 2
                    size4 = size3 * 2
            
                    pos = self.position.size
            
                    stop = self.ind.bot5[0]
                    close = self.dataclose[0]  # Current close
            
                    # Buy levels
                    buy1 = self.ind.bot[0]
                    buy2 = self.ind.bot2[0]
                    buy3 = self.ind.bot3[0]
                    buy4 = self.ind.bot4[0]
            
                    # Sell levels
                    sell1 = self.ind.top[0]
                    sell2 = self.ind.top2[0]
                    sell3 = self.ind.top3[0]
                    sell4 = self.ind.top4[0]
            
            
            1 Reply Last reply Reply Quote 0
            • run-out
              run-out last edited by

              Have a look at self.close()

              cept0r 1 Reply Last reply Reply Quote 0
              • cept0r
                cept0r @vladisld last edited by

                @vladisld
                Edited the code abit and added some more logging functions so I can inspect more..
                Now it looks like my code is not recognizing that I'm either not in a position or that after my buy order is executed it's still saying that I have a buy order pending when I don't..;
                Input;

                    def __init__(self):
                        self.dataclose = self.datas[0].close
                        self.ind = AmaBands(plot=True, subplot=False)
                        self.long = None
                        self.short = None
                
                    def next(self):
                        self.log('Close %0.2f, Mean %0.2f, Buy1 %0.2f, Sell1 %0.2f, Buy2 %0.2f, Sell2 %0.2f, Stop %0.2f'
                                 % (self.dataclose[0], self.ind[0], self.ind.bot[0],
                                    self.ind.top[0], self.ind.bot2[0], self.ind.top2[0],
                                    self.ind.bot5[0]))
                
                        orders = self.broker.get_orders_open()
                
                        if self.long:
                            self.log('Buy order is pending... %.2f' % self.dataclose[0])
                            return
                
                        if self.short:
                            self.log('Sell order is pending... %.2f' % self.dataclose[0])
                            return
                
                        mean = self.ind[0]  # Current mean(moving average) value
                
                        size1 = cerebro.broker.get_cash() * 0.035 / self.data
                        size2 = size1 * 2
                        size3 = size2 * 2
                        size4 = size3 * 2
                
                        pos = self.position.size
                
                        stop = self.ind.bot5[0]
                        close = self.dataclose[0]  # Current close
                
                        # Buy levels
                        buy1 = self.ind.bot[0]
                        buy2 = self.ind.bot2[0]
                        buy3 = self.ind.bot3[0]
                        buy4 = self.ind.bot4[0]
                
                        # Sell levels
                        sell1 = self.ind.top[0]
                        sell2 = self.ind.top2[0]
                        sell3 = self.ind.top3[0]
                        sell4 = self.ind.top4[0]
                
                        """Initializing algorithm..."""
                        if pos == 0:
                            if close < mean:
                                self.long = self.buy(price=buy1, size=1, exectype=bt.Order.Limit)
                                self.log('Close below mean buying step 1... %.2f' % close)
                
                            if pos > 0:
                                if close > mean:
                                    self.short = self.sell(price=sell1, size=-1, exectype=bt.Order.Limit)
                                    self.log('Closing long... %.2f' % close)
                

                Notes: I changed the size to use static sizing to see if that could be the problem but the output still remains the same.

                I've tried indenting it every possible way and tried several different ways to get it to sell but I'm stuck.
                Output:

                2020-06-08, Close 9766.21, Mean 9752.87, Buy1 9736.29, Sell1 9769.45, Buy2 9709.95, Sell2 9795.78, Stop 9583.57
                2020-06-08, Close 9764.69, Mean 9752.94, Buy1 9736.36, Sell1 9769.52, Buy2 9710.02, Sell2 9795.85, Stop 9583.57
                2020-06-08, Close 9765.99, Mean 9753.04, Buy1 9736.46, Sell1 9769.62, Buy2 9710.13, Sell2 9795.96, Stop 9583.57
                2020-06-08, Close 9748.40, Mean 9752.88, Buy1 9736.30, Sell1 9769.46, Buy2 9709.97, Sell2 9795.79, Stop 9583.57
                2020-06-08, Close below mean buying step 1... 9748.40
                2020-06-08, Close 9754.18, Mean 9752.90, Buy1 9736.32, Sell1 9769.48, Buy2 9709.98, Sell2 9795.81, Stop 9583.57
                2020-06-08, Buy order is pending... 9754.18
                2020-06-08, Close 9751.21, Mean 9752.87, Buy1 9736.29, Sell1 9769.45, Buy2 9709.96, Sell2 9795.78, Stop 9583.57
                2020-06-08, Buy order is pending... 9751.21
                2020-06-08, Close 9743.68, Mean 9752.69, Buy1 9736.11, Sell1 9769.27, Buy2 9709.78, Sell2 9795.60, Stop 9583.57
                2020-06-08, Buy order is pending... 9743.68
                2020-06-08, Close 9748.55, Mean 9752.64, Buy1 9736.06, Sell1 9769.22, Buy2 9709.73, Sell2 9795.55, Stop 9583.57
                2020-06-08, Buy order is pending... 9748.55
                2020-06-08, Close 9749.98, Mean 9752.63, Buy1 9736.05, Sell1 9769.21, Buy2 9709.72, Sell2 9795.54, Stop 9583.57
                
                This part is all correct it closes below the mean and issues one buy order..
                
                2020-06-08, Close 9746.61, Mean 9752.28, Buy1 9735.71, Sell1 9768.86, Buy2 9709.37, Sell2 9795.19, Stop 9583.57
                2020-06-08, Buy order is pending... 9746.61
                --------------------------------  NOTIFY ORDER  --------------------------------
                AutoOrderedDict() Order Completed
                2020-06-08, Status 4: Ref: 1, Size: 1, Price: 9736.29896
                Created: 2020-06-08 00:33:00 Price: 9736.298955328994 Size: 1
                --------------------------------------------------------------------------------
                The current close dumps into my buy order and executes correctly... 
                Now for my current issue..
                
                2020-06-08, Close 9740.00, Mean 9752.12, Buy1 9735.54, Sell1 9768.70, Buy2 9709.21, Sell2 9795.03, Stop 9583.57
                2020-06-08, Buy order is pending... 9740.00
                2020-06-08, Close 9742.04, Mean 9752.01, Buy1 9735.43, Sell1 9768.59, Buy2 9709.10, Sell2 9794.92, Stop 9583.57
                2020-06-08, Buy order is pending... 9742.04
                2020-06-08, Close 9740.01, Mean 9751.88, Buy1 9735.31, Sell1 9768.46, Buy2 9708.98, Sell2 9794.79, Stop 9583.57
                2020-06-08, Buy order is pending... 9740.01
                2020-06-08, Close 9748.58, Mean 9751.86, Buy1 9735.28, Sell1 9768.44, Buy2 9708.95, Sell2 9794.77, Stop 9583.57
                2020-06-08, Buy order is pending... 9748.58
                2020-06-08, Close 9745.62, Mean 9751.83, Buy1 9735.25, Sell1 9768.41, Buy2 9708.92, Sell2 9794.74, Stop 9583.57
                2020-06-08, Buy order is pending... 9745.62
                2020-06-08, Close 9747.26, Mean 9751.80, Buy1 9735.23, Sell1 9768.38, Buy2 9708.90, Sell2 9794.71, Stop 9583.57
                2020-06-08, Buy order is pending... 9747.26
                2020-06-08, Close 9741.02, Mean 9751.69, Buy1 9735.11, Sell1 9768.27, Buy2 9708.79, Sell2 9794.60, Stop 9583.57
                2020-06-08, Buy order is pending... 9741.02
                2020-06-08, Close 9757.18, Mean 9751.76, Buy1 9735.18, Sell1 9768.34, Buy2 9708.85, Sell2 9794.66, Stop 9583.57
                2020-06-08, Buy order is pending... 9757.18
                2020-06-08, Close 9750.71, Mean 9751.75, Buy1 9735.17, Sell1 9768.32, Buy2 9708.84, Sell2 9794.65, Stop 9583.57
                2020-06-08, Buy order is pending... 9750.71
                2020-06-08, Close 9743.98, Mean 9751.70, Buy1 9735.13, Sell1 9768.28, Buy2 9708.80, Sell2 9794.61, Stop 9583.57
                2020-06-08, Buy order is pending... 9743.98
                2020-06-08, Close 9745.09, Mean 9751.66, Buy1 9735.08, Sell1 9768.24, Buy2 9708.75, Sell2 9794.57, Stop 9583.57
                2020-06-08, Buy order is pending... 9745.09
                2020-06-08, Close 9742.41, Mean 9751.45, Buy1 9734.87, Sell1 9768.02, Buy2 9708.54, Sell2 9794.35, Stop 9583.57
                2020-06-08, Buy order is pending... 9742.41
                2020-06-08, Close 9748.90, Mean 9751.41, Buy1 9734.83, Sell1 9767.99, Buy2 9708.51, Sell2 9794.32, Stop 9583.57
                2020-06-08, Buy order is pending... 9748.90
                2020-06-08, Close 9749.60, Mean 9751.37, Buy1 9734.79, Sell1 9767.95, Buy2 9708.46, Sell2 9794.27, Stop 9583.57
                2020-06-08, Buy order is pending... 9749.60
                2020-06-08, Close 9747.53, Mean 9751.29, Buy1 9734.71, Sell1 9767.86, Buy2 9708.38, Sell2 9794.19, Stop 9583.57
                2020-06-08, Buy order is pending... 9747.53
                2020-06-08, Close 9749.77, Mean 9751.26, Buy1 9734.68, Sell1 9767.84, Buy2 9708.35, Sell2 9794.16, Stop 9583.57
                2020-06-08, Buy order is pending... 9749.77
                2020-06-08, Close 9745.11, Mean 9751.09, Buy1 9734.51, Sell1 9767.66, Buy2 9708.18, Sell2 9793.99, Stop 9583.57
                2020-06-08, Buy order is pending... 9745.11
                2020-06-08, Close 9743.66, Mean 9750.86, Buy1 9734.29, Sell1 9767.44, Buy2 9707.96, Sell2 9793.77, Stop 9583.57
                2020-06-08, Buy order is pending... 9743.66
                2020-06-08, Close 9741.85, Mean 9750.65, Buy1 9734.07, Sell1 9767.22, Buy2 9707.74, Sell2 9793.55, Stop 9583.57
                2020-06-08, Buy order is pending... 9741.85
                2020-06-08, Close 9746.00, Mean 9750.58, Buy1 9734.00, Sell1 9767.16, Buy2 9707.68, Sell2 9793.48, Stop 9583.57
                2020-06-08, Buy order is pending... 9746.00
                2020-06-08, Close 9743.43, Mean 9750.47, Buy1 9733.90, Sell1 9767.05, Buy2 9707.57, Sell2 9793.38, Stop 9583.57
                2020-06-08, Buy order is pending... 9743.43
                2020-06-08, Close 9757.60, Mean 9750.54, Buy1 9733.97, Sell1 9767.12, Buy2 9707.64, Sell2 9793.44, Stop 9583.57
                2020-06-08, Buy order is pending... 9757.60
                2020-06-08, Close 9755.63, Mean 9750.58, Buy1 9734.00, Sell1 9767.15, Buy2 9707.67, Sell2 9793.48, Stop 9583.57
                2020-06-08, Buy order is pending... 9755.63
                2020-06-08, Close 9756.01, Mean 9750.68, Buy1 9734.10, Sell1 9767.25, Buy2 9707.77, Sell2 9793.58, Stop 9583.57
                2020-06-08, Buy order is pending... 9756.01
                2020-06-08, Close 9762.49, Mean 9750.89, Buy1 9734.31, Sell1 9767.47, Buy2 9707.98, Sell2 9793.79, Stop 9583.57
                2020-06-08, Buy order is pending... 9762.49
                2020-06-08, Close 9760.99, Mean 9751.04, Buy1 9734.46, Sell1 9767.62, Buy2 9708.14, Sell2 9793.95, Stop 9583.57
                2020-06-08, Buy order is pending... 9760.99
                2020-06-08, Close 9760.79, Mean 9751.17, Buy1 9734.59, Sell1 9767.75, Buy2 9708.27, Sell2 9794.08, Stop 9583.57
                2020-06-08, Buy order is pending... 9760.79
                2020-06-08, Close 9758.18, Mean 9751.33, Buy1 9734.75, Sell1 9767.90, Buy2 9708.42, Sell2 9794.23, Stop 9583.57
                2020-06-08, Buy order is pending... 9758.18
                2020-06-08, Close 9757.95, Mean 9751.42, Buy1 9734.85, Sell1 9768.00, Buy2 9708.52, Sell2 9794.33, Stop 9583.57
                2020-06-08, Buy order is pending... 9757.95
                2020-06-08, Close 9753.99, Mean 9751.45, Buy1 9734.87, Sell1 9768.03, Buy2 9708.54, Sell2 9794.36, Stop 9583.57
                2020-06-08, Buy order is pending... 9753.99
                2020-06-08, Close 9753.00, Mean 9751.48, Buy1 9734.90, Sell1 9768.06, Buy2 9708.57, Sell2 9794.38, Stop 9583.57
                2020-06-08, Buy order is pending... 9753.00
                2020-06-08, Close 9753.00, Mean 9751.50, Buy1 9734.92, Sell1 9768.08, Buy2 9708.59, Sell2 9794.41, Stop 9583.57
                2020-06-08, Buy order is pending... 9753.00
                2020-06-08, Close 9757.52, Mean 9751.65, Buy1 9735.07, Sell1 9768.22, Buy2 9708.74, Sell2 9794.55, Stop 9583.57
                2020-06-08, Buy order is pending... 9757.52
                2020-06-08, Close 9751.82, Mean 9751.65, Buy1 9735.07, Sell1 9768.23, Buy2 9708.74, Sell2 9794.56, Stop 9583.57
                2020-06-08, Buy order is pending... 9751.82
                2020-06-08, Close 9753.43, Mean 9751.67, Buy1 9735.09, Sell1 9768.25, Buy2 9708.76, Sell2 9794.58, Stop 9583.57
                2020-06-08, Buy order is pending... 9753.43
                2020-06-08, Close 9750.14, Mean 9751.66, Buy1 9735.08, Sell1 9768.24, Buy2 9708.75, Sell2 9794.57, Stop 9583.57
                2020-06-08, Buy order is pending... 9750.14
                2020-06-08, Close 9743.93, Mean 9751.61, Buy1 9735.03, Sell1 9768.19, Buy2 9708.70, Sell2 9794.52, Stop 9583.57
                2020-06-08, Buy order is pending... 9743.93
                2020-06-08, Close 9742.69, Mean 9751.40, Buy1 9734.82, Sell1 9767.98, Buy2 9708.49, Sell2 9794.31, Stop 9583.57
                2020-06-08, Buy order is pending... 9742.69
                2020-06-08, Close 9744.33, Mean 9751.32, Buy1 9734.74, Sell1 9767.90, Buy2 9708.41, Sell2 9794.23, Stop 9583.57
                2020-06-08, Buy order is pending... 9744.33
                2020-06-08, Close 9746.19, Mean 9751.29, Buy1 9734.71, Sell1 9767.87, Buy2 9708.38, Sell2 9794.19, Stop 9583.57
                2020-06-08, Buy order is pending... 9746.19
                2020-06-08, Close 9753.83, Mean 9751.32, Buy1 9734.75, Sell1 9767.90, Buy2 9708.42, Sell2 9794.23, Stop 9583.57
                2020-06-08, Buy order is pending... 9753.83
                2020-06-08, Close 9747.48, Mean 9751.29, Buy1 9734.71, Sell1 9767.87, Buy2 9708.38, Sell2 9794.20, Stop 9583.57
                2020-06-08, Buy order is pending... 9747.48
                2020-06-08, Close 9743.32, Mean 9751.21, Buy1 9734.63, Sell1 9767.79, Buy2 9708.31, Sell2 9794.12, Stop 9583.57
                2020-06-08, Buy order is pending... 9743.32
                2020-06-08, Close 9747.62, Mean 9751.19, Buy1 9734.61, Sell1 9767.77, Buy2 9708.28, Sell2 9794.10, Stop 9583.57
                2020-06-08, Buy order is pending... 9747.62
                2020-06-08, Close 9750.02, Mean 9751.18, Buy1 9734.61, Sell1 9767.76, Buy2 9708.28, Sell2 9794.09, Stop 9583.57
                2020-06-08, Buy order is pending... 9750.02
                2020-06-08, Close 9747.23, Mean 9751.16, Buy1 9734.58, Sell1 9767.73, Buy2 9708.25, Sell2 9794.06, Stop 9583.57
                2020-06-08, Buy order is pending... 9747.23
                2020-06-08, Close 9742.82, Mean 9751.11, Buy1 9734.53, Sell1 9767.68, Buy2 9708.20, Sell2 9794.01, Stop 9583.57
                2020-06-08, Buy order is pending... 9742.82
                2020-06-08, Close 9742.71, Mean 9751.06, Buy1 9734.49, Sell1 9767.64, Buy2 9708.16, Sell2 9793.97, Stop 9583.57
                2020-06-08, Buy order is pending... 9742.71
                2020-06-08, Close 9746.00, Mean 9751.02, Buy1 9734.45, Sell1 9767.60, Buy2 9708.12, Sell2 9793.93, Stop 9583.57
                2020-06-08, Buy order is pending... 9746.00
                
                

                It's still saying I have a buy order pending which is not correct because my buy order executed earlier so I'm wondering what I did to cause this..
                And for some reason my code does not recognize that I'm in a position because it had plenty of opportunities to create sell orders and execute them and that was one of my conditions to issue a sell.

                1 Reply Last reply Reply Quote 0
                • cept0r
                  cept0r @run-out last edited by

                  @run-out
                  All right, I've tried this:

                          """Initializing algorithm..."""
                          if pos == 0:
                              if close < mean:
                                  self.long = self.buy(price=buy1, size=1, exectype=bt.Order.Limit)
                                  self.log('Close below mean buying step 1... %.2f' % close)
                              elif close >= sell1:
                                  self.long = self.close()
                                  self.log('Closing long... %.2f' % close)
                  

                  Still won't execute a sell..

                  Also tried this:

                          """Initializing algorithm..."""
                          if pos == 0:
                              if close < mean:
                                  self.long = self.buy(price=buy1, size=1, exectype=bt.Order.Limit)
                                  self.log('Close below mean buying step 1... %.2f' % close)
                                  if close >= sell1:
                                      self.close(self.long)
                                      self.log('Closing long... %.2f' % close)
                  

                  Same output..
                  Trying:

                          """Initializing algorithm..."""
                          if pos == 0:
                              if close < mean:
                                  self.long = self.buy(price=buy1, size=1, exectype=bt.Order.Limit)
                                  self.log('Close below mean buying step 1... %.2f' % close)
                          else:
                              if close >= sell1:
                                  self.close(self.long)
                                  self.log('Closing long... %.2f' % close)
                  

                  Still the same output...

                  I will make it work!
                  There are only so many combinations..
                  And eventually I got to figure it out!

                  1 Reply Last reply Reply Quote 0
                  • A
                    ab_trader last edited by

                    In the notify_order() you need to set self.long = None as soon as your buying order is executed. Cause self.long will always be equal to the latest buy order you issued.

                    1 Reply Last reply Reply Quote 0
                    • run-out
                      run-out last edited by

                      Try getting rid of self.long and then just check for open orders using self.order.

                      cept0r 1 Reply Last reply Reply Quote 0
                      • A
                        ab_trader last edited by

                        Also I would put pending order check into the order issuing conditions to have everything in the same place:

                                if pos == 0:
                                    if close < mean:
                                        self.long = self.buy(price=buy1, size=1, exectype=bt.Order.Limit)
                                        self.log('Close below mean buying step 1... %.2f' % close)
                        
                                    if pos > 0 and not self.long:
                                        if close > mean:
                                            self.short = self.sell(price=sell1, size=-1, exectype=bt.Order.Limit)
                                            self.log('Closing long... %.2f' % close)
                        
                        cept0r 2 Replies Last reply Reply Quote 2
                        • cept0r
                          cept0r @run-out last edited by

                          @run-out

                                  if pos == 0:
                                      if close < mean:
                                          self.buy(price=buy1, size=1, exectype=bt.Order.Limit)
                                          self.log('Close below mean buying step 1... %.2f' % close)
                                  if close >= sell1 and pos > 0:
                                      self.close()
                                      self.log('Closing long... %.2f' % close)
                          

                          When I removed "self.long =" I managed to get some sells executed running this code, looks like I'm getting some more possible solutions.
                          Trying them now..

                          cept0r 1 Reply Last reply Reply Quote 1
                          • cept0r
                            cept0r @ab_trader last edited by

                            @ab_trader
                            Added "self.long = None" into my notify_order() and changed my next() to what you suggested

                                    """Initializing algorithm..."""
                                    if pos == 0:
                                        if close < mean:
                                            self.long = self.buy(price=buy1, size=1, exectype=bt.Order.Limit)
                                            self.log('Close below mean buying step 1... %.2f' % close)
                            
                                        if pos > 0 and not self.long:
                                            if close > mean:
                                                self.short = self.sell(price=sell1, size=-1, exectype=bt.Order.Limit)
                                                self.log('Closing long... %.2f' % close)
                            
                                def notify_order(self, order):
                                    date = self.data.datetime.datetime().date()
                            
                                    if order.status == order.Completed:
                                        self.long = None
                                        print('-' * 32, ' NOTIFY ORDER ', '-' * 32)
                                        print('{} Order Completed'.format(order.info['name']))
                                        print('{}, Status {}: Ref: {}, Size: {}, Price: {}'.format(
                                            date,
                                            order.status,
                                            order.ref,
                                            order.size,
                                            'NA' if not order.price else round(order.price, 5)
                                        ))
                                        print('Created: {} Price: {} Size: {}'.format(bt.num2date(order.created.dt),
                                                                                      order.created.price, order.created.size))
                                        print('-' * 80)
                            
                                def notify_trade(self, trade):
                                    if trade.isclosed:
                                        dt = self.data.datetime.date()
                                        print('---------------------------- TRADE ---------------------------------')
                                        print("1: Data Name:                            {}".format(trade.data._name))
                                        print("2: Bar Num:                              {}".format(len(trade.data)))
                                        print("3: Current date:                         {}".format(dt))
                                        print('4: Status:                               Trade Complete')
                                        print('5: Ref:                                  {}'.format(trade.ref))
                                        print('6: PnL:                                  {}'.format(round(trade.pnl, 2)))
                                        print('--------------------------------------------------------------------')
                            
                            

                            output:

                            Starting Portfolio Value: 100000.00
                            2020-06-08, Close 9766.21, Mean 9752.87, Buy1 9736.29, Sell1 9769.45, Buy2 9709.95, Sell2 9795.78, Stop 9583.57
                            2020-06-08, Close 9764.69, Mean 9752.94, Buy1 9736.36, Sell1 9769.52, Buy2 9710.02, Sell2 9795.85, Stop 9583.57
                            2020-06-08, Close 9765.99, Mean 9753.04, Buy1 9736.46, Sell1 9769.62, Buy2 9710.13, Sell2 9795.96, Stop 9583.57
                            2020-06-08, Close 9748.40, Mean 9752.88, Buy1 9736.30, Sell1 9769.46, Buy2 9709.97, Sell2 9795.79, Stop 9583.57
                            2020-06-08, Close below mean buying step 1... 9748.40
                            2020-06-08, Close 9754.18, Mean 9752.90, Buy1 9736.32, Sell1 9769.48, Buy2 9709.98, Sell2 9795.81, Stop 9583.57
                            2020-06-08, Buy order is pending... 9754.18
                            2020-06-08, Close 9751.21, Mean 9752.87, Buy1 9736.29, Sell1 9769.45, Buy2 9709.96, Sell2 9795.78, Stop 9583.57
                            2020-06-08, Buy order is pending... 9751.21
                            2020-06-08, Close 9743.68, Mean 9752.69, Buy1 9736.11, Sell1 9769.27, Buy2 9709.78, Sell2 9795.60, Stop 9583.57
                            2020-06-08, Buy order is pending... 9743.68
                            2020-06-08, Close 9748.55, Mean 9752.64, Buy1 9736.06, Sell1 9769.22, Buy2 9709.73, Sell2 9795.55, Stop 9583.57
                            2020-06-08, Buy order is pending... 9748.55
                            2020-06-08, Close 9749.98, Mean 9752.63, Buy1 9736.05, Sell1 9769.21, Buy2 9709.72, Sell2 9795.54, Stop 9583.57
                            2020-06-08, Buy order is pending... 9749.98
                            2020-06-08, Close 9750.05, Mean 9752.61, Buy1 9736.03, Sell1 9769.19, Buy2 9709.70, Sell2 9795.53, Stop 9583.57
                            2020-06-08, Buy order is pending... 9750.05
                            2020-06-08, Close 9760.51, Mean 9752.71, Buy1 9736.13, Sell1 9769.29, Buy2 9709.80, Sell2 9795.62, Stop 9583.57
                            2020-06-08, Buy order is pending... 9760.51
                            2020-06-08, Close 9755.00, Mean 9752.73, Buy1 9736.15, Sell1 9769.31, Buy2 9709.82, Sell2 9795.64, Stop 9583.57
                            2020-06-08, Buy order is pending... 9755.00
                            2020-06-08, Close 9750.00, Mean 9752.69, Buy1 9736.11, Sell1 9769.27, Buy2 9709.78, Sell2 9795.61, Stop 9583.57
                            2020-06-08, Buy order is pending... 9750.00
                            2020-06-08, Close 9746.81, Mean 9752.62, Buy1 9736.04, Sell1 9769.19, Buy2 9709.70, Sell2 9795.53, Stop 9583.57
                            2020-06-08, Buy order is pending... 9746.81
                            2020-06-08, Close 9750.27, Mean 9752.58, Buy1 9736.00, Sell1 9769.16, Buy2 9709.67, Sell2 9795.49, Stop 9583.57
                            2020-06-08, Buy order is pending... 9750.27
                            2020-06-08, Close 9752.74, Mean 9752.58, Buy1 9736.01, Sell1 9769.16, Buy2 9709.67, Sell2 9795.50, Stop 9583.57
                            2020-06-08, Buy order is pending... 9752.74
                            2020-06-08, Close 9754.03, Mean 9752.61, Buy1 9736.03, Sell1 9769.19, Buy2 9709.70, Sell2 9795.52, Stop 9583.57
                            2020-06-08, Buy order is pending... 9754.03
                            2020-06-08, Close 9749.99, Mean 9752.58, Buy1 9736.00, Sell1 9769.16, Buy2 9709.67, Sell2 9795.49, Stop 9583.57
                            2020-06-08, Buy order is pending... 9749.99
                            2020-06-08, Close 9750.64, Mean 9752.55, Buy1 9735.97, Sell1 9769.13, Buy2 9709.64, Sell2 9795.47, Stop 9583.57
                            2020-06-08, Buy order is pending... 9750.64
                            2020-06-08, Close 9747.30, Mean 9752.49, Buy1 9735.91, Sell1 9769.07, Buy2 9709.58, Sell2 9795.40, Stop 9583.57
                            2020-06-08, Buy order is pending... 9747.30
                            2020-06-08, Close 9744.35, Mean 9752.43, Buy1 9735.86, Sell1 9769.01, Buy2 9709.52, Sell2 9795.35, Stop 9583.57
                            2020-06-08, Buy order is pending... 9744.35
                            2020-06-08, Close 9746.74, Mean 9752.40, Buy1 9735.82, Sell1 9768.97, Buy2 9709.48, Sell2 9795.31, Stop 9583.57
                            2020-06-08, Buy order is pending... 9746.74
                            2020-06-08, Close 9744.97, Mean 9752.27, Buy1 9735.69, Sell1 9768.84, Buy2 9709.36, Sell2 9795.18, Stop 9583.57
                            2020-06-08, Buy order is pending... 9744.97
                            2020-06-08, Close 9749.00, Mean 9752.23, Buy1 9735.65, Sell1 9768.81, Buy2 9709.32, Sell2 9795.14, Stop 9583.57
                            2020-06-08, Buy order is pending... 9749.00
                            2020-06-08, Close 9746.80, Mean 9752.17, Buy1 9735.59, Sell1 9768.75, Buy2 9709.26, Sell2 9795.08, Stop 9583.57
                            2020-06-08, Buy order is pending... 9746.80
                            2020-06-08, Close 9741.20, Mean 9751.81, Buy1 9735.24, Sell1 9768.39, Buy2 9708.91, Sell2 9794.72, Stop 9583.57
                            2020-06-08, Buy order is pending... 9741.20
                            2020-06-08, Close 9746.26, Mean 9751.70, Buy1 9735.12, Sell1 9768.27, Buy2 9708.79, Sell2 9794.60, Stop 9583.57
                            2020-06-08, Buy order is pending... 9746.26
                            2020-06-08, Close 9749.24, Mean 9751.67, Buy1 9735.09, Sell1 9768.25, Buy2 9708.76, Sell2 9794.58, Stop 9583.57
                            2020-06-08, Buy order is pending... 9749.24
                            2020-06-08, Close 9761.65, Mean 9751.72, Buy1 9735.14, Sell1 9768.30, Buy2 9708.81, Sell2 9794.63, Stop 9583.57
                            2020-06-08, Buy order is pending... 9761.65
                            2020-06-08, Close 9761.17, Mean 9751.77, Buy1 9735.19, Sell1 9768.34, Buy2 9708.86, Sell2 9794.67, Stop 9583.57
                            2020-06-08, Buy order is pending... 9761.17
                            2020-06-08, Close 9769.26, Mean 9751.87, Buy1 9735.29, Sell1 9768.45, Buy2 9708.96, Sell2 9794.78, Stop 9583.57
                            2020-06-08, Buy order is pending... 9769.26
                            2020-06-08, Close 9765.29, Mean 9751.93, Buy1 9735.36, Sell1 9768.51, Buy2 9709.03, Sell2 9794.84, Stop 9583.57
                            2020-06-08, Buy order is pending... 9765.29
                            2020-06-08, Close 9764.99, Mean 9752.00, Buy1 9735.42, Sell1 9768.57, Buy2 9709.09, Sell2 9794.90, Stop 9583.57
                            2020-06-08, Buy order is pending... 9764.99
                            2020-06-08, Close 9767.91, Mean 9752.43, Buy1 9735.85, Sell1 9769.01, Buy2 9709.52, Sell2 9795.34, Stop 9583.57
                            2020-06-08, Buy order is pending... 9767.91
                            2020-06-08, Close 9768.19, Mean 9752.74, Buy1 9736.16, Sell1 9769.32, Buy2 9709.83, Sell2 9795.65, Stop 9583.57
                            2020-06-08, Buy order is pending... 9768.19
                            2020-06-08, Close 9761.22, Mean 9752.85, Buy1 9736.27, Sell1 9769.43, Buy2 9709.94, Sell2 9795.76, Stop 9583.57
                            2020-06-08, Buy order is pending... 9761.22
                            2020-06-08, Close 9758.42, Mean 9752.97, Buy1 9736.39, Sell1 9769.55, Buy2 9710.06, Sell2 9795.88, Stop 9583.57
                            2020-06-08, Buy order is pending... 9758.42
                            2020-06-08, Close 9755.81, Mean 9753.00, Buy1 9736.42, Sell1 9769.58, Buy2 9710.09, Sell2 9795.91, Stop 9583.57
                            2020-06-08, Buy order is pending... 9755.81
                            2020-06-08, Close 9749.90, Mean 9752.99, Buy1 9736.41, Sell1 9769.57, Buy2 9710.07, Sell2 9795.90, Stop 9583.57
                            2020-06-08, Buy order is pending... 9749.90
                            2020-06-08, Close 9751.36, Mean 9752.98, Buy1 9736.40, Sell1 9769.56, Buy2 9710.07, Sell2 9795.89, Stop 9583.57
                            2020-06-08, Buy order is pending... 9751.36
                            2020-06-08, Close 9740.88, Mean 9752.64, Buy1 9736.06, Sell1 9769.22, Buy2 9709.73, Sell2 9795.55, Stop 9583.57
                            2020-06-08, Buy order is pending... 9740.88
                            2020-06-08, Close 9747.44, Mean 9752.58, Buy1 9736.00, Sell1 9769.16, Buy2 9709.67, Sell2 9795.49, Stop 9583.57
                            2020-06-08, Buy order is pending... 9747.44
                            2020-06-08, Close 9748.33, Mean 9752.56, Buy1 9735.98, Sell1 9769.14, Buy2 9709.65, Sell2 9795.47, Stop 9583.57
                            2020-06-08, Buy order is pending... 9748.33
                            2020-06-08, Close 9749.90, Mean 9752.54, Buy1 9735.96, Sell1 9769.12, Buy2 9709.63, Sell2 9795.45, Stop 9583.57
                            2020-06-08, Buy order is pending... 9749.90
                            2020-06-08, Close 9741.00, Mean 9752.39, Buy1 9735.82, Sell1 9768.97, Buy2 9709.48, Sell2 9795.31, Stop 9583.57
                            2020-06-08, Buy order is pending... 9741.00
                            2020-06-08, Close 9746.80, Mean 9752.34, Buy1 9735.77, Sell1 9768.92, Buy2 9709.43, Sell2 9795.25, Stop 9583.57
                            2020-06-08, Buy order is pending... 9746.80
                            2020-06-08, Close 9746.61, Mean 9752.28, Buy1 9735.71, Sell1 9768.86, Buy2 9709.37, Sell2 9795.19, Stop 9583.57
                            2020-06-08, Buy order is pending... 9746.61
                            --------------------------------  NOTIFY ORDER  --------------------------------
                            AutoOrderedDict() Order Completed
                            2020-06-08, Status 4: Ref: 1, Size: 1, Price: 9736.29896
                            Created: 2020-06-08 00:33:00 Price: 9736.298955328994 Size: 1
                            --------------------------------------------------------------------------------
                            
                            

                            Only issues one buy then it stops, now I'm trying:

                                    if pos == 0:
                                        if close < mean:
                                            self.long = self.buy(price=buy1, size=1, exectype=bt.Order.Limit)
                                            self.log('Close below mean buying step 1... %.2f' % close)
                            
                                        if pos > 0 and self.long:
                                            if close > mean:
                                                self.short = self.sell(price=sell1, size=-1, exectype=bt.Order.Limit)
                                                self.log('Closing long... %.2f' % close)
                            
                            

                            If position size greater than zero and self.long instead of not self.long..
                            New output:

                            2020-06-08, Close 9766.21, Mean 9752.87, Buy1 9736.29, Sell1 9769.45, Buy2 9709.95, Sell2 9795.78, Stop 9583.57
                            2020-06-08, Close 9764.69, Mean 9752.94, Buy1 9736.36, Sell1 9769.52, Buy2 9710.02, Sell2 9795.85, Stop 9583.57
                            2020-06-08, Close 9765.99, Mean 9753.04, Buy1 9736.46, Sell1 9769.62, Buy2 9710.13, Sell2 9795.96, Stop 9583.57
                            2020-06-08, Close 9748.40, Mean 9752.88, Buy1 9736.30, Sell1 9769.46, Buy2 9709.97, Sell2 9795.79, Stop 9583.57
                            2020-06-08, Close below mean buying step 1... 9748.40
                            2020-06-08, Close 9754.18, Mean 9752.90, Buy1 9736.32, Sell1 9769.48, Buy2 9709.98, Sell2 9795.81, Stop 9583.57
                            2020-06-08, Buy order is pending... 9754.18
                            2020-06-08, Close 9751.21, Mean 9752.87, Buy1 9736.29, Sell1 9769.45, Buy2 9709.96, Sell2 9795.78, Stop 9583.57
                            2020-06-08, Buy order is pending... 9751.21
                            2020-06-08, Close 9743.68, Mean 9752.69, Buy1 9736.11, Sell1 9769.27, Buy2 9709.78, Sell2 9795.60, Stop 9583.57
                            2020-06-08, Buy order is pending... 9743.68
                            2020-06-08, Close 9748.55, Mean 9752.64, Buy1 9736.06, Sell1 9769.22, Buy2 9709.73, Sell2 9795.55, Stop 9583.57
                            2020-06-08, Buy order is pending... 9748.55
                            2020-06-08, Close 9749.98, Mean 9752.63, Buy1 9736.05, Sell1 9769.21, Buy2 9709.72, Sell2 9795.54, Stop 9583.57
                            2020-06-08, Buy order is pending... 9749.98
                            2020-06-08, Close 9750.05, Mean 9752.61, Buy1 9736.03, Sell1 9769.19, Buy2 9709.70, Sell2 9795.53, Stop 9583.57
                            2020-06-08, Buy order is pending... 9750.05
                            2020-06-08, Close 9760.51, Mean 9752.71, Buy1 9736.13, Sell1 9769.29, Buy2 9709.80, Sell2 9795.62, Stop 9583.57
                            2020-06-08, Buy order is pending... 9760.51
                            2020-06-08, Close 9755.00, Mean 9752.73, Buy1 9736.15, Sell1 9769.31, Buy2 9709.82, Sell2 9795.64, Stop 9583.57
                            2020-06-08, Buy order is pending... 9755.00
                            2020-06-08, Close 9750.00, Mean 9752.69, Buy1 9736.11, Sell1 9769.27, Buy2 9709.78, Sell2 9795.61, Stop 9583.57
                            2020-06-08, Buy order is pending... 9750.00
                            2020-06-08, Close 9746.81, Mean 9752.62, Buy1 9736.04, Sell1 9769.19, Buy2 9709.70, Sell2 9795.53, Stop 9583.57
                            2020-06-08, Buy order is pending... 9746.81
                            2020-06-08, Close 9750.27, Mean 9752.58, Buy1 9736.00, Sell1 9769.16, Buy2 9709.67, Sell2 9795.49, Stop 9583.57
                            2020-06-08, Buy order is pending... 9750.27
                            2020-06-08, Close 9752.74, Mean 9752.58, Buy1 9736.01, Sell1 9769.16, Buy2 9709.67, Sell2 9795.50, Stop 9583.57
                            2020-06-08, Buy order is pending... 9752.74
                            2020-06-08, Close 9754.03, Mean 9752.61, Buy1 9736.03, Sell1 9769.19, Buy2 9709.70, Sell2 9795.52, Stop 9583.57
                            2020-06-08, Buy order is pending... 9754.03
                            2020-06-08, Close 9749.99, Mean 9752.58, Buy1 9736.00, Sell1 9769.16, Buy2 9709.67, Sell2 9795.49, Stop 9583.57
                            2020-06-08, Buy order is pending... 9749.99
                            2020-06-08, Close 9750.64, Mean 9752.55, Buy1 9735.97, Sell1 9769.13, Buy2 9709.64, Sell2 9795.47, Stop 9583.57
                            2020-06-08, Buy order is pending... 9750.64
                            2020-06-08, Close 9747.30, Mean 9752.49, Buy1 9735.91, Sell1 9769.07, Buy2 9709.58, Sell2 9795.40, Stop 9583.57
                            2020-06-08, Buy order is pending... 9747.30
                            2020-06-08, Close 9744.35, Mean 9752.43, Buy1 9735.86, Sell1 9769.01, Buy2 9709.52, Sell2 9795.35, Stop 9583.57
                            2020-06-08, Buy order is pending... 9744.35
                            2020-06-08, Close 9746.74, Mean 9752.40, Buy1 9735.82, Sell1 9768.97, Buy2 9709.48, Sell2 9795.31, Stop 9583.57
                            2020-06-08, Buy order is pending... 9746.74
                            2020-06-08, Close 9744.97, Mean 9752.27, Buy1 9735.69, Sell1 9768.84, Buy2 9709.36, Sell2 9795.18, Stop 9583.57
                            2020-06-08, Buy order is pending... 9744.97
                            2020-06-08, Close 9749.00, Mean 9752.23, Buy1 9735.65, Sell1 9768.81, Buy2 9709.32, Sell2 9795.14, Stop 9583.57
                            2020-06-08, Buy order is pending... 9749.00
                            2020-06-08, Close 9746.80, Mean 9752.17, Buy1 9735.59, Sell1 9768.75, Buy2 9709.26, Sell2 9795.08, Stop 9583.57
                            2020-06-08, Buy order is pending... 9746.80
                            2020-06-08, Close 9741.20, Mean 9751.81, Buy1 9735.24, Sell1 9768.39, Buy2 9708.91, Sell2 9794.72, Stop 9583.57
                            2020-06-08, Buy order is pending... 9741.20
                            2020-06-08, Close 9746.26, Mean 9751.70, Buy1 9735.12, Sell1 9768.27, Buy2 9708.79, Sell2 9794.60, Stop 9583.57
                            2020-06-08, Buy order is pending... 9746.26
                            2020-06-08, Close 9749.24, Mean 9751.67, Buy1 9735.09, Sell1 9768.25, Buy2 9708.76, Sell2 9794.58, Stop 9583.57
                            2020-06-08, Buy order is pending... 9749.24
                            2020-06-08, Close 9761.65, Mean 9751.72, Buy1 9735.14, Sell1 9768.30, Buy2 9708.81, Sell2 9794.63, Stop 9583.57
                            2020-06-08, Buy order is pending... 9761.65
                            2020-06-08, Close 9761.17, Mean 9751.77, Buy1 9735.19, Sell1 9768.34, Buy2 9708.86, Sell2 9794.67, Stop 9583.57
                            2020-06-08, Buy order is pending... 9761.17
                            2020-06-08, Close 9769.26, Mean 9751.87, Buy1 9735.29, Sell1 9768.45, Buy2 9708.96, Sell2 9794.78, Stop 9583.57
                            2020-06-08, Buy order is pending... 9769.26
                            2020-06-08, Close 9765.29, Mean 9751.93, Buy1 9735.36, Sell1 9768.51, Buy2 9709.03, Sell2 9794.84, Stop 9583.57
                            2020-06-08, Buy order is pending... 9765.29
                            2020-06-08, Close 9764.99, Mean 9752.00, Buy1 9735.42, Sell1 9768.57, Buy2 9709.09, Sell2 9794.90, Stop 9583.57
                            2020-06-08, Buy order is pending... 9764.99
                            2020-06-08, Close 9767.91, Mean 9752.43, Buy1 9735.85, Sell1 9769.01, Buy2 9709.52, Sell2 9795.34, Stop 9583.57
                            2020-06-08, Buy order is pending... 9767.91
                            2020-06-08, Close 9768.19, Mean 9752.74, Buy1 9736.16, Sell1 9769.32, Buy2 9709.83, Sell2 9795.65, Stop 9583.57
                            2020-06-08, Buy order is pending... 9768.19
                            2020-06-08, Close 9761.22, Mean 9752.85, Buy1 9736.27, Sell1 9769.43, Buy2 9709.94, Sell2 9795.76, Stop 9583.57
                            2020-06-08, Buy order is pending... 9761.22
                            2020-06-08, Close 9758.42, Mean 9752.97, Buy1 9736.39, Sell1 9769.55, Buy2 9710.06, Sell2 9795.88, Stop 9583.57
                            2020-06-08, Buy order is pending... 9758.42
                            2020-06-08, Close 9755.81, Mean 9753.00, Buy1 9736.42, Sell1 9769.58, Buy2 9710.09, Sell2 9795.91, Stop 9583.57
                            2020-06-08, Buy order is pending... 9755.81
                            2020-06-08, Close 9749.90, Mean 9752.99, Buy1 9736.41, Sell1 9769.57, Buy2 9710.07, Sell2 9795.90, Stop 9583.57
                            2020-06-08, Buy order is pending... 9749.90
                            2020-06-08, Close 9751.36, Mean 9752.98, Buy1 9736.40, Sell1 9769.56, Buy2 9710.07, Sell2 9795.89, Stop 9583.57
                            2020-06-08, Buy order is pending... 9751.36
                            2020-06-08, Close 9740.88, Mean 9752.64, Buy1 9736.06, Sell1 9769.22, Buy2 9709.73, Sell2 9795.55, Stop 9583.57
                            2020-06-08, Buy order is pending... 9740.88
                            2020-06-08, Close 9747.44, Mean 9752.58, Buy1 9736.00, Sell1 9769.16, Buy2 9709.67, Sell2 9795.49, Stop 9583.57
                            2020-06-08, Buy order is pending... 9747.44
                            2020-06-08, Close 9748.33, Mean 9752.56, Buy1 9735.98, Sell1 9769.14, Buy2 9709.65, Sell2 9795.47, Stop 9583.57
                            2020-06-08, Buy order is pending... 9748.33
                            2020-06-08, Close 9749.90, Mean 9752.54, Buy1 9735.96, Sell1 9769.12, Buy2 9709.63, Sell2 9795.45, Stop 9583.57
                            2020-06-08, Buy order is pending... 9749.90
                            2020-06-08, Close 9741.00, Mean 9752.39, Buy1 9735.82, Sell1 9768.97, Buy2 9709.48, Sell2 9795.31, Stop 9583.57
                            2020-06-08, Buy order is pending... 9741.00
                            2020-06-08, Close 9746.80, Mean 9752.34, Buy1 9735.77, Sell1 9768.92, Buy2 9709.43, Sell2 9795.25, Stop 9583.57
                            2020-06-08, Buy order is pending... 9746.80
                            2020-06-08, Close 9746.61, Mean 9752.28, Buy1 9735.71, Sell1 9768.86, Buy2 9709.37, Sell2 9795.19, Stop 9583.57
                            2020-06-08, Buy order is pending... 9746.61
                            --------------------------------  NOTIFY ORDER  --------------------------------
                            AutoOrderedDict() Order Completed
                            2020-06-08, Status 4: Ref: 1, Size: 1, Price: 9736.29896
                            Created: 2020-06-08 00:33:00 Price: 9736.298955328994 Size: 1
                            --------------------------------------------------------------------------------
                            2020-06-08, Close 9740.00, Mean 9752.12, Buy1 9735.54, Sell1 9768.70, Buy2 9709.21, Sell2 9795.03, Stop 9583.57
                            2020-06-08, Close 9742.04, Mean 9752.01, Buy1 9735.43, Sell1 9768.59, Buy2 9709.10, Sell2 9794.92, Stop 9583.57
                            2020-06-08, Close 9740.01, Mean 9751.88, Buy1 9735.31, Sell1 9768.46, Buy2 9708.98, Sell2 9794.79, Stop 9583.57
                            2020-06-08, Close 9748.58, Mean 9751.86, Buy1 9735.28, Sell1 9768.44, Buy2 9708.95, Sell2 9794.77, Stop 9583.57
                            2020-06-08, Close 9745.62, Mean 9751.83, Buy1 9735.25, Sell1 9768.41, Buy2 9708.92, Sell2 9794.74, Stop 9583.57
                            2020-06-08, Close 9747.26, Mean 9751.80, Buy1 9735.23, Sell1 9768.38, Buy2 9708.90, Sell2 9794.71, Stop 9583.57
                            2020-06-08, Close 9741.02, Mean 9751.69, Buy1 9735.11, Sell1 9768.27, Buy2 9708.79, Sell2 9794.60, Stop 9583.57
                            2020-06-08, Close 9757.18, Mean 9751.76, Buy1 9735.18, Sell1 9768.34, Buy2 9708.85, Sell2 9794.66, Stop 9583.57
                            2020-06-08, Close 9750.71, Mean 9751.75, Buy1 9735.17, Sell1 9768.32, Buy2 9708.84, Sell2 9794.65, Stop 9583.57
                            2020-06-08, Close 9743.98, Mean 9751.70, Buy1 9735.13, Sell1 9768.28, Buy2 9708.80, Sell2 9794.61, Stop 9583.57
                            
                            

                            Which is the same output as before... now I'm back to square 0.
                            Well thanks anyways I'll keep at it.
                            If this was easy it would not be worthwhile.

                            A 1 Reply Last reply Reply Quote 0
                            • cept0r
                              cept0r @cept0r last edited by

                              Trying this now.. removing both self.long and self.short and structuring it this way.

                                      if pos == 0:
                                          if close < mean:
                                              self.buy(price=buy1, size=1, exectype=bt.Order.Limit)
                                              self.log('Close below mean buying step 1... %.2f' % close)
                              
                                          if pos > 0 and self.long:
                                              if close > mean:
                                                  self.sell(price=sell1, size=-1, exectype=bt.Order.Limit)
                                                  self.log('Closing long... %.2f' % close)
                              

                              This drained all my available cash at broker.. so going back to my previous solution and I'll work from there.

                              Output:

                              Starting Portfolio Value: 100000.00
                              2020-06-08, Close 9766.21, Mean 9752.87, Buy1 9736.29, Sell1 9769.45, Buy2 9709.95, Sell2 9795.78, Stop 9583.57
                              2020-06-08, Close 9764.69, Mean 9752.94, Buy1 9736.36, Sell1 9769.52, Buy2 9710.02, Sell2 9795.85, Stop 9583.57
                              2020-06-08, Close 9765.99, Mean 9753.04, Buy1 9736.46, Sell1 9769.62, Buy2 9710.13, Sell2 9795.96, Stop 9583.57
                              2020-06-08, Close 9748.40, Mean 9752.88, Buy1 9736.30, Sell1 9769.46, Buy2 9709.97, Sell2 9795.79, Stop 9583.57
                              2020-06-08, Close below mean buying step 1... 9748.40
                              2020-06-08, Close 9754.18, Mean 9752.90, Buy1 9736.32, Sell1 9769.48, Buy2 9709.98, Sell2 9795.81, Stop 9583.57
                              2020-06-08, Close 9751.21, Mean 9752.87, Buy1 9736.29, Sell1 9769.45, Buy2 9709.96, Sell2 9795.78, Stop 9583.57
                              2020-06-08, Close below mean buying step 1... 9751.21
                              2020-06-08, Close 9743.68, Mean 9752.69, Buy1 9736.11, Sell1 9769.27, Buy2 9709.78, Sell2 9795.60, Stop 9583.57
                              2020-06-08, Close below mean buying step 1... 9743.68
                              2020-06-08, Close 9748.55, Mean 9752.64, Buy1 9736.06, Sell1 9769.22, Buy2 9709.73, Sell2 9795.55, Stop 9583.57
                              2020-06-08, Close below mean buying step 1... 9748.55
                              2020-06-08, Close 9749.98, Mean 9752.63, Buy1 9736.05, Sell1 9769.21, Buy2 9709.72, Sell2 9795.54, Stop 9583.57
                              2020-06-08, Close below mean buying step 1... 9749.98
                              2020-06-08, Close 9750.05, Mean 9752.61, Buy1 9736.03, Sell1 9769.19, Buy2 9709.70, Sell2 9795.53, Stop 9583.57
                              2020-06-08, Close below mean buying step 1... 9750.05
                              2020-06-08, Close 9760.51, Mean 9752.71, Buy1 9736.13, Sell1 9769.29, Buy2 9709.80, Sell2 9795.62, Stop 9583.57
                              2020-06-08, Close 9755.00, Mean 9752.73, Buy1 9736.15, Sell1 9769.31, Buy2 9709.82, Sell2 9795.64, Stop 9583.57
                              2020-06-08, Close 9750.00, Mean 9752.69, Buy1 9736.11, Sell1 9769.27, Buy2 9709.78, Sell2 9795.61, Stop 9583.57
                              2020-06-08, Close below mean buying step 1... 9750.00
                              2020-06-08, Close 9746.81, Mean 9752.62, Buy1 9736.04, Sell1 9769.19, Buy2 9709.70, Sell2 9795.53, Stop 9583.57
                              2020-06-08, Close below mean buying step 1... 9746.81
                              2020-06-08, Close 9750.27, Mean 9752.58, Buy1 9736.00, Sell1 9769.16, Buy2 9709.67, Sell2 9795.49, Stop 9583.57
                              2020-06-08, Close below mean buying step 1... 9750.27
                              2020-06-08, Close 9752.74, Mean 9752.58, Buy1 9736.01, Sell1 9769.16, Buy2 9709.67, Sell2 9795.50, Stop 9583.57
                              2020-06-08, Close 9754.03, Mean 9752.61, Buy1 9736.03, Sell1 9769.19, Buy2 9709.70, Sell2 9795.52, Stop 9583.57
                              2020-06-08, Close 9749.99, Mean 9752.58, Buy1 9736.00, Sell1 9769.16, Buy2 9709.67, Sell2 9795.49, Stop 9583.57
                              2020-06-08, Close below mean buying step 1... 9749.99
                              2020-06-08, Close 9750.64, Mean 9752.55, Buy1 9735.97, Sell1 9769.13, Buy2 9709.64, Sell2 9795.47, Stop 9583.57
                              2020-06-08, Close below mean buying step 1... 9750.64
                              2020-06-08, Close 9747.30, Mean 9752.49, Buy1 9735.91, Sell1 9769.07, Buy2 9709.58, Sell2 9795.40, Stop 9583.57
                              2020-06-08, Close below mean buying step 1... 9747.30
                              2020-06-08, Close 9744.35, Mean 9752.43, Buy1 9735.86, Sell1 9769.01, Buy2 9709.52, Sell2 9795.35, Stop 9583.57
                              2020-06-08, Close below mean buying step 1... 9744.35
                              2020-06-08, Close 9746.74, Mean 9752.40, Buy1 9735.82, Sell1 9768.97, Buy2 9709.48, Sell2 9795.31, Stop 9583.57
                              2020-06-08, Close below mean buying step 1... 9746.74
                              2020-06-08, Close 9744.97, Mean 9752.27, Buy1 9735.69, Sell1 9768.84, Buy2 9709.36, Sell2 9795.18, Stop 9583.57
                              2020-06-08, Close below mean buying step 1... 9744.97
                              2020-06-08, Close 9749.00, Mean 9752.23, Buy1 9735.65, Sell1 9768.81, Buy2 9709.32, Sell2 9795.14, Stop 9583.57
                              2020-06-08, Close below mean buying step 1... 9749.00
                              2020-06-08, Close 9746.80, Mean 9752.17, Buy1 9735.59, Sell1 9768.75, Buy2 9709.26, Sell2 9795.08, Stop 9583.57
                              2020-06-08, Close below mean buying step 1... 9746.80
                              2020-06-08, Close 9741.20, Mean 9751.81, Buy1 9735.24, Sell1 9768.39, Buy2 9708.91, Sell2 9794.72, Stop 9583.57
                              2020-06-08, Close below mean buying step 1... 9741.20
                              2020-06-08, Close 9746.26, Mean 9751.70, Buy1 9735.12, Sell1 9768.27, Buy2 9708.79, Sell2 9794.60, Stop 9583.57
                              2020-06-08, Close below mean buying step 1... 9746.26
                              2020-06-08, Close 9749.24, Mean 9751.67, Buy1 9735.09, Sell1 9768.25, Buy2 9708.76, Sell2 9794.58, Stop 9583.57
                              2020-06-08, Close below mean buying step 1... 9749.24
                              2020-06-08, Close 9761.65, Mean 9751.72, Buy1 9735.14, Sell1 9768.30, Buy2 9708.81, Sell2 9794.63, Stop 9583.57
                              2020-06-08, Close 9761.17, Mean 9751.77, Buy1 9735.19, Sell1 9768.34, Buy2 9708.86, Sell2 9794.67, Stop 9583.57
                              2020-06-08, Close 9769.26, Mean 9751.87, Buy1 9735.29, Sell1 9768.45, Buy2 9708.96, Sell2 9794.78, Stop 9583.57
                              2020-06-08, Close 9765.29, Mean 9751.93, Buy1 9735.36, Sell1 9768.51, Buy2 9709.03, Sell2 9794.84, Stop 9583.57
                              2020-06-08, Close 9764.99, Mean 9752.00, Buy1 9735.42, Sell1 9768.57, Buy2 9709.09, Sell2 9794.90, Stop 9583.57
                              2020-06-08, Close 9767.91, Mean 9752.43, Buy1 9735.85, Sell1 9769.01, Buy2 9709.52, Sell2 9795.34, Stop 9583.57
                              2020-06-08, Close 9768.19, Mean 9752.74, Buy1 9736.16, Sell1 9769.32, Buy2 9709.83, Sell2 9795.65, Stop 9583.57
                              2020-06-08, Close 9761.22, Mean 9752.85, Buy1 9736.27, Sell1 9769.43, Buy2 9709.94, Sell2 9795.76, Stop 9583.57
                              2020-06-08, Close 9758.42, Mean 9752.97, Buy1 9736.39, Sell1 9769.55, Buy2 9710.06, Sell2 9795.88, Stop 9583.57
                              2020-06-08, Close 9755.81, Mean 9753.00, Buy1 9736.42, Sell1 9769.58, Buy2 9710.09, Sell2 9795.91, Stop 9583.57
                              2020-06-08, Close 9749.90, Mean 9752.99, Buy1 9736.41, Sell1 9769.57, Buy2 9710.07, Sell2 9795.90, Stop 9583.57
                              2020-06-08, Close below mean buying step 1... 9749.90
                              2020-06-08, Close 9751.36, Mean 9752.98, Buy1 9736.40, Sell1 9769.56, Buy2 9710.07, Sell2 9795.89, Stop 9583.57
                              2020-06-08, Close below mean buying step 1... 9751.36
                              2020-06-08, Close 9740.88, Mean 9752.64, Buy1 9736.06, Sell1 9769.22, Buy2 9709.73, Sell2 9795.55, Stop 9583.57
                              2020-06-08, Close below mean buying step 1... 9740.88
                              2020-06-08, Close 9747.44, Mean 9752.58, Buy1 9736.00, Sell1 9769.16, Buy2 9709.67, Sell2 9795.49, Stop 9583.57
                              2020-06-08, Close below mean buying step 1... 9747.44
                              2020-06-08, Close 9748.33, Mean 9752.56, Buy1 9735.98, Sell1 9769.14, Buy2 9709.65, Sell2 9795.47, Stop 9583.57
                              2020-06-08, Close below mean buying step 1... 9748.33
                              2020-06-08, Close 9749.90, Mean 9752.54, Buy1 9735.96, Sell1 9769.12, Buy2 9709.63, Sell2 9795.45, Stop 9583.57
                              2020-06-08, Close below mean buying step 1... 9749.90
                              2020-06-08, Close 9741.00, Mean 9752.39, Buy1 9735.82, Sell1 9768.97, Buy2 9709.48, Sell2 9795.31, Stop 9583.57
                              2020-06-08, Close below mean buying step 1... 9741.00
                              2020-06-08, Close 9746.80, Mean 9752.34, Buy1 9735.77, Sell1 9768.92, Buy2 9709.43, Sell2 9795.25, Stop 9583.57
                              2020-06-08, Close below mean buying step 1... 9746.80
                              2020-06-08, Close 9746.61, Mean 9752.28, Buy1 9735.71, Sell1 9768.86, Buy2 9709.37, Sell2 9795.19, Stop 9583.57
                              2020-06-08, Close below mean buying step 1... 9746.61
                              --------------------------------  NOTIFY ORDER  --------------------------------
                              AutoOrderedDict() Order Completed
                              2020-06-08, Status 4: Ref: 1, Size: 1, Price: 9736.29896
                              Created: 2020-06-08 00:33:00 Price: 9736.298955328994 Size: 1
                              --------------------------------------------------------------------------------
                              --------------------------------  NOTIFY ORDER  --------------------------------
                              AutoOrderedDict() Order Completed
                              2020-06-08, Status 4: Ref: 2, Size: 1, Price: 9736.29155
                              Created: 2020-06-08 00:35:00 Price: 9736.29155470442 Size: 1
                              --------------------------------------------------------------------------------
                              --------------------------------  NOTIFY ORDER  --------------------------------
                              AutoOrderedDict() Order Completed
                              2020-06-08, Status 4: Ref: 3, Size: 1, Price: 9736.11193
                              Created: 2020-06-08 00:36:00 Price: 9736.111929566061 Size: 1
                              --------------------------------------------------------------------------------
                              --------------------------------  NOTIFY ORDER  --------------------------------
                              AutoOrderedDict() Order Completed
                              2020-06-08, Status 4: Ref: 4, Size: 1, Price: 9736.06164
                              Created: 2020-06-08 00:37:00 Price: 9736.061644894588 Size: 1
                              --------------------------------------------------------------------------------
                              --------------------------------  NOTIFY ORDER  --------------------------------
                              AutoOrderedDict() Order Completed
                              2020-06-08, Status 4: Ref: 5, Size: 1, Price: 9736.04927
                              Created: 2020-06-08 00:38:00 Price: 9736.049271101505 Size: 1
                              --------------------------------------------------------------------------------
                              --------------------------------  NOTIFY ORDER  --------------------------------
                              AutoOrderedDict() Order Completed
                              2020-06-08, Status 4: Ref: 6, Size: 1, Price: 9736.03439
                              Created: 2020-06-08 00:39:00 Price: 9736.034389641902 Size: 1
                              --------------------------------------------------------------------------------
                              --------------------------------  NOTIFY ORDER  --------------------------------
                              AutoOrderedDict() Order Completed
                              2020-06-08, Status 4: Ref: 7, Size: 1, Price: 9736.11408
                              Created: 2020-06-08 00:42:00 Price: 9736.114080702468 Size: 1
                              --------------------------------------------------------------------------------
                              --------------------------------  NOTIFY ORDER  --------------------------------
                              AutoOrderedDict() Order Completed
                              2020-06-08, Status 4: Ref: 8, Size: 1, Price: 9736.03599
                              Created: 2020-06-08 00:43:00 Price: 9736.035992975514 Size: 1
                              --------------------------------------------------------------------------------
                              --------------------------------  NOTIFY ORDER  --------------------------------
                              AutoOrderedDict() Order Completed
                              2020-06-08, Status 4: Ref: 9, Size: 1, Price: 9736.00286
                              Created: 2020-06-08 00:44:00 Price: 9736.00285874349 Size: 1
                              --------------------------------------------------------------------------------
                              --------------------------------  NOTIFY ORDER  --------------------------------
                              AutoOrderedDict() Order Completed
                              2020-06-08, Status 4: Ref: 10, Size: 1, Price: 9735.99812
                              Created: 2020-06-08 00:47:00 Price: 9735.99811721294 Size: 1
                              
                              1 Reply Last reply Reply Quote 0
                              • A
                                ab_trader @cept0r last edited by

                                @cept0r said in Issuing multiple orders, how do I only issue one order at a time?:

                                Only issues one buy then it stops, now I'm trying

                                Why do you expect more buying orders to happen? Price history is ended.

                                Since you don't print the bar times I go with the output line numbers. Based on your outputs - the order is issued on the line 5 or 6, buy limit at 9736.30. This order was executed at the very end of your output block - probably at the at time the prices went lower than 9736.30.

                                cept0r 1 Reply Last reply Reply Quote 2
                                • cept0r
                                  cept0r last edited by

                                  Oh my bad, I'm not expecting any more buy orders to happen. I think I managed to solve it now and I understand what I'm doing wrong.
                                  Thank you @ab_trader and @run-out ! I'll keep at it

                                  1 Reply Last reply Reply Quote 1
                                  • cept0r
                                    cept0r @ab_trader last edited by

                                    @ab_trader
                                    I'm trying to get this " not self.long / self.short " condition to execute as I need a third condition in my strategy in order for it to work properly. But When I run this:

                                            if pos == 0:
                                                if close < mean:
                                                    self.long = self.buy(price=buy1, size=1, exectype=bt.Order.Limit)
                                                    self.log('Close below mean buying step 1... %.2f' % close)
                                                    if not self.long and pos > 0:
                                                        self.sell(price=sell1, size=-pos, exectype=bt.Order.Limit)
                                                        self.log('Buy order 1 hit creating sell order... %.2f' % close)
                                    

                                    Or

                                            if pos == 0:
                                                if close < mean:
                                                    self.long = self.buy(price=buy1, size=1, exectype=bt.Order.Limit)
                                                    self.log('Close below mean buying step 1... %.2f' % close)
                                    
                                                if pos > 0 and not self.long:
                                                    if close > mean:
                                                        self.short = self.sell(price=sell1, size=-1, exectype=bt.Order.Limit)
                                                        self.log('Closing long... %.2f' % close)
                                    

                                    It only executes the buy but it does not recognize the "not self.long" condition here is the output: (same for both)

                                    019-12-15, Close 7023.99, Mean 7045.62, Buy1 7033.64, Sell1 7057.60, Buy2 7014.62, Sell2 7076.62, Stop 6978.05
                                    2019-12-15, Close below mean buying step 1... 7023.99
                                    --------------------------------  NOTIFY ORDER  --------------------------------
                                    AutoOrderedDict() Order Completed
                                    2019-12-15, Status 4: Ref: 1, Size: 1, Price: 7033.64111
                                    Created: 2019-12-15 00:30:00 Price: 7033.641114933333 Size: 1
                                    --------------------------------------------------------------------------------
                                    2019-12-15, Close 7014.82, Mean 7043.32, Buy1 7031.35, Sell1 7055.29, Buy2 7012.33, Sell2 7074.31, Stop 6978.05
                                    2019-12-15, Close 7021.39, Mean 7041.74, Buy1 7029.77, Sell1 7053.71, Buy2 7010.75, Sell2 7072.72, Stop 6978.05
                                    2019-12-15, Close 7016.95, Mean 7039.94, Buy1 7027.97, Sell1 7051.90, Buy2 7008.96, Sell2 7070.91, Stop 6978.05
                                    2019-12-15, Close 7020.44, Mean 7038.95, Buy1 7026.98, Sell1 7050.91, Buy2 7007.98, Sell2 7069.92, Stop 6978.05
                                    2019-12-15, Close 7021.28, Mean 7037.46, Buy1 7025.49, Sell1 7049.42, Buy2 7006.49, Sell2 7068.42, Stop 6978.05
                                    2019-12-15, Close 7037.76, Mean 7037.47, Buy1 7025.50, Sell1 7049.43, Buy2 7006.50, Sell2 7068.43, Stop 6978.05
                                    2019-12-15, Close 7037.51, Mean 7037.47, Buy1 7025.50, Sell1 7049.43, Buy2 7006.50, Sell2 7068.43, Stop 6978.05
                                    2019-12-15, Close 7035.25, Mean 7037.42, Buy1 7025.46, Sell1 7049.39, Buy2 7006.46, Sell2 7068.39, Stop 6978.05
                                    2019-12-15, Close 7038.30, Mean 7037.44, Buy1 7025.47, Sell1 7049.40, Buy2 7006.47, Sell2 7068.40, Stop 6978.05
                                    2019-12-15, Close 7038.36, Mean 7037.44, Buy1 7025.48, Sell1 7049.41, Buy2 7006.48, Sell2 7068.41, Stop 6978.05
                                    2019-12-15, Close 7030.42, Mean 7037.20, Buy1 7025.23, Sell1 7049.16, Buy2 7006.23, Sell2 7068.16, Stop 6978.05
                                    2019-12-15, Close 7027.24, Mean 7036.78, Buy1 7024.81, Sell1 7048.74, Buy2 7005.81, Sell2 7067.74, Stop 6978.05
                                    2019-12-15, Close 7032.76, Mean 7036.66, Buy1 7024.70, Sell1 7048.62, Buy2 7005.70, Sell2 7067.62, Stop 6978.05
                                    2019-12-15, Close 7040.45, Mean 7036.69, Buy1 7024.73, Sell1 7048.66, Buy2 7005.73, Sell2 7067.66, Stop 6978.05
                                    2019-12-15, Close 7047.92, Mean 7036.74, Buy1 7024.78, Sell1 7048.71, Buy2 7005.78, Sell2 7067.70, Stop 6978.05
                                    2019-12-15, Close 7044.39, Mean 7036.78, Buy1 7024.82, Sell1 7048.75, Buy2 7005.82, Sell2 7067.75, Stop 6978.05
                                    2019-12-15, Close 7043.37, Mean 7036.82, Buy1 7024.85, Sell1 7048.78, Buy2 7005.85, Sell2 7067.78, Stop 6978.05
                                    2019-12-15, Close 7047.54, Mean 7036.89, Buy1 7024.93, Sell1 7048.85, Buy2 7005.93, Sell2 7067.85, Stop 6978.05
                                    2019-12-15, Close 7044.09, Mean 7036.92, Buy1 7024.96, Sell1 7048.89, Buy2 7005.96, Sell2 7067.89, Stop 6978.05
                                    2019-12-15, Close 7038.41, Mean 7036.93, Buy1 7024.97, Sell1 7048.90, Buy2 7005.97, Sell2 7067.90, Stop 6978.05
                                    2019-12-15, Close 7042.26, Mean 7036.98, Buy1 7025.01, Sell1 7048.94, Buy2 7006.02, Sell2 7067.94, Stop 6978.05
                                    2019-12-15, Close 7039.05, Mean 7036.99, Buy1 7025.03, Sell1 7048.95, Buy2 7006.03, Sell2 7067.95, Stop 6978.05
                                    2019-12-15, Close 7042.52, Mean 7037.02, Buy1 7025.05, Sell1 7048.98, Buy2 7006.05, Sell2 7067.98, Stop 6978.05
                                    2019-12-15, Close 7037.30, Mean 7037.02, Buy1 7025.06, Sell1 7048.98, Buy2 7006.06, Sell2 7067.98, Stop 6978.05
                                    2019-12-15, Close 7034.75, Mean 7037.01, Buy1 7025.04, Sell1 7048.97, Buy2 7006.04, Sell2 7067.97, Stop 6978.05
                                    2019-12-15, Close 7036.46, Mean 7037.00, Buy1 7025.04, Sell1 7048.97, Buy2 7006.04, Sell2 7067.97, Stop 6978.05
                                    2019-12-15, Close 7037.07, Mean 7037.01, Buy1 7025.04, Sell1 7048.97, Buy2 7006.04, Sell2 7067.97, Stop 6978.05
                                    2019-12-15, Close 7042.83, Mean 7037.09, Buy1 7025.12, Sell1 7049.05, Buy2 7006.12, Sell2 7068.05, Stop 6978.05
                                    2019-12-15, Close 7046.28, Mean 7037.26, Buy1 7025.29, Sell1 7049.22, Buy2 7006.29, Sell2 7068.22, Stop 6978.05
                                    2019-12-15, Close 7041.87, Mean 7037.35, Buy1 7025.39, Sell1 7049.32, Buy2 7006.39, Sell2 7068.32, Stop 6978.05
                                    2019-12-15, Close 7043.00, Mean 7037.59, Buy1 7025.62, Sell1 7049.55, Buy2 7006.62, Sell2 7068.55, Stop 6978.05
                                    2019-12-15, Close 7044.89, Mean 7037.84, Buy1 7025.87, Sell1 7049.80, Buy2 7006.87, Sell2 7068.80, Stop 6978.05
                                    2019-12-15, Close 7048.59, Mean 7038.39, Buy1 7026.43, Sell1 7050.36, Buy2 7007.42, Sell2 7069.36, Stop 6978.05
                                    2019-12-15, Close 7052.48, Mean 7039.13, Buy1 7027.16, Sell1 7051.10, Buy2 7008.16, Sell2 7070.10, Stop 6978.05
                                    
                                    

                                    At the end closing price reaches sell1 so it should execute but it won't even create a sell order. So I'm wondering what I'm doing wrong?

                                    A 1 Reply Last reply Reply Quote 0
                                    • cept0r
                                      cept0r @ab_trader last edited by

                                      @ab_trader
                                      On another note:

                                              if pos == 0:
                                                  if close < mean:
                                                      self.long = self.buy(price=buy1, size=1, exectype=bt.Order.Limit)
                                                      self.log('Close below mean buying step 1... %.2f' % close)
                                      
                                              if pos > 0:
                                                  if close > mean:
                                                      self.short = self.sell(price=sell1, size=-1, exectype=bt.Order.Limit)
                                                      self.log('Closing long... %.2f' % close)
                                      
                                      

                                      This works.

                                              if pos == 0:
                                                  if close < mean:
                                                      self.long = self.buy(price=buy1, size=1, exectype=bt.Order.Limit)
                                                      self.log('Close below mean buying step 1... %.2f' % close)
                                      
                                                  if pos > 0:
                                                      if close > mean:
                                                          self.short = self.sell(price=sell1, size=-1, exectype=bt.Order.Limit)
                                                          self.log('Closing long... %.2f' % close)
                                      
                                      

                                      This does not.
                                      So I can't indent and the extra conditions won't matter since it won't work indented so I'm trying:

                                              orders = self.broker.get_orders_open()
                                              orders = list(orders)
                                      

                                      Then I'll do if orders == 1 and pos < size2 etc..

                                      1 Reply Last reply Reply Quote 0
                                      • cept0r
                                        cept0r last edited by

                                        That did not take either.. hmm well if I use static sizes that could work temporarily if only just to see if I can manage to finish my trade logic today.

                                        1 Reply Last reply Reply Quote 0
                                        • A
                                          ab_trader @cept0r last edited by

                                          @cept0r said in Issuing multiple orders, how do I only issue one order at a time?:

                                          So I'm wondering what I'm doing wrong?

                                          Evidently your script can work only for pos == 0 case. As soon as position open (and we can see this in the logs) i.e. pos != 0, your second condition if pos > 0 and not self.long: doesn't work anymore.

                                          1 Reply Last reply Reply Quote 2
                                          • 1 / 1
                                          • First post
                                            Last post
                                          Copyright © 2016, 2017, 2018 NodeBB Forums | Contributors
                                          $(document).ready(function () { app.coldLoad(); }); }