Trend Trigger Factor Indicator (TTF) in Python?

Question:

I am trying to convert TTF Indicator from TradingView Pine Script to Python. (with no plotting)

This is the Pine Script code I am trying to convert:

//@version=3
// Copyright (c) 2018-present, Alex Orekhov (everget)
// Trend Trigger Factor script may be freely distributed under the MIT license.
study("Trend Trigger Factor", shorttitle="TTF")

length = input(title="Lookback Length", type=integer, defval=15)
upperLevel = input(title="Upper Trigger Level", type=integer, defval=100, minval=1)
lowerLevel = input(title="Lower Trigger Level", type=integer, defval=-100, maxval=-1)
highlightBreakouts = input(title="Highlight Overbought/Oversold Breakouts ?", type=bool, defval=true)
src = input(title="Source", type=source, defval=close)

hh = highest(length)
ll = lowest(length)

buyPower = hh - nz(ll[length])
sellPower = nz(hh[length]) - ll

ttf = 200 * (buyPower - sellPower) / (buyPower + sellPower)

ttfColor = ttf > upperLevel ? #0ebb23 : ttf < lowerLevel ? #ff0000 : #f4b77d
plot(ttf, title="TTF", linewidth=2, color=ttfColor, transp=0)

transparent = color(white, 100)

maxLevelPlot = hline(200, title="Max Level", linestyle=dotted, color=transparent)
upperLevelPlot = hline(upperLevel, title="Upper Trigger Level", linestyle=dotted)
hline(0, title="Zero Level", linestyle=dotted)
lowerLevelPlot = hline(lowerLevel, title="Lower Trigger Level", linestyle=dotted)
minLevelPlot = hline(-200, title="Min Level", linestyle=dotted, color=transparent)

fill(upperLevelPlot, lowerLevelPlot, color=purple, transp=95)

upperFillColor = ttf > upperLevel and highlightBreakouts ? green : transparent
lowerFillColor = ttf < lowerLevel and highlightBreakouts ? red : transparent

fill(maxLevelPlot, upperLevelPlot, color=upperFillColor, transp=90)
fill(minLevelPlot, lowerLevelPlot, color=lowerFillColor, transp=90)

Here is what I done so far:

from finta import TA
#import pandas_ta as ta
import yfinance as yf
import pandas as pd
import numpy as np

ohlc = yf.download('BTC-USD', start='2022-08-01', interval='1d')

length = 15

hh = ohlc['High'].rolling(length).max()
ll = ohlc['Low'].rolling(length).min()

buyPower = hh - ll.fillna(0) 
sellPower = hh.fillna(0) - ll

ttf = 200 * (buyPower - sellPower) / (buyPower + sellPower)

I don’t know what I’m not doing the right way but everytime TTF is like this way below:

Date
2022-07-31    NaN
2022-08-01    NaN
2022-08-02    NaN
2022-08-03    NaN
2022-08-04    NaN
             ... 
2022-11-14    0.0
2022-11-15    0.0
2022-11-16    0.0
2022-11-17    0.0
2022-11-18    0.0
Length: 111, dtype: float64

I’m thinking that these two Pine Script functions below I did them in the wrong way:

buyPower = hh - nz(ll[length])
sellPower = nz(hh[length]) - ll

But I’m not sure and also I don’t know what would be their Python equivalent.

Any idea, please?

Thank you in advance!

Asked By: YoYoYo

||

Answers:

After I struggled a little bit I have found the right answer.
I was right about where it could be the wrong part in my code above:

buyPower = hh - nz(ll[length])
sellPower = nz(hh[length]) - ll

It is not equal to this:

buyPower = hh - ll.fillna(0) 
sellPower = hh.fillna(0) - ll

The correct python conversion is like so:

buyPower = hh - ll.shift(length).fillna(0) 
sellPower = hh.shift(length).fillna(0) - ll
Answered By: YoYoYo