Site Meter The Lawyer Trader: TrendFuzz
Showing posts with label TrendFuzz. Show all posts
Showing posts with label TrendFuzz. Show all posts

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

Friday, September 7, 2012

Do More of What Works...

I've been reading Jack Schwager's Hedge Fund Market Wizards, which is a phenomenal book and is just as good as his first two and I highly recommend it to anyone who trades or is interested in trading.  One of the hedge fund managers in the book gives the advice of "find out what works and do more of that" or something to that effect.  Although simplistic, there is a lot to this.  One thing that I've been doing more of lately is active currency trading.  Why?  Simply because my account records show that it works for me.

That is why I want to talk about how smooth the currency market and in particular the Eur/Usd currency pair has been lately for short-term trend and swing trading.  When trading currencies, you must have a routine and specific set ups that you look for and then trade them.  And when those set ups are working well you have to trade them more.  Right now, the opening range breakout trade has been excellent.  The past two days have provided great opportunities to make significant returns with only trading this set up.

I have addressed the opening range break out trade in several posts, but I'll quickly explain what it is for those that are unfamiliar with it.  Basically, you take the first 15 minutes of trading (or it can be 5 minutes or 30..different people use different times) and you note the high and low of the period.  That creates the range. Then you patiently wait for the price action to trade out of the range and trade in that direction.  Generally, I'll day trade with the 5 minute and 1 minute charts.  I'll first look to the hourly and daily charts to determine the bias or the primary trend and then look to for break outs in that direction to trade.  So for the past few days, the bias has been to the higher side...that tells me the path of least resistance is higher and I should look for long trades.

Then I wait for the break out and put the trade on immediately after a 5 minute bar closes outside of the range.  After that I trail a stop with the 1 minute chart and look to book partial profits into momentum and use the trailing stop on the remainder of the position.  If the currency trades back into the range or to the bottom of the range you get out..or have a stop sitting there.  With currencies, it is a little difficult to determine the open.  I will generally trade the 8:30 stock market open with the Eur/Usd since it is pretty correlated with the US stock market.  After I've exited a profitable trade, I look for pullbacks and the support to initiate a follow up trade back to the highs.  After this, I'll look for the highs to be taken out and then trade that break out.  Then I'll look for another pullback from the new high and once it finds support I'll put on another trade.  That's it..plain and simple.  If I'm seeing follow through and the trade is working, I'll keep doing it over and over and over.

Here's a 5 minute chart with today's Eur/Usd with an opening range plot on the chart and some of the trades that I made today.
The opening range is the yellow shaded region.  The bars are all green on this chart because my trendfuzz indicator was in Strong Up Trend mode all day long...not a common event.  Notice that I did not take the OR break below the opening range, as this was a low probability trade given that the trendfuzz was green and the overall bias was up...that's not to say that it always turns out this way, just that today was a perfect day not to take the short trade.  Other days I might take the short trade even with a prevailing bias to the upside, I would just trade a smaller position and book profits very quickly.  Today was a little different because yesterday was incredibly strong and my personal sentiment is very bullish.

This type of trading can be very profitable and very rewarding, you just have to maintain discipline to wait for the proper entries and cut losses quickly.  It is by no means easy and often is very frustrating which is why some people are not suited for day trading.  Over the years, I've gone through periods where I thought I was good at it and was very suited for it and other times where I questioned whether it was for me.  The thing that I've noticed is that day trading equities is a little more hit or miss with me but currencies have been profitable for several years..I just have to stick with it and trade through those incredibly difficult draw downs that have a tendency to leave traders gun shy right at the moment that the next winning trade needs to be put on.  Ok, that's enough rambling for one day.  Hope everyone had a great week and has a great weekend.

TLT

Wednesday, November 2, 2011

Market Recap Video


Trying something new...playing around with the screen recorders..maybe gonna start recording some trading examples and market commentary.  Here's a video where I talk about today's trading in the Eur/Usd.  There's a couple of opening range breakout setups and I walk you through what types of things that I'm looking at when I put on these trades.  Volume is not the best, so turn it up to hear...I'll do better with that next time.

