Programmatic Support and Resistance for Thinkorswim

@ArleenT

The script from the first post needs to be copied and pasted into a new study you need to create.

1. Copy the code,
2. then click on the "study" link at the top of any chart you have,
3. then click "edit studies,
4. then click "create" in the lower left panel, a new window will pop up,
5. at the top name your study with a name you won't forget,
6. then replace the sample code in the window with the new code you copied,
7. and click "OK".

And now you have the study in your studies listings with the name you gave it.

Bob
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

@natar
Uccn05u.png
@netarchitech what's the lower volume study and can you share it? TIA
 
Last edited by a moderator:
Added two more lookbackperiods and a function to turn the cloud off for the 2nd and 3rd periods. I did this because I like to trade on the three-minute time frame. my first lookbackperiod is 5, which gives me fifteen minutes worth oh data. my second lookbackperiod is 4 on a fifteen-minute time frame which gives me an hour's worth of data. My third lookbackperiod is 4 on an hour's time frame, which gives me four hours worth of data.
Code:
#Programmatic_SupportResistance_WickZoneMod
# CWPARKER remastered
declare upper;
input TimeFrame1 = AggregationPeriod.THREE_MIN;
input TimeFrame2 = AggregationPeriod.FIFTEEN_MIN;
input TimeFrame3 = AggregationPeriod.HOUR;
input LookbackPeriod1 = 5;
input LookbackPeriod2 = 5;
input LookbackPeriod3 = 5;
input HideTimeFrame2 = no;
input HideTimeFrame2_cloud = no;
input HideTimeFrame3 = no;
input HideTimeFrame3_cloud = no;
input HideCurrentTF = no;
input HideSwings = no;
input SwingsLagBar = 1;
def C = close (period =  TimeFrame1);
def O = open (period =  TimeFrame1);
def H = HIGH(period =  TimeFrame1);
DEF L = LOW(period =  TimeFrame1);
##############################################
def H2 = HIGH(period =  TimeFrame2);
DEF L2 = LOW(period =  TimeFrame2);
def C2 = close (period =  TimeFrame2);
def O2 = open (period =  TimeFrame2);
#############################################
def H3 = HIGH(period =  TimeFrame3);
DEF L3 = LOW(period =  TimeFrame3);
def C3 = close (period =  TimeFrame3);
def O3 = open (period =  TimeFrame3);
#########################################

#--------------------------------------------------------------
def _highInPeriod1 = Highest(h, LookbackPeriod1);
def _lowInPeriod1 = Lowest(l, LookbackPeriod1);
#--------------------------------------------------------------
def marketLow1 = if _lowInPeriod1 < _lowInPeriod1[-LookbackPeriod1] then _lowInPeriod1 else _lowInPeriod1[-LookbackPeriod1];
def _markedLow1 = l == marketLow1;

rec _lastMarkedLow1 = CompoundValue(1, if IsNaN(_markedLow1) then _lastMarkedLow1[1] else if _markedLow1 then l else _lastMarkedLow1[1], l);
rec _lastMarkedLow11 = CompoundValue(1, if IsNaN(_markedLow1) then _lastMarkedLow11[1] else if _markedLow1 then Min(o, c) else _lastMarkedLow11[1], l);
#--------------------------------------------------------------
def marketHigh1 = if _highInPeriod1 > _highInPeriod1[-LookbackPeriod1] then _highInPeriod1 else _highInPeriod1[-LookbackPeriod1];
def _markedHigh1 = h == marketHigh1;

rec _lastMarkedHigh1 = CompoundValue(1, if IsNaN(_markedHigh1) then _lastMarkedHigh1[1] else if _markedHigh1 then H else _lastMarkedHigh1[1], h);
rec _lastMarkedHigh11 = CompoundValue(1, if IsNaN(_markedHigh1) then _lastMarkedHigh11[1] else if _markedHigh1 then Max(o, c) else _lastMarkedHigh11[1], h);
#--------------------------------------------------------------
plot Resistance1 = _lastMarkedHigh1;
plot Resistance11 = _lastMarkedHigh11;
plot Support1 = _lastMarkedLow1;
plot Support11 = _lastMarkedLow11;
#--------------------------------------------------------------
Resistance11.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Resistance11.SetDefaultColor(Color.pink);
Resistance11.SetHiding(HideCurrentTF);
Resistance1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Resistance1.SetDefaultColor(Color.pink);
Resistance1.SetHiding(HideCurrentTF);
AddCloud(Resistance1, Resistance11, Color.pink, Color.pink);
#--------------------------------------------------------------
Support1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Support1.SetDefaultColor(Color.white);
Support1.SetHiding(HideCurrentTF);
Support11.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Support11.SetDefaultColor(Color.white);
Support11.SetHiding(HideCurrentTF);
AddCloud(Support1, Support11, Color.white, Color.white);

