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

Strategy runs with no end - is it looping?

  • I seem to have a situation where my strategy runs with no end on Cerebro. Anyone able to help me pinpoint where I got wrong?

    import backtrader as bt
    import backtrader.indicators as btind 
    class Radge_Breakout_One(bt.Strategy):
        params = (('n', 90), ('rate_of_change', 0.3) , ('m', 200), ('trail_stop_percent', 0.2), ('percent_reduction', 0.5)) 
        def __init__(self):
            self.last_n = {'High': {}, 'Low': {}}  # Eg: {'High' : {'BTC/USD':4000.21}, 'Low' : {'BTC/USD':2987.31}}
            self.view = {}  # Eg: {'BTC/USD':'Bull', 'ETH/USD':'Bear', 'BNB/USD':'Neutral'}
            self.sig1 = {}  # Eg: {'BTC/USD':'Long', 'ETH/USD':'Short', 'BNB/USD':'None'}
            self.sig2 = {}  # Eg: {'BTC/USD':'Long', 'ETH/USD':'Short', 'BNB/USD':'None'}
            self.rate_of_change = {}
            for d in self.getdatanames():
                self.last_n['High'][d] = btind.Highest(self.getdatabyname(d).get(ago=-1, size=self.p.n+1), period=self.p.n)
                self.last_n['Low'][d] = btind.Lowest(self.getdatabyname(d).get(ago=-1, size=self.p.n+1), period=self.p.n)
                # Update new bull/bear views
                self.view[d] = btind.PercentChange(period=self.p.m) >= 0
                self.sig1[d] = self.getdatabyname(d).high > self.last_n['High'][d]
                self.sig2[d] = self.getdatabyname(d).low < self.last_n['Low'][d]
                self.rate_of_change[d] = btind.RateOfChange(period=self.p.n)
        def next(self):
            for d in self.getdatanames():
                pos = self.getpositionbyname(d).size or 0
                if pos == 0:
                    if self.view[d] is True:
                        if self.sig1[d] is True:
                            if self.rate_of_change[d] >= self.p.rate_of_change:
                      , exectype=bt.Order.StopTrail, price=self.getdatabyname(d).close[0], trailpercent=self.p.trail_stop_percent)
                    elif self.view[d] is False:
                        if self.sig2[d] is True:
                            if self.rate_of_change[d] <= self.p.rate_of_change:
                                self.sell(data=self.getdatabyname(d), exectype=bt.Order.StopTrail, price=self.getdatabyname(d).close[0], trailpercent=self.p.trail_stop_percent)

  • administrators

    Strategies DO NOT loop. They are passive and called by the system, i.e.: they are event based.

    If something is wrong in your code ... it is in the code you don't show.

    In any case ... all these if something is True tests are WRONG

    @Joshua-Cheong said in Strategy runs with no end - is it looping?:

     if self.view[d] is True:

    You don't know if a True value is being stored. You want to test for something to be true and not for something to be True. Hence

    if self.view[d]:

Log in to reply