Can't get code to work for a super simple condition. I mean...super, super simple...

greco26

Active member
Hi All,

I'm having a strange issue when I want to combine 2 very simple conditions. In logical terms I want "show me the highest bar number of this pattern but not past xxx bar number. Each condition works fine on its own but when I combine the two conditions, nada. I included troubleshooting bubbles which shows everything is working as it should yet the combined condition def DispLowBN = bn == highestall(DispLow) and !BnPastCross ; just won't work. Odd. Any thoughts for you super smart folks?


Code:
def bn = BarNumber();
def TimeFrame = getaggregationperiod();
def DayOpen = open(period=aggregationperiod.day);
def DayClose = close(period=aggregationperiod.day);
def IsRed = DayClose < DayOpen;

#######################################################################################################

#PIVOT SECTION

input LBPeriod = 10;
#--------------------------------------------------------------

def HH = Highest(high(Period = TimeFrame), LBPeriod);
def marketHigh1 = if HH > HH[-LBPeriod] then HH else HH[-LBPeriod];
def markedHigh1 = high(Period = TimeFrame) == marketHigh1;
def PivotHigh = CompoundValue(1, if IsNaN( markedHigh1) then PivotHigh[1] else if markedHigh1 then high(Period = TimeFrame) else PivotHigh[1], high(Period = TimeFrame));
def LastPivotHigh = if PivotHigh != PivotHigh[1] then PivotHigh[1] else LastPivotHigh[1];
plot PH = PivotHigh;

#--------------------------------------------------------------

def LL = Lowest(low(Period = TimeFrame), LBPeriod);
def marketLow1 = if LL < LL[-LBPeriod] then LL else LL[-LBPeriod];
def markedLow1 = low(Period = TimeFrame) == marketLow1;
def PivotLow = CompoundValue(1, if IsNaN(markedLow1) then PivotLow[1] else if markedLow1 then low(Period = TimeFrame) else PivotLow[1], low(Period = TimeFrame));

plot PL = PivotLow ;

#--------------------------------------------------------------
PH.SetPaintingStrategy(PaintingStrategy.DASHES);
PH.SetDefaultColor(Color.GREEN);

#--------------------------------------------------------------
PL.SetPaintingStrategy(PaintingStrategy.DASHES);
PL.SetDefaultColor(Color.RED);

#######################################################################################################

#PATTERN SECTION

def PHCount = if bn == 1 then 0 else if high == ph then PHCount[1] +1 else PHCount[1];
def PHCountMax = highestall(PHCount);
def PHRevCount = PHCountMax-PHCount +1;

def PHBN = if PHRevCount==1 and PHRevCount[1]==2 then bn else PHBN[1];
def PHCrossbn = if PHRevCount==1 and high crosses above PH then bn else PHCrossbn[1];
def PHPrice = PH;

def LastPHBN = if PHRevCount==2 and PHRevCount[1]==3 then bn else LastPhBN[1];
def LastPHPrice = if bn == LastPHBN then High else LastPHPrice[1];


def LastPHCross = if PHRevCount==2 and high crosses above LastPHPrice then bn else LastPHCross[1];
AddChartBubble(bn==LastPHCross, high, LastPHCross, Color.green,yes);

#Find the pattern
def DispLow = if low < low[1] and low < low[-1] then bn else 0 ;
AddChartBubble(DispLow, low, DispLow, Color.red);

#Exclude bar numbers above xxx
def BnPastCross = BN >= LastPHCross;
AddChartBubble(BnPastCross, low, BnPastCross, Color.white);

#show be the highest DispLow but not past the BN from LastPHCross
def DispLowBN = bn == highestall(DispLow) and !BnPastCross ;
AddChartBubble(DispLowBN, low, DispLowBN, Color.cyan);
 
Solution
Never mind. This is the second night in a row that I sat here troubleshooting for hours and then finally decided to post to the community, only to figure it out 10 minutes after I posted. The issue was that I didn't use HighestAll. Thanks anyway if you've gotten this far and see that I've totally wasted the last minute of your time reading this, Im super sorry!
Code:
#show be the highest DispLow but not past the BN from LastPHCross
def DispLowBN =  if BN >= highestall(LastPHCross) then 0 else DispLow ;
def Hall = highestall(DispLowBN);
AddChartBubble(bn==Hall, low, Hall, Color.cyan);

;-)
Never mind. This is the second night in a row that I sat here troubleshooting for hours and then finally decided to post to the community, only to figure it out 10 minutes after I posted. The issue was that I didn't use HighestAll. Thanks anyway if you've gotten this far and see that I've totally wasted the last minute of your time reading this, Im super sorry!
Code:
#show be the highest DispLow but not past the BN from LastPHCross
def DispLowBN =  if BN >= highestall(LastPHCross) then 0 else DispLow ;
def Hall = highestall(DispLowBN);
AddChartBubble(bn==Hall, low, Hall, Color.cyan);

