Engulfing Bars

TNTrader5159

Active member
Hello Trader/Coders!
Is it advisable to use multiple studies to create a study set for monitoring many time-frames simultaneously? Only one input needs to be changed in the code in order to do so, but copying the entire code block into the same study for the various inputs would necessitate the creation of new variable names and make a tedious mess of things. So which way is more efficient for monitoring and resource management?
<p>
In the below example of code, only the <ratio1> input would be changed with many different values to be reckoned. Is it better to copy/paste the code below this for every increment and alter the variable names accordingly, or should I use multiple studies with only the <ratio1> value changed to leave the variable names unchanged in each one?

Code:
def nonum = Double.NaN;
def bn = BarNumber();

# User options
input showengbub = yes;
input showenglines = yes;

# Engulfing bars
input ratio1 = 3;

# Multiplying factor
input minratio = Double.Pi;

# Engulfed bars
def ratio2 = minratio * ratio1;

# Bars since high/low of tail
def tailbarsago = ratio1 - 1;

# Maximum number of bars to look back for engulfed range
def lookback = 144 * ratio1;

# Count the number of bars engulfed in bearish pattern
def engbarsbear = fold countbear = ratio1 to lookback * ratio1 with bear while high[tailbarsago] > GetValue(high, countbear) and low[tailbarsago] > low[ratio1] and low < GetValue(low, countbear) do bear + 1;

# Verify at least <ratio2> bars engulfed
def engbarsminbear = if engbarsbear >= ratio2 then engbarsbear else 0;

# Count the number of bars engulfed in a bullish pattern
def engbarsbull = fold countbull = ratio1 to lookback * ratio1 with bull while low[tailbarsago] < GetValue(low, countbull) and high[tailbarsago] < high[tailbarsago + 1] and high > GetValue(high, countbull) do bull + 1;

# Verify at least <ratio2> bars engulfed
def engbarsminbull = if engbarsbull >= ratio2 then engbarsbull else 0;

# Display qualified numbers of engulfed bars in red or green bubble
addchartbubble(engbarsminbear, high * 1.00025, engbarsminbear / ratio1, color.light_red, yes);
addchartbubble(engbarsminbull, low * 0.99925, engbarsminbull / ratio1, color.light_green, no);
 
Have a look at this page:
https://tlc.thinkorswim.com/center/reference/thinkScript/Reserved-Words/script

ThinkScript allows you to create embedded scripts (functions within a script) which can be called as many times as you like in your script. Make sure that one of the 'inputs' to that script is the aggregation period, and that you send the intended agg period for each of your plots. It might look like this:
Code:
script MA {
input period = AggregationPeriod.TEN_MIN;
plot Data = close(period = period);
};

plot MA1 = MA(AggregationPeriod.FIFTEEN_MIN);
plot MA2 = MA(AggregationPeriod.HOUR);
plot MA3 = MA(AggregationPeriod.FOUR_HOURS);

Obviously, I didn't take your block of code for the script, but you get the idea.

Good luck,
mashume
 
Hello Trader/Coders!
Is it advisable to use multiple studies to create a study set for monitoring many time-frames simultaneously? Only one input needs to be changed in the code in order to do so, but copying the entire code block into the same study for the various inputs would necessitate the creation of new variable names and make a tedious mess of things. So which way is more efficient for monitoring and resource management?
<p>
In the below example of code, only the <ratio1> input would be changed with many different values to be reckoned. Is it better to copy/paste the code below this for every increment and alter the variable names accordingly, or should I use multiple studies with only the <ratio1> value changed to leave the variable names unchanged in each one?

Code:
def nonum = Double.NaN;
def bn = BarNumber();

# User options
input showengbub = yes;
input showenglines = yes;

# Engulfing bars
input ratio1 = 3;

# Multiplying factor
input minratio = Double.Pi;

# Engulfed bars
def ratio2 = minratio * ratio1;

# Bars since high/low of tail
def tailbarsago = ratio1 - 1;

# Maximum number of bars to look back for engulfed range
def lookback = 144 * ratio1;