#--------------------------------------------------------------
def LowSwingForw = Lowest(l, SwingsLagBar)[-SwingsLagBar];
def LowSwingBack = Lowest(l, LookbackPeriod1)[1];
def SwingLow = if l < LowSwingForw and l <= LowSwingBack then 1 else 0;
plot LowSwing = if SwingLow then l else Double.NaN;
LowSwing.Hide();
#--------------------------------------------------------------
def HighSwingForw = Highest(h, SwingsLagBar)[-SwingsLagBar];
def HighSwingBack = Highest(h, LookbackPeriod1)[1];
def SwingHigh = if h > HighSwingForw and h >= HighSwingBack then 1 else 0;
plot HighSwing = if SwingHigh then h 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);
#--------------------------------------------------------------
Alert(HighSwing, "SupRes : Swing High", Alert.BAR, Sound.Bell);
Alert(LowSwing, "SupRes : Swing Low", Alert.BAR, Sound.Bell);
#--------------------------------------------------------------
AddLabel(HighSwing, "SupRes : Swing High", Color.MAGENTA);
AddLabel(LowSwing, "SupRes : Swing Low", Color.YELLOW);
#--------------------------------------------------------------
def _highInPeriod2 = Highest(h2, LookbackPeriod2);
def _lowInPeriod2 = Lowest(l2, LookbackPeriod2);
#--------------------------------------------------------------
def marketLow2 = if _lowInPeriod2 < _lowInPeriod2[-LookbackPeriod2] then _lowInPeriod2 else _lowInPeriod2[-LookbackPeriod2];
def _markedLow2 = l2 == marketLow2;

rec _lastMarkedLow2 = CompoundValue(1, if IsNaN(_markedLow2) then _lastMarkedLow2[1] else if _markedLow2 then l2 else _lastMarkedLow2[1], l2);
rec _lastMarkedLow22 = CompoundValue(1, if IsNaN(_markedLow2) then _lastMarkedLow22[1] else if _markedLow2 then Min(o2, c2) else _lastMarkedLow22[1], l2);
#--------------------------------------------------------------
def marketHigh2 = if _highInPeriod2 > _highInPeriod2[-LookbackPeriod2] then _highInPeriod2 else _highInPeriod2[-LookbackPeriod2];
def _markedHigh2 = h2 == marketHigh2;

rec _lastMarkedHigh2 = CompoundValue(1, if IsNaN(_markedHigh2) then _lastMarkedHigh2[1] else if _markedHigh2 then h2 else _lastMarkedHigh2[1], h2);
rec _lastMarkedHigh22 = CompoundValue(1, if IsNaN(_markedHigh2) then _lastMarkedHigh22[1] else if _markedHigh2 then Max(o2, c2) else _lastMarkedHigh22[1], h2);
#--------------------------------------------------------------
plot Resistance2 = _lastMarkedHigh2;
plot Resistance22 = _lastMarkedHigh22;
plot Support2 = _lastMarkedLow2;
plot Support22 = _lastMarkedLow22;
#--------------------------------------------------------------
Resistance2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Resistance2.SetDefaultColor(Color.RED);
Resistance2.SetHiding(HideTimeFrame2);
Resistance22.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Resistance22.SetDefaultColor(Color.RED);
Resistance22.SetHiding(HideTimeFrame2);
AddCloud(if !HideTimeFrame2_cloud  then RESistance2 else Double.NaN, RESistance22 , Color.RED, Color.RED);
#--------------------------------------------------------------
Support2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Support2.SetDefaultColor(Color.LIGHT_GREEN);
Support2.SetHiding(HideTimeFrame2);
Support22.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Support22.SetDefaultColor(Color.LIGHT_GREEN);
Support22.SetHiding(HideTimeFrame2);
AddCloud(if !HideTimeFrame2_cloud  then SUPPort2  else Double.NaN, SUPPort22  , Color.LIGHT_GREEN, Color.LIGHT_GREEN);
#--------------------------------------------------------------
def _highInPeriod3 = Highest(h3, LookbackPeriod3);
def _lowInPeriod3 = Lowest(l3, LookbackPeriod3);
#--------------------------------------------------------------
def marketLow3 = if _lowInPeriod3 < _lowInPeriod3[-LookbackPeriod3] then _lowInPeriod3 else _lowInPeriod3[-LookbackPeriod3];
def _markedLow3 = l3 == marketLow3;

rec _lastMarkedLow3 = CompoundValue(1, if IsNaN(_markedLow3) then _lastMarkedLow3[1] else if _markedLow3 then l3 else _lastMarkedLow3[1], l3);
rec _lastMarkedLow33 = CompoundValue(1, if IsNaN(_markedLow3) then _lastMarkedLow33[1] else if _markedLow3 then Min(o3, c3) else _lastMarkedLow33[1], l3);
#--------------------------------------------------------------
def marketHigh3 = if _highInPeriod3 > _highInPeriod3[-LookbackPeriod3] then _highInPeriod3 else _highInPeriod3[-LookbackPeriod3];
def _markedHigh3 = h3 == marketHigh3;

rec _lastMarkedHigh3 = CompoundValue(1, if IsNaN(_markedHigh3) then _lastMarkedHigh3[1] else if _markedHigh3 then h3 else _lastMarkedHigh3[1], h3);
rec _lastMarkedHigh33 = CompoundValue(1, if IsNaN(_markedHigh3) then _lastMarkedHigh33[1] else if _markedHigh3 then  Max(o3, c3) else _lastMarkedHigh33[1], h3);

