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
Can calculate EMA Incrementally? #114
Comments
I pushed an experimental "Streaming API" (in the Any function you can import from the Function API ( |
Thanks very much, that's wonderful. Dose the original c/c++ API support this kind of "Streaming API"? Cause my online software is using the c/c++ version while python version is using for back-testing. |
Yes, my You can see, for example, the code for calling https://github.com/mrjbq7/ta-lib/blob/master/talib/stream.pyx#L6486 |
I am sorry for that I must info this issue, the steaming API stream.EMA looks doesn't work well while stream.MA is good. Following is the test code. The key reason for EMA issue may be it need EMA[i-1] when calculate EMA[i], but MA not.
|
Hmm, I'll need to look into that -- maybe there is a way for me to call it differently to have the numerical differences become smaller. |
As I know, ta-lib doesn't support incrementally calculation. Streaming API is just using part of data to calculate. refer http://www.kbasm.com/blog/ta-lib-not-incremental-and-wrong.html |
@dahai001 The value differences coming out of the EMA function are actually an intrinsic property of exponential moving averages having a "memory" of past values effecting the most recent values. In other words, even though you're using a period of 3, the 4th, 5th, ... Nth values of the input array (if available) all factor into the calculation (by exponentially decreasing factors as you go further back in time). Functions which behave this way are said to have an "unstable period" in upstream TA-Lib nomenclature, and can be identified through the abstract API:
|
Hmm, @briancappello makes sense! If we keep the "Streaming API" around, we probably should document that somewhere. Or maybe remove / warning the functions that are like that? |
@mrjbq7 Yea, probably would be a good idea to document the unstable functions somewhere. What were your primary design goals with the streaming API? It maybe could be worth the performance trade-off to provide as input to unstable functions more values than the are absolutely necessary (as defined by the lookback), just not the entire array. For the EMA function at least, and depending upon how much accuracy one desires, this value seems to be about 20 times the timeperiod: import numpy as np
import talib as ta
def test_ema(timeperiod):
arr = np.random.rand(5000) * 50 # fake if highly volatile price data
trim = timeperiod
while True:
func_ema_val = ta.func.EMA(arr, timeperiod)[-1]
stream_ema_val = ta.func.EMA(arr[-trim:], timeperiod)[-1]
if func_ema_val == stream_ema_val: # or convert to str and chop at desired accuracy
break
trim += 1
return trim*1. / timeperiod
multipliers = [test_ema(timeperiod) for timeperiod in xrange(2, 500)]
max(multipliers) < 20 # True Exact values vary depending on the random input array, but, they're consistently between 16x and 19x the timeperiod. Also, whether or not multiplying by the timeperiod is always the right thing to do, I am not sure. It seems to work well enough for the EMA function, but I haven't tested any others to see if the relationship holds up. PS. Can you push the script you used to generate the streaming API? |
I've had several people ask about "just calculate the most recent bar" functionality (mostly for the performance improvement that might give). I wasn't aware of the unstable functions, although that makes total sense. I pushed |
Yeah, incrementally calculation is a very useful feature when we build an online trade system using ta-lib. Can we change the c/c++ ta-lib to support this feature? |
@mrjbq7 , I've done such incremental indicators in my fork 2 years ago. The main idea is that TA-Lib process arrays of data element by element thus it can be paused and continued after any processing step while you're able to store algorithm internal state between steps. So there are few new functions were added for each indicator:
This means that among with User have:
And instead of calling
you can use something like
Thus indicator can be easily applied to data received online at runtime. But of course you pay some memory usage overhead for that. Still state objects are small enough to operate hundreds of indicators at once. I think were was also some state object serialization/deserialization functions to even save and restore this process from drive or rollback state. And some other fancy helper funcs. New functions are properly declared with modified templates and The problems are:
Anyway, sooner or later I want to finalize it to releasable state. So if someone want to try the code in its own project or just need some internal C modifications in it - let me know and it may happen faster. |
Has the Streaming API been updated in a while or is it stable to be used now? |
The streaming API works fine, but has some difference of behavior due to not keeping memory in the same way as operating on all the previous data points. The suggested state object idea is a good one, but so far we are trying to match features only in the released TA-Lib. |
So the other way you are able to use it for live/streaming data? |
i need this feature on websocket data, hope it can be released near soon... |
We are working with the author of TA-Lib to participate in maintaining the C library, which would allow us to consider new use-cases like this. |
I would encourage trying out the streaming API, which does also work, albeit uses only "N previous observations" in the case of EMA, rather than all previous. |
Any progress ? |
What does resampledata do that breaks the ta-lib calculation ?On Aug 2, 2023, at 5:34 AM, zgpnuaa ***@***.***> wrote:
Any progress ?
This has also led to differences in MACD and brokerage software.
Standard MACD vs Talib MACD - Different Values
—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you were mentioned.Message ID: ***@***.***>
|
Maybe there was an error when using dataframe.resample(). This is for resampling, which means combining minute-level OHLC data into hourly OHLC data. However, the trading hours are from 9:30 to 11:30 and 13:00 to 15:00. I noticed that resample is not working correctly for trading hours starting at half past nine (9:30). |
I want to calculate EMA every minute using the latest minute k-bar, is there a way to do this? For now, it need calculate from the begin time k-bars.
The text was updated successfully, but these errors were encountered: