implement Custom indicator



  • I am new to Backtrader but learning fast. I have a small custom indicator that i need help with . When i call the indicator directly by calculating it in my strategy i get the value but when i use the indicator method and call it without calculation in my strategy it throws an error

     class ForexIndex(bt.Indicator):
         lines = ('myindex',)
    
         params = (('period', 10),)
    
        def __init__(self):
                self.lines.myindex =  ( (14.197569 * pow(self.data0.close, -0.668) * pow(self.data1.close, -0.202) * pow(self.data2.close, 0.130))
    
     class MyStrategy(bt.Strategy):
           
            # i did forget to post this earlier when i posted this snippet
            def __init__(self):
    
           #myindex --method 1-- this does not work
           self.myindex =  ForexIndex()
    
           #myindex --Method 2--crude-- i get a value but when i try to use it or retrieve  and use it in other calculation i can't get it 
           self.myindex =  ( (14.197569 * pow(self.data0.close, -0.668) * pow(self.data1.close, -0.202) * pow(self.data2.close, 0.130))
           print("myindex_value", myindex)
    
     class IBConnectorLive(IBConnector):
              def __init__(self, ticker='', host='127.0.0.1', port=7967, clientId=950, sequence_length=30, debug=True):
    
             self.ibstore = bt.stores.IBStore(host=host,
                                         port=port,
                                         clientId=clientId,
                                         reconnect=3,
                                         timeout=20
                                         )
       
        #create our data ticker list
        ticker = ['EUR.USD-CASH-IDEALPRO',  'GBP.USD-CASH-IDEALPRO', 'USD.CAD-CASH-IDEALPRO'] 
    
        cerebro = bt.Cerebro(stdstats=False)               
        
    
        # EUR.USD-CASH-IDEALPRO realtime IB dataset
        self.data0 = self.ibstore.getdata(dataname=ticker[0], timeframe=bt.TimeFrame.Seconds, compression=15, backfill=False )
        cerebro.adddata(self.data0)
       
    
        # GBP.USD-CASH-IDEALPRO realtime IB dataset
        self.data1 = self.ibstore.getdata(dataname=ticker[1], timeframe=bt.TimeFrame.Seconds, compression=15, backfill=False,)
        cerebro.adddata(self.data1)
    
        # USD.JPY-CASH-IDEALPRO realtime IB dataset
        self.data2 = self.ibstore.getdata(dataname=ticker[2], timeframe=bt.TimeFrame.Seconds, compression=15, backfill=False)
                                         )
        cerebro.adddata(self.data2)
    

    i am getting an error when i call the indicator :

    ERROR:
    AttributeError: 'Lines_LineSeries_LineIterator_DataAccessor_Indicat' object has no attribute 'data1'

    Any help is well appreciated


  • administrators



  • i have gone through the same documents but can't figure out why its seeing data0 but not data1, data2 . I have defined data0, data1, data2 but it only gets data0


  • administrators

    @developeralgo said in implement Custom indicator:

    self.myindex =  ForexIndex()
    

    You are not explicitly supplying data feeds to the indicator. From the documentation and the aforementioned post:

    This is intentional. If no data is passed, the 1st data of the parent (in this case the Strategy in which is being created) will be automatically passed in the background

    Only 1 data is passed to your indicator.

    You can pass 2 data feeds explicitly or use _mindatas as explained in the post from the other community topic. For reference: Blog - Crossing over numbers

    In your case it would seem you need _mindatas = 3.

    Explicit would probably be better than implicit, which is better suited for single data indicators.



  • Sorry but tried _mindata = 3 and also tried to define data0 , data1 & data2 explicitly but still failed, i am kinda stuck. its frustrating , i know it should be a simple thing .


  • administrators

    @developeralgo said in implement Custom indicator:

     class MyStrategy(bt.Strategy):
    
           #myindex --method 1-- this does not work
           self.myindex =  ForexIndex()
    
           #myindex --Method 2--crude-- i get a value but when i try to use it or retrieve  and use it in other calculation i can't get it 
           self.myindex =  ( (14.197569 * pow(self.data0.close, -0.668) * pow(self.data1.close, -0.202) * pow(self.data2.close, 0.130))
           print("myindex_value", myindex)
    

    On a second quick look.

    Your class has no __init__ and in case it had one you overwrite self.mindex with a calculation.

    It would be good if you showed how you have used _mindatas (with an s at the end) and the definition of the data feeds. A one line complaining about not having been possible cannot be looked into.


Log in to reply
 

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