#--------------------------------------------------------------
plot Resistance3 = _lastMarkedHigh3;
plot Resistance33 = _lastMarkedHigh33;
plot Support3 = _lastMarkedLow3;
plot Support33 = _lastMarkedLow33;
#--------------------------------------------------------------
Resistance3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Resistance3.SetDefaultColor(Color.MAGENTA);
Resistance3.SetHiding(HideTimeFrame3);
Resistance33.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Resistance33.SetDefaultColor(Color.MAGENTA );
Resistance33.SetHiding(HideTimeFrame3);
AddCloud(if !HideTimeFrame3_Cloud  then RESistance3 else Double.NaN, RESistance33 , Color.MAGENTA, Color.MAGENTA);
#--------------------------------------------------------------
Support3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Support3.SetDefaultColor(Color.DARK_GREEN);
Support3.SetHiding(HideTimeFrame3);
Support33.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Support33.SetDefaultColor(Color.DARK_GREEN);
Support33.SetHiding(HideTimeFrame3);
AddCloud(if !HideTimeFrame3_Cloud  then Support3  else Double.NaN, Support33 , Color.DARK_GREEN, Color.DARK_GREEN);

What loopback settings would I have to use if I was to use this code on a 1 min chart, to display s/r for 1/5/15/Daily.
How would I use the above on a intraday chart ( say 1/5/15 mins) , that will show the daily s/r levels from a yearly chart?
Thanks
 
Last edited by a moderator:
Excellent script!!

I add it twice to my charts , one with a lookback period = 13
and the other with lookback period = 6.
It then closely matches the indicator available in Trading View (with settings Leftbar =13, Rightbar 6 ) i.e
tradingview.com/v/vcg3prja/ search for Higher High Lower Low indicator by LonesomeTheBlue

I have to in my mind eliminate stale levels and very cumbersome.

It will be awesome if someone can tweak this script to match Trading views, which basically has two periods, one leftbar and the other rightbar. I am investigating this as I write. It is complicated task not sure when I will be able to finish .
 
Last edited:
Excellent script!!

I add it twice to my charts , one with a lookback period = 13
and the other with lookback period = 6.
It then closely matches the indicator available in Trading View (with settings Leftbar =13, Rightbar 6 ) i.e
tradingview.com/v/vcg3prja/ search for Higher High Lower Low indicator by LonesomeTheBlue

I have to in my mind eliminate stale levels and very cumbersome.

It will be awesome if someone can tweak this script to match Trading views, which basically has two periods, one leftbar and the other rightbar. I am investigating this as I write. It is complicated task not sure when I will be able to finish .
I have a portion of script from the old funwiththinkscript forum that might be what you are looking for. It just calculates the peaks and valleys slightly different than the programmatic support/resistance indicator.
Code:
##Robert_Payne
# define a peak as having a high greater than the two previous candles AND the two following candles
def peak = high > highest(high[1], 2) and high > highest(high[-2], 2);

# use a recursive variable to remember the peak value.
# if peak conditions are met, then set the value to the current high
# otherwise set the value to what it was before
def PeakValue = if peak then high else PeakValue[1];

# ----- define a valley as any point which is lower than the three preceding lows and the three following lows
def Valley = low < Lowest(low[1], 2) and low < Lowest(low[-2], 2);

def ValleyValue = if Valley then low else ValleyValue[1];


###Modified DeusMecanicus
plot Resistance1 = PeakValue;
plot Support1 = ValleyValue;
def lowerresist = Resistance1 < Resistance1[1];
def highersupport = Support1 > Support1[1];
def recresist = if Resistance1 > Resistance1[1] then 0 else if lowerresist then 1 else recresist[1];
def recsupport = if Support1 < Support1[1] then 0 else if highersupport then 1 else recsupport[1];

plot insideresistance = if recresist and recsupport then Resistance1 else Double.NaN;
plot insidesupport = if recsupport and recresist then Support1 else Double.NaN;
#--------------------------------------------------------------
insideresistance.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
insideresistance.SetDefaultColor(Color.MAGENTA);
insideresistance.SetLineWeight(5);

#--------------------------------------------------------------
insidesupport.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
insidesupport.SetDefaultColor(Color.CYAN);
insidesupport.SetLineWeight(5);
#--------------------------------------------------------------
#--------------------------------------------------------------
Resistance1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Resistance1.SetDefaultColor(Color.MAGENTA);

#--------------------------------------------------------------
Support1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Support1.SetDefaultColor(Color.CYAN);
 
This is fantastic!!! Thanks
@DeusMecanicus

It calculates levels better than tradingviews , I think. I will test more.
I hope it is not difficult to add labels "HH" , "LL" it will give great mental focus if we can.
LH and HH are in the same color Red but to a trader it makes a hell lot of difference.
 
Last edited by a moderator:
This indicator screwed me. On 3/23/22 NVDA broke support $260 on the 4 hour one solid bar next day the bars were broken up into two and new pivot was formed.... I had puts overnight... dont like this indicator
 


Code:
#Programmatic_SupportResistance_WickZoneMod
# CWPARKER remastered
declare upper;
input TimeFrame1 = AggregationPeriod.THREE_MIN;
input TimeFrame2 = AggregationPeriod.FIFTEEN_MIN;
input TimeFrame3 = AggregationPeriod.HOUR;
input LookbackPeriod1 = 5;
input LookbackPeriod2 = 5;
input LookbackPeriod3 = 5;
input HideTimeFrame2 = no;
input HideTimeFrame2_cloud = no;
input HideTimeFrame3 = no;
input HideTimeFrame3_cloud = no;
input HideCurrentTF = no;
input HideSwings = no;
input SwingsLagBar = 1;
def C = close (period =  TimeFrame1);
def O = open (period =  TimeFrame1);
def H = HIGH(period =  TimeFrame1);
DEF L = LOW(period =  TimeFrame1);
##############################################
def H2 = HIGH(period =  TimeFrame2);
DEF L2 = LOW(period =  TimeFrame2);
def C2 = close (period =  TimeFrame2);
def O2 = open (period =  TimeFrame2);
#############################################
def H3 = HIGH(period =  TimeFrame3);
DEF L3 = LOW(period =  TimeFrame3);
def C3 = close (period =  TimeFrame3);
def O3 = open (period =  TimeFrame3);
#########################################

