ta-lib-python icon indicating copy to clipboard operation
ta-lib-python copied to clipboard

How to support different timescale ?

Open eromoe opened this issue 2 years ago • 4 comments

For example, I have 1 minute scale tick data, and I'd like add 5 minute/ 15 minute feature upon .

I wrote a function

def apply_over(func, arr, stride):
	n = len(arr)
	s = np.empty(n).reshape(-1, stride)

	for i in range(stride):
		s[:,i] = func(arr[i::stride])

	return s.reshape(n,)

This generate 5 minute SMA features for each 1 minute tick .

arr = np.arange(n).astype(float)
sma_5_5 = apply_over(lambda a: talib.SMA(a, 5) , arr, 5)

I'd like adapt above code to every talib function, though there are many Indicator need multi input. So wonder is there any suggestion way to wrap talib function with my apply_over fucntion ?

Or I have to rewrite all the functions ...

eromoe avatar Mar 14 '23 03:03 eromoe

Is your issue that you have a column of TA-Lib output and want to “adjust” that output to a different timescale?

Normally one would adjust the OHLCV data to a different timescale and then call TA-Lib functions on that new timescale.

On Tue, Mar 14, 2023 at 9:20 AM eromoe @.***> wrote:

For example, I have 1 minute scale tick data, and I'd like add 5 minute/ 15 minute feature upon .

I wrote a function

def apply_over(func, arr, stride): n = len(arr) s = np.empty(n).reshape(-1, stride)

for i in range(5): s[:,i] = func(arr[i::5])

return s.reshape(n,)

This generate 5 minute SMA features for each 1 minute tick .

arr = np.arange(n).astype(float) sma_5_5 = apply_over(lambda a: talib.SMA(a, 5) , arr, 5)

I'd like adapt above code to every talib function, though there are many Indicator need multi input. So wonder is there any suggestion way to wrap talib function with my apply_over fucntion ?

Or I have to rewrite all the functions ...

— Reply to this email directly, view it on GitHub https://github.com/TA-Lib/ta-lib-python/issues/583, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAF5AYCWKDDVW5JLQGYNX3W37THDANCNFSM6AAAAAAVZ42Y4U . You are receiving this because you are subscribed to this thread.Message ID: @.***>

mrjbq7 avatar Mar 14 '23 03:03 mrjbq7

No, arr is input . you can think arr = np.arange(1000).astype(float) as a stock close price at minute level . It is a 1000 time tick collection.

On every tick , I need calculate sma5 on

  • 1 minute scale (simply talib.SMA(arr, 5) )
  • 5 minute scale (apply_over(lambda a: talib.SMA(a, 5) , arr, 5) )
  • 15 minute scale (apply_over(lambda a: talib.SMA(a, 5) , arr, 15) )

For example: A series ...,500, 501, 502, 503, 504 , 505,....

At 505,

  • 1 minute scale = SMA([ 501, 502, 503, 504 , 505])
  • 5 minute scale = SMA([ 485, 490, 495, 500 , 505])

Using pandas resmple(freq='5min').first() would make gaps , shrink arr length from 1000 to 200 . You need do it 5 times with each shift [0,1,2,3,4] and apply SMA to make sure every tick have 5 minute feature, , like what I do in apply_over

eromoe avatar Mar 14 '23 04:03 eromoe

I am looking for a way, either inherit a interface or a wrap to achieve my goal efficiently. Avoid writing too much replicated codes.

eromoe avatar Mar 14 '23 04:03 eromoe

Hello @eromoe , I'm curious if you've had any progress on this issue. I am currently working on similar code myself.

Marc-Bogonovich avatar Sep 23 '23 07:09 Marc-Bogonovich