Trend Trigger Factor For ThinkOrSwim

mikeytrader13

New member
Hi all this indicator was originally developed by M. H. Pee (Stocks & Commodities V.22:12 (28-36): Trend Trigger Factor.
https://www.tradingview.com/script/WA9Z4CV2-Trend-Trigger-Factor/
If its beneficial to your trading journey then all the help will be greatly appreciated, thank you.
mod note. more info on the Trend Trigger Factor:
https://www.tradingpedia.com/forex-...end Trigger Factor is,during most of the time.


nhe9f0H.png
 
Last edited by a moderator:

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Code:
# //@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")
# ThinkOrSwim conversion by mashume 2022.10.05

declare lower;

input length = 15;
input upperLevel = 100;
input lowerLevel = -100;
input src = CLOSE;

script nz {
    input data = 0;
    def ret_val = if IsNaN(data) then 0 else data;
    plot return = ret_val;
}

def hh = Highest(src, length);
def ll = Lowest(src, length);

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

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

ttf.AssignValueColor(if ttf > upperLevel then color.dark_green else if ttf < lowerLevel then color.dark_red else color.dark_orange);

AddCloud(upperLevel,   lowerLevel,   Color.light_GRAY,   Color.light_GRAY);

plot midline = 0;
plot upper = upperLevel;
plot lower = lowerLevel;
midline.SetDefaultColor(color.gray);
upper.SetDefaultColor(color.gray);
lower.SetDefaultColor(color.gray);

def lowest = if ttf < lowerLevel then -200 else Double.NaN;
def highest = if ttf > upperLevel then 200 else Double.NaN;

AddCloud(upperLevel, highest, Color.GREEN, Color.GREEN);
AddCloud(lowerLevel, lowest, Color.RED, Color.RED);
 
According to tradingpedia, there is a different way to calculate the TTF. The following code calculates by that methodology.

EDIT:
This also seems to be the original calculation method per this article:
http://traders.com/documentation/feedbk_docs/2004/12/Abstracts_new/Pee/pee.html

Code:
declare lower;

# https://www.tradingpedia.com/forex-trading-indicators/trend-trigger-factor-indicator/#:~:text=The%20Trend%20Trigger%20Factor%20is,during%20most%20of%20the%20time.

input length = 15;
input upperLevel = 100;
input lowerLevel = -100;

# – 15-day Buy Power = (Highest high of day 1 through day 15) – (Lowest low of day 16 through day 30)
def BuyPower = highest(high, length) - lowest(low[length], length);

# – 15-day Sell Power = (Highest high of day 16 through day 30) – (Lowest low of day 1 through day 15)
def SellPower = highest(high[length], length) - lowest(low, length);

# Having estimated the two variables, we then move on to the TTF calculation itself:
# – 15-day Trend Trigger Factor = [(Buy Power – Sell Power) / (0.5 x (Buy Power + Sell Power))] x 100

plot TTF = ( (BuyPower - SellPower) / (0.5 * (BuyPower + SellPower)) ) * 100;

ttf.AssignValueColor(if ttf > upperLevel then color.dark_green else if ttf < lowerLevel then color.dark_red else color.dark_orange);

AddCloud(upperLevel,   lowerLevel,   Color.light_GRAY,   Color.light_GRAY);

plot midline = 0;
plot upper = upperLevel;
plot lower = lowerLevel;
midline.SetDefaultColor(color.gray);
upper.SetDefaultColor(color.gray);
lower.SetDefaultColor(color.gray);

def lowest = if ttf < lowerLevel then -200 else Double.NaN;
def highest = if ttf > upperLevel then 200 else Double.NaN;

AddCloud(upperLevel, highest, Color.GREEN, Color.GREEN);
AddCloud(lowerLevel, lowest, Color.RED, Color.RED);

There are subtle differences between the methods. The original method seems faster, but also stays in the coloured zones longer. I make no promises about which one is 'better'. They are just different. As always, the indicator is just a tool and there are a thousand ways to make a screwdriver.

-mashume
 
Last edited:
According to tradingpedia, there is a different way to calculate the TTF. The following code calculates by that methodology.

EDIT:
This also seems to be the original calculation method per this article:
http://traders.com/documentation/feedbk_docs/2004/12/Abstracts_new/Pee/pee.html

Code:
declare lower;

# https://www.tradingpedia.com/forex-trading-indicators/trend-trigger-factor-indicator/#:~:text=The%20Trend%20Trigger%20Factor%20is,during%20most%20of%20the%20time.

input length = 15;
input upperLevel = 100;
input lowerLevel = -100;

# – 15-day Buy Power = (Highest high of day 1 through day 15) – (Lowest low of day 16 through day 30)
def BuyPower = highest(high, length) - lowest(low[length], length);

# – 15-day Sell Power = (Highest high of day 16 through day 30) – (Lowest low of day 1 through day 15)
def SellPower = highest(high[length], length) - lowest(low, length);

# Having estimated the two variables, we then move on to the TTF calculation itself:
# – 15-day Trend Trigger Factor = [(Buy Power – Sell Power) / (0.5 x (Buy Power + Sell Power))] x 100

plot TTF = ( (BuyPower - SellPower) / (0.5 * (BuyPower + SellPower)) ) * 100;

ttf.AssignValueColor(if ttf > upperLevel then color.dark_green else if ttf < lowerLevel then color.dark_red else color.dark_orange);

AddCloud(upperLevel,   lowerLevel,   Color.light_GRAY,   Color.light_GRAY);

plot midline = 0;
plot upper = upperLevel;
plot lower = lowerLevel;
midline.SetDefaultColor(color.gray);
upper.SetDefaultColor(color.gray);
lower.SetDefaultColor(color.gray);

def lowest = if ttf < lowerLevel then -200 else Double.NaN;
def highest = if ttf > upperLevel then 200 else Double.NaN;

AddCloud(upperLevel, highest, Color.GREEN, Color.GREEN);
AddCloud(lowerLevel, lowest, Color.RED, Color.RED);

There are subtle differences between the methods. The original method seems faster, but also stays in the coloured zones longer. I make no promises about which one is 'better'. They are just different. As always, the indicator is just a tool and there are a thousand ways to make a screwdriver.

-mashume

Thanks I will checkout this methodology. Also I was reading some settings from the forex version where they apply a smoothing filter and a sensitivity filter for the signal line. Maybe it can improve the way the indicator works.

For further reading checkout the page;
https://stonehillforex.com/2022/10/trend-trigger-factor-as-a-confirmation-indicator/
 
The version they post on their page is significantly more complex... I'll look into converting it at some point, just for comparison. Thanks for posting the link. I had seen their page when I was reading up on the TTF indicator, but hadn't really dug into what they had written up.

Make that 1001 ways to make a screwdriver.

-mashume
 
The version they post on their page is significantly more complex... I'll look into converting it at some point, just for comparison. Thanks for posting the link. I had seen their page when I was reading up on the TTF indicator, but hadn't really dug into what they had written up.

Make that 1001 ways to make a screwdriver.

-mashume
So,

I'm not sure this is all correct. The habit Tradeview has of being able to redefine variables using the variable itself is rather troublesome to my functional-programming oriented mind. Take it as you want. It plots, and it seems reasonable. The b parameter does alter the sensitivity. The others seem to do what they should as well... I just don't know if it's all the right thing it's doing.

Take this one with a BIG grain of salt.

-mashume

Code:
#  //+------------------------------------------------------------------+
#  //|                                                   ttf.mq4        |
#  //|                      Copyright � 2005, Nick Bilak                |
#  //|                                                                  |
#  //+------------------------------------------------------------------+
#  #property copyright "Copyright � 2005, Nick Bilak"
#  #property link      "beluck[at]gmail.com"
#  //----
#  #property indicator_separate_window
#  #property indicator_buffers 2
#  #property indicator_color1 LightSeaGreen
#  #property indicator_color2 Red
declare lower;

#  //---- input parameters
#  extern int TTFbars=8;
input TTFbars = 8;

#  //15=default number of bars for computation
#  extern int TopLine=75;
input TopLine = 75;

#  extern int BottomLine=-75;
input BottomLine = -75;

#  extern int t3_period=3;
input t3_period = 3;

#  extern double b=0.7;
input b = 0.7;

#  //---- buffers
#  double MainBuffer[];
#  double SignalBuffer[];
#  //----
#  int draw_begin1=0;
#  int draw_begin2=0;
#  double b2=0;
#  double b3=0;
#  double c1=0;
#  double c2=0;
#  double c3=0;
#  double c4=0;
#  double r=0;
#  double w1=0;
#  double w2=0;
#  double e1=0;
#  double e2=0;
#  double e3=0;
#  double e4=0;
#  double e5=0;
#  double e6=0;
#  double  HighestHighRecent=0;
#  double  HighestHighOlder =0;
#  double  LowestLowRecent =0;
#  double  LowestLowOlder =0;
#  double  BuyPower =0;
#  double  SellPower=0;
#  double  TTF=0;
#  //+------------------------------------------------------------------+
#  //| Custom indicator initialization function                         |
#  //+------------------------------------------------------------------+
#  int init()
#    {
#     string short_name;
#  //---- 2 additional buffers are used for counting.
#     IndicatorBuffers(2);
#  //---- indicator lines
#     SetIndexStyle(0,DRAW_LINE);
#     SetIndexBuffer(0, MainBuffer);
#     SetIndexStyle(1,DRAW_LINE);
#     SetIndexBuffer(1, SignalBuffer);
#  //---- name for DataWindow and indicator subwindow label
#     short_name="TTF("+TTFbars;
#     IndicatorShortName(short_name);
#     SetIndexLabel(0,short_name);
#     SetIndexLabel(1,"Signal");
#  //----
#     draw_begin1=TTFbars*2+1;
#     draw_begin2=draw_begin1;
#     SetIndexDrawBegin(0,draw_begin1);
#     SetIndexDrawBegin(1,draw_begin2);
#  //----   
#     b2=b*b;
def b2 = b * b;

#     b3=b2*b;
def b3 = b2 * b;

#     c1=-b3;
def c1 = -b3;

#     c2=(3*(b2+b3));
def c2 = (3*(b2+b3));

#     c3=-3*(2*b2+b+b3);
def c3 = -3*(2*b2+b+b3);

#     c4=(1+3*b+b3+3*b2);
def c4 = (1+3*b+b3+3*b2);

#     r=t3_period;
#     if (r<1) r=1;
#     r=1 + 0.5*(r-1);
def r = if t3_period < 1 then 1 else 1 + 0.5 * (t3_period - 1);

#  //----
#     w1=2/(r + 1);
def w1 = 2 / (r + 1);


#     w2=1 - w1;
def w2 = 1 - w1;

#  //----
#     return(0);
#    }
#  //+------------------------------------------------------------------+
#  //| ttf                                                              |
#  //+------------------------------------------------------------------+
#  int start()
#    {
#     int    i,k;
#     int    counted_bars=IndicatorCounted();
#     double price;
#  //----
#     if(Bars<=draw_begin2) return(0);
#  //---- initial zero
#     if(counted_bars<1)
#       {
#        for(i=1;i<=draw_begin1;i++) MainBuffer[Bars-i]=0;
#        for(i=1;i<=draw_begin2;i++) SignalBuffer[Bars-i]=0;
#       }
#  //---- %K line
#     i=Bars-draw_begin1;
#     if(counted_bars>draw_begin1) i=Bars-counted_bars-1;
#     while(i>=0)
#       {
#        HighestHighRecent=High[Highest(NULL,0,MODE_HIGH,TTFbars,i)];//High[Highest(MODE_HIGH,shift+TTFbars-1,TTFbars)];
#        HighestHighOlder =High[Highest(NULL,0,MODE_HIGH,TTFbars,i+TTFbars)];//High[Highest(MODE_HIGH,shift+TTFbars*2-1,TTFbars)];
#        LowestLowRecent =Low[Lowest(NULL,0,MODE_LOW,TTFbars,i)];//Low [Lowest (MODE_LOW ,shift+TTFbars-1,TTFbars)];
#        LowestLowOlder =Low[Lowest(NULL,0,MODE_LOW,TTFbars,i+TTFbars)];//Low [Lowest (MODE_LOW ,shift+TTFbars*2-1,TTFbars)];
#        BuyPower =HighestHighRecent-LowestLowOlder;
#        SellPower=HighestHighOlder -LowestLowRecent;
#        TTF=(BuyPower-SellPower)/(0.5*(BuyPower+SellPower))*100;
def BuyPower = highest(high, TTFbars) - lowest(low[TTFbars], TTFbars);
def SellPower = highest(high[TTFbars], TTFbars) - lowest(low, TTFbars);
def TTF_init = ( (BuyPower - SellPower) / (0.5 * (BuyPower + SellPower)) ) * 100;

#  //----

def e1 = w1*TTF_init + w2 * 1;
def e2 = w1*e1 + w2*e2[1];
def e3 = w1*e2 + w2*e3[1];
def e4 = w1*e3 + w2*e4[1];
def e5 = w1*e4 + w2*e5[1];
def e6 = w1*e5 + w2*e6[1];

#  //----
plot TTF = c1*e6 + c2*e5 + c3*e4 + c4*e3;

#        MainBuffer[i]=TTF;
#        i--;
#       }
#  //---- last counted bar will be recounted
#     if(counted_bars>0) counted_bars--;
#     int limit=Bars-counted_bars;
#  //---- signal line is simple movimg average
plot zero = 0;
zero.setDefaultColor(color.gray);
plot signal = if TTF >= 0 then topLine else BottomLine;
signal.SetDefaultColor(color.red);

#       for(i=0; i<limit; i++) 
#       {
#        if (MainBuffer[i]>=0)
#           SignalBuffer[i]=TopLine;
#        else
#           SignalBuffer[i]=BottomLine;
#       }
#  //----
#     return(0);
#    }
#  //+------------------------------------------------------------------+#
 
So,

I'm not sure this is all correct. The habit Tradeview has of being able to redefine variables using the variable itself is rather troublesome to my functional-programming oriented mind. Take it as you want. It plots, and it seems reasonable. The b parameter does alter the sensitivity. The others seem to do what they should as well... I just don't know if it's all the right thing it's doing.

Take this one with a BIG grain of salt.

-mashume

Code:
#  //+------------------------------------------------------------------+
#  //|                                                   ttf.mq4        |
#  //|                      Copyright � 2005, Nick Bilak                |
#  //|                                                                  |
#  //+------------------------------------------------------------------+
#  #property copyright "Copyright � 2005, Nick Bilak"
#  #property link      "beluck[at]gmail.com"
#  //----
#  #property indicator_separate_window
#  #property indicator_buffers 2
#  #property indicator_color1 LightSeaGreen
#  #property indicator_color2 Red
declare lower;

#  //---- input parameters
#  extern int TTFbars=8;
input TTFbars = 8;

#  //15=default number of bars for computation
#  extern int TopLine=75;
input TopLine = 75;

#  extern int BottomLine=-75;
input BottomLine = -75;

#  extern int t3_period=3;
input t3_period = 3;

#  extern double b=0.7;
input b = 0.7;

#  //---- buffers
#  double MainBuffer[];
#  double SignalBuffer[];
#  //----
#  int draw_begin1=0;
#  int draw_begin2=0;
#  double b2=0;
#  double b3=0;
#  double c1=0;
#  double c2=0;
#  double c3=0;
#  double c4=0;
#  double r=0;
#  double w1=0;
#  double w2=0;
#  double e1=0;
#  double e2=0;
#  double e3=0;
#  double e4=0;
#  double e5=0;
#  double e6=0;
#  double  HighestHighRecent=0;
#  double  HighestHighOlder =0;
#  double  LowestLowRecent =0;
#  double  LowestLowOlder =0;
#  double  BuyPower =0;
#  double  SellPower=0;
#  double  TTF=0;
#  //+------------------------------------------------------------------+
#  //| Custom indicator initialization function                         |
#  //+------------------------------------------------------------------+
#  int init()
#    {
#     string short_name;
#  //---- 2 additional buffers are used for counting.
#     IndicatorBuffers(2);
#  //---- indicator lines
#     SetIndexStyle(0,DRAW_LINE);
#     SetIndexBuffer(0, MainBuffer);
#     SetIndexStyle(1,DRAW_LINE);
#     SetIndexBuffer(1, SignalBuffer);
#  //---- name for DataWindow and indicator subwindow label
#     short_name="TTF("+TTFbars;
#     IndicatorShortName(short_name);
#     SetIndexLabel(0,short_name);
#     SetIndexLabel(1,"Signal");
#  //----
#     draw_begin1=TTFbars*2+1;
#     draw_begin2=draw_begin1;
#     SetIndexDrawBegin(0,draw_begin1);
#     SetIndexDrawBegin(1,draw_begin2);
#  //----  
#     b2=b*b;
def b2 = b * b;

#     b3=b2*b;
def b3 = b2 * b;

#     c1=-b3;
def c1 = -b3;

#     c2=(3*(b2+b3));
def c2 = (3*(b2+b3));

#     c3=-3*(2*b2+b+b3);
def c3 = -3*(2*b2+b+b3);

#     c4=(1+3*b+b3+3*b2);
def c4 = (1+3*b+b3+3*b2);

#     r=t3_period;
#     if (r<1) r=1;
#     r=1 + 0.5*(r-1);
def r = if t3_period < 1 then 1 else 1 + 0.5 * (t3_period - 1);

#  //----
#     w1=2/(r + 1);
def w1 = 2 / (r + 1);


#     w2=1 - w1;
def w2 = 1 - w1;

#  //----
#     return(0);
#    }
#  //+------------------------------------------------------------------+
#  //| ttf                                                              |
#  //+------------------------------------------------------------------+
#  int start()
#    {
#     int    i,k;
#     int    counted_bars=IndicatorCounted();
#     double price;
#  //----
#     if(Bars<=draw_begin2) return(0);
#  //---- initial zero
#     if(counted_bars<1)
#       {
#        for(i=1;i<=draw_begin1;i++) MainBuffer[Bars-i]=0;
#        for(i=1;i<=draw_begin2;i++) SignalBuffer[Bars-i]=0;
#       }
#  //---- %K line
#     i=Bars-draw_begin1;
#     if(counted_bars>draw_begin1) i=Bars-counted_bars-1;
#     while(i>=0)
#       {
#        HighestHighRecent=High[Highest(NULL,0,MODE_HIGH,TTFbars,i)];//High[Highest(MODE_HIGH,shift+TTFbars-1,TTFbars)];
#        HighestHighOlder =High[Highest(NULL,0,MODE_HIGH,TTFbars,i+TTFbars)];//High[Highest(MODE_HIGH,shift+TTFbars*2-1,TTFbars)];
#        LowestLowRecent =Low[Lowest(NULL,0,MODE_LOW,TTFbars,i)];//Low [Lowest (MODE_LOW ,shift+TTFbars-1,TTFbars)];
#        LowestLowOlder =Low[Lowest(NULL,0,MODE_LOW,TTFbars,i+TTFbars)];//Low [Lowest (MODE_LOW ,shift+TTFbars*2-1,TTFbars)];
#        BuyPower =HighestHighRecent-LowestLowOlder;
#        SellPower=HighestHighOlder -LowestLowRecent;
#        TTF=(BuyPower-SellPower)/(0.5*(BuyPower+SellPower))*100;
def BuyPower = highest(high, TTFbars) - lowest(low[TTFbars], TTFbars);
def SellPower = highest(high[TTFbars], TTFbars) - lowest(low, TTFbars);
def TTF_init = ( (BuyPower - SellPower) / (0.5 * (BuyPower + SellPower)) ) * 100;

#  //----

def e1 = w1*TTF_init + w2 * 1;
def e2 = w1*e1 + w2*e2[1];
def e3 = w1*e2 + w2*e3[1];
def e4 = w1*e3 + w2*e4[1];
def e5 = w1*e4 + w2*e5[1];
def e6 = w1*e5 + w2*e6[1];

#  //----
plot TTF = c1*e6 + c2*e5 + c3*e4 + c4*e3;

#        MainBuffer[i]=TTF;
#        i--;
#       }
#  //---- last counted bar will be recounted
#     if(counted_bars>0) counted_bars--;
#     int limit=Bars-counted_bars;
#  //---- signal line is simple movimg average
plot zero = 0;
zero.setDefaultColor(color.gray);
plot signal = if TTF >= 0 then topLine else BottomLine;
signal.SetDefaultColor(color.red);

#       for(i=0; i<limit; i++)
#       {
#        if (MainBuffer[i]>=0)
#           SignalBuffer[i]=TopLine;
#        else
#           SignalBuffer[i]=BottomLine;
#       }
#  //----
#     return(0);
#    }
#  //+------------------------------------------------------------------+#
wow @mashume thank you so much I will test it out!
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
471 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top