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

Bug (with code and screenshot): only 1 trade executes modifying example on homepage



  • (looked to submit this bug on github directly, but didn't see the option there. also didn't see any bug sections of forums, apologies if this isn't posted to the ideal spot...)

    Bug: only 1 trade executes from code snippet on home page with a few reasonable modifications

    Screenshot: https://imgur.com/a/bd2hUqi
    0_1545308018150_backtraderbug.png

    Modifications:

    • params = (('pfast', 10), ('pslow', 30),) change to ==> params = (('pfast', 50), ('pslow', 200),)
    • data = bt.feeds.YahooFinanceData(dataname='MSFT', fromdate=datetime(2011, 1, 1), todate=datetime(2012, 12, 31)) change to ==> data = bt.feeds.YahooFinanceData(dataname='ESNT', fromdate=datetime(2014, 1, 1),
      todate=datetime(2018, 12, 1))
    • add setting: cerebro.addsizer(bt.sizers.AllInSizer)

    Note:

    • switching ticker symbol from 'ESNT' to 'SPY' over the same period makes zero trades despite several signal crossovers.
    • switching ticker symbol to 'SPY' and using date range (2003,1,1) to (2018,12,1) makes a few trades, but also misses a few trades that should execute.

    Full code to reproduce:
    from datetime import datetime
    import backtrader as bt
    class SmaCross(bt.SignalStrategy):
    params = (('pfast', 50), ('pslow', 200),)
    def init(self):
    sma1, sma2 = bt.ind.SMA(period=self.p.pfast), bt.ind.SMA(period=self.p.pslow)
    self.signal_add(bt.SIGNAL_LONG, bt.ind.CrossOver(sma1, sma2))
    cerebro = bt.Cerebro()
    data = bt.feeds.YahooFinanceData(dataname='ESNT', fromdate=datetime(2014, 1, 1),
    todate=datetime(2018, 12, 1))
    cerebro.adddata(data)
    cerebro.addsizer(bt.sizers.AllInSizer)
    cerebro.addstrategy(SmaCross)
    cerebro.run()
    cerebro.plot()



  • Update: the bug appears to be triggered primarily by cerebro.addsizer(bt.sizers.AllInSizer)

    Not sure why or how to fix, but omitting that line at least gets trades firing on all signals as expected.


  • administrators

    Bug

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

  • administrators

    @backtraderuser said in Bug (with code and screenshot): only 1 trade executes modifying example on homepage:

    looked to submit this bug on github directly, but didn't see the option there

    Indeed. It is to avoid people starting discussions about bugs which aren't and other esoteric things.

    @backtraderuser said in Bug (with code and screenshot): only 1 trade executes modifying example on homepage:

    Update: the bug appears to be triggered primarily by cerebro.addsizer(bt.sizers.AllInSizer)

    That's a bug in your code.

    1. AllIn Strategies are doomed to fail in real-life.

    You still want to go for them, because you think you know better.

    1. See:

    And finally, floating point precision can also kill you.



  • "That's a bug in your code.

    AllIn Strategies are doomed to fail in real-life."

    Interesting perspective. There's a difference between a bad strategy and a code implementation that fails to execute the intuitive meaning represented to the user. Not naive enough to attempt an All-in strategy in real life, but interested in modeling one as a benchmark to compare against other (better) strategies.

    Food for thought: If merely including the default implementation of AllInSizer in my code introduces a bug, why offer AllInSizer at all?

    Not interested in debating semantics and appreciate the (otherwise) great software. If this is not a "bug", so be it.


  • administrators

    @backtraderuser said in Bug (with code and screenshot): only 1 trade executes modifying example on homepage:

    Food for thought: If merely including the default implementation of AllInSizer in my code introduces a bug, why offer AllInSizer at all?

    You have written something with a logic and your expectation is that it will do "x", but it is doing "y". It is obviously a bug. You are using the wrong pieces of the puzzle.

    If I allocate memory with malloc and expect it to be initialized with zeroes ... my code is buggy, because I should be using calloc. And my code won't do what I am expecting it to do.

    @backtraderuser said in Bug (with code and screenshot): only 1 trade executes modifying example on homepage:

    code implementation that fails to execute the intuitive meaning represented to the user

    You are expecting one thing from the framework without understanding what the framework does. If you put an AllInSizer and execute it together with a Market order on the closing price of an OHLC bar, your order may never be executed. Simply because the execution price is the next incoming tick which is the next O, which may gap away from the calculated size, leaving you with not enough cash reserves for the order to be executed. Your broker would reject such an order and so does the framework.

    Do the same with a Limit order and the execution (if possible because the price is visited) will happen with the calculated size. The link for order management and execution (form above):

    And because the framework does what it is designed to do (avoid cheating) and is not meeting your (wrong) expectations you immediately call for a bug.

    You may of course be interested in modeling an AllIn strategy and using the close price for it (it seems to be the trend, probably due to cryptocurrency) and that's why you may use cheat-on-open and cheat-on-close.

    The default behavior is to be as realistic as possible, given the inherent constraints of backtesting with OHLC prices. And cheat-on-open and cheat-on-close reduce the expected realism of order execution.


Log in to reply
 

});