# Count the number of bars engulfed in bearish pattern
def engbarsbear = fold countbear = ratio1 to lookback * ratio1 with bear while high[tailbarsago] > GetValue(high, countbear) and low[tailbarsago] > low[ratio1] and low < GetValue(low, countbear) do bear + 1;

# Verify at least <ratio2> bars engulfed
def engbarsminbear = if engbarsbear >= ratio2 then engbarsbear else 0;

# Count the number of bars engulfed in a bullish pattern
def engbarsbull = fold countbull = ratio1 to lookback * ratio1 with bull while low[tailbarsago] < GetValue(low, countbull) and high[tailbarsago] < high[tailbarsago + 1] and high > GetValue(high, countbull) do bull + 1;

# Verify at least <ratio2> bars engulfed
def engbarsminbull = if engbarsbull >= ratio2 then engbarsbull else 0;

# Display qualified numbers of engulfed bars in red or green bubble
addchartbubble(engbarsminbear, high * 1.00025, engbarsminbear / ratio1, color.light_red, yes);
addchartbubble(engbarsminbull, low * 0.99925, engbarsminbull / ratio1, color.light_green, no);

here is the code in post 1, converted to use script, and 3 different values for ratio (at the end)


Ruby:
# tnt_script_ex_01

#https://usethinkscript.com/threads/multiple-studies-to-change-one-input.13274/
#Multiple studies to change one input?
# TNTrader5159  11/7

# In the below example of code, 
#  only the <ratio1> input would be changed with many different values to be reckoned.


def nonum = Double.NaN;
def bn = BarNumber();

# User options
input showengbub = yes;
input showenglines = yes;

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

script barbearbull {

# Engulfing bars
input ratio1 = 3;

# Multiplying factor
input minratio = Double.Pi;

# Engulfed bars
def ratio2 = minratio * ratio1;

# Bars since high/low of tail
def tailbarsago = ratio1 - 1;

# Maximum number of bars to look back for engulfed range
def lookback = 144 * ratio1;


# Count the number of bars engulfed in bearish pattern
def engbarsbear =
# fold countbear = ratio1 to lookback * ratio1
 fold countbear = ratio1 to lookback
 with bear
 while high[tailbarsago] > GetValue(high, countbear) and low[tailbarsago] > low[ratio1] and low < GetValue(low, countbear)
 do bear + 1;

# Verify at least <ratio2> bars engulfed
def engbarsminbear = if engbarsbear >= ratio2 then engbarsbear else 0;


# Count the number of bars engulfed in a bullish pattern
def engbarsbull =
# fold countbull = ratio1 to lookback * ratio1
 fold countbull = ratio1 to lookback
 with bull
 while low[tailbarsago] < GetValue(low, countbull) and high[tailbarsago] < high[tailbarsago + 1] and high > GetValue(high, countbull)
 do bull + 1;

# Verify at least <ratio2> bars engulfed
def engbarsminbull = if engbarsbull >= ratio2 then engbarsbull else 0;

plot zbarbear = engbarsminbear;
plot zbarbull = engbarsminbull;
}

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


def vert = 0.04;

input ratio1 = 2;
def bear1 = barbearbull(ratio1).zbarbear;
def bull1 = barbearbull(ratio1).zbarbull;
# Display qualified numbers of engulfed bars in red or green bubble
#addchartbubble(engbarsminbear, high * 1.00025, engbarsminbear / ratio1, color.light_red, yes);
#addchartbubble(engbarsminbull, low * 0.99925, engbarsminbull / ratio1, color.light_green, no);
#addchartbubble(bear1, high * 1.00025, bear1 / ratio1, color.light_red, yes);
#addchartbubble(bull1, low * 0.99925, bull1 / ratio1, color.light_green, no);
addchartbubble(bear1, (high*(1+(1*vert))), bear1 / ratio1, color.light_red, yes);
addchartbubble(bull1, (low*(1-(1*vert))), bull1 / ratio1, color.light_green, no);


input ratio2 = 3;
def bear2 = barbearbull(ratio2).zbarbear;
def bull2 = barbearbull(ratio2).zbarbull;
addchartbubble(bear2, (high*(1+(2*vert))), bear2 / ratio2, color.light_orange, yes);
addchartbubble(bull2, (low*(1-(2*vert))), bull2 / ratio2, color.lime, no);


