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

100% in the market strategy



  • I seem to be having a hard time building a strategy that is long/short 100% of the time. I will try to give as much info as I can think of that is needed for help, if you need more info just let me know.

           #this is called to place first order, i put one , but it still places 2 trades (see logs
            if self.position.size == 0:
                if self.buy_sig:
                    self.buy(size=1)
                elif self.sell_sig:
                    self.sell(size=1)
    
            #signal to buy
            if self.position.size <= 0:
                if self.buy_sig:
                    self.buy()
            #signal to sell
            if self.position.size >= 0:
                if self.sell_sig:
                    self.sell()
    

    I am using a sizer to reverse the order:
    cerebro.addsizer(bt.sizers.FixedReverser, stake=1)

    this is what the output log looks like to better help describe the needed results:

    2016-01-06, Buy Sig nan Sell Sig nan Position 0
    2016-01-06, Buy Sig 1.0 Sell Sig 0.0 Position 0
    2016-01-06, Buy Sig 1.0 Sell Sig 0.0 Position 2
    2016-01-06, Buy Sig 1.0 Sell Sig 0.0 Position 2
    2016-01-07, Buy Sig 1.0 Sell Sig 0.0 Position 2
    2016-01-07, Buy Sig 0.0 Sell Sig 0.0 Position 2
    2016-01-07, Buy Sig 0.0 Sell Sig 0.0 Position 2
    2016-01-07, Buy Sig 0.0 Sell Sig 1.0 Position 2
    2016-01-07, Buy Sig 0.0 Sell Sig 1.0 Position 0
    2016-01-07, Buy Sig 0.0 Sell Sig 1.0 Position -2
    2016-01-07, Buy Sig 0.0 Sell Sig 1.0 Position -2
    2016-01-07, Buy Sig 0.0 Sell Sig 1.0 Position -2
    2016-01-07, Buy Sig 0.0 Sell Sig 1.0 Position -2
    
    

    the problems

    • when i enter my first trade, even with size set it still places a size order of 2, trying to reverse the order
      *when looking at the logs you can see we have a position of +2 and when the sell signal comes in it skips a 'line' then places the trade the next line. what is needed to be done to get that to flip from +2 to -2 in the same line?


  • For buy signal your code issues buy order twice - in the first if and in the second if. This is a reason why you have position equal 2. In order to avoid this you need to make it as follows:

           #this is called to place first order, i put one , but it still places 2 trades (see logs
            if self.position.size == 0:
                if self.buy_sig:
                    self.buy(size=1)
                elif self.sell_sig:
                    self.sell(size=1)
    
            #signal to buy
            if self.position.size < 0:
                if self.buy_sig:
                    self.buy()
            #signal to sell
            if self.position.size > 0:
                if self.sell_sig:
                    self.sell()
    


  • that solved both problems, i see the mistake now. silly clearly. thank you for the help =)

    2016-01-06, Buy Sig nan Sell Sig nan Position 0
    2016-01-06, Buy Sig nan Sell Sig nan Position 0
    2016-01-06, Buy Sig nan Sell Sig nan Position 0
    2016-01-06, Buy Sig 1.0 Sell Sig 0.0 Position 0
    2016-01-06, Buy Sig 1.0 Sell Sig 0.0 Position 1
    2016-01-06, Buy Sig 1.0 Sell Sig 0.0 Position 1
    2016-01-06, Buy Sig 1.0 Sell Sig 0.0 Position 1
    2016-01-07, Buy Sig 0.0 Sell Sig 0.0 Position 1
    2016-01-07, Buy Sig 0.0 Sell Sig 0.0 Position 1
    2016-01-07, Buy Sig 0.0 Sell Sig 1.0 Position 1
    2016-01-07, Buy Sig 0.0 Sell Sig 1.0 Position -1
    2016-01-07, Buy Sig 0.0 Sell Sig 1.0 Position -1
    2016-01-07, Buy Sig 0.0 Sell Sig 1.0 Position -1
    2016-01-07, Buy Sig 0.0 Sell Sig 1.0 Position -1
    2016-01-07, Buy Sig 0.0 Sell Sig 1.0 Position -1
    2016-01-07, Buy Sig 0.0 Sell Sig 1.0 Position -1
    

Log in to reply
 

Looks like your connection to Backtrader Community was lost, please wait while we try to reconnect.