;-)
 
Solution

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

Never mind. This is the second night in a row that I sat here troubleshooting for hours and then finally decided to post to the community, only to figure it out 10 minutes after I posted. The issue was that I didn't use HighestAll. Thanks anyway if you've gotten this far and see that I've totally wasted the last minute of your time reading this, Im super sorry!
Code:
#show be the highest DispLow but not past the BN from LastPHCross
def DispLowBN =  if BN >= highestall(LastPHCross) then 0 else DispLow ;
def Hall = highestall(DispLowBN);
AddChartBubble(bn==Hall, low, Hall, Color.cyan);

;-)

isn't that how it goes? stare at something too long, do something else, come back and figure it out...


your bubbles are above their insertion point, pointing down, covering up candles, because the 5th parameter is missing.
5th parameter default is yes, point down.

add a no after color, to make a bubble point up, not down.

AddChartBubble(bn==Hall, low, Hall, Color.cyan, no);
 
Haha, yes indeed. Thats always how it goes. Im still having 1 issue on the code for something else now but I'm troubleshooting it today/tonight to see if I can figure it out. If I can't, I will post to this thread to ask for help. Also, thanks for the note about the bubble. I have a tendency to leave the 5th parameter off but I know its there as an option.
 
The following code is supposed to trigger when all of the conditions of ShortTrigger is met. The issue I'm having is that only the last instance (yellow bubble and then the entry after the yellow bubble) is correctly meeting the ShortTrigger requirements. Anything historical is not triggering at the correct locations. In logical terms, the sequence is as follows;

if price crosses above the pivot high (red bubble named "High Stop Run")
and then price falls below the very last "displacement low" red bubble
and then the high of any bar that crosses above the bearish low of the fair value gap (magenta lines),
then condition is true.

I think it has something to do with the arrays or the way I am counting the bars to find the last 'displacement low' but I can't figure it out. Any thoughts?

Tim






Code:
##########################################################################




def bn = BarNumber();

input Trend_TimeFrame = {default Current, Five, Ten, Fifteen, Twenty, Thirty, Sixty, Two_Hour, Four_Hour, Day};

def TimeFrame;
switch (Trend_TimeFrame) {
case Current:
    TimeFrame = GetAggregationPeriod();
case Five:
    TimeFrame = AggregationPeriod.FIVE_MIN;
case Ten:
    TimeFrame = AggregationPeriod.TEN_MIN;
case Fifteen:
    TimeFrame = AggregationPeriod.FIFTEEN_MIN;
case Twenty:
    TimeFrame = AggregationPeriod.TWENTY_MIN;
case Thirty:
    TimeFrame = AggregationPeriod.THIRTY_MIN;
case Sixty:
    TimeFrame = AggregationPeriod.HOUR;
case Two_Hour:
    TimeFrame = AggregationPeriod.TWO_HOURS;
case Four_Hour:
    TimeFrame = AggregationPeriod.FOUR_HOURS;
case Day:
    TimeFrame = AggregationPeriod.DAY;
}

AddLabel(yes, Trend_TimeFrame, Color.WHITE);



def DayOpen = open(period=aggregationperiod.day);
def DayClose = close(period=aggregationperiod.day);
def IsGreen = DayClose > DayOpen;
def IsRed = DayClose < DayOpen;

#######################################################################################################

#PIVOT SECTION

input LBPeriod = 10;

#--------------------------------------------------------------

def HH = Highest(high(Period = TimeFrame), LBPeriod);
def marketHigh1 = if HH > HH[-LBPeriod] then HH else HH[-LBPeriod];
def markedHigh1 = high(Period = TimeFrame) == marketHigh1;

def PivotHigh = CompoundValue(1, if IsNaN( markedHigh1) then PivotHigh[1] else if markedHigh1 then high(Period = TimeFrame) else PivotHigh[1], high(Period = TimeFrame));

def LastPivotHigh = if PivotHigh != PivotHigh[1] then PivotHigh[1] else LastPivotHigh[1];



#-------------------------------------------------------------

plot PH = PivotHigh;



#--------------------------------------------------------------

def LL = Lowest(low(Period = TimeFrame), LBPeriod);
#--------------------------------------------------------------
def marketLow1 = if LL < LL[-LBPeriod] then LL else LL[-LBPeriod];
def markedLow1 = low(Period = TimeFrame) == marketLow1;
def PivotLow = CompoundValue(1, if IsNaN(markedLow1) then PivotLow[1] else if markedLow1 then low(Period = TimeFrame) else PivotLow[1], low(Period = TimeFrame));

plot PL = PivotLow ;

#--------------------------------------------------------------
PH.SetPaintingStrategy(PaintingStrategy.DASHES);
PH.SetDefaultColor(Color.GREEN);

#--------------------------------------------------------------
PL.SetPaintingStrategy(PaintingStrategy.DASHES);
PL.SetDefaultColor(Color.RED);



##LONG

def PLCount = if bn == 1 then 0 else if low == pl then PLCount[1] + 1 else PLCount[1];
def PLCountMax = HighestAll(PLCount);
def PLRevCount = PLCountMax - PLCount + 1;

def PLBN = if PLRevCount == 1 and PLRevCount[1] == 2 then bn else PLBN[1];
def PLCrossbn = if PLRevCount == 1 and low crosses below PL then bn else PLCrossbn[1];
def PLPrice = PL;

def LastPLBN = if PLRevCount == 2 and PLRevCount[1] == 3 then bn else LastPLBN[1];
def LastPLPrice = if bn == LastPLBN then low else LastPLPrice[1];
def LastPLCross = if PLRevCount == 2 and low crosses below LastPLPrice then bn else LastPLCross[1];

#Find the pattern
def DispHigh = if high > high[1] and high > high[-1] then bn else 0 ;
#Exclude bar numbers above xxx
def BnPastCrossHi = bn >= LastPLCross;
#show be the highest DispLow but not past the BN from LastPHCross
def DispHighBN =  if bn >= HighestAll(LastPLCross) then 0 else DispHigh ;
def HallDispHighBN = HighestAll(DispHighBN);

def HallDispHighPrice = if bn == HallDispHighBN then high else HallDispHighPrice[1];
def LongWatchActivated = if bn > BnPastCrossHi and high crosses above HallDispHighPrice  then 1 else LongWatchActivated[1];


AddChartBubble(bn == LastPLCross, High, "Low Stop Run", Color.Green);
AddChartBubble(bn == HallDispHighBN, High, "Displacement High", Color.Green);
AddChartBubble(LongWatchActivated, high, "Long Watch Active", Color.GREEN);


##SHORT

def PHCount = if bn == 1 then 0 else if high == ph then PHCount[1] + 1 else PHCount[1];
def PHCountMax = HighestAll(PHCount);
def PHRevCount = PHCountMax - PHCount + 1;

def PHBN = if PHRevCount == 1 and PHRevCount[1] == 2 then bn else PHBN[1];
def PHCrossbn = if PHRevCount == 1 and high crosses above PH then bn else PHCrossbn[1];
def PHPrice = PH;

def LastPHBN = if PHRevCount == 2 and PHRevCount[1] == 3 then bn else LastPHBN[1];
def LastPHPrice = if bn == LastPHBN then high else LastPHPrice[1];
def LastPHCross = if PHRevCount == 2 and high crosses above LastPHPrice then bn else LastPHCross[1];


#Find the pattern
def DispLow = if low < low[1] and low < low[-1] then bn else 0 ;
#Exclude bar numbers above xxx
def BnPastCross = bn >= LastPHCross;
#show be the highest DispLow but not past the BN from LastPHCross
def DispLowBN =  if bn >= HighestAll(LastPHCross) then 0 else DispLow ;
def HallDispLowBN = HighestAll(DispLowBN);

def HallDispLowPrice = if bn == HallDispLowBN then low else HallDispLowPrice[1];
def ShortWatchActivated = if bn > BnPastCross and low crosses below HallDispLowPrice  then 1 else ShortWatchActivated[1];


AddChartBubble(bn == LastPHCross, high, "High Stop Run", Color.RED);
AddChartBubble(bn == HallDispLowBN, low, "Displacement Low", Color.RED);
#AddChartBubble(ShortWatchActivated, low, "Short Watch Active", Color.RED);



# FVG  Fair Value Gaps Bearish & Bullish Levels
# @TradeForOpp  5/6/2022
input threshold = 0.001;
def aggregation = GetAggregationPeriod();
#input aggregation = AggregationPeriod.fifteen_min;

def fvgBear = if low(period = aggregation)[2] - high(period = aggregation) > 0 then 1 else 0;
def fvgBearPctg = if AbsValue((high(period = aggregation) - low(period = aggregation)[2]) / low(period = aggregation)[2]) > threshold then 1 else 0;

def fvgBull = if low(period = aggregation) - high(period = aggregation)[2] > 0 then 1 else 0;
def fvgBullPctg = if AbsValue((low(period = aggregation) - high(period = aggregation)[2]) / high(period = aggregation)[2]) > threshold then 1 else 0;

