Site Meter The Lawyer Trader: Thinkscript: The Trend_Fuzz Indicator for Think or Swim

Tuesday, June 18, 2013

Thinkscript: The Trend_Fuzz Indicator for Think or Swim

Here is the thinksript code for one of my favorite custom made indicators.  I've used it quite a bit over the last three or four years..now maybe you can get some use out of it as well. Here's a chart of the SPY daily chart with the indicator:


 Here's the code:

###delete this line when pasting into thing or swim###

input ThermoLookBackBars = 50;
input PlotType = {default AdaptiveMovingAverages, Standard};

def HighLowScore = 1000 * ((high - high[1]) / (high[1]) +
(low - low[1]) / low[1]);

#######ATR TrailingStop Code
input trailType = {default modified, unmodified};
input ATRPeriod = 5;
input ATRFactor = 3.5;
input firstTrade = {default long, short};

def HiLo = Min(high - low, 1.5 * Average(high - low, ATRPeriod));
def HRef = if low <= high[1]
    then high - close[1]
    else (high - close[1]) - 0.5 * (low - high[1]);
def LRef = if high >= low[1]
    then close[1] - low
    else (close[1] - low) - 0.5 * (low[1] - high);
def ATRMod = ExpAverage(Max(HiLo, Max(HRef, LRef)), 2 * ATRPeriod - 1);

def loss;
switch (trailType) {
case modified:
    loss = ATRFactor * ATRMod;
case unmodified:
    loss = ATRFactor * AvgTrueRange(high, close, low, ATRPeriod);
}

rec state = {default init, long, short};
rec trail;
switch (state[1]) {
case init:
    if (!IsNaN(loss)) {
        switch (firstTrade) {
        case long:
            state = state.long;
            trail =  close - loss;
        case short:
            state = state.short;
            trail = close + loss;
    }
    } else {
        state = state.init;
        trail = Double.NaN;
    }
case long:
    if (close > trail[1]) {
        state = state.long;
        trail = Max(trail[1], close - loss);
    }
    else {
        state = state.short;
        trail = close + loss;
    }
case short:
    if (close < trail[1]) {
        state = state.short;
        trail = Min(trail[1], close + loss);
    }
    else {
        state = state.long;
        trail =  close - loss;
    }
}

def BuySignal = Crosses(state == state.long, 0, CrossingDirection.ABOVE);
def SellSignal = Crosses(state == state.short, 0, CrossingDirection.ABOVE);

plot TrailingStop = trail;
TrailingStop.Hide();
####End ATR Trailing Stop Code

def A = Highest(high[1], ThermoLookBackBars);
def B = Lowest(low[1], ThermoLookBackBars);

def FiftyTwoWeekHigh = A;

def FiftyTwoWeekLow = B;

def FiftyTwoWeekScore = 10 * (((high
- FiftyTwoWeekHigh) / FiftyTwoWeekHigh) + 
((low - FiftyTwoWeekLow) / FiftyTwoWeekLow));

def ThermoScore = ExpAverage(HighLowScore + FiftyTwoWeekScore, ThermoLookBackBars);

input FastLengthShort = 5;
input SlowLengthShort = 15;
input EffRatioShort = 10;
input FastLengthLong = 10;
input SlowLengthLong = 25;
input EffRatioLong = 5;

def AMA = MovAvgAdaptive(ThermoScore, FastLengthShort, SlowLengthShort, EffRatioShort);
def AMA2 = MovAvgAdaptive(ThermoScore, FastLengthLong, SlowLengthLong, EffRatioLong);

plot Line1;
Line1.Hide();
plot Line2;
Line2.Hide();

switch (PlotType) {
case AdaptiveMovingAverages:
    Line1 = AMA;
    Line2 = AMA2;
case Standard:
    Line1 = ThermoScore;
    Line2 = ThermoScore;
}

def InvisibleLine = close * 0;
plot Line3 = InvisibleLine;
Line3.Hide();

def Buy = Line1 > 0 and Line2 < 0 and state == state.long;
def StrongBuy = Line1 > 0 and Line2 >= 0 and state == state.long;
def Sell = Line1 < 0 and Line2 > 0 and state == state.short;
def StrongSell = Line1 < 0 and Line2 <= 0 and state == state.short;


