Watchlist column for price with respect to pivot points

D

Deleted2090

Guest
Need some help writing code. What I am trying to do is if the price of a ticker gets within a certain percentage +/- value a pivot level for it, indicate that pivot level in a watch list.

So for example, if ticker XYZ current price is 100, and its pivot levels are S3 = 80, S2 = 90, S1 = 95, PP = 98, R1 = 105, R2 = 110, R3 = 120, then if the price changes to 99, then it is only 1% away from the PP level of 98, then display PP in the watchlist column. If price changes to 104, display R1 as it is closes to the price and so on. I was using this code as my starting base.

The formula for pivots I am using is as below:

Code:
#Pivot point PP = High + Low + Close divided by 3
#First resistance R1: Pivot x 2 - Low
#Second resistance R2: Pivot + High - Low
#Third resistance R3: Daily High + 2 x (Pivot Point – Daily Low)
#First support S1: Pivot x 2 - High
#Second support S2: Pivot - High + Low
#Third support S3: Daily Low – 2 x (Daily High – Pivot Point)

I have edited the code in the above link to get to here, but obviously its not working correctly and just showing the label as 0 for all tickers in the watchlist

Code:
def AggregationPeriod = AggregationPeriod.DAY;
def nan = Double.NaN;
def isRollover = GetYYYYMMDD() != GetYYYYMMDD()[1];
def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD());
def afterEnd = GetTime() > RegularTradingEnd(GetYYYYMMDD());
def firstBarOfDay = if (beforeStart[1] == 1 and beforeStart == 0) or (isRollover and beforeStart == 0) then 1 else 0;
def lastBarOfDay = if
    (afterEnd[-1] == 1 and afterEnd == 0) or
    (isRollover[-1] and firstBarOfDay[-1])
    then 1
    else 0;

#Pivot point PP = High + Low + Close divided by 3
#First resistance R1: Pivot x 2 - Low
#Second resistance R2: Pivot + High - Low
#Third resistance R3: Daily High + 2 x (Pivot Point – Daily Low)
#First support S1: Pivot x 2 - High
#Second support S2: Pivot - High + Low
#Third support S3: Daily Low – 2 x (Daily High – Pivot Point)

def pc = close(period = AggregationPeriod)[1];
def ph = high(period = AggregationPeriod)[1];
def pl = low(period = AggregationPeriod)[1];

def pp = if firstBarOfDay then (pc+ph+pl)/3 else if lastBarOfDay then nan else pp[1];
def s1 = if firstBarOfDay then (pp * 2) - ph else if lastBarOfDay then nan else s1[1];
def s2 = if firstBarOfDay then pp - ph + pl else if lastBarOfDay then nan else s2[1];
def s3 = if firstBarOfDay then pl - (2 * (ph - pp)) else if lastBarOfDay then nan else s3[1];
def r1 = if firstBarOfDay then (pp * 2) - pl else if lastBarOfDay then nan else r1[1];
def r2 = if firstBarOfDay then pp + ph - pl else if lastBarOfDay then nan else r2[1];
def r3 = if firstBarOfDay then ph - (2 * (pp - pl)) else if lastBarOfDay then nan else r3[1];

def lower_range = 0.998;
def higher_range = 1.002;
def current_price = close;
def pp_label = if Between(current_price,(lower_range * pp),(higher_range * pp)) then 1 else 0;
def s1_label = if Between(current_price,(lower_range * s1),(higher_range * s1)) then 1 else 0;
def s2_label = if Between(current_price,(lower_range * s2),(higher_range * s2)) then 1 else 0;
def s3_label = if Between(current_price,(lower_range * s3),(higher_range * s3)) then 1 else 0;
def r1_label = if Between(current_price,(lower_range * r1),(higher_range * r1)) then 1 else 0;
def r2_label = if Between(current_price,(lower_range * r2),(higher_range * r2)) then 1 else 0;
def r3_label = if Between(current_price,(lower_range * r3),(higher_range * r3)) then 1 else 0;

