how to record the highest price within a period

oldyoungguy

New member
VIP
I am using Heikin Ashi bar chart. I am trying to record the highest price during a period. ThinkScript doesn't allow me to use the def variable to compare with other value. I cannot get it works correctly. Please help.

pk9cLGI.jpg


I expect all the bubbles on the right side should display the value of 284.38.

My code :

Code:
def emaLen9 = 9;
def emaLen3dot5 = 3.5;
def priceType = FundamentalType.CLOSE;
def averageType = AverageType.EXPONENTIAL;

def baseTF = GetAggregationPeriod();

def baseTF_mve9 = MovingAverage(averageType, Fundamental(priceType, period = baseTF), emaLen9);
def baseTF_mve3dot5 = MovingAverage(averageType, Fundamental(priceType, period = baseTF), emaLen3dot5);

def baseTF_O = open(period = baseTF);
def baseTF_h = high(period = baseTF);
def baseTF_l = low(period = baseTF);
def baseTF_c = close(period = baseTF);
def baseTF_HAclose = (baseTF_O + baseTF_h + baseTF_l + baseTF_c) / 4;
def baseTF_HAopen = CompoundValue(1, (baseTF_HAopen[1] + baseTF_HAclose[1]) / 2, baseTF_HAclose);
def baseTF_HAhigh = Max(Max(baseTF_h, baseTF_HAopen), baseTF_HAclose);
def baseTF_HAlow = Min(Min(baseTF_l, baseTF_HAopen), baseTF_HAclose);

def baseTF_isCrossedAbove = baseTF_mve3dot5 crosses above baseTF_mve9;
def baseTF_isCrossBelow = baseTF_mve3dot5 crosses below baseTF_mve9;

def baseTF_isCrossBelowBefore;
def baseTF_isCrossAboveBefore;

if(baseTF_isCrossBelow)
{
    baseTF_isCrossBelowBefore = yes;
    baseTF_isCrossAboveBefore = no;
}
else if(baseTF_isCrossedAbove)
{
    baseTF_isCrossBelowBefore = no;
    baseTF_isCrossAboveBefore = yes;
}
else
{
    baseTF_isCrossBelowBefore = baseTF_isCrossBelowBefore[1];
    baseTF_isCrossAboveBefore = baseTF_isCrossAboveBefore[1];
}

def rangeHighest ;

if(baseTF_isCrossAboveBefore)
{
   #if(baseTF_HAhigh > rangeHighest ) #-- shold be like this, but thinkScript doesn't like this, syntex error
   #if(baseTF_HAhigh > baseTF_HAhigh[1] && baseTF_HAhigh > rangeHighest ) #-- no syntex error but nothing show up
    if(baseTF_HAhigh > baseTF_HAhigh[1]) #-- the result is NOT fully correct
    {
        rangeHighest = baseTF_HAhigh;
    }
    else
    {
        rangeHighest =  rangeHighest[1];
    }
}
else
{
    rangeHighest =  rangeHighest[1];
}

AddChartBubble(yes, high, rangeHighest , Color.WHITE, yes);
 
Solution
I am using Heikin Ashi bar chart. I am trying to record the highest price during a period. ThinkScript doesn't allow me to use the def variable to compare with other value. I cannot get it works correctly. Please help.


I expect all the bubbles on the right side should display the value of 284.38.

My code :

Code:
def emaLen9 = 9;
def emaLen3dot5 = 3.5;
def priceType = FundamentalType.CLOSE;
def averageType = AverageType.EXPONENTIAL;

def baseTF = GetAggregationPeriod();

def baseTF_mve9 = MovingAverage(averageType, Fundamental(priceType, period = baseTF), emaLen9);
def baseTF_mve3dot5 = MovingAverage(averageType, Fundamental(priceType, period = baseTF), emaLen3dot5);

