Swing High/Low Indicator for ThinkorSwim

DudeDastic

New member
Hi all,

I'm trying to get pivot lines to display for a Williams Fractal indicator. I'd like the lines to repaint, that is only show the most recent swings only. Here's the code I'm using (came up in a internet search):

Code:
# Bill Williams Fractal Template
# Coded By: Rigel May 2018

#Define "n" as the number of periods and keep a minimum value of 2 for error handling.
input n=2;

# Williams Fractals are a 5 point lagging indicator that will draw 2 candles behind.
# The purpose of the indicator is to plot points of trend reversals.
# Often these are paired with trailing stop indicators such as Parabolic SAR, Volatility Stop, and SuperTrend.

# Down pointing fractals occur over candles when:
#   High(n-2) < High(n)
#   High(n-1) < High(n)
#   High(n + 1) < High(n)
#   High(n + 2) < High(n)
#dnFractal = (high[n-2] < high[n]) and (high[n-1] < high[n]) and (high[n+1] < high[n]) and (high[n+2] < high[n])

def isupfractal = if low < low[1] and low < low[2] and low < low[-1] and low < low[-2] then low else double.nan;
# Up pointing fractals occur under candles when:
#   Low(n-2) > Low(n)
#   Low(n-1) > Low(n)
#   Low(n + 1) > Low(n)
#   Low(n + 2) > Low(n)
#upFractal = (low[n-2] > low[n]) and (low[n-1] > low[n]) and (low[n+1] > low[n]) and (low[n+2] > low[n])
def isdownfractal = if high > high[1] and high > high[2] and high > high[-1] and high > high[-2] then high else double.nan;
# Plot the fractals as shapes on the chart.


plot upfractal = if( isupfractal, isupfractal+ (1 * tickSize()) , double.nan);
upfractal.SetPaintingStrategy(paintingStrategy.ARROW_UP);
plot downfractal = if( isdownfractal, isdownfractal - (1 * tickSize()), double.nan);
downfractal.SetPaintingStrategy(paintingStrategy.ARROW_DOWN);
plot Data = close;

I tried setting the PaintingStrategy to LINE or similar type but no success. what am I doing wrong?
 

MerryDay

Administrative
Staff member
Staff
VIP
@ext99k yes, it repaints.
If you look at a script and if there is a fold operation using highest and/or lowest that is the indication that it repaints.
Fold is a recursive operation that keeps redefining the highest and lowest and redrawing them.
 

David45

Member
2019 Donor
In reference to the code in post #21, is there a way to paint the candlesticks RED when the swingHigh signal appears and continue to paint all subsequent candlesticks RED until a swingLow signal appears? At which point the candlesticks would paint GREEN and continue to paint all subsequent candlesticks GREEN until a swingLow signal appears?

Thanks very much for any assistance with this!
 

SleepyZ

Well-known member
In reference to the code in post #21, is there a way to paint the candlesticks RED when the swingHigh signal appears and continue to paint all subsequent candlesticks RED until a swingLow signal appears? At which point the candlesticks would paint GREEN and continue to paint all subsequent candlesticks GREEN until a swingLow signal appears?

Thanks very much for any assistance with this!

As the code in post #21 is not producing a zigzag pattern (for example, there can be multiple swinglows after a swinghigh and vice versa), the coloring using that code may not be what you are expecting.

The following code can be added to the bottom of the code in post #21.

Ruby:
def slow=if lowarrow then 1 else if slow[1]>=1 and !higharrow then 1 else 0;
assignpriceColor(if slow==1 then color.green else color.red);
 

maka123

New member
VIP
Hi All

i am currently trying to create a strategy that when price cross my prior swing low resistance line, it will trigger a buy order.
Below is the script that identify only last swing low resistance line. I need create a strategy, that able to trigger buy order on all the prior swing low resistance line in the chart when price cross it.

# SWING LOW
# define swing low points
input length = 10;
def bn = BarNumber();
def lastBar = HighestAll(if IsNaN(close) then 0 else bn);
def offset = Min(length - 1, lastBar - bn);
def swingLow = low < Lowest(low[1], length - 1) and low == GetValue(Lowest(low, length), -offset);


# identify the very last swing low point
def lowPointOneBarNumber = HighestAll(if swingLow then bn else 0);
#def lowPointOneValue = if bn == lowPointOneBarNumber then low else lowPointOneValue[1];
#plot low1 = if bn < lowPointOneBarNumber then Double.NaN else lowPointOneValue;
#low1.SetDefaultColor(Color.LIGHT_RED);


# identify the 2nd to last swing low point
def lowPointTwoBarNumber = HighestAll(if swingLow and bn < lowPointOneBarNumber then bn else 0);
def lowPointTwoValue = if bn == lowPointTwoBarNumber then low else lowPointTwoValue[1];
plot low2 = if bn < lowPointTwoBarNumber then Double.NaN else lowPointTwoValue;
low2.SetPaintingStrategy(PaintingStrategy.DASHES);
low2.SetDefaultColor(Color.black);
low2.setlineWeight(1);
 

Similar threads

Top