B3 Consolidation Box: Breakout / Breakdown Indicator for ThinkorSwim

Jonas J

New member
I have followed the steps to set up the scanner but I am getting many results (per my other scan criteria) and none of them show a break out within 1 bar. Some tickers broke out weeks or even months ago. Why are those showing up? How do I get just stocks that just turned green within 1 candle? (I'm sorry but I don't know how to upload a picture onto this post for better clarification) Thx
 

Mark-RO

New member
I have followed the steps to set up the scanner but I am getting many results (per my other scan criteria) and none of them show a break out within 1 bar. Some tickers broke out weeks or even months ago. Why are those showing up? How do I get just stocks that just turned green within 1 candle? (I'm sorry but I don't know how to upload a picture onto this post for better clarification) Thx
@Jonas J, that scanner is not 100% precise, especially if it's not tuned to be matched with the script that shows the results. For what time frame are you scanning? And, what are your other scanning criteria?
I would try to simplify things..scan aiming only for daily charts and fine-tune things until both the scanner and script will give a match. On the other hand; markets have been quite volatile lately, so no scan can predict things with precision under such wide swings.
 

Jonas J

New member
Thanks @Mark-RO , I actually cut the original script down, getting rid of all the plots of H and L targets and painting strategies and am now able to scan for tickers that cross above the Balance Line within whatever bar timeframe I want.
 

tenacity11

Active member
2019 Donor
Thanks @Mark-RO , I actually cut the original script down, getting rid of all the plots of H and L targets and painting strategies and am now able to scan for tickers that cross above the Balance Line within whatever bar timeframe I want.
Do you mind sharing that scan. Appreciate it
 

Jonas J

New member
@tenacity11, here you go. This needs to be loaded as a Study first, then it can be used in the Scan Tab:
Ruby:
input BarsUsedForRange = 2;
input BarsRequiredToRemainInRange = 7;
input TargetMultiple = 0.5;
input ColorPrice = yes;
input HideTargets = no;
input HideBalance = no;
input HideBoxLines = no;
input HideCloud = no;
input HideLabels = no;

# Identify Consolidation

def HH = highest(high[1], BarsUsedForRange);
def LL = lowest(low[1], BarsUsedForRange);
def maxH = highest(hh, BarsRequiredToRemainInRange);
def maxL = lowest(ll, BarsRequiredToRemainInRange);
def HHn = if maxH == maxH[1] or maxL == maxL then maxH else HHn[1];
def LLn = if maxH == maxH[1] or maxL == maxL then maxL else LLn[1];
def Bh = if high <= HHn and HHn == HHn[1] then HHn else double.nan;
def Bl = if low >= LLn and LLn == LLn[1] then LLn else double.nan;
def CountH = if isnan(Bh) or isnan(Bl) then 2 else CountH[1] + 1;
def CountL = if isnan(Bh) or isnan(Bl) then 2 else CountL[1] + 1;
def ExpH = if barnumber() == 1 then double.nan else
if CountH[-BarsRequiredToRemainInRange] >= BarsRequiredToRemainInRange then HHn[-BarsRequiredToRemainInRange] else
if High <= ExpH[1] then ExpH[1] else double.nan;

def ExpL = if barnumber() == 1 then double.nan else
if Countl[-BarsRequiredToRemainInRange] >= BarsRequiredToRemainInRange then LLn[-BarsRequiredToRemainInRange] else
if Low >= ExpL[1] then ExpL[1] else double.nan;

# Plot the High and Low of the Box; Paint Cloud

def BoxHigh = if !isnan(expL) and !isnan(ExpH) then ExpH else double.nan;
def BoxLow = if !isnan(expL) and !isnan(ExpH) then ExpL else double.nan;


# Things to the Right of a Finished Box

def eH = if barnumber() == 1 then double.nan else if !isnan(BoxHigh[1]) and isnan(BoxHigh) then BoxHigh[1] else eh[1];
def eL = if barnumber() == 1 then double.nan else if !isnan(BoxLow[1]) and isnan(BoxLow) then BoxLow[1] else el[1];
def diff = (eh - el) * TargetMultiple;

plot Balance = if isnan(boxhigh) and isnan(boxlow) then (eh+el)/2 else double.nan;
 
Last edited:

cswu1211

Member
VIP
@tenacity11, here you go. This needs to be loaded as a Study first, then it can be used in the Scan Tab:
Ruby:
input BarsUsedForRange = 2;
input BarsRequiredToRemainInRange = 7;
input TargetMultiple = 0.5;
input ColorPrice = yes;
input HideTargets = no;
input HideBalance = no;
input HideBoxLines = no;
input HideCloud = no;
input HideLabels = no;

# Identify Consolidation

def HH = highest(high[1], BarsUsedForRange);
def LL = lowest(low[1], BarsUsedForRange);
def maxH = highest(hh, BarsRequiredToRemainInRange);
def maxL = lowest(ll, BarsRequiredToRemainInRange);
def HHn = if maxH == maxH[1] or maxL == maxL then maxH else HHn[1];
def LLn = if maxH == maxH[1] or maxL == maxL then maxL else LLn[1];
def Bh = if high <= HHn and HHn == HHn[1] then HHn else double.nan;
def Bl = if low >= LLn and LLn == LLn[1] then LLn else double.nan;
def CountH = if isnan(Bh) or isnan(Bl) then 2 else CountH[1] + 1;
def CountL = if isnan(Bh) or isnan(Bl) then 2 else CountL[1] + 1;
def ExpH = if barnumber() == 1 then double.nan else
if CountH[-BarsRequiredToRemainInRange] >= BarsRequiredToRemainInRange then HHn[-BarsRequiredToRemainInRange] else
if High <= ExpH[1] then ExpH[1] else double.nan;

def ExpL = if barnumber() == 1 then double.nan else
if Countl[-BarsRequiredToRemainInRange] >= BarsRequiredToRemainInRange then LLn[-BarsRequiredToRemainInRange] else
if Low >= ExpL[1] then ExpL[1] else double.nan;

# Plot the High and Low of the Box; Paint Cloud

def BoxHigh = if !isnan(expL) and !isnan(ExpH) then ExpH else double.nan;
def BoxLow = if !isnan(expL) and !isnan(ExpH) then ExpL else double.nan;


# Things to the Right of a Finished Box

def eH = if barnumber() == 1 then double.nan else if !isnan(BoxHigh[1]) and isnan(BoxHigh) then BoxHigh[1] else eh[1];
def eL = if barnumber() == 1 then double.nan else if !isnan(BoxLow[1]) and isnan(BoxLow) then BoxLow[1] else el[1];
def diff = (eh - el) * TargetMultiple;

plot Balance = if isnan(boxhigh) and isnan(boxlow) then (eh+el)/2 else double.nan;
Just set the scan condition as: Balance is TRUE?!
 

jlpoller

New member
Hi all,

@BenTen I really like this study, but am pretty data driven and want to backtest this on multiple stocks and multiple timeframes so i can build my trading strategy around it. I recently began backtesting in TOS and get the overall concept as well as some of the syntax needed to run the backtesting. However, i am having a helluvatime peeling out certain parameters due to the complexity within the code.

Here is what i am looking to backtest on this.

