For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

Sell bracket position after a holding period



  • Can someone post a simple example of how to sell a bracket order after a set number of days if it has not sold yet?



  • Could you clarify your question and provide and example?



  •     def next(self):
            self.log('Close, %.2f' % self.dataclose[0])
            # Check if an order is pending ... 
            if self.order:
                return
    
            if not self.position:                 #BUY 
                if self.bbreak_bottom:
                    self.log('BUY CREATE, %.2f' % self.dataclose[0])
                    self.buy_bracket(limitprice=self.dataclose * (1.0 + 
    self.p.takeprofit),
                                                         price=self.dataclose[0],
                                                         stopprice=self.dataclose * (1.0 - self.p.stoploss))


  • @Tim-Donnelly So i have a simple bracket order like this, but I want to be able to close the position after x amount of days if it hasn't sold yet. I'm sorry code is a bit messy it won't let me edit it.



  • @Tim-Donnelly said in Sell bracket position after a holding period:

    You should be able to use valid. See the sample usage, in particular here:

        params = dict(
            limdays=3,
            limdays2=1000,
    )
    ...
    valid1 = datetime.timedelta(self.p.limdays)
    valid2 = valid3 = datetime.timedelta(self.p.limdays2)
    ...
    os = self.buy_bracket(
                            price=p1, valid=valid1,
                            stopprice=p2, stopargs=dict(valid=valid2),
                            limitprice=p3, limitargs=dict(valid=valid3),)
    




  • @run-out Thank you! Will this automatically sell/close the position after x amount of days because I can't get that to work. I thought the "valid" argument only sets an expiration date for the limit and stop orders after the Sell executed, as opposed to automatically selling/closing the position.



  • Here's an example of what I tried to do but it just keeps selling every 5 days even when I no longer have shares to sell.

    def next(self):
        self.log('Close, %.2f' % self.dataclose[0])
    
        if self.order:
            return
    
      # Check if an order is pending ... if yes, we cannot send a 2nd one
        if not self.position:
            if self.bbreak_bottom:
                if self.dataclose < cash:
                    self.buy_bracket(price=self.dataclose[0],
                  stopprice=self.dataclose * (1.0 - self.p.stoploss),
                  limitprice=self.dataclose * (1.0 + self.p.takeprofit))
    
        else:
            # Already in the market ... we might sell
            if len(self) >= (self.bar_executed + 5):
            # SELL, SELL, SELL!!! (with all possible default parameters)
                self.log('SELL CREATE, %.2f' % self.dataclose[0])
    
                # Keep track of the created order to avoid a 2nd order
                self.order = self.sell()
    
    

    I copied and pasted the sell part from the Quick Start Guide.



  • You are selling short every 5 days and there's no offsetting buy to close out the short sale. If you want to close a position use self.close.


Log in to reply
 

});