How to calculate Heiken Chaiken Osc using pandas python

684 views Asked by At

I am calculating indicator data for stock market and am having difficulty calculating Heiken Chaiken Osc because of a variable called 'Accdist' According to google it is calculated like this.

Formula is as follows:

  1. Money Flow Multiplier = [(Close - Low) - (High - Close)] /(High - Low)

  2. Money Flow Volume = Money Flow Multiplier x Volume for the Period

  3. ADL = Previous ADL + Current Period's Money Flow Volume

Python code (using Talib + Numpy + Pandas)

data["cmf"] = (((data["close"] - data["low"]) - (data["high"] - data["close"])) / (data["high"] - data["low"]))

data["cmfv"] = data["cmf"] * data["volume"]

data["accdist"] = data["accdist"].shift(periods=1) + data["cmfv"]

Also tried it in pine script, but no luck there either. Any help would be appreciated.

Money_multiplier = ((close-low) - (high - close)) / (high - low)
Money_volume = Money_multiplier * volume
ADL = ADL[1] + Money_volume

Here is the pinescript code I am trying to emulate as I have indicator working, but need to translate to python(probelem is the accdist variable)

osc = ema(accdist, 3) - ema(accdist, 10)
1

There are 1 answers

0
Cliff Robbins On BEST ANSWER

I ran across this same question and here is what I ended up with using python, numpy and pandas.

I used the formula: Money Flow Multiplier = ((Close value – Low value) – (High value – Close value)) / (High value – Low value)

Money Flow Volume = Money Flow Multiplier x Volume for the Period

CMF = 21-day Average of the Daily Money Flow / 21-day Average of the Volume

Here we read in a basic fx or stock data file:

import numpy as np
import pandas as pd

names = ['Date','Time','Open','High','Low','Close','Volume']
ask_series = pd.read_csv(ask_file,index_col=False,header=None,names=names))

ask_series["cmfm"] = (((ask_series["close"] - ask_series["low"]) - (ask_series["high"] - ask_series["close"])) / (ask_series["high"] - ask_series["low"]))
ask_series["cmfv"] = ask_series["cmfm"] * ask_series["volume"]
ask_series["cmf"] = ask_series['cmfv'].rolling(window=21).mean() / ask_series['volume'].rolling(window=21).mean() 

ask_series.tail()