#--------------------------------------------------------------
def _highInPeriod1 = Highest(h, LookbackPeriod1);
def _lowInPeriod1 = Lowest(l, LookbackPeriod1);
#--------------------------------------------------------------
def marketLow1 = if _lowInPeriod1 < _lowInPeriod1[-LookbackPeriod1] then _lowInPeriod1 else _lowInPeriod1[-LookbackPeriod1];
def _markedLow1 = l == marketLow1;

rec _lastMarkedLow1 = CompoundValue(1, if IsNaN(_markedLow1) then _lastMarkedLow1[1] else if _markedLow1 then l else _lastMarkedLow1[1], l);
rec _lastMarkedLow11 = CompoundValue(1, if IsNaN(_markedLow1) then _lastMarkedLow11[1] else if _markedLow1 then Min(o, c) else _lastMarkedLow11[1], l);
#--------------------------------------------------------------
def marketHigh1 = if _highInPeriod1 > _highInPeriod1[-LookbackPeriod1] then _highInPeriod1 else _highInPeriod1[-LookbackPeriod1];
def _markedHigh1 = h == marketHigh1;

rec _lastMarkedHigh1 = CompoundValue(1, if IsNaN(_markedHigh1) then _lastMarkedHigh1[1] else if _markedHigh1 then H else _lastMarkedHigh1[1], h);
rec _lastMarkedHigh11 = CompoundValue(1, if IsNaN(_markedHigh1) then _lastMarkedHigh11[1] else if _markedHigh1 then Max(o, c) else _lastMarkedHigh11[1], h);
#--------------------------------------------------------------
plot Resistance1 = _lastMarkedHigh1;
plot Resistance11 = _lastMarkedHigh11;
plot Support1 = _lastMarkedLow1;
plot Support11 = _lastMarkedLow11;
#--------------------------------------------------------------
Resistance11.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Resistance11.SetDefaultColor(Color.pink);
Resistance11.SetHiding(HideCurrentTF);
Resistance1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Resistance1.SetDefaultColor(Color.pink);
Resistance1.SetHiding(HideCurrentTF);
def res = if Resistance1 == Resistance1[-1] then Resistance1 else DOUble.NaN;
AddCloud(res, Resistance11, Color.pink, Color.pink);
#--------------------------------------------------------------
Support1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Support1.SetDefaultColor(Color.white);
Support1.SetHiding(HideCurrentTF);
Support11.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Support11.SetDefaultColor(Color.white);
Support11.SetHiding(HideCurrentTF);
def sup = if Support1 == Support1[-1] then Support1 else DOUble.NaN;
AddCloud(sup, Support11, Color.white, Color.white);

#--------------------------------------------------------------
def LowSwingForw = Lowest(l, SwingsLagBar)[-SwingsLagBar];
def LowSwingBack = Lowest(l, LookbackPeriod1)[1];
def SwingLow = if l < LowSwingForw and l <= LowSwingBack then 1 else 0;
plot LowSwing = if SwingLow then l else Double.NaN;
LowSwing.Hide();
#--------------------------------------------------------------
def HighSwingForw = Highest(h, SwingsLagBar)[-SwingsLagBar];
def HighSwingBack = Highest(h, LookbackPeriod1)[1];
def SwingHigh = if h > HighSwingForw and h >= HighSwingBack then 1 else 0;
plot HighSwing = if SwingHigh then h 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);
#--------------------------------------------------------------
Alert(HighSwing, "SupRes : Swing High", Alert.BAR, Sound.Bell);
Alert(LowSwing, "SupRes : Swing Low", Alert.BAR, Sound.Bell);
#--------------------------------------------------------------
AddLabel(HighSwing, "SupRes : Swing High", Color.MAGENTA);
AddLabel(LowSwing, "SupRes : Swing Low", Color.YELLOW);
#--------------------------------------------------------------
def _highInPeriod2 = Highest(h2, LookbackPeriod2);
def _lowInPeriod2 = Lowest(l2, LookbackPeriod2);
#--------------------------------------------------------------
def marketLow2 = if _lowInPeriod2 < _lowInPeriod2[-LookbackPeriod2] then _lowInPeriod2 else _lowInPeriod2[-LookbackPeriod2];
def _markedLow2 = l2 == marketLow2;

rec _lastMarkedLow2 = CompoundValue(1, if IsNaN(_markedLow2) then _lastMarkedLow2[1] else if _markedLow2 then l2 else _lastMarkedLow2[1], l2);
rec _lastMarkedLow22 = CompoundValue(1, if IsNaN(_markedLow2) then _lastMarkedLow22[1] else if _markedLow2 then Min(o2, c2) else _lastMarkedLow22[1], l2);
#--------------------------------------------------------------
def marketHigh2 = if _highInPeriod2 > _highInPeriod2[-LookbackPeriod2] then _highInPeriod2 else _highInPeriod2[-LookbackPeriod2];
def _markedHigh2 = h2 == marketHigh2;

