Two Legged Pullback Indicator For ThinkOrSwim

tradegeek

Active member
2019 Donor
Code:
# "Normal" PullBack Zone
# Attempting to define a typical pullback area using imp_volatility()
# v.01 7.31.18
# Nube

input AcceptableBarsToEntry = 7;
input HeadroomTicks = 1;
input Fixins = no;

def c  = close;
def na = Double.NaN;
def bn = BarNumber();
def tick = TickSize();

def firstThing  = Average(c,20);
def secondThing = Average(c,100);
def upCross   = c > secondThing &&
                firstThing crosses above secondThing;
def upStop    = c crosses below secondThing;
def downCross = c < secondThing &&
                firstThing crosses below secondThing;
def downStop  = c crosses above firstThing;

def iv  = if   !IsNaN(imp_volatility())
          then imp_volatility()
          else iv[1];
def SD1 = c * iv * Sqrt((AcceptableBarsToEntry/(AggregationPeriod.DAY/GetAggregationPeriod())) / 365);
def SD2 = SD1 * 2;

def buyBar = if   upCross
                  then bn
                  else buyBar[1];
def buyStop = if   bn == buyBar
              then c - SD2 - tick * HeadroomTicks
              else if   upStop
                   then na
                   else buyStop[1];
def buyPrice   = if   bn == buyBar
                 then c
                 else buyPrice[1];
def buyRetrace = if   bn == buyBar
                 then c - SD1
                 else if   upStop
                      then na
                      else buyRetrace[1];
def buyRiskOff = if   bn == buyBar
                 then c + SD1
                 else if   upStop
                      then na
                      else buyRiskOff[1];

def sellBar = if   downCross
              then bn
              else sellBar[1];
def sellPrice = if   bn == sellBar
                then c
                else sellPrice[1];
def sellStop = if   bn == sellBar
               then c + SD2 + tick * HeadroomTicks
               else if   downStop
                    then na
                    else sellStop[1];
def sellRetrace = if   bn == sellBar
                  then c + SD1
                  else if   downStop
                       then na
                       else sellRetrace[1];
def sellRiskOff = if   bn == sellBar
                  then c - SD1
                  else if   downStop
                       then na
                       else sellRiskOff[1];

plot
LongEntry;
LongEntry.  SetDefaultColor(CreateColor(100,200,100));
LongEntry.  SetPaintingStrategy(PaintingStrategy.Arrow_Up);
LongEntry = if   bn == buyBar
            then low - tick * HeadroomTicks
            else na;
          
plot
LongRetrace = Round(buyRetrace,2);
LongRetrace.  SetStyle(Curve.Short_Dash);
LongRetrace.  SetDefaultColor(CreateColor(100,200,100));

plot
LongStop = Round(buyStop,2);
LongStop.  SetDefaultColor(CreateColor(200,100,100));
LongStop.  SetStyle(Curve.Firm);

plot
LongRiskOff = Round(buyRiskOff,2);
LongRiskOff.  SetDefaultColor(Color.Pink);
LongRiskOff.  SetStyle(Curve.Medium_Dash);

plot
ShortEntry;
ShortEntry.  SetDefaultColor(CreateColor(200,100,100));
ShortEntry.  SetPaintingStrategy(PaintingStrategy.Arrow_Down);
ShortEntry = if   bn == sellBar
             then high + tick * HeadroomTicks
             else na;
          
plot
ShortRetrace = Round(sellRetrace,2);
ShortRetrace.  SetStyle(Curve.Short_Dash);
ShortRetrace.  SetDefaultColor(CreateColor(200,100,100));

plot
ShortStop = Round(sellStop,2);
ShortStop.  SetDefaultColor(CreateColor(100,200,100));
ShortStop.  SetStyle(Curve.Firm);

plot
ShortRiskOff = Round(sellRiskOff,2);
ShortRiskOff.  SetDefaultColor(CreateColor(155,225,155));
ShortRiskOff.  SetStyle(Curve.Medium_Dash);

AddChartBubble(Fixins && LongEntry, LongStop, "Long Stop \n" +LongStop, Color.Red,0);
AddChartBubble(Fixins && LongEntry, LongRiskOff, "Long Risk Off: " +LongRiskOff+ "\n 1 SD: "+ Round(SD1,2), Color.Pink);
AddChartBubble(Fixins && ShortEntry, ShortStop, "Short Stop \n" +ShortStop, Color.Green);
AddChartBubble(Fixins && ShortEntry, ShortRiskOff, "Short Risk Off: " +ShortRiskOff+ "\n 1 SD: "+ Round(SD1,2), CreateColor(155,225,155), 0);

def LE = if !IsNaN(c) then buyPrice else na;
def SE = if !IsNaN(c) then sellPrice else na;
AddCloud(if (Fixins, LE, na), LongRetrace, CreateColor(155,225,155));
AddCloud(if (Fixins, ShortRetrace, na), SE, Color.Pink);

# f/ "Normal" PullBack Zone
 
Last edited by a moderator:

gus_1973

New member
@tradegeek
I am giving this a try. I was wondering what is the meaning when the lines stretch to the right for many bars, even when the opening position signal is pretty old. This means that the opening is still valid? Thanks for sharing this signal!
 
Last edited by a moderator:

MerryDay

Administrative
Staff member
Staff
VIP
Lifetime
@tradegeek
I am giving this a try. I was wondering what is the meaning when the lines stretch to the right for many bars, even when the opening position signal is pretty old. This means that the opening is still valid? Thanks for sharing this signal!
The answer would be maybe. No indicator can be used in isolation. You would need to use your other indicators to determine if you were still in an applicable trading zone.
 

MerryDay

Administrative
Staff member
Staff
VIP
Lifetime

Similar threads

Top