Help with getting historical variable. My code is not right.

greco26

Active member
Hi All! The following code is for a watchlist column Im using intraday. In lines 60 and 61 of my code below (def priorRevLong and def priorRevShort) I am trying to identify older occasions when RevShort and RevLong has occurred. I don't think my code is working as I have it written. Can anyone help point me in the right direction? Thanks in advance and, as always, you guys ROCK!!

Code:
def H = high;
def L = low;
def C = close;
def O = open;



def bn = BarNumber();
def isRollover = GetYYYYMMDD() != GetYYYYMMDD()[1];
def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD());
def firstBarOfDay = if (beforeStart[1] == 1 and beforeStart == 0) or (isRollover and beforeStart == 0) then bn else firstBarOfDay[1];
def DayOpen = if bn == firstbarofday then open else DayOpen[1];
def DayUp = c > DayOpen;
def DayDn = c < DayOpen;

def inside = H < H[1] and L > L[1] ;
#for reversal back to long side
def twodnbullish =  H < H[1] and L < L[1];# and (c > fiftypct or c > o); #for reversal back to long side
def twoupbearish =  L > L[1] and H > H[1];# and (c < fiftypct or c < o); #for reversal back to short side

#def risk = 10;
def longsig = twodnbullish or inside;
def shortsig = twoupbearish or inside;

def sequenceCount = 2;
def maxSideLength = sequenceCount + 10;
def upRightSide = fold i1 = 1 to maxSideLength + 1 with count1 while count1 != sequenceCount and count1 != -1 do
if GetValue(H, -i1, -maxSideLength) > H or (GetValue(H, -i1, -maxSideLength) == H and count1 == 0) then -1
else if GetValue(H, -i1, -maxSideLength) < H then count1 + 1 else count1;
def upLeftSide = fold i2 = 1 to maxSideLength + 1 with count2 while count2 != sequenceCount and count2 != -1 do
if GetValue(H, i2, maxSideLength) > H or (GetValue(H, i2, maxSideLength) == H and count2 >= 1) then -1
else if GetValue(H, i2, maxSideLength) < H then count2 + 1 else count2;
def downRightSide = fold i3 = 1 to maxSideLength + 1 with count3 while count3 != sequenceCount and count3 != -1 do
if GetValue(L, -i3, -maxSideLength) < L or (GetValue(L, -i3, -maxSideLength) == L and count3 == 0) then -1
else if GetValue(H, -i3, -maxSideLength) > L then count3 + 1 else count3;
def downLeftSide = fold i4 = 1 to maxSideLength + 1 with count4 while count4 != sequenceCount and count4 != -1 do
if GetValue(L, i4, maxSideLength) < L or (GetValue(L, i4, maxSideLength) == L and count4 >= 1) then -1
else if GetValue(L, i4, maxSideLength) > L then count4 + 1 else count4;
def UpFractal = if upRightSide == sequenceCount and upLeftSide == sequenceCount then H else Double.NaN;
def DownFractal = if downRightSide == sequenceCount and downLeftSide == sequenceCount then L else Double.NaN;
def UpFractalBN = if !IsNaN(UpFractal) then bn else UpFractalBN[1];
def DnFractalBN = if !IsNaN(DownFractal) then bn else DnFractalBN[1];
def UpH = GetValue(H, bn - UpFractalBN); #if bn == UpFractalBN then H else UpH[1];
def DnL = GetValue(L, bn - DnFractalBN); #if bn == DnFractalBN then L else DnL[1];

def BFrange = UpH - DnL;
def NormalizedClose = C - DnL;

def calc = (NormalizedClose / BFrange)*100;

def Above = calc >= 100;
#def Seventy = calc >= 70 and calc < 100;
#def Mid = calc >= 30 and calc < 70;
#def Thirty = calc >0 and calc < 30;
def Below = calc <= 0;

def revshort = daydn and Above and shortsig;
def revlong = dayup and Below and longsig ;

def priorRevLong =  if revlong then 777 else if revlong[1] then 1 else if revlong[2] then 2 else if revlong[3] then 3 else if revlong[4] then 4 else if revlong[5] then 5 else if revlong[6] then 6 else if revlong[7] then 7 else 0;
def priorRevshort = if revshort then 888 else if revshort[1] then 1 else if revshort[2]  then 2 else if revshort[3] then 3 else if revshort[4] then 4 else if revshort[5] then 5 else if revshort[6] then 6 else if revshort[7] then 7 else 0;

addlabel(yes,if dayup and priorrevlong == 777 then "🔃" else if dayup and priorrevlong != 0 then "👆"  else if daydn and priorrevshort == 888 then "🔃" else if daydn and priorrevshort != 0 then "👇"  else " ", if dayup and priorrevlong == 777 then color.cyan else if dayup and priorrevlong != 0 then color.green else if daydn and priorrevshort == 888 then color.magenta else if daydn and priorrevshort != 0 then color.red else color.black );
 
Did you write this yourself? Not shitting on you, but good job. Coding is difficult. The problem is your if/else statements. You wrote `if {statement} then 1 else if {statement} then 2 else if {statement} then 3.... You cannot write if/else statements like that. Logically, it makes sense to a human, but a computer can only handle 1's and 0's. Ill give you a hint (you can remove the numbers):

1. if {...} then {...}
2. else if {...} then {...}
3. else if {...} then {...}
4. else if {...} then {...}
5. else if {...} then {...}
6. else {};

Honestly it was kinda hard to read, maybe see if that helps and repost it
 
@moxiesmiley thank you for your help! Yes, I wrote parts of this while other parts I pulled from a Williams Fractal candlestick pattern in TOS and I also got some help on a few lines from the forum.

Ok, I made some changes and grouped everything together however I think the problem is that the historical revlong or revshort code isn't properly identifying the prior instance of the variable (ie, revlong[1] or revlong[2] or revlong[3] or revlong[4] or revlong[5] or revlong[6] or revlong[7]). Its recognizing something but I doing think what its recognizing is actually the valid prior variable I intended. Does this make sense? So, for example, revlong[2] isn't actually pulling a valid occurrence of when revlong occurred 2 times prior to the most recent revlong. Hope this is making sense....



Code:
# Broadening Formation with Actionalable signals at TOP or BOTTOM of the Tri Watchlist Column


#Created by Tim G
#v 01/30/2022


def H = high;
def L = low;
def C = close;
def O = open;



def bn = BarNumber();
def isRollover = GetYYYYMMDD() != GetYYYYMMDD()[1];
def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD());
def firstBarOfDay = if (beforeStart[1] == 1 and beforeStart == 0) or (isRollover and beforeStart == 0) then bn else firstBarOfDay[1];
def DayOpen = if bn == firstbarofday then open else DayOpen[1];
def DayUp = c > DayOpen;
def DayDn = c < DayOpen;

def inside = H < H[1] and L > L[1] ;
#for reversal back to long side
def twodnbullish =  H < H[1] and L < L[1];# and (c > fiftypct or c > o); #for reversal back to long side
def twoupbearish =  L > L[1] and H > H[1];# and (c < fiftypct or c < o); #for reversal back to short side

#def risk = 10;
def longsig = twodnbullish or inside;
def shortsig = twoupbearish or inside;

def sequenceCount = 2;
def maxSideLength = sequenceCount + 10;
def upRightSide = fold i1 = 1 to maxSideLength + 1 with count1 while count1 != sequenceCount and count1 != -1 do
if GetValue(H, -i1, -maxSideLength) > H or (GetValue(H, -i1, -maxSideLength) == H and count1 == 0) then -1
else if GetValue(H, -i1, -maxSideLength) < H then count1 + 1 else count1;
def upLeftSide = fold i2 = 1 to maxSideLength + 1 with count2 while count2 != sequenceCount and count2 != -1 do
if GetValue(H, i2, maxSideLength) > H or (GetValue(H, i2, maxSideLength) == H and count2 >= 1) then -1
else if GetValue(H, i2, maxSideLength) < H then count2 + 1 else count2;
def downRightSide = fold i3 = 1 to maxSideLength + 1 with count3 while count3 != sequenceCount and count3 != -1 do
if GetValue(L, -i3, -maxSideLength) < L or (GetValue(L, -i3, -maxSideLength) == L and count3 == 0) then -1
else if GetValue(H, -i3, -maxSideLength) > L then count3 + 1 else count3;
def downLeftSide = fold i4 = 1 to maxSideLength + 1 with count4 while count4 != sequenceCount and count4 != -1 do
if GetValue(L, i4, maxSideLength) < L or (GetValue(L, i4, maxSideLength) == L and count4 >= 1) then -1
else if GetValue(L, i4, maxSideLength) > L then count4 + 1 else count4;
def UpFractal = if upRightSide == sequenceCount and upLeftSide == sequenceCount then H else Double.NaN;
def DownFractal = if downRightSide == sequenceCount and downLeftSide == sequenceCount then L else Double.NaN;
def UpFractalBN = if !IsNaN(UpFractal) then bn else UpFractalBN[1];
def DnFractalBN = if !IsNaN(DownFractal) then bn else DnFractalBN[1];
def UpH = GetValue(H, bn - UpFractalBN); #if bn == UpFractalBN then H else UpH[1];
def DnL = GetValue(L, bn - DnFractalBN); #if bn == DnFractalBN then L else DnL[1];

def BFrange = UpH - DnL;
def NormalizedClose = C - DnL;

def calc = (NormalizedClose / BFrange)*100;

def Above = calc >= 100;
#def Seventy = calc >= 70 and calc < 100;
#def Mid = calc >= 30 and calc < 70;
#def Thirty = calc >0 and calc < 30;
def Below = calc <= 0;

def revlong = dayup and Below and longsig ;
def revshort = daydn and Above and shortsig;

def priorRevLong =  revlong[1] or revlong[2] or revlong[3] or revlong[4] or revlong[5] or revlong[6] or revlong[7];
def priorRevshort = revshort[1] or revshort[2]  or revshort[3] or revshort[4] or revshort[5] or revshort[6] or revshort[7];

addlabel(yes,if dayup and revlong then "🔃" else if daydn and revshort then "🔃" else if dayup and priorrevlong then "👆"  else if daydn and priorrevshort then "👇"  else " ", if dayup and revlong then color.cyan else if daydn and revshort then color.magenta else if dayup and priorrevlong then color.green else if daydn and priorrevshort then color.red else color.black );
 
actually, I think it may be easier to do another way altogether. All I really want to know is if revlong or revshort has occurred over the prior 10 bars then yes else no. I just don't know how to write that syntax.
 
Did you write this yourself? Not shitting on you, but good job. Coding is difficult. The problem is your if/else statements. You wrote `if {statement} then 1 else if {statement} then 2 else if {statement} then 3.... You cannot write if/else statements like that. Logically, it makes sense to a human, but a computer can only handle 1's and 0's. Ill give you a hint (you can remove the numbers):

