TTM with SuperTrend

rvaidyamath

New member
Hello there

It has been wonderful learning here and learnt so much and thought of contributing some; few of these are taken from here and combined with my own and have done some pretty decent i can say.... well feedback and critics are always welcome...

I need some help here as you see there are multiple indicators to make a trade condition on ; need help on adding an input dup trade yes/no; if its yes... we can take it as signals come ; if it's no... we can only have one position at a time. I tried with lookback period, etc. and struggled pretty much... thought of asking all the experts here...

#Begin of Code

#TTM with SuperTrend ; use this for individual education purpose
#
#DATA Area Begins
def o = open;
def h = high;
def l = low;
def c = close;

#AddChartBubble(YES , low, "CLOSE: " + C , Color.WHITE, yes);
input averageType = AverageType.EXPONENTIAL;
#def is_Doji = IsDoji() or hammer();
input slowEmaPeriod = 20;

input Light_Blue_BAR = 2;
input DarK_Blue_BAR = -1;
input Red_BAR = -2 ;
input Yellow_BAR = +1;

input Green_Dot = 2;
input Red_Dot = -2;
input Light_Red_dot = -1;
input Light_Green_dot = 1;
#Super Trend

input AtrMult = 0.5;
#input nATR = 14;
input nATR = 10;
input AvgType = AverageType.eXPOnential;
input PaintBars = yes;

def ATR = ATR("length" = nATR, "average type" = AvgType);
def UP_Band_Basic = HL2 + (AtrMult * ATR);

def LW_Band_Basic = HL2 + (-AtrMult * ATR);

def UP_Band = if ((UP_Band_Basic < UP_Band[1]) or (close[1] > UP_Band[1])) then UP_Band_Basic else UP_Band[1];

def LW_Band = if ((LW_Band_Basic > LW_Band[1]) or (close[1] < LW_Band[1])) then LW_Band_Basic else LW_Band[1];

def ST = if ((ST[1] == UP_Band[1]) and (close < UP_Band)) then UP_Band
else if ((ST[1] == UP_Band[1]) and (close > Up_Band)) then LW_Band
else if ((ST[1] == LW_Band[1]) and (close > LW_Band)) then LW_Band
else if ((ST[1] == LW_Band) and (close < LW_Band)) then UP_Band
else LW_Band;

plot SuperTrend = ST;

#AddChartBubble(YES , low, "ST: " + ST , Color.WHITE, yes);
SuperTrend.AssignValueColor(if close < ST then Color.RED else Color.GREEN);
AssignPriceColor(if PaintBars and close < ST

then Color.RED

else if PaintBars and close > ST

then Color.GREEN

else Color.CURRENT);

plot ST_Line = Supertrend ;
ST_Line.SetPaintingStrategy(PaintingStrategy.line);
ST_Line.SetDefaultColor(Color.cyan);
ST_Line.SetLineWeight(4);

def ST_Up = if c > ST_Line then 1 else 0 ;
def ST_Dn = if c < ST_Line then 1 else 0 ;

DEF ST_CHANGE = IF (ST_Up[1] == 1 AND ST_Dn == 1) or
(ST_Dn[1] == 1 AND ST_Up == 1)
THEN 1 ELSE 0;

#AddChartBubble(ST_Up , low, "STuP: " + ST_Line , Color.WHITE, yes);
#AddChartBubble(ST_Dn , low, "STdN: " + ST_Line , Color.PINK, yes);

#TTM Squeeze
input nBB = 2.0;
#def slowEmaPeriod = 20.0;
input nK_High = 1.0;
input nK_Mid = 1.5;
input nK_Low = 2.0;
#def price = close;

def momentum = TTM_Squeeze(price = close, length = slowEmaPeriod, nk = nK_Mid, nbb = nBB)."Histogram";
def oscillator = momentum;
def BolKelDelta_Mid = reference BollingerBands("num_dev_up" = nBB, "length" = slowEmaPeriod )."upperband" - KeltnerChannels("factor" = nK_Mid, "length" = slowEmaPeriod)."Upper_Band";
def BolKelDelta_Low = reference BollingerBands("num_dev_up" = nBB, "length" = slowEmaPeriod )."upperband" - KeltnerChannels("factor" = nK_Low, "length" = slowEmaPeriod)."Upper_Band";
def BolKelDelta_High = reference BollingerBands("num_dev_up" = nBB, "length" = slowEmaPeriod )."upperband" - KeltnerChannels("factor" = nK_High, "length" = slowEmaPeriod)."Upper_Band";
#oscillator.DefineColor("Up", CreateColor(0, 255, 255));
#oscillator.DefineColor("UpDecreasing", CreateColor(0, 0, 255));
#oscillator.DefineColor("Down", CreateColor(255, 0, 0));
#oscillator.DefineColor("DownDecreasing", CreateColor(255, 255, 0));
#oscillator.AssignValueColor(
#if oscillator[1] < oscillator then if oscillator[0] >= 0
#then oscillator.Color("Up")
#else oscillator.Color("DownDecreasing")
#else if oscillator >= 0
#then oscillator.Color("UpDecreasing")
#else oscillator.Color("Down") );
#oscillator.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
#oscillator.SetLineWeight(5);