AddLabel(yes,if pp_label == 1 then "PP" else if s1_label == 1 then "S1" else if s2_label == 1 then "S2" else if s3_label == 1 then "S3" else if r1_label == 1 then "R1" else if r2_label == 1 then "R2" else if r3_label == 1 then "R3" else "0");

Can someone help me out a bit on this?

Update: Nov 1, 2020

Not completely sure if this is gonna work 100%, but I am testing it on a watchlist column and it pretty much works for me

Code:
def AggregationPeriod = AggregationPeriod.DAY;
def nan = Double.NaN;
def isRollover = GetYYYYMMDD() != GetYYYYMMDD()[1];
def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD());
def afterEnd = GetTime() > RegularTradingEnd(GetYYYYMMDD());
def firstBarOfDay = if (beforeStart[1] == 1 and beforeStart == 0) or (isRollover and beforeStart == 0) then 1 else 0;
def lastBarOfDay = if
    (afterEnd[-1] == 1 and afterEnd == 0) or
    (isRollover[-1] and firstBarOfDay[-1])
    then 1
    else 0;

#Pivot point PP = High + Low + Close divided by 3
#First resistance R1: Pivot x 2 - Low
#Second resistance R2: Pivot + High - Low
#Third resistance R3: Daily High + 2 x (Pivot Point – Daily Low)
#First support S1: Pivot x 2 - High
#Second support S2: Pivot - High + Low
#Third support S3: Daily Low – 2 x (Daily High – Pivot Point)

def pc = close(period = AggregationPeriod)[1];
def ph = high(period = AggregationPeriod)[1];
def pl = low(period = AggregationPeriod)[1];

def pp = (pc+ph+pl)/3;
def s1 = (pp * 2) - ph;
def s2 = pp - ph + pl;
def s3 = pl - (2 * (ph - pp));
def r1 = (pp * 2) - pl;
def r2 = pp + ph - pl;
def r3 = ph + (2 * (pp - pl));

def lower_range = 0.9985;
def higher_range = 1.0015;
def current_price = close(period = AggregationPeriod);
plot day_close = close(period = AggregationPeriod);
AssignBackgroundColor(if between(current_price,(pp*lower_range),(pp*higher_range)) and (current_price >= pp) then Color.DARK_GREEN else if between(current_price,(pp*lower_range),(pp*higher_range)) and (current_price <= pp) then Color.DARK_RED else if between(current_price,(s1*lower_range),(s1*higher_range)) and (current_price >= s1) then Color.DARK_GREEN else if between(current_price,(s1*lower_range),(s1*higher_range)) and (current_price <= s1) then Color.DARK_RED else if between(current_price,(s2*lower_range),(s2*higher_range)) and (current_price >= s2) then Color.DARK_GREEN else if between(current_price,(s2*lower_range),(s2*higher_range)) and (current_price <= s2) then Color.DARK_RED else if between(current_price,(s3*lower_range),(s3*higher_range)) and (current_price >= s3) then Color.DARK_GREEN else if between(current_price,(s3*lower_range),(s3*higher_range)) and (current_price <= s3) then Color.DARK_RED else if between(current_price,(r1*lower_range),(r1*higher_range)) and (current_price >= r1) then Color.DARK_GREEN else if between(current_price,(r1*lower_range),(r1*higher_range)) and (current_price <= r1) then Color.DARK_RED else if between(current_price,(r2*lower_range),(r2*higher_range)) and (current_price >= r2) then Color.DARK_GREEN else if between(current_price,(r2*lower_range),(r2*higher_range)) and (current_price <= r2) then Color.DARK_RED else if between(current_price,(r3*lower_range),(r3*higher_range)) and (current_price >= r3) then Color.DARK_GREEN else if between(current_price,(r3*lower_range),(r3*higher_range)) and (current_price <= r3) then Color.DARK_RED else Color.BLACK);
AddLabel(yes,if between(current_price,(pp*lower_range),(pp*higher_range)) then AsText(pp, NumberFormat.TWO_DECIMAL_PLACES)+"_PP" else if between(current_price,(s1*lower_range),(s1*higher_range)) then AsText(s1, NumberFormat.TWO_DECIMAL_PLACES)+"_S1" else if between(current_price,(s2*lower_range),(s2*higher_range)) then AsText(s2, NumberFormat.TWO_DECIMAL_PLACES)+"_S2" else if between(current_price,(s3*lower_range),(s3*higher_range)) then AsText(s3, NumberFormat.TWO_DECIMAL_PLACES)+"_S3" else if between(current_price,(r1*lower_range),(r1*higher_range)) then AsText(r1, NumberFormat.TWO_DECIMAL_PLACES)+"_R1" else if between(current_price,(r2*lower_range),(r2*higher_range)) then AsText(r2, NumberFormat.TWO_DECIMAL_PLACES)+"_R2" else if between(current_price,(r3*lower_range),(r3*higher_range)) then AsText(r3, NumberFormat.TWO_DECIMAL_PLACES)+"_R3" else if between(current_price,S1,PP) then "S1_PP" else if between(current_price,S2,S1) then "S2_S1" else if between(current_price,S3,S2) then "S3_S2" else if between(current_price,PP,R1) then "PP_R1" else if between(current_price,R1,R2) then "R1_R2" else if between(current_price,R2,R3) then "R2_R3" else "U");

