S

#### sobiswas

##### New member

**VIP**

Hi,

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 https://usethinkscript.com/threads/fibonacci-pivot-points-for-thinkorswim.592/. The formula for pivots I am using is as below.

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

Can someone help me out a bit on this?

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 https://usethinkscript.com/threads/fibonacci-pivot-points-for-thinkorswim.592/. 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");
```

Last edited: