If statement throws error

shahgols

New member
Hi all, I am trying to create a script that will add a bubble when the stock goes up 10% AND the stock jumps 3% in the past 5 minutes. In other words, I want the bubbles to show up only after the stock has risen 10%. I wrote this script, the 3% part works, but TOS shows error "Invalid statement: if at 13:1" for the If statement. Can you please help fix it? Thank you.

Code:
input aggregationPeriod = AggregationPeriod.MIN;
def prev_close = close(period = aggregationPeriod)[5];

input priceType = PriceType.LAST;
def LastPrice = close(priceType = priceType);
def rise = close >= 1.03 * prev_close;


def agg = AggregationPeriod.DAY;
def TenPercentLine = (open(period = agg) * .1) + open(period = agg);

if close > TenPercentLine then
AddChartBubble(rise, low, "3%", Color.GREEN, no)
else Double.NaN;
 
Last edited:
Solution
Hi all, I am trying to create a script that will add a bubble when the stock goes up 10% AND the stock jumps 3% in the past 5 minutes. In other words, I want the bubbles to show up only after the stock has risen 10%. I wrote this script, the 3% part works, but TOS shows error "Invalid statement: if at 13:1" for the If statement. Can you please help fix it? Thank you.

here is a new study


look for a small % gain then a larger gain

this will look for,
. a percent gain (default 3%),
. over x bars (default 5),
. then look for a second gain (default 10%),
. calculated from the low of the 3% gain.

each day, the 3% gain is reset.

when a 3% gain is found, a white wedge is drawn above the bar.
when a 10% gain is found, a green...
Hi all, I am trying to create a script that will add a bubble when the stock goes up 10% AND the stock jumps 3% in the past 5 minutes. In other words, I want the bubbles to show up only after the stock has risen 10%. I wrote this script, the 3% part works, but TOS shows error "Invalid statement: if at 13:1" for the If statement. Can you please help fix it? Thank you.

Code:
input aggregationPeriod = AggregationPeriod.MIN;
def prev_close = close(period = aggregationPeriod)[5];

input priceType = PriceType.LAST;
def LastPrice = close(priceType = priceType);
def rise = close >= 1.03 * prev_close;


def agg = AggregationPeriod.DAY;
def TenPercentLine = (open(period = agg) * .1) + open(period = agg);

if close > TenPercentLine then
AddChartBubble(rise, low, "3%", Color.GREEN, no)
else Double.NaN;

how can '3% works' if there is an error?
in some previous version?

you didn't assign a variable to the if-then.
you can't put a bubble within an if-then.

restructure the if-then to be set to a variable, used to enable the bubble.

Code:
AddChartBubble(rise, low, "3%", Color.GREEN, no);

def bub10 = if close > TenPercentLine then 1 else 0;

AddChartBubble(bub10, low, "10%", Color.GREEN, no);
 

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

Hi again, I apologize, I am terrible at thinkscript, everything that I have put together, I have copy/pasted from here and there. Can you please check this to see if it does what I want? Thank you!

Code:
input aggregationPeriod = AggregationPeriod.MIN;
def prev_close = close(period = aggregationPeriod)[5];

input priceType = PriceType.LAST;
def LastPrice = close(priceType = priceType);
#plot rise = LastPrice >= 1.03 * prev_close;

def agg = AggregationPeriod.DAY;
plot TenPercentLine = (open(period = agg) * .1) + open(period = agg);

def bub10 = if close > (open(period = agg) * 1.13) then 1 else 0;

AddChartBubble(bub10, low, "3%", Color.GREEN, no);
 
Hi all, I am trying to create a script that will add a bubble when the stock goes up 10% AND the stock jumps 3% in the past 5 minutes. In other words, I want the bubbles to show up only after the stock has risen 10%. I wrote this script, the 3% part works, but TOS shows error "Invalid statement: if at 13:1" for the If statement. Can you please help fix it? Thank you.

don't need to apologize, you are trying. learning new things takes time.

your rules are fuzzy
1. what chart time are you looking at? 1 minute bars?

2. 3% rise. comparing the first and last bars over 5 minutes, you could miss big moves that start on the 2nd bar. better to find the highest and lowest during the time.

3. 10% rise. what is the starting point ? 5 minutes ago ? after the 3% rise?
are you looking for a total of 10% rise , or 3 + 10 , for 13% ?
what is the time period during which, the price goes up 10%? 1 min, 5 min , 1 day, 1 year?
is there a max time when no 10% happens, and you reset the 3% trigger?
or if a new 3% rise happens, will that reset the starting point, and start looking for a new 10%?
 
Hi all, I am trying to create a script that will add a bubble when the stock goes up 10% AND the stock jumps 3% in the past 5 minutes. In other words, I want the bubbles to show up only after the stock has risen 10%. I wrote this script, the 3% part works, but TOS shows error "Invalid statement: if at 13:1" for the If statement. Can you please help fix it? Thank you.

here is a new study


look for a small % gain then a larger gain

this will look for,
. a percent gain (default 3%),
. over x bars (default 5),
. then look for a second gain (default 10%),
. calculated from the low of the 3% gain.

each day, the 3% gain is reset.

when a 3% gain is found, a white wedge is drawn above the bar.
when a 10% gain is found, a green up arrow is drawn.


test with NSPR 1m 4Day chart , with 2% and 4%.

Ruby:
# gains_3then10_0

def na = double.nan;
def bn = barnumber();
# -------------------
def diffday = if GetDay() != GetDay()[1] then 1 else 0;

input rise1_percent = 3.0;
input rise1_bars = 5;

input rise2_percent = 10.0;

def hi1 = Highest(high, rise1_bars);
def lo1 = Lowest(low, rise1_bars);
def r1per =  Round(((hi1 - lo1) / lo1) * 100, 1);

# reset 3% trigger each day. keep low value when a 3% rise
def r1gain = if diffday then 0
 else if r1per >= rise1_percent then lo1
 else r1gain[1];

def r1first = if r1gain[1] == 0 and r1gain > 0 then 1 else 0;

# draw a wedge on 3% gains
plot w1 = if r1first then 1 else na;
w1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_up);
w1.SetDefaultColor(Color.white);
w1.setlineweight(3);
w1.hidebubble();

# calc the 2nd %
def r2per = Round(((close - lo1) / lo1) * 100, 1);

def r2gain = if r1gain == 0 then 0
 else if r2per >= rise2_percent then 1
 else 0;

# plot up arrow on rise2 gain
plot arrow = if r2gain then low else na;
arrow.SetPaintingStrategy(PaintingStrategy.arrow_up);
arrow.SetDefaultColor(Color.green);
arrow.setlineweight(4);
arrow.hidebubble();
#
 
Solution

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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