# Programmatic Support and Resistance for Thinkorswim

#### joshua74133

##### Member
VIP
im wanting it to ding and change the background color when the price touches the topline. my brain is numb trying to figure it out

Code:
``````input magnitude = 2;
input showValues = yes;
def top = high >= Highest(high[1], magnitude) and high >= Highest(high[-magnitude], magnitude);
def topvalue = if BarNumber() < magnitude then Double.NaN else if top    then high else topvalue[1];
plot topline = topvalue;
def countp = if IsNaN(top) and !IsNaN(top[1]) then 1 else countp[1] + 1;
plot peaktop = if IsNaN(top) then GetValue(topline, countp) else Double.NaN;
def bottom = low <= Lowest(low[1], magnitude) and low <= Lowest(low[-magnitude], magnitude);
def bottomValue = if BarNumber() < magnitude then Double.NaN else if bottom then low else bottomValue[1];
plot bottomline = bottomValue;

topline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
topline.SetDefaultColor(Color.RED);
peaktop.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
peaktop.SetDefaultColor(Color.RED);
bottomline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
bottomline.SetDefaultColor(Color.GREEN);``````

Last edited by a moderator:

#### BenTen

Staff member
Staff
VIP

Code:
``````def condition = close crosses above Topline;
AssignPriceColor(if condition then color.green else color.white);

#### joshua74133

##### Member
VIP
@BenTen i tried to add another condition but IF is in red.

Code:
``````def condition = if open is equal to topline and RSI > over_Bought then
AssignPriceColor(if condition then Color.GREEN else Color.CURRENT);

here is the whole script

Code:
``````declare lower;

input length = 7;
input over_Bought = 50;
input over_Sold = 30;
input price = close;
input averageType = AverageType.WILDERS;
input showBreakoutSignals = no;

def NetChgAvg = MovingAverage(averageType, price - price[1], length);
def TotChgAvg = MovingAverage(averageType, AbsValue(price - price[1]), length);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;

plot RSI = 50 * (ChgRatio + 1);
plot OverSold = over_Sold;
plot OverBought = over_Bought;
plot UpSignal = if RSI crosses above OverSold then OverSold else Double.NaN;
plot DownSignal = if RSI crosses below OverBought then OverBought else Double.NaN;

UpSignal.SetHiding(!showBreakoutSignals);
DownSignal.SetHiding(!showBreakoutSignals);

RSI.DefineColor("OverBought", GetColor(5));
RSI.DefineColor("Normal", GetColor(7));
RSI.DefineColor("OverSold", GetColor(1));
RSI.AssignValueColor(if RSI > over_Bought then RSI.Color("OverBought") else if RSI < over_Sold then RSI.Color("OverSold") else RSI.Color("Normal"));
OverSold.SetDefaultColor(GetColor(8));
OverBought.SetDefaultColor(GetColor(8));
UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
DownSignal.SetDefaultColor(Color.DOWNTICK);
DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);

input magnitude = 2;
input showValues = yes;
def top = high >= Highest(high[1], magnitude) and high >= Highest(high[-magnitude], magnitude);
def topvalue = if BarNumber() < magnitude then Double.NaN else if top    then high else topvalue[1];
plot topline = topvalue;
def countp = if IsNaN(top) and !IsNaN(top[1]) then 1 else countp[1] + 1;
plot peaktop = if IsNaN(top) then GetValue(topline, countp) else Double.NaN;
def bottom = low <= Lowest(low[1], magnitude) and low <= Lowest(low[-magnitude], magnitude);
def bottomValue = if BarNumber() < magnitude then Double.NaN else if bottom then low else bottomValue[1];
plot bottomline = bottomValue;

topline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
topline.SetDefaultColor(Color.RED);
peaktop.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
peaktop.SetDefaultColor(Color.RED);
bottomline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
bottomline.SetDefaultColor(Color.GREEN);

def condition = if open is equal to topline and RSI > over_Bought then
AssignPriceColor(if condition then Color.GREEN else Color.CURRENT);

#### joshua74133

##### Member
VIP
went a different route with the script
AssignBackgroundColor( if open is equal to topline and RSI > over_Bought
then Color. blue
else Color.CURRENT);

#### Wave

##### New member
VIP
Is it possible to convert the following indicator to Tradingview indicator? This indicator is support/resistance lines from this post.

Last edited by a moderator:

VIP

#### BenTen

Staff member
Staff
VIP
@Wave When it comes to support and resistance levels, I draw them myself. I'm not sure which one to recommend. Plus, I don't use TradingView.

#### Bentley

##### New member
VIP

Code:
``````def condition = close crosses above Topline;
AssignPriceColor(if condition then color.green else color.white);
Hi Ben,
Was just wondering how this Topline come about...
Cannot find it in both the script or TOS learning center...
Was looking to turn it into a scan...
Like when price cross the topline or bottomline?

Thanks

#### BenTen

Staff member
Staff
VIP
@Bentley It's part of the original code posted on the first page of this thread.

#### Emp1011

##### New member
Can you convert Stockchart "Price labels" to TOS?

#### BenTen

Staff member
Staff
VIP
@Emp1011 Use the script below.

Note: if you want to change the distance between each pivot bubbles to be shorter or longer, then adjust the value of "LookbackPeriod".

Code:
``````declare upper;

input LookbackPeriod = 5;
input HideCurrentTF = no;
input HideTimeFrame2 = no;
input HideTimeFrame3 = no;
input TimeFrame2 = {"15 MIN", "1 MIN", "2 MIN", "3 MIN", "4 MIN", "5 MIN", "10 MIN", "20 MIN", "30 MIN", "1 HOUR", "2 HOURS", "4 HOURS", DAY, "2 DAYS", "3 DAYS", "4 DAYS", default WEEK, MONTH, "OPT EXP"};
input TimeFrame3 = {"30 MIN", "1 MIN", "2 MIN", "3 MIN", "4 MIN", "5 MIN", "10 MIN", "15 MIN", "20 MIN", "1 HOUR", "2 HOURS", "4 HOURS", DAY, "2 DAYS", "3 DAYS", "4 DAYS", WEEK, default MONTH, "OPT EXP"};
input HideSwings = no;
input SwingsLagBar = 1;

#--------------------------------------------------------------
def _highInPeriod1 = Highest(high, LookbackPeriod);
def _lowInPeriod1 = Lowest(low, LookbackPeriod);
#--------------------------------------------------------------
def marketLow1 = if _lowInPeriod1 < _lowInPeriod1[-LookbackPeriod] then _lowInPeriod1 else _lowInPeriod1[-LookbackPeriod];
def _markedLow1 = low == marketLow1;

rec _lastMarkedLow1 = CompoundValue(1, if IsNaN(_markedLow1) then _lastMarkedLow1[1] else if _markedLow1 then low else _lastMarkedLow1[1], low);
#--------------------------------------------------------------
def marketHigh1 = if _highInPeriod1 > _highInPeriod1[-LookbackPeriod] then _highInPeriod1 else _highInPeriod1[-LookbackPeriod];
def _markedHigh1 = high == marketHigh1;

rec _lastMarkedHigh1 = CompoundValue(1, if IsNaN(_markedHigh1) then _lastMarkedHigh1[1] else if _markedHigh1 then high else _lastMarkedHigh1[1], high);
#--------------------------------------------------------------
#plot Resistance1 = _lastMarkedHigh1;
#plot Support1 = _lastMarkedLow1;
#--------------------------------------------------------------
#Resistance1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
#Resistance1.SetDefaultColor(Color.MAGENTA);
#Resistance1.SetHiding(HideCurrentTF);
#--------------------------------------------------------------
#Support1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
#Support1.SetDefaultColor(Color.YELLOW);
#Support1.SetHiding(HideCurrentTF);
#--------------------------------------------------------------
def LowSwingForw = Lowest(low, SwingsLagBar)[-SwingsLagBar];
def LowSwingBack = Lowest(low, LookbackPeriod)[1];
def SwingLow = if low < LowSwingForw and low <= LowSwingBack then 1 else 0;
plot LowSwing = if SwingLow then low else Double.NaN;
LowSwing.hide();
#--------------------------------------------------------------
def HighSwingForw = Highest(high, SwingsLagBar)[-SwingsLagBar];
def HighSwingBack = Highest(high, LookbackPeriod)[1];
def SwingHigh = if high > HighSwingForw and high >= HighSwingBack then 1 else 0;
plot HighSwing = if SwingHigh then high else Double.NaN;
HighSwing.hide();
#--------------------------------------------------------------
HighSwing.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
HighSwing.SetLineWeight(5);
HighSwing.SetDefaultColor(Color.MAGENTA);
HighSwing.SetHiding(HideSwings);
LowSwing.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
LowSwing.SetLineWeight(5);
LowSwing.SetDefaultColor(Color.YELLOW);
LowSwing.SetHiding(HideSwings);
#--------------------------------------------------------------
#--------------------------------------------------------------
AddLabel(HighSwing, "SupRes : Swing High", Color.MAGENTA);
AddLabel(LowSwing, "SupRes : Swing Low", Color.YELLOW);
#--------------------------------------------------------------
def _highInPeriod2 = Highest(high(period = TimeFrame2), LookbackPeriod);
def _lowInPeriod2 = Lowest(low(period = TimeFrame2), LookbackPeriod);
#--------------------------------------------------------------
def marketLow2 = if _lowInPeriod2 < _lowInPeriod2[-LookbackPeriod] then _lowInPeriod2 else _lowInPeriod2[-LookbackPeriod];
def _markedLow2 = low(period = TimeFrame2) == marketLow2;

rec _lastMarkedLow2 = CompoundValue(1, if IsNaN(_markedLow2) then _lastMarkedLow2[1] else if _markedLow2 then low(period = TimeFrame2) else _lastMarkedLow2[1], low(period = TimeFrame2));
#--------------------------------------------------------------
def marketHigh2 = if _highInPeriod2 > _highInPeriod2[-LookbackPeriod] then _highInPeriod2 else _highInPeriod2[-LookbackPeriod];
def _markedHigh2 = high(period = TimeFrame2) == marketHigh2;

rec _lastMarkedHigh2 = CompoundValue(1, if IsNaN(_markedHigh2) then _lastMarkedHigh2[1] else if _markedHigh2 then high(period = TimeFrame2) else _lastMarkedHigh2[1], high(period = TimeFrame2));
#--------------------------------------------------------------
plot Resistance2 = _lastMarkedHigh2;
Resistance2.hide();
plot Support2 = _lastMarkedLow2;
Support2.hide();
#--------------------------------------------------------------
Resistance2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Resistance2.SetDefaultColor(Color.MAGENTA);
Resistance2.SetHiding(HideTimeFrame2);
#--------------------------------------------------------------
Support2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Support2.SetDefaultColor(Color.YELLOW);
Support2.SetHiding(HideTimeFrame2);
#--------------------------------------------------------------
def _highInPeriod3 = Highest(high(period = TimeFrame3), LookbackPeriod);
def _lowInPeriod3 = Lowest(low(period = TimeFrame3), LookbackPeriod);
#--------------------------------------------------------------
def marketLow3 = if _lowInPeriod3 < _lowInPeriod3[-LookbackPeriod] then _lowInPeriod3 else _lowInPeriod3[-LookbackPeriod];
def _markedLow3 = low(period = TimeFrame3) == marketLow3;

rec _lastMarkedLow3 = CompoundValue(1, if IsNaN(_markedLow3) then _lastMarkedLow3[1] else if _markedLow3 then low(period = TimeFrame3) else _lastMarkedLow3[1], low(period = TimeFrame3));
#--------------------------------------------------------------
def marketHigh3 = if _highInPeriod3 > _highInPeriod3[-LookbackPeriod] then _highInPeriod3 else _highInPeriod3[-LookbackPeriod];
def _markedHigh3 = high(period = TimeFrame3) == marketHigh3;

rec _lastMarkedHigh3 = CompoundValue(1, if IsNaN(_markedHigh3) then _lastMarkedHigh3[1] else if _markedHigh3 then high(period = TimeFrame3) else _lastMarkedHigh3[1], high(period = TimeFrame3));
#--------------------------------------------------------------
plot Resistance3 = _lastMarkedHigh3;
Resistance3.hide();
plot Support3 = _lastMarkedLow3;
Support3.hide();
#--------------------------------------------------------------
Resistance3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Resistance3.SetDefaultColor(Color.MAGENTA);
Resistance3.SetHiding(HideTimeFrame3);
#--------------------------------------------------------------
Support3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Support3.SetDefaultColor(Color.YELLOW);
Support3.SetHiding(HideTimeFrame3);