AssignPriceColor(if Buy then Color.DARK_GREEN else if StrongBuy then Color.GREEN else if Sell then Color.DARK_RED else if StrongSell then Color.RED else Color.BLUE);

AddLabel(yes, Concat("Current Reading is ", (if Buy then "Up Trend" else if StrongBuy then "Strong Up Trend" else if Sell then "Down Trend" else if StrongSell then "Strong Down Trend" else "Neutral")),  if Buy then Color.DARK_GREEN else if StrongBuy then Color.GREEN else if Sell then Color.DARK_RED else if StrongSell then Color.RED else Color.GRAY);

#######Stochastic Code

declare upper;

input over_bought = 80;
input over_sold = 20;
input KPeriod = 10;
input DPeriod = 10;
input priceH = high;
input priceL = low;
input priceC = close;
input slowing_period = 3;
input smoothingType = {default SMA, EMA};

def lowest_k = Lowest(priceL, KPeriod);
def c1 = priceC - lowest_k;
def c2 = Highest(priceH, KPeriod) - lowest_k;
def FastK = if c2 != 0 then c1 / c2 * 100 else 0;

def FullK;
def FullD;

switch (smoothingType) {
case SMA:
    FullK = Average(FastK, slowing_period);
    FullD = Average(FullK, DPeriod);
case EMA:
    FullK = ExpAverage(FastK, slowing_period);
    FullD = ExpAverage(FullK, DPeriod);
}

def pricefilterup = if close > close[50] then 1 else 0;
def pricefilterdown = if close < close [50] then 1 else 0;

def OverBoughtAdd = if FullK < over_bought and FullK[1] >= over_bought then 1 else 0;
def OverSoldAdd = if FullK > over_sold and FullK[1] <= over_sold then 1 else 0;

def na = Double.NaN;

#Plot arrows
plot up = if StrongBuy and OverSoldAdd  and pricefilterup then low - (3 * TickSize()) else na;
plot down = if StrongSell and OverBoughtAdd and pricefilterdown then high + (3 * TickSize()) else na;
up.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
down.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);

###delete this line when pasting into think or swim###

Take care,

TLT

6 comments:

Neil Shaffer said...

Hi,

I hope this message finds you well,

My name is Neil; I'm the affiliate manager @ BinaryOffers.com
I saw that you are promoting few interesting offers,
So I'd like to take this opportunity and invite you to join us.

We offer an Extremely high commissions and the best service online.

I'll be more than happy to start working with you and introduce to you few of our offers
You can send me an email or add me to your Skype (aff.binaryoffers) so we can discuss

All the best my friend,
Neil
Neil@binaryoffers.org

Brett said...

Thank you TLT. This looks like a fantastic indicator. I'll dig in some more. If you have other suggestions, feel free to continue sharing, as it appears it has been a while for updates. I'm just a geeky trader looking for great tools, and yours looks like a nifty one. Thanks again for sharing, and if I can contribute or glean any more, please let me know where best to keep up on your most recent updates (here or elsewhere). Have a super weekend!

Anonymous said...

Thank you for sharing this indicator. It is an excellent indicator of which works well on multiple charts. I use it in combination with a 1 minute chart and it provides a good idea of the trend. By trading the first pullback in a new trend and utilizing a 2:1/ 3:1 risk-reward ratio, this gives above 50% accuracy. Thanks again!

Josiah said...

Thanks for sharing, TLT, great little thinkscript. I'll have to plug it in and see how it behaves.

Anonymous said...

Thank you very much George. I also am using ToS and appreciate the code. With other indicators you have posted a brief description of how they work and what they are for. this post doesn't have that information. Can you please fill us in on what the indicator is trying to accomplish? How it is using the ATR? etc.


THANKS!

George said...

Anonymous,

This is basically a trend following indicator. It's just accomplishing the classification of a trend, bull/bear, strong bull, strong bear, etc. The atr is used in a trailing atr, which is part of what the indicator uses to determine an uptrend vs. downtrend. It works well with some instruments in some time frames for an indication of buying or selling..