Skip to content

Instantly share code, notes, and snippets.

@mouchh
Created November 29, 2020 16:44
Show Gist options
  • Save mouchh/0e4e83443d43f3d68cfe220ccac3f36a to your computer and use it in GitHub Desktop.
Save mouchh/0e4e83443d43f3d68cfe220ccac3f36a to your computer and use it in GitHub Desktop.
Compute stochastic from OHLC dataframe which give the same value as Trading View Stoch(14, 3, 5)
# I was not able to retrieve same results as Trading View with talib STOCH function...
# I may misuse talib params but I found it easier to rewrite the computation, following Trading View doc
# https://www.tradingview.com/support/solutions/43000502332-stochastic-stoch/
# watchout on your params ;)
stoch = 100 * (dataframe['close'] - dataframe['low'].rolling(14).min()) / (dataframe['high'].rolling(14).max() - dataframe['low'].rolling(14).min())
dataframe['stoch_slowk'] = ta.SMA(stoch, 5)
dataframe['stoch_slowd'] = ta.SMA(dataframe['stoch_slowk'], 3)
@aleskxyz
Copy link

This parameters gives the same result:
slowk, slowd = ta.STOCH(dataframe['high'], dataframe['low'], dataframe['close'], fastk_period=14, slowk_period=5, slowk_matype=0, slowd_period=3, slowd_matype=0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment