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

Make talib support multi timescale. #581

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft

Conversation

eromoe
Copy link

@eromoe eromoe commented Apr 4, 2023

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)

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


Because I really like vectorbt, so share this idea here.

@eromoe eromoe marked this pull request as draft April 4, 2023 08:52
@eromoe
Copy link
Author

eromoe commented Apr 4, 2023


import scipy.stats as ss

arr = ss.halfnorm.rvs(10, 5, (100,))

v1 = vbt.mtalib('SMA', 5).run(arr, 5).real
v2 = vbt.talib('SMA').run(arr, 5).real


dfs = [v1, v2]

fig = go.Figure()
# a = a.set_index('ds').reset_index()
for a in dfs:
    fig.add_trace(
        go.Line(
            x=np.arange(len(a)),
            y=a,
        )
    )
fig.show()

image

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

Successfully merging this pull request may close these issues.

None yet

1 participant