How to Get the Range of the Tallest Red Candle of the Day So Far?

T

Trismagistus

New member
My purpose is simple: to find the range length of the tallest red candle to appear, so far, in today's trading.

I'm having a heckuva time finding how to get that, since most thinkscripts have more complex goals and concepts, and discussions tend to focus around averages or other calculations like ATR.

I don't need to plot the number, nor scan for it. I don't need the average over time. It might be most useful, however, if I can specify some number of 1m candles into the day as a starting point, since the first 10 minutes or so tend to have the largest volitility in range values.

Does anyone know how I might be able to get that number?

Many thanks in advance.

-- Tris
 
T

Trismagistus

New member
Ok, the code below is a complete mess. If only I could assign a global variable that I could change, it would be so simple. This code is the result of me banging my head on this problem for at least 8 hours. I don't have it to the point where I expect it to make sense. I'm just trying to get the values I want, add them together, and produce a number. Any help anyone can offer would be greatly appreciated.

I'm clearly missing some fundamental thinkscript concepts. I can put the following in the thinkscript editor and it allows it, but...

Code:
# DOESN'T WORK. DON'T TRY THIS AT HOME
#
def thisCandleRange = high - low;
def prevCandleRange = high[1] - low[1];
def nextCandleRange = high[-1] - low[-1];
def isRed = if open[1] > low[1] then yes else no;
def FormerBiggestRedRange =0;

AddLabel (yes, "isRed: " +  isRed + " ", Color.LIME );
AddLabel (yes, "thisCandleRange: " + thisCandleRange + " ", Color.LIME );
AddLabel (yes, "prevCandleRange: " + prevCandleRange + " ", Color.LIME );
AddLabel (yes, "nextCandleRange: " + nextCandleRange + " ", Color.LIME );
AddLabel (yes, "thisCandleRange[4]: " + thisCandleRange[4] + " ", Color.LIME );

# === Without the below, the above works, so far, just for producing some range
values. With the below, however, even the above AddLabels disappear.
# ==================================================
def TheBiggestRedRange;
if (isRed == yes && TheBiggestRedRange == double.nan ) {
    # This candle is the first red candle in our period, so grab its range.
    TheBiggestRedRange = thisCandleRange;
} else {
    if ( isRed == yes && (thisCandleRange > TheBiggestRedRange[1] ) ) {
        # This red candle's range is larger than the previous red's range
        # so save its range
         TheBiggestRedRange = thisCandleRange;
    } else {
        # Preserve the previous TheBiggestRedRange
        TheBiggestRedRange = TheBiggestRedRange[1];
    }
}

# if ( (thisCandleRange > FormerBiggestRedRange[1]) ) then FormerBiggestRedRange[1] = thisCandleRange;

AddLabel (yes, "TheBiggestRedRange: " + TheBiggestRedRange + " ", Color.YELLOW );
Perhaps pseudocode will explain what I'm trying to do better than my thinkscript attempts so far:

Context: 1m chart;
Period: Intraday since the opening bell;
Find the range of each red candle's high to low;
Compare it against the ranges of all the other red candles;
Return the range of the longest red candle;

That's it. I'll put the return value in a label.
 
Last edited by a moderator:
M

mashume

Active member
VIP
See if this is close to what you're after:

Code:
declare upper;

def istoday = if GetLastDay() == GetDay() then 1 else 0;

def redRange = if istoday == 1   
    and close < open
    and high - low > redRange[1]
    then high - low
    else redRange[1];

AddLabel(visible = Yes, text=Concat("Largest Red Candle Range: ", redRange));
-mashume
 
T

Trismagistus

New member
Mashume, THANK YOU!!! That's so elegant.

Before I saw your answer, I banged my head on finding a solution for another four hours. I never could get it right with explicit code like yours. So I started looking for functions to see if ThinkScript had something built in, and found this "HighestAll()" function which outputs the same answer:

def BiggestRed = HighestAll( if open > close then high - low else double.nan);
AddLabel (yes, "BiggestRed: " + BiggestRed + " ", Color.LIME );

Very handy, but I always prefer explicit code like yours in case I want to do something special. So THANK YOU again!

The one remaining challenge is that I want to exclude the first ten 1m bars after the open, since huge drops frequently happen there -- the scale of which often isn't repeated throughout the rest of the trading day.

I had hopes for using the "Highest(high[1], length)" with "BarNumber()-10" for the length parameter but the Highest() function throws an exception of "only constants expected here" for the length. Haven't found a way around that, yet.
 
M

mashume

Active member
VIP
Perhaps like this:
Code:
declare upper;

def istoday = if GetLastDay() == GetDay() then 1 else 0;
input ActivateTime  = 0940.0;

def Active = if  SecondsFromTime(ActivateTime) >= 0
                 then 1
                 else 0;

def redRange = if istoday == 1
    and Active == 1
    and close < open
    and high - low > redRange[1]
    then high - low
    else redRange[1];

AddLabel(visible = Yes, text=Concat("Largest Red Candle Range: ", redRange));

AddVerticalLine(Active[1] == 0 and Active == 1, "Start Measuring Candles");
-mashume
 
T

Trismagistus

New member
Mashume! That works great! Big improvement and a significant difference on some charts. And the vertical line is so helpful! Thank you! Fantastic. 👏👏‼
 
R

rwfarrell

New member
Does anyone have a script for candle fill color? I have a specific candle code but I am only looking to change the color of fill up or fill down? Thank you.
 
P

Pensar

Active member
VIP
@rwfarrell To change candle fill color, simply go to "Chart Settings>Appearance" and choose the desired color for "Fill Up" and/or "Fill Down".
 
R

rwfarrell

New member
@Pensar I know how to do that under the appearance. What I am asking is there does anyone have a script to change the fill up or fill down for candles. I have a script looking for a specific candle. When this candle appears on my chart it automatically changes the border color and candle fill color. I am looking to keep the border color as it is (green or red) but I am looking for a script or command to add to the script I have that will leave the fill up or fill down color.white.
 
P

Pensar

Active member
VIP
@rwfarrell I understand now. Unfortunately, I don't believe thinkScript has that capability. It can only change the color of the entire bar.
 
3AMBH

3AMBH

Member
2019 Donor
VIP
Hi Mashume, Thanks for posting the codes.

Is there anyway to code this so it shows PREVIOUS BAR range?

Thanks.
 

Similar threads

Top