In the video, I mention that the Adaptive Opening Range Indicator was from Thinkscripter, however, I misspoke.  It is actually an indicator that was programmed by ReadTheProspectus and is available here.  If you're not familiar with the site, you should spend some time with it and consider donating to get access to the "Donors Only" codes.  I don't know the administrator of that site and gain nothing from this recommendation...just think it's a useful website and a good blog.

TLT

Thursday, June 2, 2011

Fresh Strong Buy Signal on the EUR/USD and some Think Script Code

I'm long the Eur/Usd...my TrendFuzz system just switched from Buy to Strong Buy. The pair may make a run for it's recent highs, which would make for a great trade.  Here's the chart:

Note the bars changed from dark green to bright green indicating a "strong buy" signal.  This combined with the Trend Confirm Dots (a separate trend following system) makes the pair look bullish right now.

A quick explanation of the TrendFuzz system is in order and I'll briefly describe it since I've been referencing it for a little while now.  TrendFuzz, is a trend following system that combines a market thermometer indicator or thermo with a volatility break out system.  The thermo measures an instrument's ability to make higher highs or lower lows (similar to what A. Elder described in Come Into My Trading Room) and it combines this score with another score based on current price in relation to a couple of past prices..like prices from 20 and 50 bars ago.  So, one part of the trendfuzz indicator is based on an increased/decreased average of thermo scores and the other part is just a volatility break out confirmation...the instrument needs to move 2x its historic volatility kind of thing.

This indicator is great for catching trends and does a decent job of minimizing risk.  The volatility filter helps filter out moves that are likely to whip saw...although not all of the moves get filtered out.  The "fuzz" in trend fuzz alludes to "fuzzy logic." This is because it is an adaptive system that can change its criteria for readings based on recent trading activity.  This means that an uptrend reading from today can have different criteria than an uptrend from a month ago...hence a fuzzy definition. 

I'm not going to share the code or the internals of the full system, but, I will share the basic code for the Thermo...which is what a good part of the system revolves around.  Some of the programming might be a little redundant and amateurish because, well, I'm not a professional programmer...just a self taught market junky that knows enough to get by.  The code used in the system is similar to this one, but it has been tweeked quite a bit.  This Thermo indicator is still rather interesting and can be useful to anyone interested in a new indicator. It uses a 52 Week High/Low score verbiage but its not really referencing the 52 week high/low..that is leftover from the original indicator.  Now can be set to look back at any bar you want..like 10 bars, 20 bars or even 100 bars back, just make sure the aggregation period on the chart is the same selected for the indicator.  Here's the code for thinkorswim:
####################################################
declare lower;

input ThermoAGPeriod = {default MONTH, MIN, HOUR, DAY, WEEK};
input ThermoLookBackBars = 12;
input PlotType = {default ExpMovingAverages, Standard};

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

def A = highest(high(period = ThermoAGPeriod));
def B = lowest(low(period = ThermoAGPeriod));

def FiftyTwoWeekHigh = A[ThermoLookBackBars];

def FiftyTwoWeekLow = B[ThermoLookBackBars];

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

def ThermoScore = HighLowScore + FiftyTwoWeekScore;

input MAPeriodShort = 9;
input MAPeriodLong = 50;

def EMA = ExpAverage(ThermoScore, MAPeriodShort);
def EMA2 = ExpAverage (ThermoScore, MAPeriodLong);

plot Line1;
Plot Line2;

Switch (PlotType) {
case ExpMovingAverages:
    Line1 = EMA;
    Line2 = EMA2;
case Standard:
    Line1 = ThermoScore;
    Line2 = ThermoScore;
}

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


addcloud(Line1, Line2, Color.CYAN, color.Dark_RED);
addcloud(Line2, Line3, Color.Dark_Gray, Color.Magenta);     

#####################################################

Enjoy and let me know if you have any questions.

TLT