1. if {...} then {...}
2. else if {...} then {...}
3. else if {...} then {...}
4. else if {...} then {...}
5. else if {...} then {...}
6. else {};

Honestly it was kinda hard to read, maybe see if that helps and repost it
not sure what you are trying to say here,
you say someone's if then formatting is wrong, when it isn't.
then give an example, that is the same as their code, except it includes indexing numbers for some reason, which adds confusion.

in VBA , end if is used to close each one. which creates nicely formatted code.

in thinkscript, if thens can be combined and strung along. there is no command to close them. it may seem messy, but it works.
 
not sure what you are trying to say here,
you say someone's if then formatting is wrong, when it isn't.
then give an example, that is the same as their code, except it includes indexing numbers for some reason, which adds confusion.

in VBA , end if is used to close each one. which creates nicely formatted code.

in thinkscript, if thens can be combined and strung along. there is no command to close them. it may seem messy, but it works.
def priorRevLong = if revlong then 777 else if revlong[1] then 1 else if revlong[2] then 2 else if revlong[3] then 3 else if revlong[4] then 4 else if revlong[5] then 5 else if revlong[6] then 6 else if revlong[7] then 7 else 0;

I quickly looked over his code and saw his if/else statements. I rewrote it below for easier reading. Can you write thinkscript with then [#]? I always learned it as if/ else if/ elif/ else. If it's incorrect advice apologize

def priorRevLong = if revlong then 777,
else if revlong[1] then 1 ,
else if revlong[2] then 2 ,
else if revlong[3] then 3 ,
else if revlong[4] then 4 ,
else if revlong[5] then 5 ,
else if revlong[6] then 6 ,
else if revlong[7] then 7 ,
else 0;
 
ok, so im getting closer. Need help with the syntax for the last line if possible.

def revlong = dayup and Below and longsig ;
def revlongBN = if BN == revlong then BN else revlongBN[1];

The next step is what I don't know how the write the syntax for:
def HasOccuredInLast10Bars = if BN < revlongBN then has revlong occurred in any of the past 10 bars older than revlongBN?
 
@greco26
BN is BarNumber() and revlong is a boolean condition, correct?
Why are you comparing them? def revlongBN = if BN == revlong then BN else revlongBN[1];
ahh, these are the things that I struggle with being new to coding. Basically, I want to know when the last time the same event occurred. I guess I shouldn’t be looking for the BN but rather the event as a whole? Not sure how to write that.
 
@greco26
Since revlong is a condition test on its own, you just need to check revlong.
If revlong is true then you want to assign BN to revlongBN.

def revlongBN = if revlong then BN else revlongBN[1];
 
@greco26
Since revlongBN is a running variable that changes to the current bar number every time revlong is true then when revlong is true, revlongBN is the current bar number and revlongBn[1] is the bar number of the previous bar at which revlong was true.

def revlongBN = if revlong then BN else revlongBN[1];
def HasOccuredInLast10Bars = if revlong then revlongBN - revlongBN[1] <= 10 else HasOccuredInLast10Bars[1];
 
Ah, ok. I’ll give this a try. This will only return true if revlong occurred in the previous 10 bars from the current revlong? The final HasOccuredInLast10Bars[1] is throwing my thinking off as I would think that would keep looking for an occurrence even past the prior 10 bars of the current revlong until it finds the scenario.
 
Crap, actually I messed this up. I want to know if a revlong occurred within the last 10 bars regardless if the current bar is a revlong…. Would I write it as follows?

HasOccuredInLast10Bars = if BN then BN - revlongBN <= 10 else HasOccuredInLast10Bars[1];
 
@greco26
Since revlongBN is a running variable that changes to the current bar number every time revlong is true then when revlong is true, revlongBN is the current bar number and revlongBn[1] is the bar number of the previous bar at which revlong was true.

def revlongBN = if revlong then BN else revlongBN[1];
def HasOccuredInLast10Bars = if revlong then revlongBN - revlongBN[1] <= 10 else HasOccuredInLast10Bars[1];
So I see what the error is. I want to find the prior instance of when the variable was true and not the bar before the last true instance. Currently, RevLongBN[1] is finding the bar before the last true instance. That all said, I think this would still work for what I need. All I want to do is to identify if revlong has occurred within the last 10 bars so this should still work....I think.
 
@greco26
revlongBN[1] is not the bar before the last true instance of revlong.
It is the barnumber of the bar at which revlong was true prior the to most recent instance of revlong being true,
but only if the current bar is a true instance of revlong,

All I want to do is to identify if revlong has occurred within the last 10 bars
The code in post#17,
HasOccuredInLast10Bars = BN - revlongBN <= 10;

I want to find the prior instance of when the variable was true
You have to define a second variable to carry the previous value of revlongBN when revlongBN changes at a true instance of revlong.
 

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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