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

Setting cash order size



  • Hi guys,

    I want to set the order size of every order to be 100 USD
    How can I do that?

    For example, I have the code as follow, which buy at open price when openinterest of last candle is 1 and sell at open price when openinterest of last candle is 2

    import backtrader as bt
    from datetime import datetime
     
    
    class testStrat(bt.Strategy):
     
     
        def __init__(self):
          self.openinterest = self.datas[0].openinterest
    
        def next(self):
          if not self.position:
              if self.openinterest[0] == 2:
                  self.sell()
              if self.openinterest[0] == 1:
                  self.buy()
          else:
              if self.position.size > 0:
                if self.openinterest[0] == 2:
                  self.close()
                  self.sell()
                elif self.openinterest[0] == 1:
                  self.buy()
              else:
                if self.openinterest[0] == 1:
                  self.close()
                  self.buy()
                elif self.openinterest[0] == 2:
                  self.sell()
    
    # Create an instance of cerebro
    cerebro = bt.Cerebro()
     
    # Add our strategy
    cerebro.addstrategy(testStrat)
    
    # Add the Data Feed to Cerebro
    data = bt.feeds.PandasData(dataname=newdf)
    cerebro.adddata(data)
    
    #Variable for our starting cash
    startcash = 1000
    # Set our desired cash start
    cerebro.broker.setcash(startcash)
    
    # 0.1% ... divide by 100 to remove the %
    cerebro.broker.setcommission(commission=0.001)
    
    # Add a sizer
    cerebro.addsizer(bt.sizers.PercentSizer,percents = 1)
    
    # use the fractional scheme if requested
    cerebro.broker.addcommissioninfo(CommInfoFractional())
    
    
    # Run over everything
    cerebro.run()
     
    #Get final portfolio Value
    portvalue = cerebro.broker.getvalue()
    pnl = portvalue - startcash
     
    #Print out the final result
    print('Final Portfolio Value: ${}'.format(round(portvalue,2)))
    print('P/L: ${}'.format(round(pnl,2)))
    


  • Docs - Target Orders, order_target_value



  • @ab_trader said in Setting cash order size:

    Docs - Target Orders, order_target_value

    Hi Sir,
    Thank you very much for your reply
    I have read the docs and I really didn't understand clearly
    So in my case I will need to use order_target_value(target = 100)?
    How I can apply to this?

        def next(self):
          if not self.position:
              if self.openinterest[0] == 2:
                  self.sell()
              if self.openinterest[0] == 1:
                  self.buy()
          else:
              if self.position.size > 0:
                if self.openinterest[0] == 2:
                  self.close()
                  self.sell()
                elif self.openinterest[0] == 1:
                  self.buy()
              else:
                if self.openinterest[0] == 1:
                  self.close()
                  self.buy()
                elif self.openinterest[0] == 2:
                  self.sell()
    


  • Try this (not tested)

        def next(self):
            if self.openinterest[0] == 2:
                self.order_target_value(target = -100)
            if self.openinterest[0] == 1:
                self.order_target_value(target = 100)
    

    But with all my respect, you may want to apply more efforts to understand docs and programming by yourself, if you want to get to success faster. This was probably easiest task from what you can expect on algotrading road.


Log in to reply
 

});