input ratio3 = 4;
def bear3 = barbearbull(ratio3).zbarbear;
def bull3 = barbearbull(ratio3).zbarbull;
addchartbubble(bear3, (high*(1+(3*vert))), bear3 / ratio3, color.yellow, yes);
addchartbubble(bull3, (low*(1-(3*vert))), bull3 / ratio3, color.cyan, no);

addchartbubble(0, low*0.97,
ratio1 + "\n" +
bear1 + "\n" +
bull1
, color.yellow, no);
#

taRnq43.jpg
 
The idea is to iterate this loop using <engbarsbull2 - 1, 2, 3, 4, etc> or <engbarsbear2 - 1,2,3,4, etc> to a variable ending count until it either gets to the count or drops below a set minimum value as represented by an input. The actual occurrence is the varaible number of bars back would be checked a bar at a time going forward to find the first bar outside the range of the rest of the bars, as long as the number of bars remaining is >= a set variable input minimum number. This would be the point at which to stop the count. How would this best be done? Three fold loops at the points where engbarsbull2 - <iterated count> or 1 to engbarsbull2 is determined?

Code:
def tfbull = if getvalue(high, engbarsbull2) > getvalue(high, engbarsbull2 -1) or getvalue(low, engbarsbull2) < getvalue(low, engbarsbull2 - 1) then engbarsbull2 else engbarsbull2 - 1;

or

def tfbear = if getvalue(high, engbarsbear2) > getvalue(high, engbarsbear2 -1) or getvalue(low, engbarsbear2) < getvalue(low, engbarsbear2 - 1) then engbarsbear2 else engbarsbear2 - 1;
 
The idea is to iterate this loop using <engbarsbull2 - 1, 2, 3, 4, etc> or <engbarsbear2 - 1,2,3,4, etc> to a variable ending count until it either gets to the count or drops below a set minimum value as represented by an input. The actual occurrence is the varaible number of bars back would be checked a bar at a time going forward to find the first bar outside the range of the rest of the bars, as long as the number of bars remaining is >= a set variable input minimum number. This would be the point at which to stop the count. How would this best be done? Three fold loops at the points where engbarsbull2 - <iterated count> or 1 to engbarsbull2 is determined?


if you want to stop a fold loop before it runs through all the 'to' numbers, use a while code line.
you set it up so , 'while' something is true, the fold keeps looping.

Code:
def x = 22;
def a = fold i = 1 to 50
  with p
  while i < x
  do p + 1;

# p will count up to 21, so a will be = to 21.
# when i = 22, it isn't < 22 anymore, and the loop stops.

addlabel(1, "a= " + a, color.cyan);
#


--------------------------

here are your notes restructured. writing your rules like this makes a little more sense to me.

to iterate a loop,
. using <engbarsbull2 - 1, 2, 3, 4, etc> or <engbarsbear2 - 1,2,3,4, etc>
. to a variable ending count
. until,
. it either gets to the count
. or drops below a set minimum value as represented by an input.


----------------------

this is a test study to allow you to change a bunch of variables and experiment with.
on every bar, it looks at some past bars and finds how many consecutive bars are within the starting range.
bubbles are drawn on each bar, with the range high, low, and quantity of smaller bars.


Code:
#
def bn = barnumber();
def na = double.nan;

input loop_start = 1;
input loop_end = 99;

input range_top_close_factor = 1.01;
input range_bottom_close_factor = 0.99;

# calc a range , from prev bar, high and low, * a factor
def rng_top = if bn == 1 then close else close[1] * range_top_close_factor;
def rng_bot = if bn == 1 then close else close[1] * range_bottom_close_factor;

input count_max = 15;
input count_min = 4;

# look at past bars, to see if the high and low, are within a range
# keep looking at prev bars as long long as they are within the range
def count = fold loop1 = loop_start to loop_end
 with temp
 while (getvalue(high, loop1) <= rng_top and getvalue(low, loop1) >= rng_bot) 
 do temp + 1;


def desired_qty = if (count >= count_min and count <= count_max ) then 1 else 0;
def more_qty = if (count > count_max) then 1 else 0;

addchartbubble(1, low*0.995,
rng_top + "\n" +
rng_bot + "\n" +
count + "\n" +
desired_qty
, (if more_qty then color.orange else if desired_qty then color.green else color.gray), no);
#


YZl2faw.jpg
 
if you want to stop a fold loop before it runs through all the 'to' numbers, use a while code line.
you set it up so , 'while' something is true, the fold keeps looping.

Code:
def x = 22;
def a = fold i = 1 to 50
  with p
  while i < x
  do p + 1;

# p will count up to 21, so a will be = to 21.
# when i = 22, it isn't < 22 anymore, and the loop stops.

addlabel(1, "a= " + a, color.cyan);
#


--------------------------

here are your notes restructured. writing your rules like this makes a little more sense to me.

to iterate a loop,
. using <engbarsbull2 - 1, 2, 3, 4, etc> or <engbarsbear2 - 1,2,3,4, etc>
. to a variable ending count
. until,
. it either gets to the count
. or drops below a set minimum value as represented by an input.


----------------------

this is a test study to allow you to change a bunch of variables and experiment with.
on every bar, it looks at some past bars and finds how many consecutive bars are within the starting range.
bubbles are drawn on each bar, with the range high, low, and quantity of smaller bars.


Code:
#
def bn = barnumber();
def na = double.nan;

input loop_start = 1;
input loop_end = 99;

input range_top_close_factor = 1.01;
input range_bottom_close_factor = 0.99;

# calc a range , from prev bar, high and low, * a factor
def rng_top = if bn == 1 then close else close[1] * range_top_close_factor;
def rng_bot = if bn == 1 then close else close[1] * range_bottom_close_factor;

input count_max = 15;
input count_min = 4;

# look at past bars, to see if the high and low, are within a range
# keep looking at prev bars as long long as they are within the range
def count = fold loop1 = loop_start to loop_end
 with temp
 while (getvalue(high, loop1) <= rng_top and getvalue(low, loop1) >= rng_bot)
 do temp + 1;


def desired_qty = if (count >= count_min and count <= count_max ) then 1 else 0;
def more_qty = if (count > count_max) then 1 else 0;

addchartbubble(1, low*0.995,
rng_top + "\n" +
rng_bot + "\n" +
count + "\n" +
desired_qty
, (if more_qty then color.orange else if desired_qty then color.green else color.gray), no);
#


YZl2faw.jpg
It's a way to come up with the Thrust Factor, which is the farther back of either the high or low of a determined VARIABLE range of engulfed bars. If for example, a bar is determined to engulf the 7 previous bars (call the variable engbars), start at the 7th bar back and compare <getvalue(high, engbars)> to getvalue(high, engbars - 1) and <getvalue(low, engbars)> to <getvalue(low, engbars - 1)> to see if its high > the high of the 6 subsequent bars in the range OR its low < the low of the 6 subsequent bars in the range. If YES to either condition, then the Thrust Factor is engbars (7) and the loop stops. If NO to both conditions (the bar lies inside the range of the 6 subsequent bars), repeat the comparisons for the 6th bar back, then if necessary the 5th bar back, and on with this series until a bar lies outside the range of the shrinking number of subsequent bars. The loop would go from <1 to engbars - minratio>, right? The minratio input is Pi. In any case, the loop must terminate before the Thrust Factor it returns would fall below the minimum ratio of Pi, at which point the Thrust Factor would be ZERO and not reckoned on the chart.
<p>
Is this all logical? I ask because I have yet to digest your code above to see if it accomplishes this purpose.
 
How would I do a lookback period for engulfed bars that would be the most recent bar number <BarNumber()> minus the bar number from where the lookback begins in order to look back for a period of UP TO all the bars loaded into the chart?
 