def baseTF_O = open(period = baseTF);
def baseTF_h = high(period...
I am using Heikin Ashi bar chart. I am trying to record the highest price during a period. ThinkScript doesn't allow me to use the def variable to compare with other value. I cannot get it works correctly. Please help.


I expect all the bubbles on the right side should display the value of 284.38.

My code :

Code:
def emaLen9 = 9;
def emaLen3dot5 = 3.5;
def priceType = FundamentalType.CLOSE;
def averageType = AverageType.EXPONENTIAL;

def baseTF = GetAggregationPeriod();

def baseTF_mve9 = MovingAverage(averageType, Fundamental(priceType, period = baseTF), emaLen9);
def baseTF_mve3dot5 = MovingAverage(averageType, Fundamental(priceType, period = baseTF), emaLen3dot5);

def baseTF_O = open(period = baseTF);
def baseTF_h = high(period = baseTF);
def baseTF_l = low(period = baseTF);
def baseTF_c = close(period = baseTF);
def baseTF_HAclose = (baseTF_O + baseTF_h + baseTF_l + baseTF_c) / 4;
def baseTF_HAopen = CompoundValue(1, (baseTF_HAopen[1] + baseTF_HAclose[1]) / 2, baseTF_HAclose);
def baseTF_HAhigh = Max(Max(baseTF_h, baseTF_HAopen), baseTF_HAclose);
def baseTF_HAlow = Min(Min(baseTF_l, baseTF_HAopen), baseTF_HAclose);

def baseTF_isCrossedAbove = baseTF_mve3dot5 crosses above baseTF_mve9;
def baseTF_isCrossBelow = baseTF_mve3dot5 crosses below baseTF_mve9;

def baseTF_isCrossBelowBefore;
def baseTF_isCrossAboveBefore;

if(baseTF_isCrossBelow)
{
    baseTF_isCrossBelowBefore = yes;
    baseTF_isCrossAboveBefore = no;
}
else if(baseTF_isCrossedAbove)
{
    baseTF_isCrossBelowBefore = no;
    baseTF_isCrossAboveBefore = yes;
}
else
{
    baseTF_isCrossBelowBefore = baseTF_isCrossBelowBefore[1];
    baseTF_isCrossAboveBefore = baseTF_isCrossAboveBefore[1];
}

def rangeHighest ;

if(baseTF_isCrossAboveBefore)
{
   #if(baseTF_HAhigh > rangeHighest ) #-- shold be like this, but thinkScript doesn't like this, syntex error
   #if(baseTF_HAhigh > baseTF_HAhigh[1] && baseTF_HAhigh > rangeHighest ) #-- no syntex error but nothing show up
    if(baseTF_HAhigh > baseTF_HAhigh[1]) #-- the result is NOT fully correct
    {
        rangeHighest = baseTF_HAhigh;
    }
    else
    {
        rangeHighest =  rangeHighest[1];
    }
}
else
{
    rangeHighest =  rangeHighest[1];
}

AddChartBubble(yes, high, rangeHighest , Color.WHITE, yes);

You are checking when a crossing happens,
then checking if a high is higher than a previous high.

that is what happened where you have your arrow on the image. the bar crossed above a line and the high is higher than the previous high, so the value changed in the bubble.

you aren't comparing the current high to a variable holding the highest high of the chart, rangeHighest[1].
you are only comparing the current high to the previous bar high.

def rangeHighest ; if(baseTF_isCrossAboveBefore) {
if(baseTF_HAhigh > baseTF_HAhigh[1]) {
rangeHighest = baseTF_HAhigh;
}
......


Do you want to find the highest high on the chart?

Do you want to find the highest high during each day?

Code:
def bn = barnumber();
input reset_high_daily = yes;
def newday = GetDay() <> GetDay()[1];
def hihi = if bn = 1 then 0
else if (reset_high_daily and newday) then high
else if high > hihi[1] then high
else hihi[1];
addchartbubble(1, low, hihi, color.yellow, no);

ps,
your code is overly complicated.

if you are using the same time frame as the chart, you don't need to specify aggregation periods within price functions.

You don't need to specify a price fundamental type close, you can just say close.

functions expect lengths to be integers, not 3.5. you can't read from 1/2 a bar. i haven't tried this, so guessing the number will be rounded down to 3.

if you use an input for defining an average type, Instead of def, then it can be changed in the settings window and you can experiment with different types of averages.

hope this helps. keep asking questions and learning
 
Solution
You are checking when a crossing happens,
then checking if a high is higher than a previous high.

that is what happened where you have your arrow on the image. the bar crossed above a line and the high is higher than the previous high, so the value changed in the bubble.

you aren't comparing the current high to a variable holding the highest high of the chart, rangeHighest[1].
you are only comparing the current high to the previous bar high.

def rangeHighest ; if(baseTF_isCrossAboveBefore) {
if(baseTF_HAhigh > baseTF_HAhigh[1]) {
rangeHighest = baseTF_HAhigh;
}
......


Do you want to find the highest high on the chart?

Do you want to find the highest high during each day?

Code:
def bn = barnumber();
input reset_high_daily = yes;
def newday = GetDay() <> GetDay()[1];
def hihi = if bn = 1 then 0
else if (reset_high_daily and newday) then high
else if high > hihi[1] then high
else hihi[1];
addchartbubble(1, low, hihi, color.yellow, no);

ps,
your code is overly complicated.

if you are using the same time frame as the chart, you don't need to specify aggregation periods within price functions.

You don't need to specify a price fundamental type close, you can just say close.

functions expect lengths to be integers, not 3.5. you can't read from 1/2 a bar. i haven't tried this, so guessing the number will be rounded down to 3.

if you use an input for defining an average type, Instead of def, then it can be changed in the settings window and you can experiment with different types of averages.

hope this helps. keep asking questions and learning
Thank you for your help.

Your this sentence resolved my issue:
you aren't comparing the current high to a variable holding the highest high of the chart, rangeHighest[1].

I tried this: if(baseTF_HAhigh > rangeHighest ) .But it should be this: if(baseTF_HAhigh > rangeHighest[1])

My code is from a bigger file involving multi-time frame. I am still new with ThinkScript so the code is not concise and the logic may not be straightforward.

Yes, keep learning!
 

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