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/

    Some trades are executing while most are not

    General Code/Help
    3
    4
    51
    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.
    • Dennis Musingila
      Dennis Musingila last edited by

      I have tried increasing cash and leverage but still I can't get all the trades to get executed. Here is a sample of my entry logic. It's is a simple rsi overbought and oversold strategy , number of trades are supposed to be at least 30 but i get only 8 executed trades,

      class MyStrategy(bt.Strategy):
      
        params = (('size', 1000),
                  ('exitbars', 5))
        def log(self, txt, dt = None):
          dt = dt or self.datas[0].datetime.datetime(0)
          print('%s, %s' % (dt.isoformat(), txt))
      
        def __init__(self):
          self.dataclose = self.data.close
          self.trade_tracker = []
          # Rsi
          self.rsi = btind.RSI(self.datas[0], period = 10)
          self.rsi_of_rsi = btind.RSI(self.rsi, period = 3)
          self.bar_executed = 0
          
          # william %r
          self.willr = btind.WilliamsR(self.datas[0],period = 30 )
          self.ma_willr = btind.SimpleMovingAverage(self.willr, period = 8)
          self.ma_willr.plotinfo.plotmaster = self.willr
          self.willr_sigs = btind.CrossOver(self.ma_willr, self.willr)
      
          self.ema_of_rsi = btind.SimpleMovingAverage(self.rsi, period = 10)
          self.ema = btind.SimpleMovingAverage(self.datas[0], period = 20)
          self.rsi_of_rsi.plotinfo.plotmaster = self.rsi
          self.buy_sigs = btind.CrossOver(self.rsi, 20)
          self.sell_sigs = btind.CrossDown(self.rsi, 70)
        def next(self):
          self.dt = self.data.datetime.datetime()
          if not self.position:
            if self.buy_sigs[0] :
              self.cash_before = self.broker.getcash()
              value = self.broker.get_value()
              entry = self.buy(size = self.params.size)
              entry.addinfo(name = 'buy')
              self.log('Buy Create %.4f,' % self.dataclose[0])
      
            elif self.sell_sigs[0]:
              value = self.broker.get_value()
              entry = self.sell(size = self.params.size)
              entry.addinfo(name = 'sell')
              self.log('sell Create %.4f' % self.dataclose[0])
            else:
              pass
      
      
      1 Reply Last reply Reply Quote 0
      • A
        ab_trader last edited by

        Please provide reasons why do you expect more trades than it actually happens. Detailed log of prices, signals and trades, plot of the results will also be helpful.

        1 Reply Last reply Reply Quote 2
        • Dennis Musingila
          Dennis Musingila last edited by

          Here is a sample output , i just realised that it's following my dates in a linear fashion you can see that sometimes it skips months only to come back to them later how can i fix that

          Starting Portofolio value: 10000.000000
          2019-11-21T17:00:00, Buy Create 1.2931,
          2019-11-21 17:00:00: Buy Order Completed
          2019-11-21 17:00:00: Order Executed Price: 1.2935463370380194, Executed size 1000
          2019-11-21 17:00:00: Position Value: 1293.5463370380194 Margin Used: 1.2935463370380194
          2019-11-21 21:00:00: Trade closed
          2019-11-21 21:00:00: PnL Gross -1.65, net -1.65 
          
          
          2019-11-25T09:00:00, Buy Create 1.2866,
          2019-11-25 09:00:00: Buy Order Completed
          2019-11-25 09:00:00: Order Executed Price: 1.2857748426142948, Executed size 1000
          2019-11-25 09:00:00: Position Value: 1285.7748426142948 Margin Used: 1.2857748426142948
          2019-11-25 13:00:00: Trade closed
          2019-11-25 13:00:00: PnL Gross 0.98, net 0.98 
          
          
          2019-11-26T09:00:00, Buy Create 1.2880,
          2019-11-26 09:00:00: Buy Order Completed
          2019-11-26 09:00:00: Order Executed Price: 1.2887484741033484, Executed size 1000
          2019-11-26 09:00:00: Position Value: 1288.7484741033484 Margin Used: 1.2887484741033484
          2019-11-26 13:00:00: Trade closed
          2019-11-26 13:00:00: PnL Gross -1.34, net -1.34 
          
          
          2019-11-27T13:00:00, Buy Create 1.2874,
          2019-11-27 13:00:00: Buy Order Completed
          2019-11-27 13:00:00: Order Executed Price: 1.2863356521414324, Executed size 1000
          2019-11-27 13:00:00: Position Value: 1286.3356521414323 Margin Used: 1.2863356521414324
          2019-11-27 17:00:00: Trade closed
          2019-11-27 17:00:00: PnL Gross 0.86, net 0.86 
          
          
          2019-11-28T13:00:00, Buy Create 1.2922,
          2019-11-28 13:00:00: Buy Order Completed
          2019-11-28 13:00:00: Order Executed Price: 1.29254290081471, Executed size 1000
          2019-11-28 13:00:00: Position Value: 1292.54290081471 Margin Used: 1.29254290081471
          2019-11-28 17:00:00: Trade closed
          2019-11-28 17:00:00: PnL Gross -0.87, net -0.87 
          
          
          2019-11-29T17:00:00, Buy Create 1.2910,
          2019-11-29 17:00:00: Buy Order Completed
          2019-11-29 17:00:00: Order Executed Price: 1.290508831256365, Executed size 1000
          2019-11-29 17:00:00: Position Value: 1290.508831256365 Margin Used: 1.290508831256365
          2019-02-12 01:00:00: Trade closed
          2019-02-12 01:00:00: PnL Gross 0.83, net 0.83 
          
          
          2019-02-12T13:00:00, Buy Create 1.2918,
          2019-02-12 13:00:00: Buy Order Completed
          2019-02-12 13:00:00: Order Executed Price: 1.2916064478517615, Executed size 1000
          2019-02-12 13:00:00: Position Value: 1291.6064478517615 Margin Used: 1.2916064478517615
          2019-02-12 17:00:00: Trade closed
          2019-02-12 17:00:00: PnL Gross 0.78, net 0.78 
          
          
          2019-04-12T17:00:00, sell Create 1.3098
          2019-04-12 17:00:00: sell Order Completed 
          2019-04-12 17:00:00: Order Executed Price: 1.306969967828107, Executed Size -1000
          2019-04-12 17:00:00: Position Value: -1306.969967828107 Margin Used: 1.306969967828107
          95
          2019-10-12 05:00:00: Trade closed
          2019-10-12 05:00:00: PnL Gross -7.85, net -7.85 
          
          
          2019-10-12T13:00:00, Buy Create 1.3169,
          2019-10-12 13:00:00: Buy Order Completed
          2019-10-12 13:00:00: Order Executed Price: 1.3160242778977713, Executed size 1000
          2019-10-12 13:00:00: Position Value: 1316.0242778977713 Margin Used: 1.3160242778977713
          2019-10-12 17:00:00: Trade closed
          2019-10-12 17:00:00: PnL Gross 0.63, net 0.63 
          
          
          2019-11-12T01:00:00, Buy Create 1.3138,
          2019-11-12 01:00:00: Buy Order Completed
          2019-11-12 01:00:00: Order Executed Price: 1.3154527222372214, Executed size 1000
          2019-11-12 01:00:00: Position Value: 1315.4527222372215 Margin Used: 1.3154527222372214
          2019-11-12 05:00:00: Trade closed
          2019-11-12 05:00:00: PnL Gross -0.91, net -0.91 
          
          
          2019-11-12T21:00:00, Buy Create 1.3187,
          2019-11-12 21:00:00: Buy Order Completed
          2019-11-12 21:00:00: Order Executed Price: 1.317365944444913, Executed size 1000
          2019-11-12 21:00:00: Position Value: 1317.365944444913 Margin Used: 1.317365944444913
          2019-11-12 21:00:00: Trade closed
          2019-11-12 21:00:00: PnL Gross 0.0, net 0.0 
          
          
          2019-12-12T09:00:00, Buy Create 1.3200,
          2019-12-12 09:00:00: Buy Order Completed
          2019-12-12 09:00:00: Order Executed Price: 1.320215118055614, Executed size 1000
          2019-12-12 09:00:00: Position Value: 1320.215118055614 Margin Used: 1.320215118055614
          2019-12-12 13:00:00: Trade closed
          2019-12-12 13:00:00: PnL Gross -1.95, net -1.95 
          
          
          2019-12-13T01:00:00, Buy Create 1.3348,
          2019-12-13 01:00:00: Buy Order Completed
          2019-12-13 01:00:00: Order Executed Price: 1.3245032886284758, Executed size 1000
          2019-12-13 01:00:00: Position Value: 1324.5032886284757 Margin Used: 1.3245032886284758
          2019-12-13 05:00:00: Trade closed
          2019-12-13 05:00:00: PnL Gross 11.74, net 11.74 
          
          
          2019-12-13T09:00:00, Buy Create 1.3431,
          2019-12-13 09:00:00: Buy Order Completed
          2019-12-13 09:00:00: Order Executed Price: 1.3396876971571188, Executed size 1000
          2019-12-13 09:00:00: Position Value: 1339.6876971571187 Margin Used: 1.3396876971571188
          2019-12-13 13:00:00: Trade closed
          2019-12-13 13:00:00: PnL Gross -1.39, net -1.39 
          
          
          2019-12-16T05:00:00, sell Create 1.3377
          2019-12-16 05:00:00: sell Order Completed 
          2019-12-16 05:00:00: Order Executed Price: 1.33614203741116, Executed Size -1000
          2019-12-16 05:00:00: Position Value: -1336.1420374111601 Margin Used: 1.33614203741116
          140
          2019-12-19 17:00:00: Trade closed
          2019-12-19 17:00:00: PnL Gross 29.96, net 29.96 
          
          
          2019-12-20T09:00:00, Buy Create 1.3029,
          2019-12-20 09:00:00: Buy Order Completed
          2019-12-20 09:00:00: Order Executed Price: 1.3026737871945513, Executed size 1000
          2019-12-20 09:00:00: Position Value: 1302.6737871945513 Margin Used: 1.3026737871945513
          2019-12-20 13:00:00: Trade closed
          2019-12-20 13:00:00: PnL Gross 0.78, net 0.78 
          
          run-out 1 Reply Last reply Reply Quote 0
          • run-out
            run-out @Dennis Musingila last edited by

            @Dennis-Musingila said in Some trades are executing while most are not:

            ou can see that sometimes it skips months only to come back to them later how can i fix that

            This would suggest that there's a problem with your data. Have you sorted the data by date correctly?

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