rec _lastMarkedHigh2 = CompoundValue(1, if IsNaN(_markedHigh2) then _lastMarkedHigh2[1] else if _markedHigh2 then h2 else _lastMarkedHigh2[1], h2);
rec _lastMarkedHigh22 = CompoundValue(1, if IsNaN(_markedHigh2) then _lastMarkedHigh22[1] else if _markedHigh2 then Max(o2, c2) else _lastMarkedHigh22[1], h2);
#--------------------------------------------------------------
plot Resistance2 = _lastMarkedHigh2;
plot Resistance22 = _lastMarkedHigh22;
plot Support2 = _lastMarkedLow2;
plot Support22 = _lastMarkedLow22;
#--------------------------------------------------------------
Resistance2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Resistance2.SetDefaultColor(Color.RED);
Resistance2.SetHiding(HideTimeFrame2);
Resistance22.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Resistance22.SetDefaultColor(Color.RED);
Resistance22.SetHiding(HideTimeFrame2);
def res2 = if RESistance2 == RESistance2[-1] then RESistance2 else DOUble.NaN;
AddCloud(if !HideTimeFrame2_cloud  then res2 else Double.NaN, RESistance22 , Color.lIGHT_RED, Color.lIGHT_RED);
#--------------------------------------------------------------
Support2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Support2.SetDefaultColor(Color.LIGHT_GREEN);
Support2.SetHiding(HideTimeFrame2);
Support22.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Support22.SetDefaultColor(Color.LIGHT_GREEN);
Support22.SetHiding(HideTimeFrame2);
def sup2 = if SUPPort2 == SUPPort2[-1] then SUPPort2 else DOUble.NaN;
AddCloud(if !HideTimeFrame2_cloud  then sup2  else Double.NaN, SUPPort22  , Color.LIGHT_GREEN, Color.LIGHT_GREEN);
#--------------------------------------------------------------
def _highInPeriod3 = Highest(h3, LookbackPeriod3);
def _lowInPeriod3 = Lowest(l3, LookbackPeriod3);
#--------------------------------------------------------------
def marketLow3 = if _lowInPeriod3 < _lowInPeriod3[-LookbackPeriod3] then _lowInPeriod3 else _lowInPeriod3[-LookbackPeriod3];
def _markedLow3 = l3 == marketLow3;

rec _lastMarkedLow3 = CompoundValue(1, if IsNaN(_markedLow3) then _lastMarkedLow3[1] else if _markedLow3 then l3 else _lastMarkedLow3[1], l3);
rec _lastMarkedLow33 = CompoundValue(1, if IsNaN(_markedLow3) then _lastMarkedLow33[1] else if _markedLow3 then Min(o3, c3) else _lastMarkedLow33[1], l3);
#--------------------------------------------------------------
def marketHigh3 = if _highInPeriod3 > _highInPeriod3[-LookbackPeriod3] then _highInPeriod3 else _highInPeriod3[-LookbackPeriod3];
def _markedHigh3 = h3 == marketHigh3;

rec _lastMarkedHigh3 = CompoundValue(1, if IsNaN(_markedHigh3) then _lastMarkedHigh3[1] else if _markedHigh3 then h3 else _lastMarkedHigh3[1], h3);
rec _lastMarkedHigh33 = CompoundValue(1, if IsNaN(_markedHigh3) then _lastMarkedHigh33[1] else if _markedHigh3 then  Max(o3, c3) else _lastMarkedHigh33[1], h3);

#--------------------------------------------------------------
plot Resistance3 = _lastMarkedHigh3;
plot Resistance33 = _lastMarkedHigh33;
plot Support3 = _lastMarkedLow3;
plot Support33 = _lastMarkedLow33;
#--------------------------------------------------------------
Resistance3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Resistance3.SetDefaultColor(Color.MAGENTA);
Resistance3.SetHiding(HideTimeFrame3);
Resistance33.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Resistance33.SetDefaultColor(Color.MAGENTA );
Resistance33.SetHiding(HideTimeFrame3);
def res3 = if RESistance3 == RESistance3[-1] then RESistance3 else DOUble.NaN;
AddCloud(if !HideTimeFrame3_Cloud  then res3 else Double.NaN, RESistance33 , Color.red, Color.red);
#--------------------------------------------------------------
Support3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Support3.SetDefaultColor(Color.DARK_GREEN);
Support3.SetHiding(HideTimeFrame3);
Support33.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Support33.SetDefaultColor(Color.DARK_GREEN);
Support33.SetHiding(HideTimeFrame3);
def sup3 = if Support3 == Support3[-1] then Support3 else DOUble.NaN;
AddCloud(if !HideTimeFrame3_Cloud  then sup3  else Double.NaN, Support33 , Color.GREEN, Color.GREEN);
 
Last edited:
Excellent!!! These provide you realistic supply and demand zones. Not fair to call it support and resistance levels . Wish we could extend the wide zones (demand/supply) on the far left of the current bar , so that we do not miss .
 


