Invalid Script {} function

OptionsX

New member
I cannot figure out why I am getting this error message: "Invalid statement: if at 19:1"
Any help would be greatly appreciated.
my complete code entry is as follows :

input length = 14;
input ADXLevels = 20;
input averageType =
AverageType.WILDERS;

def fiveMinPlus;
def fiveMinMinus;
def fiveMinAdx;
def fiveMinBullishSignal;
def fiveMinBearishSignal;
def fiveMinBullishZone;
def fiveMinBearishZone;
def fiveMinNeutralZone;
def fiveMinHiDiff;
def fiveMinLoDiff;
def fiveMinPlusDM;
def fiveMinMinusDM;
def fiveMinDX;
if GetAggregationPeriod()<=
AggregationPeriod.FIVE_MlN{

fiveMinHiDiff = high(period = "5Min") - high(period = "5 Min")[1];
fiveMinLoDiff = low(period = "5Min")[1] - low(period = "5 Min");
fiveMinPlusDM = if
fiveMinHiDiff > fiveMinLoDiff and
fiveMinHiDiff > 0 then
fiveMinHiDiff else 0;
fiveMinMinusDM = if
fiveMinLoDiff > fiveMinHiDiff and
fiveMinLoDiff > 0 then
fiveMinLoDiff else 0;
fiveMinPlus = 100 *
MovingAverage(averageType,
fiveMinPlusDM, length);
fiveMinMinus = 100 *
MovingAverage(averageType,

fiveMinMinusDM, length);
fiveMinBulishSignal =
fiveMinPlus crosses abovefiveMinMinus;
fiveMinBearishSignal =
fiveMinPlus crosses belowfiveMinMinus;

fiveMinDX = if (fiveMinPlus +
fiveMinMinus > 0) then 100 *
AbsValue(fiveMinPlus -
fiveMinMinus)/ (fiveMinPlus +
fiveMinMinus) else 0;
fiveMinAdx =
fiveMinPlus < fiveMinMinus and

fiveMinBullishZone and !
fiveMinBearishZone;

}
else{
fiveMinPlus = 0;
fiveMinMinus = O;
fiveMinAdx = 0;
fiveMinPlusDM = 0;
fiveMinMinusDM = 0;
fiveMinBullishSignal = 0;
fiveMinBearishSignal =0;
fiveMinDX = 0;

fiveMinBullishZone = 0;
fiveMinBearishZone = 0;
fiveMinNeutralZone = 0;

fiveMinHiDiff = 0;
fiveMinLoDiff = 0;

AddLabel(fiveMinBearishZone,
"5m", Color.GREEN);
AddLabel(fiveMinBearishZone,
"5m", Color.RED);
AddLabel(fiveMinNeutralZone,
"5m", Color. YELLOW);
 
Solution
At first glance, you are missing a 'THEN' statement in your first 'IF' statement and you don't have the all the same variables assigned on both sides of the }else{ statement.
Hard to tell just looking at text not in a code block.

This line doesn't make sense at all:
Ruby:
fiveMinAdx =
fiveMinPlus < fiveMinMinus and

fiveMinBullishZone and !
fiveMinBearishZone;

Can tell you more when I get home this afternoon.
At first glance, you are missing a 'THEN' statement in your first 'IF' statement and you don't have the all the same variables assigned on both sides of the }else{ statement.
Hard to tell just looking at text not in a code block.

This line doesn't make sense at all:
Ruby:
fiveMinAdx =
fiveMinPlus < fiveMinMinus and

fiveMinBullishZone and !
fiveMinBearishZone;

Can tell you more when I get home this afternoon.
 
Last edited:
Solution

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

hope you don't mind svanoy, i peeked at this and couldn't stop finding errors,....


@OptionsX
there are many typos in your code (8). that is one of the bad things in tos, not all errors are always highlighted. i marked them with a comment and *************

this line had min misspelled, with an L not an i.
if GetAggregationPeriod() <= AggregationPeriod.FIVE_MlN {
another with letter o instead of zero

if you did this, you need to slow down and pay attention when typing.


this study won't do anything.
the labels are driven by 3 variables,
fiveMinBullishZone = 0;
fiveMinBearishZone = 0;
fiveMinNeutralZone = 0;
but they don't have any formulas, all are set to 0. they were missing entirely from top half of if-then. i added them in and set to 0. you need to come up with formulas for them.


Code:
#fix_ifthen2
#https://usethinkscript.com/threads/invalid-script-function.20248/
#Invalid Script {} function

input length = 14;
input ADXLevels = 20;
input averageType = AverageType.WILDERS;

def fiveMinPlus;
def fiveMinMinus;
def fiveMinAdx;
def fiveMinBullishSignal;

def fiveMinBearishSignal;
def fiveMinBullishZone;
def fiveMinBearishZone;
def fiveMinNeutralZone;
def fiveMinHiDiff;
def fiveMinLoDiff;
def fiveMinPlusDM;
def fiveMinMinusDM;
def fiveMinDX;

#  copy words from web site - AggregationPeriod.FIVE_MIN;
#if GetAggregationPeriod() <= AggregationPeriod.FIVE_MlN {
# *************  min was misspelled
if GetAggregationPeriod() <= AggregationPeriod.FIVE_MiN {

# make a simple if-then for testing. it works
#if close > 4 then {

#    fiveMinHiDiff = high(period = "5Min") - high(period = "5 Min")[1];
#    fiveMinLoDiff = low(period = "5Min")[1] - low(period = "5 Min");
# *************  change period words
    fiveMinHiDiff = high(period = AggregationPeriod.FIVE_MIN) - high(period = AggregationPeriod.FIVE_MIN)[1];
    fiveMinLoDiff = low(period = AggregationPeriod.FIVE_MIN)[1] - low(period = AggregationPeriod.FIVE_MIN);

    fiveMinPlusDM = if fiveMinHiDiff > fiveMinLoDiff and fiveMinHiDiff > 0 then fiveMinHiDiff else 0;
    fiveMinMinusDM = if fiveMinLoDiff > fiveMinHiDiff and fiveMinLoDiff > 0 then fiveMinLoDiff else 0;
    fiveMinPlus = 100 * MovingAverage(averageType, fiveMinPlusDM, length);
    fiveMinMinus = 100 * MovingAverage(averageType, fiveMinMinusDM, length);

#    fiveMinBulishSignal = fiveMinPlus crosses above fiveMinMinus;
# ******************** misspell  bullish
    fiveMinBullishSignal = fiveMinPlus crosses above fiveMinMinus;

    fiveMinBearishSignal = fiveMinPlus crosses below fiveMinMinus;
    fiveMinDX = if (fiveMinPlus + fiveMinMinus > 0) then 100 * AbsValue(fiveMinPlus - fiveMinMinus) / (fiveMinPlus + fiveMinMinus) else 0;
    fiveMinAdx = fiveMinPlus < fiveMinMinus and fiveMinBullishZone and ! fiveMinBearishZone;

# ************* add missing variables. these are all 0, so nothing will be displayed
    fiveMinBullishZone = 0;
    fiveMinBearishZone = 0;
    fiveMinNeutralZone = 0;

} else {
    fiveMinPlus = 0;
# ************** change o to zero
    fiveMinMinus = 0;
    fiveMinAdx = 0;
    fiveMinPlusDM = 0;
    fiveMinMinusDM = 0;
    fiveMinBullishSignal = 0;
    fiveMinBearishSignal = 0;
    fiveMinDX = 0;

    fiveMinBullishZone = 0;
    fiveMinBearishZone = 0;
    fiveMinNeutralZone = 0;

    fiveMinHiDiff = 0;
    fiveMinLoDiff = 0;
# **************  add } at end
}

# ************ these wont do anything, because the variables are set to 0
#AddLabel(fiveMinBearishZone, "5m", Color.GREEN);
# *************  bear instead of bull
AddLabel(fiveMinBullishZone, "5m", Color.GREEN);
AddLabel(fiveMinBearishZone, "5m", Color.RED);
AddLabel(fiveMinNeutralZone, "5m", Color. YELLOW);

#
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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