# Ratio calculation using thinkscript

#### Tos

##### New member
Hello All. Thank you for your time and interest in helping others.

I try to findout the % gain / loss after close crosses above hullmovingavg(close,200) till now.

I am getting an error "Only constants expected here: cnt CL function indexer of close at 2:25".

My code is as follows:

def cnt = if close > hullmovingavg(close,200) then 0 else cnt[1] + 1;
plot a= ((close / close[cnt])-1)*100;

Thank you again!!

Solution
@Tos
You can't use a variable in that context.
Change it to:
Ruby:
``````def cnt = if close > hullmovingavg(close,200) then 0 else cnt[1] + 1;
plot a= ((close / GetValue(close,cnt))-1)*100;``````
@Tos
You can't use a variable in that context.
Change it to:
Ruby:
``````def cnt = if close > hullmovingavg(close,200) then 0 else cnt[1] + 1;
plot a= ((close / GetValue(close,cnt))-1)*100;``````

Tos
@Tos
You can't use a variable in that context.
Change it to:
Ruby:
``````def cnt = if close > hullmovingavg(close,200) then 0 else cnt[1] + 1;
plot a= ((close / GetValue(close,cnt))-1)*100;``````
Thank you!! Thank you!!

Hello All. Thank you for your time and interest in helping others.

I try to findout the % gain / loss after close crosses above hullmovingavg(close,200) till now.

I am getting an error "Only constants expected here: cnt CL function indexer of close at 2:25".

My code is as follows:

def cnt = if close > hullmovingavg(close,200) then 0 else cnt[1] + 1;
plot a= ((close / close[cnt])-1)*100;

Thank you again!!

what svanoy said
and you will want to change the formula, from > to crosses above.
in your formula, the ratio is never above 0.

Ruby:
``````declare lower;

def hullavg = hullmovingavg(close,200);

def cnt = if close > hullavg then 0 else cnt[1] + 1;
#plot a= ((close / close[cnt])-1)*100;

plot a = ((close / getvalue(close, cnt)) - 1) *100;
a.SetDefaultColor(Color.yellow);

addlabel(1, "hull ratio " + a + "%", color.yellow);

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

def clscross = if close crosses above hullavg then close else clscross[1];

plot b = ((close - clscross) / close) * 100;
#b.SetStyle(Curve.MEDIUM_DASH);
b.SetDefaultColor(Color.cyan);
# b.setlineweight(1);
b.hidebubble();
#``````

Thank you again!!
How to count red candles after the crosses above. Thank you!!

How to count red candles after the crosses above. Thank you!!

something like this,

Ruby:
``````def bn = barnumber();
def hullavg = hullmovingavg(close,200);

def clscross2 = if close crosses above hullavg then 1 else 0;

def redbar = (close < open);

# cnt red bars. reset when crossing
def redcnt = if bn == 1 then 0 else if clscross2 then redbar else if redbar then redcnt[1] + 1 else redcnt[1];

addlabel(1, " Red bars after a crossing " + redcnt, color.magenta);
#``````

volume pressure ratio as histogram bars Questions 2
VOLD RATIO Questions 1
call put ratio scanner Questions 3
Volume Ratio 15MIN VS Daily Questions 1
Extrinsic value ratio? Can it be done? Questions 1

87k+ Posts
214 Online

## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

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?