Code:
#Programmatic_SupportResistance_WickZoneMod
# CWPARKER remastered
declare upper;
input TimeFrame1 = AggregationPeriod.THREE_MIN;
input TimeFrame2 = AggregationPeriod.FIFTEEN_MIN;
input TimeFrame3 = AggregationPeriod.HOUR;
input LookbackPeriod1 = 5;
input LookbackPeriod2 = 5;
input LookbackPeriod3 = 5;
input HideTimeFrame2 = no;
input HideTimeFrame2_cloud = no;
input HideTimeFrame3 = no;
input HideTimeFrame3_cloud = no;
input HideCurrentTF = no;
input HideSwings = no;
input SwingsLagBar = 1;
def C = close (period =  TimeFrame1);
def O = open (period =  TimeFrame1);
def H = HIGH(period =  TimeFrame1);
DEF L = LOW(period =  TimeFrame1);
##############################################
def H2 = HIGH(period =  TimeFrame2);
DEF L2 = LOW(period =  TimeFrame2);
def C2 = close (period =  TimeFrame2);
def O2 = open (period =  TimeFrame2);
#############################################
def H3 = HIGH(period =  TimeFrame3);
DEF L3 = LOW(period =  TimeFrame3);
def C3 = close (period =  TimeFrame3);
def O3 = open (period =  TimeFrame3);
#########################################

#--------------------------------------------------------------
def _highInPeriod1 = Highest(h, LookbackPeriod1);
def _lowInPeriod1 = Lowest(l, LookbackPeriod1);
#--------------------------------------------------------------
def marketLow1 = if _lowInPeriod1 < _lowInPeriod1[-LookbackPeriod1] then _lowInPeriod1 else _lowInPeriod1[-LookbackPeriod1];
def _markedLow1 = l == marketLow1;

rec _lastMarkedLow1 = CompoundValue(1, if IsNaN(_markedLow1) then _lastMarkedLow1[1] else if _markedLow1 then l else _lastMarkedLow1[1], l);
rec _lastMarkedLow11 = CompoundValue(1, if IsNaN(_markedLow1) then _lastMarkedLow11[1] else if _markedLow1 then Min(o, c) else _lastMarkedLow11[1], l);
#--------------------------------------------------------------
def marketHigh1 = if _highInPeriod1 > _highInPeriod1[-LookbackPeriod1] then _highInPeriod1 else _highInPeriod1[-LookbackPeriod1];
def _markedHigh1 = h == marketHigh1;

rec _lastMarkedHigh1 = CompoundValue(1, if IsNaN(_markedHigh1) then _lastMarkedHigh1[1] else if _markedHigh1 then H else _lastMarkedHigh1[1], h);
rec _lastMarkedHigh11 = CompoundValue(1, if IsNaN(_markedHigh1) then _lastMarkedHigh11[1] else if _markedHigh1 then Max(o, c) else _lastMarkedHigh11[1], h);
#--------------------------------------------------------------
plot Resistance1 = _lastMarkedHigh1;
plot Resistance11 = _lastMarkedHigh11;
plot Support1 = _lastMarkedLow1;
plot Support11 = _lastMarkedLow11;
#--------------------------------------------------------------
Resistance11.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Resistance11.SetDefaultColor(Color.pink);
Resistance11.SetHiding(HideCurrentTF);
Resistance1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Resistance1.SetDefaultColor(Color.pink);
Resistance1.SetHiding(HideCurrentTF);
def res = if Resistance1 == Resistance1[-1] then Resistance1 else DOUble.NaN;
AddCloud(res, Resistance11, Color.pink, Color.pink);
#--------------------------------------------------------------
Support1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Support1.SetDefaultColor(Color.white);
Support1.SetHiding(HideCurrentTF);
Support11.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Support11.SetDefaultColor(Color.white);
Support11.SetHiding(HideCurrentTF);
def sup = if Support1 == Support1[-1] then Support1 else DOUble.NaN;
AddCloud(sup, Support11, Color.white, Color.white);

#--------------------------------------------------------------
def LowSwingForw = Lowest(l, SwingsLagBar)[-SwingsLagBar];
def LowSwingBack = Lowest(l, LookbackPeriod1)[1];
def SwingLow = if l < LowSwingForw and l <= LowSwingBack then 1 else 0;
plot LowSwing = if SwingLow then l else Double.NaN;
LowSwing.Hide();
#--------------------------------------------------------------
def HighSwingForw = Highest(h, SwingsLagBar)[-SwingsLagBar];
def HighSwingBack = Highest(h, LookbackPeriod1)[1];
def SwingHigh = if h > HighSwingForw and h >= HighSwingBack then 1 else 0;
plot HighSwing = if SwingHigh then h 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);
#--------------------------------------------------------------
Alert(HighSwing, "SupRes : Swing High", Alert.BAR, Sound.Bell);
Alert(LowSwing, "SupRes : Swing Low", Alert.BAR, Sound.Bell);
#--------------------------------------------------------------
AddLabel(HighSwing, "SupRes : Swing High", Color.MAGENTA);
AddLabel(LowSwing, "SupRes : Swing Low", Color.YELLOW);
#--------------------------------------------------------------
def _highInPeriod2 = Highest(h2, LookbackPeriod2);
def _lowInPeriod2 = Lowest(l2, LookbackPeriod2);
#--------------------------------------------------------------
def marketLow2 = if _lowInPeriod2 < _lowInPeriod2[-LookbackPeriod2] then _lowInPeriod2 else _lowInPeriod2[-LookbackPeriod2];
def _markedLow2 = l2 == marketLow2;

