TA-Lib / ta-lib-python

Python wrapper for TA-Lib (http://ta-lib.org/).
http://ta-lib.github.io/ta-lib-python
Other
9.7k stars 1.76k forks source link

Test case for CDLENGULFING returning 1 #464

Open prRZ5F4LXZ opened 3 years ago

prRZ5F4LXZ commented 3 years ago

I am trying to test CDLENGULFING. Can anybody provide a data set so that CDLENGULFING can return 1?

I have the following code, but I only see CDLENGULFING returning 0.

import talib

import requests
import pandas as pd

link = 'https://min-api.cryptocompare.com/data/histoday?fsym=BTC&tsym=USD&limit=2000&aggregate=1'

historical_get = requests.get(link)
historical_json = historical_get.json()
historical_dict = historical_json['Data']

df = pd.DataFrame(historical_dict,
                             columns=['close', 'high', 'low', 'open', 'time', 'volumefrom', 'volumeto'],
                             dtype='float64')

posix_time = pd.to_datetime(df['time'], unit='s')

# append posix_time
df.insert(0, 'Date', posix_time)

# drop unix time stamp
df.drop('time', axis = 1, inplace = True)

op = df['open']
hi = df['high']
lo = df['low']
cl = df['close']
df['CDLENGULFING'] = talib.CDLENGULFING(op, hi, lo, cl)

import sys
df.to_csv(sys.stdout, sep='\t', index=False)
trufanov-nok commented 3 years ago

As I can see from the C code the function is capable to return only values of -100, -80, 0, 80 or 100. While the commentary says:

    * Must have:
    * - first: black (white) real body
    * - second: white (black) real body that engulfs the prior real body
    * outInteger is positive (1 to 100) when bullish or negative (-1 to -100) when bearish:
    * - 100 is returned when the second candle's real body begins before and ends after the first candle's real body
    * - 80 is returned when the two real bodies match on one end (Greg Morris contemplate this case in his book
    *   "Candlestick charting explained")
    * The user should consider that an engulfing must appear in a downtrend if bullish or in an uptrend if bearish,
    * while this function does not consider it
pcawthron commented 3 years ago

The pattern tests are very simple in the source I have installed:

if( ( TA_CANDLECOLOR(i) == 1 && TA_CANDLECOLOR(i-1) == -1 &&            // white engulfs black
              inClose[i] > inOpen[i-1] && inOpen[i] < inClose[i-1]
            )
            ||
            ( TA_CANDLECOLOR(i) == -1 && TA_CANDLECOLOR(i-1) == 1 &&            // black engulfs white
              inOpen[i] > inClose[i-1] && inClose[i] < inOpen[i-1]
            )
          )
            outInteger[outIdx++] = TA_CANDLECOLOR(i) * 100;
        else
            outInteger[outIdx++] = 0;

The indicator can be seen working here:

from plotly.offline import plot
import plotly.graph_objs as go
import pandas as pd
import talib as ta
import re 
import numpy as np

o = np.array([175.47, 171.61, 175.69, 181.74, 170.92, 187.07, 174.15, 175.47, 171.61, 175.69, 181.74, 170.92])
h = np.array([ 184.68, 184.39, 179.22, 183.49, 182.87,190.78, 196.48, 184.68, 184.39, 179.22, 183.49, 182.87])
l = np.array([169.16, 169.78, 166.80, 171.63, 170.10, 176.94, 170.13, 169.16, 169.78, 166.80, 171.63, 170.10])
c = np.array([169.30, 182.14, 176.60, 177.66, 170.70, 177.32, 192.34, 169.30, 182.14, 176.60, 177.66, 170.70])

print('CDLENGULFING ', ta.CDLENGULFING(o, h, l, c))

trace = go.Candlestick( #x= pd.to_datetime(dfohlc.index.values),
            open=o,
            high=h,
            low=l,
            close=c)
data = [trace]

plot(data, filename='go_candle1.html')

CDLENGULFING [ 0 0 0 0 0 0 100 0 0 0 0 0]

runxc1 commented 2 years ago

@trufanov-nok Where can you find the C documentation? all of these functions return an integer and I can't seem to find documentation on what the integers indicate

mrjbq7 commented 2 years ago

there is a mirror of the ta-lib C code here, you can see all the functions:

https://github.com/TA-Lib/ta-lib/blob/master/src/ta_func/ta_CDLENGULFING.c#L212

trufanov-nok commented 2 years ago

@runxc1 if you mean functions return value then it's a value defined in this enum: https://github.com/TA-Lib/ta-lib/blob/master/include/ta_defs.h#L234 0 -TA_SUCCESS, 2 - TA_BAD_PARAM, etc. If you mean values returned in output array - they are indicator dependent.
Afaik, there is no much C documentation, so you better rely on commentaries in the code.