AddChartBubble(HighSwing, high, "" + Round(HighSwing), Color.gray, no);
AddChartBubble(LowSwing, low, "" + Round(LowSwing), Color.gray, no);``````

#### wam1234

##### New member
Hi

I would like to use this custom set up on my watchist to see which stock had broken their resistance

Here is the code but it doesn't work

any idea ?

Thanks

input LookbackPeriod = 2;

def _highInPeriod1 = Highest(high, LookbackPeriod);

def marketHigh1 = if _highInPeriod1 > _highInPeriod1[-LookbackPeriod] then _highInPeriod1 else _highInPeriod1[-LookbackPeriod];
def _markedHigh1 = high == marketHigh1;

rec _lastMarkedHigh1 = CompoundValue(1, if IsNaN(_markedHigh1) then _lastMarkedHigh1[1] else if _markedHigh1 then high else _lastMarkedHigh1[1], high);

plot Resistance1 = _lastMarkedHigh1;

AssignBackgroundColor(if high[1] > Resistance1 then Color.GREEN else Color.RED);

#### joshua74133

##### Member
VIP
ok so lets try this again. the code is below. all im wanting is that thumbs up if the price is greater then the prev resistance line. ive been trying to steal parts of the script from auto piv to try and figure out the code. the Auto piv has a box that says the price is above trend... i want the same type of thing for the script below. is this even possible? ive spent hours trying to figure it out.

Code:
``````input magnitude = 2;
input showValues = yes;
def top = high >= Highest(high[1], magnitude) and high >= Highest(high[-magnitude], magnitude);
def topvalue = if BarNumber() < magnitude then Double.NaN else if top    then high else topvalue[1];
plot topline = topvalue;
def countp = if IsNaN(top) and !IsNaN(top[1]) then 1 else countp[1] + 1;
plot peaktop = if IsNaN(top) then GetValue(topline, countp) else Double.NaN;
def bottom = low <= Lowest(low[1], magnitude) and low <= Lowest(low[-magnitude], magnitude);
def bottomValue = if BarNumber() < magnitude then Double.NaN else if bottom then low else bottomValue[1];
plot bottomline = bottomValue;

topline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
topline.SetDefaultColor(Color.RED);
peaktop.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
peaktop.SetDefaultColor(Color.RED);
bottomline.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
bottomline.SetDefaultColor(Color.GREEN);

input show_bubble_labels = yes;

AddLabel(yes, if close>high then "👍" else "NA", if close>high is true then color.green else color.black);``````

Last edited:

#### NahuelPatricioRodriguez

##### New member
Is there any way to hide the previous lines and keep the current ones?

#### MerryDay

Staff member
Staff
VIP
@NahuelPatricioRodriguez I was hoping maybe you could easily just unplot something in settings. No such luck.
Not that it can't be done. It's just that the logic would all need to be rewritten. There are Support & Resistance indicators that only print the current one. I am thinking a couple of Mobius ones, maybe. You should search do a search on the forum and plug&play with several of them and see if perhaps there is something that is a better fit.

#### GetRichOrDieTrying

##### Member
@GetRichOrDieTrying Did you try adding Daily to the 1min chart? It looks like it will only work intraday.
I don't understand what you mean.

I want to add these support and resistance levels from a higher timeframe on a lower timeframe chart, so I can get rid of the duplicative Daily charts, that are taking up space in my grid, when I'm only using them to populate these lines. Is that possible? I want these programmatic support and resistance levels from a monthly chart to show up on a 1 minute chart. I don't believe this is possible in the indicator's current form. There would have to be a modification to the code.

#### MerryDay

Staff member
Staff
VIP
@GetRichOrDieTrying As stated in Post#7 when others have requested changes:
@horserider As I posted above in my opening post, I found the code on another forum (link below) and shared with the uTS community. My contribution was the name in the form of the thread title, "Programmatic Support and Resistance"...