6PLPvwj.png


This is a sample watchlist of futures and the pivot levels column is working as expected. Would test it tomorrow in real time hour on tickers
 
Last edited by a moderator:

Jtru94

New member
This is fantastic! thanks so much! I tried to adjust it for a smaller timeframe with extended hours on to see if it would tell me where the pre-market price is in relation to the upcoming day's pivots, but it seems to only work with the timeframe set to day. I also looked up threads on this site pertaining to after hours information displayed on a watchlist column for inspiration but my coding knowledge has hit a wall. Does anyone know if it is possible to modify the above watchlist to display the pre-market price's relationship to the next day's pivots?
 
D

Deleted2090

Guest
@Jtru94 Yes, the period is set to DAY. I am not sure how it would be on lower timeframes though sorry. Also I avoid pre-market prices as RTH is more meaningful to me as a daytrader.
 

Jtru94

New member
@sobiswas That's ok. The reason I ask is because I am a huge fan of Frank Ochoa's Secrets of a Pivot Boss book, and he says that where a stock opens the next day in relationship to the next day's pivots and the previous day's pivots gives you loads of information about the potential price action for the day. Of course, I can review this information in the few minutes before the opening bell by looking at the extended hours. Now I can get that information right at the opening bell with your watchlist, but in case I miss any good setups from stocks on my watchlist and until the day that I can afford a faster computer and faster internet, the opening bell makes my computer take flight and by the time the watchlists display the current information, I found myself missing some great opening moves. I thought that If I could modify your watchlist to work in the pre-market as well, it might make my job a little easier. In any case, your watchlist has been VERY helpful!

And as a thank you, I made a watchlist for camarilla pivots (D):

Rich (BB code):
def AggregationPeriod = AggregationPeriod.DAY;

def closeValue = close(period = AggregationPeriod)[1];
def highValue = high(period = AggregationPeriod)[1];
def lowValue = low(period = AggregationPeriod)[1];
def dayrange = highvalue - lowValue;

def R5 = (highValue / lowValue) * closeValue;
def R4 = closeValue + dayrange * (1.1) / 2;
def R3 = closeValue + dayrange * (1.1) / 4;
def S3 = closeValue - dayrange * (1.1) / 4;
def S4 = closeValue - dayrange * (1.1) / 2;
def R6 = R5 + 1.168 * (R5 - R4);
def S5 = (closeValue - (R5 - closeValue));
def S6 = (closeValue - (R6 - closeValue));

#ranges
input range = 3.0;
def deviation = Sqrt(1/253);

