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

candlesticks screenings stocks analyzer



  • Hi,
    I am trying to create an analyzer class based on the formations of candlesticks contained in the TA-lib library which can be used together with the Backtrader. This class determines whether some stocks contains recently a given candlesticks formation and consequently it lets me create a set of companies which recently include such a formation and a set of companies which don't. However, I encountered a wall I'm trying to overcome, I would be grateful for the tip on how to overcome this , namely :

    TA-lib candlesticks pattern recognition gives the result in the form : -100, 0 or 100 .
    Inputs are prices : open , high , low close .

    class Screener_CandleSticks(bt.Analyzer):
    
        def start(self):
            self.candles = {data: bt.talib.CDLHARAMICROSS(data.open, data.high, data.low, data.close)
                            for data in self.datas}
    
        def stop(self):
            self.rets['within'] = list()
            self.rets['outsite'] = list()
    
            for  data, candle in self.candles.items():
                note = data._name, candle[0]
                if candle==100
                    self.rets['within'].append(note)
                else:
    
                    self.rets['outsite'].append(note)
    

    question: am I on the right track giving the comparative condition:

    if candle==100
    

    or maybe other values should be called here?

    a small tip would be priceless.



  • from https://github.com/mrjbq7/ta-lib/issues/74:

    The -100 denotes a bearish Tristar pattern where the middle candle body is above the other two. Conversely +100 denotes a bullish Tristar pattern where the middle body is below the adjacent ones.

    So this may apply to these, too



  • -100 = bearish pattern
    +100 = bullish pattern



  • @dasch
    Thanks for your attention.
    this entry which I used here throws me an error:
    ```

    if candle==100
                     ^
    SyntaxError: invalid syntax```
    
    

    it looks like the syntax should look different when using the analyzer class rather than the
    strategy class



  • You would need to check candles for the signal, since this will be something like a numpy Array or some kind of list. Maybe look into the data in candles to see what to do with candle ...



  • @dasch Hi
    That is, the dates cannot be a type object -->'backtrader.feeds.csvgeneric.GenericCSVData'. Data rows (open , high,low,close) must be classes 'pandas.core.series.Series' to use class object like 'bt.talib.CDLHARAMICROSS' in the analyzer of the backtrader . do i reason well ?



  • Oh i see, I did not run your code. but by reading:

    https://www.backtrader.com/blog/posts/2016-07-26-talib-integration/talib-integration/

    your approach should work.

    but what exactly do you want to?

    if you just want to look if there is such a formation in your data (without looking for the date) you can do the following in stop:

        def stop(self):
            self.rets['bullish'] = list()
            self.rets['bearish'] = list()
            self.rets['outsite'] = list()
            for data, line in self.candles.items():
                note = data._name
                bullish, bearish = False, False
                for i in range(0, len(line)):
                    if line[-i] == 100:
                        bullish = True
                    elif line[-i] == -100:
                        bearish = True
                if bullish:
                    self.rets['bullish'].append(note)
                if bearish:
                    self.rets['bearish'].append(note)
                if not bearish and not bullish:
                    self.rets['outside'].append(note)
    
    

    The code above will look through the line, going from newest data to oldest and check if there was a formation, if there was, it will be stored in a bool var (bullish, bearish).
    Then it will store the result in the rets list



  • @dasch
    Thank You so much for help . I wanted to create a stocks screening similar to the example on the backtrader's blog but with candlesticks formations in the main role. Thank You again for your attention.



  • glad it helped.

    pozdrawiam



  • @Maciek-Paciarski the example in the blog uses the last price.

    https://www.backtrader.com/blog/posts/2016-08-15-stock-screening/stock-screening/

    since you are checking the whole line, not only the last entry, you would need to iterate through all candles. with var i you can also get the date from data and other values. you would access it the same as the line you generated in init:

    data: bt.talib.CDLHARAMICROSS(data.open, data.high, data.low, data.close)
    

    so the line with bt.talib.CDLHARAMICROSS (think of a list) contains all occurrences of the pattern for every candle in data.

    with data[-i] you would get the candle on which the pattern recognition was triggered

            for data, line in self.candles.items():
                note = data._name
                for i in range(0, len(line)):
                    if line[-i] == 100:
                        bullish = True
                        print("BULLISH", data.time[-i])
                    elif line[-i] == -100:
                        bearish = True
                        print("BEARISH", data.time[-i])
    

    with

                for i in range(0, len(line)):
    

    you go through the line starting with the newest entry to the oldest entry.


Log in to reply
 

});