Would you kindly share your indicator's final version when it becomes available?
Thank you
I would gladly do so brother. The components of the system, ie pattern, impedance, rules, and account management only needed to be stitched together, and now they have lain unattended for the past 3 weeks. Admittedly, some of that is per my own personal difficulties.
<p>
I made one modification in that the trigger bar of the pattern must eclipse a qualifying pivot in the trade direction and not just a random assortment of engulfed bars.
<p>
Also, I am looking toward coding the system in EasyLanguage, since it and the TradeStation platform are more versatile than ThinkScript and TOS, and TD Ameritrade still does not support trading automation. Without automation, it's nearly impossible to catch very many good trades without constantly monitoring your chosen markets and in scrambling to make trades when signals come in around the clock. The number of opportunities is mindboggling, yet few of us humans can capture enough of them to make a good living at this and still have a life outside of staring at charts. I can't speak for others, but I prefer a walk in the woods.
<p>
edited by mod
<p>
Meanwhile, if you have any ideas or means to get this done, I'm certainly open, with my thanks for your outreach and following the progress of this winning methodology all along. edited by mod
<p>
Happy Thanksgiving from Matt Thomas in Tennessee!
 
Last edited by a moderator:
@TNTrader5159
i am still here. but just putting this on hold for a bit, holidays and other stuff.
i have made many studies for you, related to your goal script. i have made a couple like what you are asking about, that look at a period of engulfed bars and find a certain bar. they had bubbles with values to help see what the study is doing. you have a tendency to not provide feedback, which makes this project very hard to progress. you don't say yes, that number is right, yes , that is what i am looking for, or no, that's not quite right. you tend to make a post, stating your rules again...
i will study your recent posts after thanksgiving / december.
 
Last edited by a moderator:
i am still here. but just putting this on hold for a bit, holidays and other stuff.
i have made many studies for you, related to your goal script. i have made a couple like what you are asking about, that look at a period of engulfed bars and find a certain bar. they had bubbles with values to help see what the study is doing. you have a tendency to not provide feedback, which makes this project very hard to progress. you don't say yes, that number is right, yes , that is what i am looking for, or no, that's not quite right. you tend to make a post, stating your rules again...
i will study your recent posts after thanksgiving / december.
Yes many are in holiday mode now and the project hit an air pocket. ..edited by mod.. It's been a rough go and I'm endeavoring to put all your notes and those of Svanoy together for an organized effort.
<p>
Strangely, I'm still unclear on how to find the high or low of a variable range of bars. I know the secret is embedded in some of the stuff you have sent, I have saved all you have done. ..edited by mod.. Sorry to be so scattered.
 
Last edited by a moderator:
I would gladly do so brother. The components of the system, ie pattern, impedance, rules, and account management only needed to be stitched together, and now they have lain unattended for the past 3 weeks. Admittedly, some of that is per my own personal difficulties.
<p>
I made one modification in that the trigger bar of the pattern must eclipse a qualifying pivot in the trade direction and not just a random assortment of engulfed bars.
<p>
Also, I am looking toward coding the system in EasyLanguage, since it and the TradeStation platform are more versatile than ThinkScript and TOS, and TD Ameritrade still does not support trading automation. Without automation, it's nearly impossible to catch very many good trades without constantly monitoring your chosen markets and in scrambling to make trades when signals come in around the clock. The number of opportunities is mindboggling, yet few of us humans can capture enough of them to make a good living at this and still have a life outside of staring at charts. I can't speak for others, but I prefer a walk in the woods.
<p>
edited by mod
<p>
Meanwhile, if you have any ideas or means to get this done, I'm certainly open, with my thanks for your outreach and following the progress of this winning methodology all along. edited by mod
<p>
Happy Thanksgiving from Matt Thomas in Tennessee!
Thanks. You too, Matt, have a happy Thanksgiving. Be safe and take care of yourself
 
I appreciate the update. If you have an updated thinkscript code for thinkorswim, could you perhaps share it with us since I do not have a Tradestation account?

Happy holidays and early happy new year!
 
I appreciate the update. If you have an updated thinkscript code for thinkorswim, could you perhaps share it with us since I do not have a Tradestation account?

Happy holidays and early happy new year!
Happy New Year to you as well! I know many who are glad to be seeing 2022 exit stage right.

I do not have the Fake n Break system coded in ThinkScript as of now.
 
Last edited by a moderator:
Happy New Year to you as well! I know many who are glad to be seeing 2022 exit stage right.

I do not have the Fake n Break system coded in ThinkScript as of now.
Do you know the code Svanoy wrote for the Pivot Depreciation Factor? I thought I had it but apparently do not. I will look on here to see if it's in the old threads.
 

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
485 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