isbuy in SIzer is always True. Why?



  • 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!


  • administrators

    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



  • Wow. That was quick! Thank you! ^^


  • administrators

    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


Log in to reply
 

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