Skip to content
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

Differences between talib and hand writing code #196

Open
TerrenceVarada opened this issue Apr 11, 2018 · 9 comments
Open

Differences between talib and hand writing code #196

TerrenceVarada opened this issue Apr 11, 2018 · 9 comments

Comments

@TerrenceVarada
Copy link

Thanks for such a great tool. It works perfectly through my research. Now I wanna use this in spark. Since I can't import talib to the online environment, I have to rewrite the indicators through python. And I tried to compare your results with mine, I can always find some differences. Could you open your source code? Looking forward hearing from you, thanks again.

@mrjbq7
Copy link
Collaborator

mrjbq7 commented Apr 11, 2018

Good to hear. The source code is super open! First, this project is a Python wrapper around an underlying C library (http://ta-lib.org).

You can download the last release of source code (a decade ago or so) here:

http://prdownloads.sourceforge.net/ta-lib/ta-lib-0.4.0-src.tar.gz

You can also view code online (trunk contains latest code with a few unreleased commits it looks like):

https://sourceforge.net/p/ta-lib/code/HEAD/tree/trunk/ta-lib/

For example, the MOM momentum indicator you can see the source code here:

https://sourceforge.net/p/ta-lib/code/HEAD/tree/trunk/ta-lib/c/src/ta_func/ta_MOM.c

It's a little hard to read, since it supports multiple languages, and mostly also because of C performance and verbosity, but you get used to it.

And for complete-ness, this is how I call it from the Cython module:

https://github.com/mrjbq7/ta-lib/blob/master/talib/_func.pxi#L3996

@TerrenceVarada
Copy link
Author

TerrenceVarada commented Apr 12, 2018 via email

@TerrenceVarada
Copy link
Author

TerrenceVarada commented Apr 12, 2018 via email

@mrjbq7
Copy link
Collaborator

mrjbq7 commented Apr 12, 2018

Your attachments didn't come through. Maybe you can post the code and files somewhere as a http://gist.github.com or something.

But reading through the source code for the TA-Lib version of SAR:

https://sourceforge.net/p/ta-lib/code/HEAD/tree/trunk/ta-lib/c/src/ta_func/ta_SAR.c#l240

I see this comment, maybe it's helpful:

   /* Implementation of the SAR has been a little bit open to interpretation
    * since Wilder (the original author) did not define a precise algorithm
    * on how to bootstrap the algorithm. Take any existing software application
    * and you will see slight variation on how the algorithm was adapted.
    *
    * What is the initial trade direction? Long or short?
    * ===================================================
    * The interpretation of what should be the initial SAR values is
    * open to interpretation, particularly since the caller to the function
    * does not specify the initial direction of the trade.
    *
    * In TA-Lib, the following logic is used:
    *  - Calculate +DM and -DM between the first and
    *    second bar. The highest directional indication will
    *    indicate the assumed direction of the trade for the second
    *    price bar. 
    *  - In the case of a tie between +DM and -DM,
    *    the direction is LONG by default.
    *
    * What is the initial "extreme point" and thus SAR?
    * =================================================
    * The following shows how different people took different approach:
    *  - Metastock use the first price bar high/low depending of
    *    the direction. No SAR is calculated for the first price
    *    bar.
    *  - Tradestation use the closing price of the second bar. No
    *    SAR are calculated for the first price bar.
    *  - Wilder (the original author) use the SIP from the
    *    previous trade (cannot be implement here since the
    *    direction and length of the previous trade is unknonw).
    *  - The Magazine TASC seems to follow Wilder approach which
    *    is not practical here.
    *
    * TA-Lib "consume" the first price bar and use its high/low as the
    * initial SAR of the second price bar. I found that approach to be
    * the closest to Wilders idea of having the first entry day use
    * the previous extreme point, except that here the extreme point is
    * derived solely from the first price bar. I found the same approach
    * to be used by Metastock.
    */

@TerrenceVarada
Copy link
Author

TerrenceVarada commented Apr 12, 2018 via email

@mrjbq7
Copy link
Collaborator

mrjbq7 commented Apr 12, 2018

Interesting. Not sure, without spending more time on it which I don't have right now. The C code is the best place to see what differences might exist.

However, it's not my C code. I just support this Python wrapper for that TA-Lib underlying C library, and do my best to answer questions, etc. But I can't help you with the thinking of the original author or the behavior they intended.

@TerrenceVarada
Copy link
Author

TerrenceVarada commented Apr 14, 2018 via email

@bhavishyagoyal12
Copy link

It's not matching with IB PSAR as well. May i request you to let me know how can we fix that ?

@Linganna
Copy link

Thanks for such a great tool. It works perfectly through my research. Now I wanna use this in spark. Since I can't import talib to the online environment, I have to rewrite the indicators through python. And I tried to compare your results with mine, I can always find some differences. Could you open your source code? Looking forward hearing from you, thanks again.

HI @TerrenceVarada , hope you are doing good, we are also looking for the tech indicators calculation library which can be used on Spark where the calculation will happen on the cluster. When i saw your message was excited that u were also trying to do same, Just want to know, Have you made any progress on this library or if u can share some of your thoughts how to take it forward on this it will be great help, thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants