Limit plot to Max of One Candle

Gupta

New member
Hi, I have the following code:
Code:
def yesterdaysClose = if close(period = "DAY")[1] < open(period = "DAY")[1] then close(period = "DAY")[1] else double.nan;

plot yc = yesterdaysClose;
That is running on a 1min 30 day chart. I want the plot to return one candle per trading day. And if possible though I don't think it is, return the time of the one candle that is plotted.

Here is a better example of what I mean. It is much more complicated but It is better to do it right I suppose.

item?id=NWnD7gX&&image.png




Code:
#- TS_GapFill
#- http://www.thinkscripter.com
#- [email protected]
#- Last Update 28 Jan 2010

# Time related
input marketOpenTime = 0930;
input marketCloseTime = 1615;

def secondsFromOpen =  secondsFromTime(marketOpenTime);
def secondsTillClose = secondsTillTime(marketCloseTime);
def marketOpen = if secondsFromOpen >= 0 and secondsTillClose >= 0 then 1 else 0;

def newDay = if getDay() != getDay()[1] then 1 else 0;

# Price
def yesterdaysClose = close(period = "DAY")[1];

# First Open
rec regularHoursOpen = if (secondsFromOpen >= 0 and secondsFromOpen[1] < 0) or
(getDay() != getDay()[1]) then open else regularHoursOpen[1];

# First High
rec regularHoursHigh = if (secondsFromOpen >= 0 and secondsFromOpen[1] < 0) or
(getDay() != getDay()[1]) then high else regularHoursHigh[1];

# First Low
rec regularHoursLow = if (secondsFromOpen >= 0 and secondsFromOpen[1] < 0) or
(getDay() != getDay()[1]) then low else regularHoursLow[1];

rec regHoursHigh = if newDay then high else if marketOpen then

if high > regHoursHigh[1] then high else regHoursHigh[1] else high;

# Low
rec regHoursLow = if newDay then low else if marketOpen then

if low < regHoursLow[1] then low else regHoursLow[1] else low;

# First Market Open Candle
def yc = if marketOpen then yesterdaysClose else double.nan;
def o = if marketOpen then regularHoursOpen else double.nan;

# Gap up or down
def gapUp = if yc < o then 1 else 0;
def gapDown = if yc > o then 1 else 0;

# Half of gap
def hg = o + (yc - o) / 2;

def gapRemaining = if gapUp then
    Max(regHoursLow - yc, 0) else
    if gapDown then Max(yc - regHoursHigh, 0) else 0;

def percentRemaining = 100 * gapRemaining / AbsValue(yc - o);

def gapFilled = if percentRemaining == 0 then 1 else 0;

def halfGapFilled = if percentRemaining <= 50 then 1 else 0;
    plot gH = if (gapUp and !gapFilled and marketOpen and !newDay[-1])
    then regHoursLow else if (gapDown and !gapFilled and marketOpen and !newDay[-1])
    then yc else double.nan;

plot gL = if (gapUp and !gapFilled and marketOpen and !newDay[-1])
    then yc else if (gapDown and !gapFilled and marketOpen and !newDay[-1])
    then regHoursHigh else double.nan;

plot hGF = if !gapFilled and marketOpen and !newDay[-1]
    then hg else double.nan;

# Candles below half plot hGF are plotted, return 1 for gapup, return 2 for gapdown
def return1 = gL - (gL*0.001);
def return2 = gH + (gH*0.001);

# regHoursLow and regHoursHigh describes the lowest and highest candle after RTH open and before RTH close for equities
plot greaterlessGF = if regHoursLow <= hGF and GapUp then return1[1] else if regHoursHigh >= hGF and GapDown then return2[1] else double.nan;

# Coloring
gH.SetPaintingStrategy(paintingStrategy.DaSHES);

gH.AssignValueColor(if gapDown then color.darK_red else color.dark_green);

gL.SetPaintingStrategy(paintingStrategy.DASHES);

gL.AssignValueColor(if gapDown then color.darK_red else color.dark_green);

hGF.setStyle(curve.LONG_DASH);

hGF.SetDefaultColor(color.dark_gray);

AddCloud(gH, gL, color.gray, color.gray);
 
Last edited by a moderator:
I feel like I am making this more complex than it should be. I am adding a plot that counts all RTH Barnumbers and trying to make it so if the barnumber of plot greaterlessGF is greater than first value of RTH Barnumbers + 1 to not plot it but I cannot get it working.
 

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

I feel like I am making this more complex than it should be. I am adding a plot that counts all RTH Barnumbers and trying to make it so if the barnumber of plot greaterlessGF is greater than first value of RTH Barnumbers + 1 to not plot it but I cannot get it working.

hello and welcome
yes, your questions are confusing. unfortunetely, that tends to happen, when someone doesn't know how to answer their own questions, they also have trouble describing the situation.

in the first part of post1 asks about a previous day price level.
the 2nd part has code for gaps.. i don't see how they are related ??

your variable name, greaterlessgf is confusing , and causes me to focus on it and try to make sense of it ,instead of the formula.

your post2 words are confusing. i don't know what you want to plot. what do you want to see on the chart?

try to list out what to want the code to find, without using specific code.

do you want,
.. every day,
.. find the first occurance of some condition,
.. then ? plot an arrow?
 
My post was edited by an admin to include my first question and my reply together because someone said the first question was not clear.

In the picture the gold line is the plot I want to edit known as greaterlessGF (Greater than less than gap filled),
I want the gold line to only plot the first time a candle is below the blue line which is plot hGF which stands for half gap filled.

What I was trying to achieve, is that for no matter how many times a plot may be true condition wise to only plot the first candle it is true during real time hours over many days. So for every trading day there should be a max of one candle plotted or one gold line.

Instead of looking like the first picture with the gold plot going all the way to the end of RTH hours I want it to look like this picture. This is important for code I want to make later on
item?id=lnuvLLk&&image.png
 
My post was edited by an admin to include my first question and my reply together because someone said the first question was not clear.

In the picture the gold line is the plot I want to edit known as greaterlessGF (Greater than less than gap filled),
I want the gold line to only plot the first time a candle is below the blue line which is plot hGF which stands for half gap filled.

What I was trying to achieve, is that for no matter how many times a plot may be true condition wise to only plot the first candle it is true during real time hours over many days. So for every trading day there should be a max of one candle plotted or one gold line.

Instead of looking like the first picture with the gold plot going all the way to the end of RTH hours I want it to look like this picture. This is important for code I want to make later on

i'll do some experimenting

you need a way to wait for a signal to go true and latch it. here are some examples.
https://usethinkscript.com/threads/...iring-twice-in-thinkorswim.12310/#post-105468

in the meantime, does this help at all?
it shows when a gap is filled.
https://usethinkscript.com/threads/...-price-crosses-into-it-for-thinkorswim.11186/
 
I think I did it. This is a not a perfect solution at all because it still plots two candles so if I wanted to make it into a chart bubble it would be difficult but you can't see a one candle plot anyways. Not sure if this is smart to do multiple times because I just created two plots to make the one I want.

I tried to use the examples but I could not figure out how to include the condition of it being RTH and the others seemed to require two conditions to be met not one. It lead me to this solution though

Ruby:
def return = if gH > gL then  gL - (gL*0.001) else gH + (gH*0.001);

def greaterlessGF = if regHoursLow <= hGF and GapUp and MarketOpen then return[1] else if regHoursHigh >= hGF and GapDown and MarketOpen then return[1] else double.nan;

def notglGF = if marketOpen and isnan(greaterlessGF) then return[1] else double.nan;

plot first_signal = if !isnan(notglGF[1]) and !isnan(greaterlessGF[-1]) then return else double.nan;

Edit:

Nvm this fixes the two candle problem. Still takes a lot of calculation though.
Ruby:
plot first_signal = if isnan(notglGF) and !isnan(notglGF[1]) then return else double.nan;
 
Last edited:

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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