Backtest
Entry = only enter the trade if during market hours and the close is greater than the boxhigh Dark Green line of the nearest box
one of the things this may be doing is giving me the results IF there is at least 1 candle within the box that is also within market hours. Which is fine, but i would like to base my entries also on the nearest pre-market boxhigh and only enter once the market is open. i am also on the west coast, so i think the times need to be 730 and 1400 for the market hours.
Profit taking = i want to backtest each target level (this is simple and i dont really need help with this part.
Stop loss = Re-entry and close below the Boxhigh Green line into the gray cloud.

Here is what i have so far that kind of works, but there are instances where it should have triggered and plotted a trade, but didn't. perplexing. I hope this was clear enough. if not, let me know and i can ellaborate. I am probably just missing something that may be obvious, but it is escaping me. any help would be great!

Here is the code for the strategy. highlighted in bold is what i have added:

declare upper;

input BarsUsedForRange = 2;

input BarsRequiredToRemainInRange = 7;

input TargetMultiple = 0.5;

input ColorPrice = yes;

input HideTargets = no;

input HideBalance = no;

input HideBoxLines = no;

input HideCloud = no;

input HideLabels = no;

# Identify Consolidation

def HH = Highest(high[1], BarsUsedForRange);

def LL = Lowest(low[1], BarsUsedForRange);

def maxH = Highest(HH, BarsRequiredToRemainInRange);

def maxL = Lowest(LL, BarsRequiredToRemainInRange);

def HHn = if maxH == maxH[1] or maxL == maxL then maxH else HHn[1];

def LLn = if maxH == maxH[1] or maxL == maxL then maxL else LLn[1];

def Bh = if high <= HHn and HHn == HHn[1] then HHn else Double.NaN;

def Bl = if low >= LLn and LLn == LLn[1] then LLn else Double.NaN;

def CountH = if IsNaN(Bh) or IsNaN(Bl) then 2 else CountH[1] + 1;

def CountL = if IsNaN(Bh) or IsNaN(Bl) then 2 else CountL[1] + 1;

def ExpH = if BarNumber() == 1 then Double.NaN else

if CountH[-BarsRequiredToRemainInRange] >= BarsRequiredToRemainInRange then HHn[-BarsRequiredToRemainInRange] else

if high <= ExpH[1] then ExpH[1] else Double.NaN;

def ExpL = if BarNumber() == 1 then Double.NaN else

if CountL[-BarsRequiredToRemainInRange] >= BarsRequiredToRemainInRange then LLn[-BarsRequiredToRemainInRange] else

if low >= ExpL[1] then ExpL[1] else Double.NaN;

# define Market Open Parameters
def rth = if SecondsfromTime(0730) >= 0 and SecondsTillTime(1400) >= 0 then 1 else 0;


# Plot the High and Low of the Box; Paint Cloud

plot BoxHigh = if !IsNaN(ExpL) and !IsNaN(ExpH) then ExpH else Double.NaN;

plot BoxLow = if !IsNaN(ExpL) and !IsNaN(ExpH) then ExpL else Double.NaN;

BoxHigh.SetDefaultColor(Color.DARK_GREEN);

BoxHigh.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

BoxLow.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

BoxLow.SetDefaultColor(Color.DARK_RED);

BoxHigh.SetHiding(HideBoxLines);

BoxLow.SetHiding(HideBoxLines);

AddCloud(if !HideCloud then BoxHigh else Double.NaN, BoxLow, Color.GRAY, Color.GRAY);

# Things to the Right of a Finished Box

def eH = if BarNumber() == 1 then Double.NaN else if !IsNaN(BoxHigh[1]) and IsNaN(BoxHigh) then BoxHigh[1] else eH[1];

def eL = if BarNumber() == 1 then Double.NaN else if !IsNaN(BoxLow[1]) and IsNaN(BoxLow) then BoxLow[1] else eL[1];

def diff = (eH - eL) * TargetMultiple;

plot Balance = if IsNaN(BoxHigh) and IsNaN(BoxLow) then (eH + eL) / 2 else Double.NaN;

plot Htgt_1 = if IsNaN(BoxHigh) and high >= eH then eH + diff else Double.NaN;

plot Htgt_2 = if IsNaN(BoxHigh) and high >= eH then eH + diff * 2 else Double.NaN;

plot Htgt_3 = if IsNaN(BoxHigh) and high >= eH then eH + diff * 3 else Double.NaN;

plot Htgt_4 = if IsNaN(BoxHigh) and high >= eH then eH + diff * 4 else Double.NaN;

plot Htgt_5 = if IsNaN(BoxHigh) and high >= eH then eH + diff * 5 else Double.NaN;

plot Htgt_6 = if IsNaN(BoxHigh) and high >= eH then eH + diff * 6 else Double.NaN;

plot Ltgt_1 = if IsNaN(BoxLow) and low <= eL then eL - diff else Double.NaN;

plot Ltgt_2 = if IsNaN(BoxLow) and low <= eL then eL - diff * 2 else Double.NaN;

plot Ltgt_3 = if IsNaN(BoxLow) and low <= eL then eL - diff * 3 else Double.NaN;

plot Ltgt_4 = if IsNaN(BoxLow) and low <= eL then eL - diff * 4 else Double.NaN;

plot Ltgt_5 = if IsNaN(BoxLow) and low <= eL then eL - diff * 5 else Double.NaN;

plot Ltgt_6 = if IsNaN(BoxLow) and low <= eL then eL - diff * 6 else Double.NaN;

Balance.SetHiding(HideBalance);

Balance.SetDefaultColor(CreateColor(255, 255, 255));

Balance.SetPaintingStrategy(PaintingStrategy.SQUARES);

Htgt_2.SetLineWeight(2);

Htgt_4.SetLineWeight(2);

Htgt_6.SetLineWeight(2);

Htgt_1.SetDefaultColor(CreateColor( 50, 100 , 75));

Htgt_1.SetPaintingStrategy(PaintingStrategy.DASHES);

Htgt_2.SetDefaultColor(CreateColor( 50, 100 , 75));

Htgt_2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

Htgt_3.SetDefaultColor(CreateColor( 50, 100 , 75));

Htgt_3.SetPaintingStrategy(PaintingStrategy.DASHES);

Htgt_4.SetDefaultColor(CreateColor( 50, 100 , 75));

Htgt_4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

Htgt_5.SetDefaultColor(CreateColor( 50, 100 , 75));

Htgt_5.SetPaintingStrategy(PaintingStrategy.DASHES);

Htgt_6.SetDefaultColor(CreateColor( 50, 100 , 75));

Htgt_6.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

Ltgt_2.SetLineWeight(2);

Ltgt_4.SetLineWeight(2);

Ltgt_6.SetLineWeight(2);

Ltgt_1.SetDefaultColor(CreateColor( 100, 50 , 75));

Ltgt_1.SetPaintingStrategy(PaintingStrategy.DASHES);

Ltgt_2.SetDefaultColor(CreateColor( 100, 50 , 75));

Ltgt_2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

Ltgt_3.SetDefaultColor(CreateColor( 100, 50 , 75));

Ltgt_3.SetPaintingStrategy(PaintingStrategy.DASHES);

Ltgt_4.SetDefaultColor(CreateColor( 100, 50 , 75));

Ltgt_4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

Ltgt_5.SetDefaultColor(CreateColor( 100, 50 , 75));

Ltgt_5.SetPaintingStrategy(PaintingStrategy.DASHES);

Ltgt_6.SetDefaultColor(CreateColor( 100, 50 , 75));

Ltgt_6.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

Htgt_1.SetHiding(HideTargets);

Htgt_2.SetHiding(HideTargets);

Htgt_3.SetHiding(HideTargets);

Htgt_4.SetHiding(HideTargets);

Htgt_5.SetHiding(HideTargets);

Htgt_6.SetHiding(HideTargets);

Ltgt_1.SetHiding(HideTargets);

Ltgt_2.SetHiding(HideTargets);

Ltgt_3.SetHiding(HideTargets);

Ltgt_4.SetHiding(HideTargets);

Ltgt_5.SetHiding(HideTargets);

Ltgt_6.SetHiding(HideTargets);

# Labels

AddLabel(!HideLabels, "TgtLvls = " + diff + "pts each | Bal = " + Balance, if high > eH and low < eL then Color.YELLOW else if high > eH then Color.GREEN else if low < eL then Color.RED else Color.GRAY);

AddLabel(!HideLabels && high > eH && low < eL, "OUTSIDE BAR!!", Color.YELLOW);

AddLabel(!HideLabels && high > eH && low >= eL, "Long", Color.GREEN);

AddLabel(!HideLabels && high <= eH && low < eL, "Short", Color.RED);

#Price Color

AssignPriceColor(if !ColorPrice then Color.CURRENT else if !IsNaN(BoxHigh) then Color.GRAY else

if high > eH and low < eL then Color.YELLOW else

if high > eH then Color.GREEN else if low < eL then Color.RED else Color.GRAY);

#order defs
Def buylongMarket = if rth and maxh < close and close > boxhigh[BarsUsedForRange] then 1 else 0;


Def buyshort = if rth and close < boxlow[BarsUsedForRange] then 1 else 0;

def TPlong = if close > Htgt_1 then 1 else 0;
Def stoploss = if rth and close <= boxhigh[BarsUsedForRange] then 1 else 0;

AddOrder(OrderType.BUY_TO_OPEN, buylongmarket, close, 100);

AddOrder(OrderType.SELL_TO_CLOSE, tplong, close, 100);
AddOrder(OrderType.SELL_TO_CLOSE, stoploss, close, 100);
 
Last edited:

MerryDay

Administrative
Staff member
Staff
VIP
Hi all,

@BenTen I really like this study, but am pretty data driven and want to backtest this on multiple stocks and multiple timeframes so i can build my trading strategy around it. I recently began backtesting in TOS and get the overall concept as well as some of the syntax needed to run the backtesting. However, i am having a helluvatime peeling out certain parameters due to the complexity within the code.
Looks like no contributor has been able to help you. It could be that it is not clear what you are asking or you did not provide enough information.

Please reply with a detail explanation of what you are trying to accomplish. or there is a risk that your post will be deleted due to inactivity.
Provide a marked-up screenshot of what a chart that displays what all your conditions would look like.
Questions without images are much less likely to get a response!
Unsure of how to upload screenshots to the forum, Here are directions.

When you are posting your question, please follow the guidelines found here:
https://usethinkscript.com/threads/answers-to-commonly-asked-questions.6006/#post-58016

Here are some other options.

If you have this question, others will also. So if you find an answer to your question, could you post it to the forum? Your contribution will be helping a legion of like-minded traders and thinkscripters. Thanks!
 

Similar threads

Top