def squeeze = If(IsNaN(close), Double.NaN, 0);
#squeeze.SetPaintingStrategy(PaintingStrategy.POINTS);
#squeeze.SetLineWeight(3);
#squeeze.DefineColor("NoSqueeze", Color.GREEN);
#squeeze.DefineColor("SqueezeLow", Color.black);
#squeeze.DefineColor("SqueezeMid", Color.RED);
#squeeze.DefineColor("SqueezeHigh", Color.orange);
#squeeze.AssignValueColor(
#if BolKelDelta_High <= 0 then squeeze.Color("SqueezeHigh") else
#if BolKelDelta_Mid <= 0 then squeeze.Color("SqueezeMid") else
#if BolKelDelta_Low <= 0 then squeeze.Color("SqueezeLow") else squeeze.color("noSqueeze"));

def squeezeval = if BolKelDelta_High <= 0 then 1 else
if BolKelDelta_Mid <= 0 then 2 else
if BolKelDelta_Low <= 0 then 3 else 0;
AddLabel(yes , "TTM squeeze value : " + squeezeval, Color.YELLOW);
#AddChartBubble(yes , low, "T: " + squeezeval, Color.CYAN, yes);


AddLabel(yes , "Oscillator : " + AsText(oscillator), Color.white);
#AddChartBubble(yes , low, "Oscillator: " + oscillator, Color.CYAN, yes);
#input averageType = AverageType.EXPONENTIAL;

def Aoscillator = MovingAverage(averageType, oscillator , 9);
#Aoscillator.SetDefaultColor(GlobalColor("Green"));
AddLabel(yes , "Aoscillator : " + AsText(Aoscillator), Color.WHITE);
#AddChartBubble(yes , low, "Aoscillator: " + Aoscillator, Color.CYAN, yes);

def osc_rnge_up = if oscillator > Aoscillator then 1 else 0 ;
def osc_rnge_dn = if oscillator < Aoscillator then 1 else 0 ;

#AddChartBubble(osc_rnge_up , low, "OXUp " + close, Color.GREEN, yes);
#AddChartBubble(osc_rnge_dn , low, "OXDn: " + close , Color.RED, yes);

#DEF SqALERT = TTm_SQUEEZE().sQUEEZEALERT ;
def SqALERT = if BolKelDelta_High <= 0 then 3 else if BolKelDelta_Mid <= 0 then 2 else
if BolKelDelta_Low <= 0 then 1 else 0;

AddLabel(yes , "SueezeAlert : " + AsText(SqALERT), Color.WHITE);
#AddChartBubble(SqALERT == 3 , low, "SqALERT: " + SqALERT, Color.red, yes);
#AddChartBubble(SqALERT == 2 , low, "SqALERT: " + SqALERT, Color.Light_Red, yes);
#AddChartBubble(SqALERT == 1 , low, "SqALERT: " + SqALERT, Color.LIGHT_GREEN, yes);
#AddChartBubble(SqALERT == 0 , low, "SqALERT: " + SqALERT, Color.green, yes);
#
################# BAR STRENGTH
DEF BAR_TYPE = if oscillator[1] < oscillator then if oscillator[0] >= 0
then Light_Blue_BAR
else Yellow_BAR
else if oscillator >= 0
then DarK_Blue_BAR
else Red_BAR ;

#AddChartBubble(yes , low, "BAR_TYPE: " + BAR_TYPE, Color.white, yes);

DEF bAR_DOT = IF SqALERT == 1 THEN light_RED_DOT ELSE IF SqALERT == 2 THEN Light_Green_dot ELSE IF SqALERT == 3 THEN RED_DOT ELSE Green_Dot;
#AddChartBubble(yes , low, "bAR_DOT: " + bAR_DOT, Color.white, yes);
def BAR_STRENGTH = BAR_TYPE + bAR_DOT ;
#AddChartBubble(yes , low, "BAR_STRENGTH: " + BAR_STRENGTH, Color.white, yes);
#AddChartBubble(yes , high, "" + BAR_STRENGTH, Color.cyan, yes);

def Abs = MovingAverage(averageType, BAR_STRENGTH, 9) ;
#AddChartBubble(yes , low, "" + Abs, Color.white, yes);

DEF B1 = IF BAR_STRENGTH crosses above Abs and ST_Up == 1 THEN 1 ELSE 0 ;
DEF S1 = IF BAR_STRENGTH crosses BELOW Abs and ST_Dn == 1 THEN 1 ELSE 0 ;
#AddChartBubble(BAR_STRENGTH crosses above Abs and ST_Up , low, "B1" , Color.green, yes);
#AddChartBubble(BAR_STRENGTH crosses below Abs and ST_Dn , low, "S1" , Color.Red, yes);