def fvgBearH = if  fvgBear and fvgBearPctg then low(period = aggregation)[2] else 0;
def fvgBearL = if fvgBearH then high(period = aggregation) else Double.NaN;

def fvgBullH = if  fvgBull and fvgBearPctg then low(period = aggregation) else 0;
def fvgBullL = if fvgBullH then high(period = aggregation)[2] else Double.NaN;


def fvgBearMem1 = if fvgBearH then fvgBearH else fvgBearMem1[1];
def fvgBearMem2 = if fvgBearH then fvgBearL else fvgBearMem2[1];

#def fvgBearMem2 = if fvgBearH then fvgBearL else if high(period = aggregation) > fvgBearMem2[1] then Double.NaN else fvgBearMem2[1];




def fvgBullMem1 = if fvgBullH then fvgBullH else fvgBullMem1[1];
def fvgBullMem2 = if fvgBullH then fvgBullL else fvgBullMem2[1];
#def fvgBullMem1 = if fvgBullH then fvgBullH else if low(period = aggregation) < fvgBullMem1[1] then Double.NaN else fvgBullMem1[1];
#def fvgBullMem2 = if fvgBullH then fvgBullL else if low(period = aggregation) < fvgBullMem2[1] then Double.NaN else fvgBullMem2[1];

plot fvgBearM1 = fvgBearMem1;
plot fvgBearM2 = fvgBearMem2;

plot fvgBullM1 = fvgBullMem1;
plot fvgBullM2 = fvgBullMem2;

fvgBearM1.SetLineWeight(3);
fvgBearM2.SetLineWeight(3);
fvgBearM1.SetDefaultColor(Color.MAGENTA);
fvgBearM2.SetDefaultColor(Color.MAGENTA);
fvgBearM1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fvgBearM2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

fvgBullM1.SetLineWeight(3);
fvgBullM2.SetLineWeight(3);
fvgBullM1.SetDefaultColor(Color.YELLOW);
fvgBullM2.SetDefaultColor(Color.YELLOW);
fvgBullM1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fvgBullM2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

def BearBreach =  ShortWatchActivated and high crosses above fvgBearM2 ;
def BearBreachBN = if BearBreach then bn else BearBreachBN[1];
AddChartBubble(BearBreach, high, BearBreach, Color.YELLOW);

###TRIGGERS###
#def LongTrigger = trendlong and (TwoTwoLong or HammerTwoLong or  OutsideGreenTwoLong) ;
def ShortTrigger = BearBreach;


#def LE = TwoTwoLong or HammerTwoLong or OutsideGreenTwoLong;
#def LEBN = if LE then bn else LEBN[1];
#def LowStop = if bn == LEBN then low else LowStop[1];

#def SE = FracShort;
def SE = highestall(ShortTrigger) ;
def SEBN = if SE then bn else SEBN[1];
def LAllSEBN = LowestAll(BearBreachBN);
def HighStop = if bn == LAllSEBN then low[4] else 0;


#def lxstop = low < LowStop;
#def lx = high > Entry * takeprofitconverstionLong ;

def sxstop = high > HighStop;
#def sx = low < Entry * takeprofitconverstionshort ;

#AddOrder(OrderType.BUY_TO_OPEN, LE[-1], arrowcolor = Color.WHITE, tickcolor = Color.CYAN, name = "LE", price = high);
#AddOrder(OrderType.SELL_TO_CLOSE, low[-1] crosses below low, tickcolor = Color.WHITE, arrowcolor = Color.MAGENTA, name = "LX Profit", price = low);
#AddOrder(OrderType.SELL_TO_CLOSE, low crosses below LowStop, tickcolor = Color.WHITE, arrowcolor = Color.GREEN, name = "LX Stop", price = high);


AddOrder(OrderType.SELL_TO_OPEN, highestall(shorttrigger), arrowcolor = Color.WHITE, tickcolor = Color.YELLOW, name = "SE", price = high[0]);
#AddOrder(OrderType.BUY_TO_CLOSE, high[-1] crosses above fvgBearMem1, tickcolor = Color.WHITE, arrowcolor = Color.Green, name = "SX Profit", price = fvgBearMem1);
AddOrder(OrderType.BUY_TO_CLOSE, high[-1] crosses above high, tickcolor = Color.WHITE, arrowcolor = Color.MAGENTA, name = "SX Stop", price = High);
 
@halcyonguy , I've been troubleshooting this for hours but still can't figure it out. Let me know if you might have some free time to have a look. If not, then no worries at all. The amount of times you've helped teach me in the past year has been a ton so I owe you so much gratitude already already!!
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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