def R6range1 = R6 + (deviation*range);
def R6range2 = R6 - (deviation*range);
def R5range1 = R5 + (deviation*range);
def R5range2 = R5 - (deviation*range);
def R4range1 = R4 + (deviation*range);
def R4range2 = R4 - (deviation*range);
def R3range1 = R3 + (deviation*range);
def R3range2 = R3 - (deviation*range);
def S3range1 = S3 + (deviation*range);
def S3range2 = S3 - (deviation*range);
def S4range1 = S4 + (deviation*range);
def S4range2 = S4 - (deviation*range);
def S5range1 = S5 + (deviation*range);
def S5range2 = S5 - (deviation*range);
def S6range1 = S6 + (deviation*range);
def S6range2 = S6 - (deviation*range);

def current_price = close(period = AggregationPeriod);
plot day_close = close(period = AggregationPeriod);

#scan for price in ranges
def zoneR6 = between(current_price,R6range2,R6range1);
def zoneR5 = between(current_price,R5range2,R5range1);
def zoneR4 = between(current_price,R4range2,R4range1);
def zoneR3 = between(current_price,R3range2,R3range1);
def zoneS3 = between(current_price,S3range2,S3range1);
def zoneS4 = between(current_price,S4range2,S4range1);
def zoneS5 = between(current_price,S5range2,S5range1);
def zoneS6 = between(current_price,S6range2,S6range1);

##Watchlist

AssignBackgroundColor(
     if zoneR6 and (current_price >= R6) then Color.DARK_GREEN else if zoneR6 and (current_price <= R6) then Color.DARK_RED
else if zoneR5 and (current_price >= R5) then Color.DARK_GREEN else if zoneR5 and (current_price <= R5) then Color.DARK_RED
else if zoneR4 and (current_price >= R4) then Color.DARK_GREEN else if zoneR4 and (current_price <= R4) then Color.DARK_RED
else if zoneR3 and (current_price >= R3) then Color.DARK_GREEN else if zoneR3 and (current_price <= R3) then Color.DARK_RED
else if zoneS3 and (current_price >= S3) then Color.DARK_GREEN else if zoneS3 and (current_price <= S3) then Color.DARK_RED
else if zoneS4 and (current_price >= S4) then Color.DARK_GREEN else if zoneS4 and (current_price <= S4) then Color.DARK_RED
else if zoneS5 and (current_price >= S5) then Color.DARK_GREEN else if zoneS5 and (current_price <= S5) then Color.DARK_RED
else if zoneS6 and (current_price >= S6) then Color.DARK_GREEN else if zoneS6 and (current_price <= S6) then Color.DARK_RED
 else Color.BLACK);

AddLabel(yes,
if zoneR6 then AsText(R6, NumberFormat.TWO_DECIMAL_PLACES)+"_R6" else
if zoneR5 then AsText(R5, NumberFormat.TWO_DECIMAL_PLACES)+"_R5" else
if zoneR4 then AsText(R4, NumberFormat.TWO_DECIMAL_PLACES)+"_R4" else
if zoneR3 then AsText(R3, NumberFormat.TWO_DECIMAL_PLACES)+"_R3" else
if zoneS3 then AsText(S3, NumberFormat.TWO_DECIMAL_PLACES)+"_S3" else
if zoneS4 then AsText(S4, NumberFormat.TWO_DECIMAL_PLACES)+"_S4" else
if zoneS5 then AsText(S5, NumberFormat.TWO_DECIMAL_PLACES)+"_S5" else
if zoneS6 then AsText(S6, NumberFormat.TWO_DECIMAL_PLACES)+"_S6" else if
between(current_price,S3,R3) then "MID" else if
between(current_price,R3,R4) then "R3_R4" else if
between(current_price,R4,R5) then "R4_R5" else if
between(current_price,R5,R6) then "R5_R6" else if
between(current_price,S4,S3) then "S4_S3" else if
between(current_price,S5,S4) then "S5_S4" else if
between(current_price,S6,S5) then "S6_S5"  else "", color.white);
 

Similar threads

Top