DEF B2 = IF BAR_STRENGTH crosses above 0 and ST_Up == 1 THEN 1 ELSE 0 ;
DEF S2 = IF BAR_STRENGTH crosses BELOW 0 and ST_Dn == 1 THEN 1 ELSE 0 ;
#AddChartBubble(BAR_STRENGTH crosses above 0 and ST_Up , low, "B2" , Color.light_green, yes);
#AddChartBubble(BAR_STRENGTH crosses below 0 and ST_Dn , low, "S2" , Color.light_Red, yes);

DEF B3 = IF Abs crosses above 0 and ST_Up == 1 THEN 1 ELSE 0 ;
DEF S3 = IF Abs crosses below 0 and ST_Dn == 1 THEN 1 ELSE 0 ;
#AddChartBubble(Abs crosses above 0 and ST_Up , low, "B3" , Color.orange, yes);
#AddChartBubble(Abs crosses below 0 and ST_Dn , low, "S3" , Color.light_orange, yes);

DEF B4 = IF c > ST_Line THEN 1 ELSE 0 ;
DEF S4 = IF c < ST_Line THEN 1 ELSE 0 ;
#AddChartBubble(Abs crosses above 0 and ST_Up , low, "B3" , Color.orange, yes);
#AddChartBubble(Abs crosses below 0 and ST_Dn , low, "S3" , Color.light_orange, yes);

DEF B5 = IF oscillator > 0 and ST_Up == 1 THEN 1 ELSE 0 ;
DEF S5 = IF oscillator < 0 and ST_Dn == 1 THEN 1 ELSE 0 ;
#AddChartBubble(Abs crosses above 0 and ST_Up , low, "B3" , Color.orange, yes);
#AddChartBubble(Abs crosses below 0 and ST_Dn , low, "S3" , Color.light_orange, yes);

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

def BuyHA_cond1 = if ( B1 == 1 OR B2 == 1 OR B3 == 1 OR B4 == 1 OR B5 == 1) AND SqALERT == 0 AND ST_UP
then 1 else 0;
def BuyHA_cond2 = if SqALERT == 0 then 1 else 0;

def BuyHA_cond = if BuyHA_cond1 == 1 and BuyHA_cond2 == 1 then 1 else 0;
###############
def SellHA_cond1 = if ( S1 == 1 OR S2 == 1 OR S3 == 1 OR S4 == 1 OR S5 == 1) AND SqALERT == 0 AND ST_DN
then 1 else 0;
def SellHA_cond2 = if SqALERT == 0 then 1 else 0;

def SellHA_cond = if SellHA_cond1 == 1 and SellHA_cond2 then 1 else 0;


def green = if BuyHA_cond == 1 and BuyHA_cond[1] != 1 then 1 else 0 ;
def red = if SellHA_cond == 1 and SellHA_cond[1] != 1 then 1 else 0 ;
#Buy plot
plot BuyHA = if green == 1
then low else Double.NaN;
BuyHA.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
BuyHA.SetLineWeight(5);
BuyHA.SetDefaultColor(Color.green);
#Alert( BuyHA, " ", Alert.BAR, Sound.Chimes);
AddChartBubble( BuyHA , low , "OL: " + close[0], Color.GREEN, yes);

#Sell plot
plot SellHA = if red == 1
then low else Double.NaN;
SellHA.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
SellHA.SetLineWeight(5);
SellHA.SetDefaultColor(Color.red);
#Alert( SellHA, " ", Alert.BAR, Sound.Chimes);
AddChartBubble( SellHA , high , "OS: " + close[0], Color.RED, yes);


#ShortSell - CLOSE plot
PLOT SellHA_Close = if BuyHA == 1 or b4 == 1 or st_up == 1 or ST_CHANGE == 1
then low else Double.NaN;
SellHA_Close.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
SellHA_Close.SetLineWeight(5);
SellHA_Close.SetDefaultColor(Color.Light_Green);
#Alert( SellHA, " ", Alert.BAR, Sound.Chimes);
AddChartBubble( SellHA_Close, high , "cS: " + close[0], Color.GREEN, yes);


#LongBuy - Close plot
PLOT BuyHA_Close = if SellHA == 1 or S4 == 1 or st_dn == 1 or ST_CHANGE == 1
then low else Double.NaN;
BuyHA_Close.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
BuyHA_Close.SetLineWeight(5);
BuyHA_Close.SetDefaultColor(Color.Light_red);
#Alert( BuyHA, " ", Alert.BAR, Sound.Chimes);
AddChartBubble( BuyHA_Close , low , "cL: " + close[0], Color.MAGENTA, yes);

# End of code
 

Attachments

  • ST_TTMSqueeze.jpg
    ST_TTMSqueeze.jpg
    131.3 KB · Views: 253

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
419 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