Skip to content

Instantly share code, notes, and snippets.

@timotewb
Last active October 3, 2020 11:08
Show Gist options
  • Save timotewb/8da892c7f61e106a4a81474f7a0bba8d to your computer and use it in GitHub Desktop.
Save timotewb/8da892c7f61e106a4a81474f7a0bba8d to your computer and use it in GitHub Desktop.
def t3MovingAverage(period,data,vfactor=0.7,tcount=3):
#--- T3 Moving Average
# data: array, time series data e.g. daily close prices.
# period: integer, number of periods form time series array to include in calculation.
# vfactor: float, number between 1 and 0. 1 = DEMA, 0 = EMA. default 0.7.
# tcount: integer, number 1 or greater. times the data is smoothed with the generalisedDEMA. default 3 (T3).
#
# requires numpy and techind libraries
#
# see below link for maintained code
# https://github.com/codersnotepad/techind/blob/master/techind/t3MovingAverage.py
import numpy as np
import techind as ti
#---Generalised DEMA (GD)
def generalisedDEMA(period,data,vfactor):
ema1 = ti.exponentialMovingAverage(period,data)
ema2 = ti.exponentialMovingAverage(period,ema1)
# GD = EMA(period,data) x (1+vfactor) - EMA(periodT3,EMA(period,data)) x vfactor
return ema1 * (1 + vfactor) - (ema2 * vfactor)
#---Calculate smoothing passed based on tcount
if not (tcount >= 1):
raise ValueError('tcount must be 1 or more. Default = 3 to T3 calucation.')
elif tcount >= 1:
gd = generalisedDEMA(period,data,vfactor)
if tcount >1:
for i in range(tcount-1):
gd = generalisedDEMA(period,gd,vfactor)
return gd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment