Giter Club home page Giter Club logo

Comments (4)

xmatthias avatar xmatthias commented on June 18, 2024

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

from technical.

krazykoder avatar krazykoder commented on June 18, 2024

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.

from technical.

xmatthias avatar xmatthias commented on June 18, 2024

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.

from technical.

krazykoder avatar krazykoder commented on June 18, 2024

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.

from technical.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.