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/

    isbuy in SIzer is always True. Why?

    General Code/Help
    backtrader sizer
    2
    4
    1081
    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.
    • Roman Semko
      Roman Semko last edited by

      Hi guys, I am struggling with a simple example. Maybe someone else had a similar issue. I am creating a simple sizer. My problem is that it always passes True for isbuy, even for sell. Here a dead-simple example to showcase the problem:

      import os
      import datetime
      from core.yahoo import getf  # My own function, returns path to a file of ticker data.
      import backtrader as bt
      from random import randint
      
      class Sizer(bt.Sizer):
          def _getsizing(self, comminfo, cash, data, isbuy):
              print '     ISBUY?', isbuy
              return 1
          
      class Strategy(bt.Strategy):
          def next(self):
              if randint(0, 1):
                  print 'BUY'
                  self.buy()
              else:
                  print 'SELL'
                  self.sell()
      
      # getf is a function that returns a path to the file, where yahoo data has been downloaded into.
      datapath = os.path.join(os.getcwd(), getf('VGT'))  
      data = bt.feeds.YahooFinanceCSVData(
          dataname=datapath,
          fromdate=datetime.datetime(2007, 1, 1),
          todate=datetime.datetime(2012, 12, 31),
          reverse=True)
      
      cerebro = bt.Cerebro()
      cerebro.addstrategy(Strategy)
      cerebro.addsizer(Sizer)
      cerebro.adddata(data)
      cerebro.broker.setcash(5000.0)
      cerebro.run()
      

      The result? isbuy is always true!

      BUY
           ISBUY? True
      BUY
           ISBUY? True
      SELL
           ISBUY? True
      BUY
           ISBUY? True
      SELL
           ISBUY? True
      SELL
           ISBUY? True
      SELL
           ISBUY? True
      BUY
           ISBUY? True
      

      Am I doing something wrong?

      Thanks for any help!

      1 Reply Last reply Reply Quote 0
      • B
        backtrader administrators last edited by

        Nothing. This is a bug introduced recently when refactoring other parts to allow mixed used of buy / sell and the order_target_xxx family.

        Fixed now with this commit: https://github.com/mementum/backtrader/commit/b119eaa073d09817e13809e2a439034debc75821

        1 Reply Last reply Reply Quote 0
        • Roman Semko
          Roman Semko last edited by

          Wow. That was quick! Thank you! ^^

          1 Reply Last reply Reply Quote 0
          • B
            backtrader administrators last edited by

            Together with the additions to filling in plotting and the new oanda orders, enough to make a quick release:

            Community - Release 1.9.43.116

            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(); }); }