-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Examples of Candlestick Pattern Recognition #74
Comments
There are quite a number of candlestick pattern recognition methods available, you can see a list here: >>> import talib
>>> talib.get_function_groups()['Pattern Recognition'] Each one has some documentation available on how to call it, for example:
I have not used these that much, but zero typically represents no-pattern, and positive and negative values represent that the pattern is observed at that point in the data. It would be great to make a few test cases or examples showing a particular pattern and have it be detected by calling the relevant |
Thanks @mrjbq7 for your help with this. Through a bit of fumbling with this example, I was able to figure out it's expecting a numpy ndarray, though I'm not familiar with numpy and obviously have a lot to learn. This is a good example of how the actual usage eludes the beginner and where examples would help a lot. The candlestick pattern recognition functions seem to take the same inputs, so if I can figure it out for this one, I should be apple to apply it to all.
I realize this is a question better suited for Stack Overflow, and I'll bring further questions there. If anyone can help me construct the numpy.ndarray for this trivial little example, perhaps it will help someone in the future. I'll be happy to create further examples for the wiki if I can figure this out! |
Ahh, I see your issue has to do with how to deal with the So,
import talib
import numpy
dates = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri']
open = [20, 31, 50, 77, 68]
high = [28, 38, 55, 70, 66]
low = [38, 39, 56, 71, 22]
close = [45, 50, 62, 60, 15]
# either start with numpy.ndarray, or convert to it like this
# (making sure to specify the ``dtype`` (or "data type") is
# 64-bit floating point which is what ``talib`` expects):
open = numpy.array(open, dtype=float)
high = numpy.array(high, dtype=float)
low = numpy.array(low, dtype=float)
close = numpy.array(close, dtype=float)
talib.CDLTRISTAR(open, high, low, close)
import talib
import numpy
sample_data = [
['Mon', 20, 28, 38, 45],
['Tue', 31, 38, 39, 50],
['Wed', 50, 55, 56, 62],
['Thu', 77, 70, 71, 60],
['Fri', 68, 66, 22, 15],
]
# convert data to columns
sample_data = numpy.column_stack(sample_data)
# extract the columns we need, making sure to make them 64-bit floats
open = sample_data[1].astype(float)
high = sample_data[2].astype(float)
low = sample_data[3].astype(float)
close = sample_data[4].astype(float)
talib.CDLTRISTAR(open, high, low, close)
import talib
import numpy
import pandas
sample_data = [
['Mon', 20, 28, 38, 45],
['Tue', 31, 38, 39, 50],
['Wed', 50, 55, 56, 62],
['Thu', 77, 70, 71, 60],
['Fri', 68, 66, 22, 15],
]
sample_data = pandas.DataFrame(sample_data,
columns=["Day","Open","High","Low","Close"])
open = sample_data['Open']
high = sample_data['High']
low = sample_data['Low']
close = sample_data['Close']
talib.CDLTRISTAR(open, high, low, close) As an aside, it is fairly common to see people abbreviate when working with numpy and pandas, for example: import numpy as np
np.array([1,2,3,4])
import pandas as pd
pd.DataFrame([[1,2],[3,4]]) I've even see people do the same with |
@mrjbq7 First, thank you so much for taking the time to explain this so clearly. Your help is appreciated greatly. I made a contrived example to test. I ran the number in Excel and produced a graph that looks like a Tristar pattern: http://cl.ly/image/3w1D0u1Y3O1O Here's the code:
The output is:
It looks like my sample data produced a result, though I'm not clear on what +100 vs -100 is? |
@greencoder 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. This is some TA-Lib source code from ta_CDLTRISTAR.c :
|
Thanks, @pcawthron! In general, are negative values bearish and positive values bullish for the candlestick recognition functions? |
@greencoder As far as I know, yes, but it's best to check the source code. In my limited experience I've only ever seen -100, 0 and +100 so I'm not sure why the source talks of -1 to -100 and +1 to +100. |
@mrjbq7 I'm trying to create a few samples we can use for showing people how to use the pattern recognition functions as you and @pcawthron taught me above. Given this program:
It produces this output:
This is the first time I've ever worked with Numpy and I'd like to make sure that how I'm using the result is correct and elegant. To identify the day in the result, I'm doing this:
This works, but I'm not sure if there is a more elegant way to do this. (there probably is) Specifically:
Once I get this worked out, I'd like to put an example using Yahoo data in the project wiki. |
Regarding your question, non-zero values can be found several ways. In [1]: import numpy as np
In [2]: a = np.array([0,0,100,-100,0,0])
In [3]: np.nonzero(a)
Out[3]: (array([2, 3]),)
In [4]: np.where(a != 0)
Out[4]: (array([2, 3]),) |
mrjbq7, thank you for the ta-lib contribution. For the CDLHIKKAKE function, I am getting some 200 & -200 output on my price series. test = ta.CDLHIKKAKE(open,high,low,close) for x in test.nonzero(): [ 100 200 -100 -200 -100 -100 -200 -100 100 200 100 -100 100 -100 -200... according to the help, it says 200/-200 is not an valid output. Docstring: Hikkake Pattern (Pattern Recognition) Inputs: Should I assume that -200/200 to be 100's? Thank you |
200/-200 is a little strange. I'd have to look at the underling |
How do you access the documentation for each function? |
What documentation are you looking for? You can look at general documentation for various indicators: http://tadoc.org/. You could also look at the underlying C source code of ta-lib: https://sourceforge.net/p/ta-lib/code/HEAD/tree/trunk/ta-lib/c/src/ta_func/ etc. |
I was looking for the python function documentation. I found it here: https://mrjbq7.github.io/ta-lib/funcs.html Thanks |
You can also use |
@mrjbq7 HI, I want to validate the results.
It is returning array([0], dtype=int32), whereas data feeded is for pattern MARUBOZU. |
@mrjbq7 the issue with the talib-Pattern recognition is determining more than one candlestick type for a single type of ohlc value. Is their any way to solve this issue?? Can u please help |
I don't understand the problem you are having @KabeerSinghBedi. |
@neerajgupta2407 The pattern might not trigger because all the candle open=high=low=close and maybe it needs candle bodies or something to trigger. You can look at the code for the C library and see what logic it uses. |
@mrjbq7 as u can see in the image for a particular set of Ohlc it should determine it as single candle stick type but it is determining more than one type for in my case it's highwave and spinning top. |
Why can't multiple candlestick patterns trigger on the same tick of the market? |
But in that case each candle stick has a different meaning so how can at a
particular tick we can have different meanings.
…On Jun 30, 2017 11:31 AM, "John Benediktsson" ***@***.***> wrote:
Why can't multiple candlestick patterns trigger on the same tick of the
market?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#74 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AZFxqan6Jpi-k5iCuyB--JbB-UbNVEbSks5sJI9IgaJpZM4DVhUe>
.
|
But in that case each candle stick has a different meaning so how can at a particular tick we can have different meanings. |
If i'm making changes in the ta_global.c file how to see that this changes are because if i'm changing the candle settings i'm not able to see the changes in pattern recognition method. |
I don't see why more than one candlestick pattern might be recognized on the addition of a new candle. Are the two you observed inconsistent? What changes are you making to ta_global.c? That's not something I can help you troubleshoot without access to what you are doing and not likely to have time to do so. |
Hi.. for example convert data to columnssample_data = numpy.column_stack(sample_data) extract the columns we need, making sure to make them 64-bit floatsopen = sample_data[1].astype(float) talib.CDLTRISTAR(open, high, low, close) OR Please advice.. |
Typical time series arrays are oldest to newest. |
Here's an example that uses CDLHAMMER:
|
@mrjbq7 Any news about the 200/-200 issue? Following the documentation this should not happen. (including the inline documentation of the C code) |
What is the issue? And what documentation are you talking about? This is in https://svn.code.sf.net/p/ta-lib/code/trunk/ta-lib/c/src/ta_func/ta_CDLHIKKAKE.c /* Generated */ if( i <= patternIdx+3 &&
/* Generated */ ( ( patternResult > 0 && inClose[i] > inHigh[patternIdx-1] ) // close higher than the high of 2nd
/* Generated */ ||
/* Generated */ ( patternResult < 0 && inClose[i] < inLow[patternIdx-1] ) // close lower than the low of 2nd
/* Generated */ )
/* Generated */ ) {
/* Generated */ outInteger[outIdx++] = patternResult + 100 * ( patternResult > 0 ? 1 : -1 );
/* Generated */ patternIdx = 0;
/* Generated */ } else
/* Generated */ outInteger[outIdx++] = 0; That implies that the I'm not sure there is anything to do here... |
Well, I'm not familiar with the talib C code so far and honestly, it is hard to read. But if you say so... Btw, I'm referring to this piece of inline documentation:
But it looks like this applies only to the block below it. This 200 magic happens at the bottom, in an undocumented, generated block. Goodbye readability! PS: I guess the key here is to do the math once manually to understand it. Then the C code becomes more readable. Anyhow, I was just playing around a bit. Thanks for your help! |
I think the 200 comes from this "confirmation" line in the documentation:
Assuming the bullish hikkake result is in the range [1,100] then that confirmation bar is in the range [101,200]. |
Also, I don't mind at all the questions. I learn something usually every time someone tries to figure out how the TA-Lib C library works! |
Thanks @pcawthron for your code. I always learn best with example code. |
Do you know of any examples of using this library to do Candlestick Pattern Recognition? I'm struggling a bit trying to get started - I've got plenty of OHLCV data but I'm not sure how to use it here.
I'd be happy to help contribute working examples to this project's wiki if someone could help me get going.
The text was updated successfully, but these errors were encountered: