• Get $40 off VIP by signing up for a free account! Sign Up

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;
 
  • Like
Reactions: Tos
Solution

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

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();
#
 
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);
#
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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