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

Trend Step Channel - appreciate it #269

Open
krazykoder opened this issue Jun 12, 2022 · 4 comments
Open

Trend Step Channel - appreciate it #269

krazykoder opened this issue Jun 12, 2022 · 4 comments
Labels
Enhancement New feature or request pinescript Indicators to be converted from pinescript

Comments

@krazykoder
Copy link

https://www.tradingview.com/script/nXhuJpwr-Efficient-Trend-Step-Channel/
Can anyone code this indicator in python/numba please. Seems like i am getting stuck at this line not sure what it means
abs(change(src,length))/sum(abs(change(src)),length)
the result is supposedly a series but the seems to be a ratio of two series. Appreciate any help.

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © alexgrover

//@version=4
study("Efficient Trend Step Channel",overlay=true)
length = input(100),fast = input(50),slow = input(200),src = input(close)
//----
er = abs(change(src,length))/sum(abs(change(src)),length)
dev = er*stdev(src*2,fast) + (1-er)*stdev(src*2,slow)
//----
a=0.,a := src > nz(a[1],src) + dev ? src : src < nz(a[1],src) - dev ? src : nz(a[1],src)
upper = valuewhen(change(a),a + dev,0)
lower = valuewhen(change(a),a - dev,0)
@xmatthias xmatthias added the Enhancement New feature or request label Jun 12, 2022
@xmatthias
Copy link
Member

if you're stuck at some line - best share what you have so far, so we can determine where the most likely error is ?

@krazykoder
Copy link
Author

krazykoder commented Jun 13, 2022

Thanks for responding, appreciate it. I have gotten this far. Basically i have verified that deviation dev is correct and matching trading view values. Where I am stuck is these 3 lines from the pine version

a=0.,a := src > nz(a[1],src) + dev ? src : src < nz(a[1],src) - dev ? src : nz(a[1],src)
upper = valuewhen(change(a),a + dev,0)
lower = valuewhen(change(a),a - dev,0)

Below is my python version (non optimized). I am using pandas ts for stddev, mom calculation which is equivalent of change

import pandas_ta as ta
## dfInd is the input dataframe that contain ohlcv data 

length = 100
fast = 50
slow = 200
src = dfInd['Close']  # control src : Close, hl2, hlc3, etc 

slowChange = ta.mom(src,length=length).abs()
fastChangeSum = ta.mom(src,length=1).abs().rolling(length).sum()

er = slowChange/fastChangeSum
dev = er*ta.stdev(src*2,fast) + (1-er)*ta.stdev(src*2,slow) # verified and correct 

## ------------- Code below this line is where i am struggling !! 

a = pd.DataFrame(0., index=dfInd.index, columns=['a'])
a=a['a']
# len(src.values)
# len(src.shift().fillna(0).values)

# # a['a'] = src.shift().fillna(0).values

value = np.where(a.shift(1).notnull(), a, src)

tsc = np.where (src > value + dev , value, src)
# tsc = np.where (src > value + dev , src, (np.where (src < value - dev , src , value)))  

dfInd['TSC'] = tsc
dfInd['TSCdev'] = dev

# ----------- not reached to upper and lower bands calculations yet
# chang_a = ta.mom(dfInd['TSC'],1)
# dfInd['TSC_U'] = dfInd['TSC'] + dev
# dfInd['TSC_L'] = dfInd['TSC'] - dev

Appreciate if you can take a look at it.

@xmatthias
Copy link
Member

a[1] is referencing the prior row.
as this happens as part of the assignment to a - this means that you cannot really vectorize this indicator - but you'll have to loop - as the result of each row depends on the result from the calculation of the prior row.

You can find an example for this for example here:

for i in range(length, len(df)):
df["VIDYA"].iat[i] = (
alpha * df["k"].iat[i] * df["close"].iat[i]
+ (1 - alpha * df["k"].iat[i]) * df["VIDYA"].iat[i - 1]
)

Obviously, this will have a quite severe performance penalty - as you'll loop through a dataframe with potentially 1000nds rows.

@krazykoder
Copy link
Author

I understand what you are referring. Thanks for the quick response.
I will try out just looping through as you suggested here and see what results i get.

@xmatthias xmatthias added the pinescript Indicators to be converted from pinescript label Aug 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement New feature or request pinescript Indicators to be converted from pinescript
Projects
None yet
Development

No branches or pull requests

2 participants