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

trouble with feeds for _getsizing



  • Hello guys,
    I try to build a simple algo with percent based sizers.
    In method "def runstrat():", I simply call the sizer: "cerebro.addsizer(RiskSizer, rs=0.5)"

    The instance of the sizers is quite simple:

    class RiskSizer(bt.Sizer):
        params = (('rs', 0.03),)
    
        def _getsizing(self, comminfo, cash, data, isbuy):
            if isbuy == True:
                size = (cash*self.params.rs)/data[0]
            else:
                size = (cash*self.params.rs)/data[0] * -1
    
            return size
    

    It raises this error: KeyError

    For being sure, this - fixed version - works well (as expected):

    class RiskSizer(bt.Sizer):
        params = (('rs', 0.03),)
    
        def _getsizing(self, comminfo, cash, data, isbuy):
            if isbuy == True:
                size = self.params.rs*10
            else:
                size = self.params.rs*10 * -1
    
            return size
    

    In another version, the risk based sizers works good as well.
    As I try to find the cause of this misbehaviour, I've found the issues is related to the (prob.) missing parameters of the method: _getsizing (self, comminfo, cash, data, isbuy). It seems to me that the parameters are not fed.

    Any hints where to search for?
    v

    Note:
    The strategy is quite simple and straightforward:

            if self.position and self.buysell == -1:
                """self.log('SELL CREATE, %.4f' % self.data.close[0])"""
                self.sell()
    
            elif not self.position and self.buysell == 1:
                self.buy()
    


  • Hello everyone,
    I don't full grasp why, however when I try the sizer from library:

    class RiskSizer(bt.Sizer):
        params = (
            ('rs', 0.2),
            ('retint', False),  # return an int size or rather the float value
        )
    
        def __init__(self):
            pass
    
        def _getsizing(self, comminfo, cash, data, isbuy):
            position = self.broker.getposition(data)
            if not position:
                size = math.floor((cash  * self.params.rs) / data.close[0])
            else:
                size = position.size
    
            if self.p.retint:
                size = int(size)
    
            return size
    

    It works fine. :-)



  • @vaclavku try to call data.close[0] instead of data[0] in your implementation, may also work. Just a guess.


  • administrators

    @vaclavku said in trouble with feeds for _getsizing:

    It raises this error: KeyError

    And don't you believe that showing the actual error and not what you think is important ... could actually help?

    @vaclavku said in trouble with feeds for _getsizing:

    As I try to find the cause of this misbehaviour, I've found the issues is related to the (prob.) missing parameters of the method: _getsizing (self, comminfo, cash, data, isbuy). It seems to me that the parameters are not fed.

    And why does it seem that to you? Or is it just a bogus claim?

    @vaclavku said in trouble with feeds for _getsizing:

    however when I try the sizer from library:
    class RiskSizer(bt.Sizer):
    params = (
    ('rs', 0.2),
    ('retint', False), # return an int size or rather the float value
    )

    def __init__(self):
        pass
    
    def _getsizing(self, comminfo, cash, data, isbuy):
        position = self.broker.getposition(data)
        if not position:
            size = math.floor((cash  * self.params.rs) / data.close[0])
        else:
            size = position.size
    
        if self.p.retint:
            size = int(size)
    
        return size
    

    It works fine. :-)

    That's NOT and Sizer from the library. It's something you have modified.



  • Many thanks. I'm enjoying this nice conversation.
    Always very appreciated.
    @backtrader : You're always right. I'm not accurate, however I do my best to improve the ability. Also thanks to you.
    Finally, you're also right: It's a modified piece of the library.


Log in to reply
 

});