rec _lastMarkedLow2 = CompoundValue(1, if IsNaN(_markedLow2) then _lastMarkedLow2[1] else if _markedLow2 then l2 else _lastMarkedLow2[1], l2);
rec _lastMarkedLow22 = CompoundValue(1, if IsNaN(_markedLow2) then _lastMarkedLow22[1] else if _markedLow2 then Min(o2, c2) else _lastMarkedLow22[1], l2);
#--------------------------------------------------------------
def marketHigh2 = if _highInPeriod2 > _highInPeriod2[-LookbackPeriod2] then _highInPeriod2 else _highInPeriod2[-LookbackPeriod2];
def _markedHigh2 = h2 == marketHigh2;

rec _lastMarkedHigh2 = CompoundValue(1, if IsNaN(_markedHigh2) then _lastMarkedHigh2[1] else if _markedHigh2 then h2 else _lastMarkedHigh2[1], h2);
rec _lastMarkedHigh22 = CompoundValue(1, if IsNaN(_markedHigh2) then _lastMarkedHigh22[1] else if _markedHigh2 then Max(o2, c2) else _lastMarkedHigh22[1], h2);
#--------------------------------------------------------------
plot Resistance2 = _lastMarkedHigh2;
plot Resistance22 = _lastMarkedHigh22;
plot Support2 = _lastMarkedLow2;
plot Support22 = _lastMarkedLow22;
#--------------------------------------------------------------
Resistance2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Resistance2.SetDefaultColor(Color.RED);
Resistance2.SetHiding(HideTimeFrame2);
Resistance22.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Resistance22.SetDefaultColor(Color.RED);
Resistance22.SetHiding(HideTimeFrame2);
def res2 = if RESistance2 == RESistance2[-1] then RESistance2 else DOUble.NaN;
AddCloud(if !HideTimeFrame2_cloud  then res2 else Double.NaN, RESistance22 , Color.lIGHT_RED, Color.lIGHT_RED);
#--------------------------------------------------------------
Support2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Support2.SetDefaultColor(Color.LIGHT_GREEN);
Support2.SetHiding(HideTimeFrame2);
Support22.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Support22.SetDefaultColor(Color.LIGHT_GREEN);
Support22.SetHiding(HideTimeFrame2);
def sup2 = if SUPPort2 == SUPPort2[-1] then SUPPort2 else DOUble.NaN;
AddCloud(if !HideTimeFrame2_cloud  then sup2  else Double.NaN, SUPPort22  , Color.LIGHT_GREEN, Color.LIGHT_GREEN);
#--------------------------------------------------------------
def _highInPeriod3 = Highest(h3, LookbackPeriod3);
def _lowInPeriod3 = Lowest(l3, LookbackPeriod3);
#--------------------------------------------------------------
def marketLow3 = if _lowInPeriod3 < _lowInPeriod3[-LookbackPeriod3] then _lowInPeriod3 else _lowInPeriod3[-LookbackPeriod3];
def _markedLow3 = l3 == marketLow3;

rec _lastMarkedLow3 = CompoundValue(1, if IsNaN(_markedLow3) then _lastMarkedLow3[1] else if _markedLow3 then l3 else _lastMarkedLow3[1], l3);
rec _lastMarkedLow33 = CompoundValue(1, if IsNaN(_markedLow3) then _lastMarkedLow33[1] else if _markedLow3 then Min(o3, c3) else _lastMarkedLow33[1], l3);
#--------------------------------------------------------------
def marketHigh3 = if _highInPeriod3 > _highInPeriod3[-LookbackPeriod3] then _highInPeriod3 else _highInPeriod3[-LookbackPeriod3];
def _markedHigh3 = h3 == marketHigh3;

rec _lastMarkedHigh3 = CompoundValue(1, if IsNaN(_markedHigh3) then _lastMarkedHigh3[1] else if _markedHigh3 then h3 else _lastMarkedHigh3[1], h3);
rec _lastMarkedHigh33 = CompoundValue(1, if IsNaN(_markedHigh3) then _lastMarkedHigh33[1] else if _markedHigh3 then  Max(o3, c3) else _lastMarkedHigh33[1], h3);

#--------------------------------------------------------------
plot Resistance3 = _lastMarkedHigh3;
plot Resistance33 = _lastMarkedHigh33;
plot Support3 = _lastMarkedLow3;
plot Support33 = _lastMarkedLow33;
#--------------------------------------------------------------
Resistance3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Resistance3.SetDefaultColor(Color.MAGENTA);
Resistance3.SetHiding(HideTimeFrame3);
Resistance33.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Resistance33.SetDefaultColor(Color.MAGENTA );
Resistance33.SetHiding(HideTimeFrame3);
def res3 = if RESistance3 == RESistance3[-1] then RESistance3 else DOUble.NaN;
AddCloud(if !HideTimeFrame3_Cloud  then res3 else Double.NaN, RESistance33 , Color.red, Color.red);
#--------------------------------------------------------------
Support3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Support3.SetDefaultColor(Color.DARK_GREEN);
Support3.SetHiding(HideTimeFrame3);
Support33.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Support33.SetDefaultColor(Color.DARK_GREEN);
Support33.SetHiding(HideTimeFrame3);
def sup3 = if Support3 == Support3[-1] then Support3 else DOUble.NaN;
AddCloud(if !HideTimeFrame3_Cloud  then sup3  else Double.NaN, Support33 , Color.GREEN, Color.GREEN);
Tested it for the whole month and I love it. Great contribution with the boxes. Thank you
 
Below is the Study I wish to alert. I am trying to get it to Alert when a new Pivot (Support/Resistance) Line appears on the chart. Right now, the study alerts when the price is greater than a previously plotted Support/Resistance Line. In the image below, I have drawn white arrows pointing to where and when I would like the study to give a new alert signal.
I would like one new alert each time a new line is plotted on the chart. That's all. I do not wish for an alert for each new bar or tick.
Thank you for any and all thoughts.



# Mobius_Scalper_Pivots
# V01.2011

input n = 4;
input ShowLines = yes;
input SoundAlerts = yes;
input price = close;
input magnitude = 5;

def h = high;
def l = low;
def Firstbar = BarNumber();
def Highest = fold i = 1 to n + 1
with p = 1
while p
do h > GetValue(h, -i);
def A = if (Firstbar > n and
h == Highest(h, n) and
Highest)
then h
else Double.NaN;
def Lowest = fold j = 1 to n + 1
with q = 1
while q
do l < GetValue(l, -j);
def B = if (Firstbar > n and
l == Lowest(l, n) and Lowest)
then l
else Double.NaN;
def Al = if !IsNaN(A)
then A
else Al[1];
def Bl = if !IsNaN(B)
then B
else Bl[1];

plot PriceHigh = Round(A, 2);
PriceHigh.SetPaintingStrategy(PaintingStrategy.VALUES_ABOVE);
PriceHigh.SetDefaultColor(Color.WHITE);
plot HighLine = if Al > 0
then Al
else Double.NaN;
HighLine.SetHiding(!ShowLines);
HighLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
HighLine.SetDefaultColor(Color.GREEN);

plot PriceLow = Round(B, 2);
PriceLow.SetPaintingStrategy(PaintingStrategy.VALUES_BELOW);
PriceLow.SetDefaultColor(Color.WHITE);

plot LowLine = if Bl > 0
then Bl
else Double.NaN;
LowLine.SetHiding(!ShowLines);
LowLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
LowLine.SetDefaultColor(Color.RED);


#Alerts
Alert(SoundAlerts and h > Al , "Up", Alert.BAR, Sound.Bell);
Alert(SoundAlerts and l < Bl, "Down", Alert.BAR, Sound.Ding);
14910[/ATTACH]']
VkDz8bk.jpg


Here is list of what I have tried, and what has failed.
# Alerts
Alert(SoundAlerts and h > Al , "Up", Alert.BAR, Sound.Bell);
Alert(SoundAlerts and l < Bl, "Down", Alert.BAR, Sound.Ding);

Alert(SoundAlerts and PriceHigh, "Up", Alert.BAR, Sound.Bell);
Alert(SoundAlerts and PriceLow, "Down", Alert.BAR, Sound.Ding);

Alert(SoundAlerts and HighLine , "Up", Alert.BAR, Sound.Bell);
Alert(SoundAlerts and LowLine, "Down", Alert.BAR, Sound.Ding);

Alert(SoundAlerts and Al , "Up", Alert.BAR, Sound.Bell);
Alert(SoundAlerts and Bl, "Down", Alert.BAR, Sound.Ding);

Alert(SoundAlerts and A, "Up", Alert.BAR, Sound.Bell);
Alert(SoundAlerts and B, "Down", Alert.BAR, Sound.Ding);

Alert(SoundAlerts and Highest , "Up", Alert.BAR, Sound.Bell);
Alert(SoundAlerts and Lowest, "Down", Alert.BAR, Sound.Ding);

Alert(SoundAlerts and h > Al , "Up", Alert.BAR, Sound.Bell);
Alert(SoundAlerts and l < Bl, "Down", Alert.BAR, Sound.Ding);
 

Attachments

  • VkDz8bk.jpg
    VkDz8bk.jpg
    310.7 KB · Views: 178
Last edited:
Below is the Study I wish to alert. I am trying to get it to Alert when a new Pivot (Support/Resistance) Line appears on the chart. Right now, the study alerts when the price is greater than a previously plotted Support/Resistance Line. In the image below, I have drawn white arrows pointing to where and when I would like the study to give a new alert signal.
I would like one new alert each time a new line is plotted on the chart. That's all. I do not wish for an alert for each new bar or tick.
Thank you for any and all thoughts.
Given that this is a repainting indicator, it doesn't know there was a previous line or that this is a new one that was drawn. Repainters 'paint' over the old information and it is no longer available.

How about alert when there is a plot but no previous plot? Essentially, the 1st time of the current line?
Ruby:
Alert(lowline and !lowline[1] , "New Line", Alert.BAR, Sound.Bell);
 
Added two more lookbackperiods and a function to turn the cloud off for the 2nd and 3rd periods. I did this because I like to trade on the three-minute time frame. my first lookbackperiod is 5, which gives me fifteen minutes worth oh data. my second lookbackperiod is 4 on a fifteen-minute time frame which gives me an hour's worth of data. My third lookbackperiod is 4 on an hour's time frame, which gives me four hours worth of data.
This is absolutely incredible -- can I ask, how reactive is this? Is there a lag of X bars based on the lookback, or do they form as soon as each timeframe closes? Depending on how quick this study forms these zones this may be the most impressive study I've ever seen, at least relative to my preferred style of trading.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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