thinkScript GetSymbol() Variable Lookup

jpbcx43

New member
I have a script that plots price lines based on some calculations, however I am currently defining variables based on inputs. The goal here is to define variables dynamically based on the current symbol.

Example
  • If the current symbol is AMD, define Price1 as 50 and Price2 as 20.
  • If the current symbol is SPX, define Price1 as 3000 and Price2 as 2900.

For some reason, this logic is displaying "Invalid statement: if at 1:1" and "Syntax error: Semicolon expected at 1:1". Can anyone help? I really can't tell what I am missing, or if switch statement would be better.

Code:
if GetSymbol() == "SPX" {
    Price1 = "2900";
    Price2 = "3000";
} else

if GetSymbol() == "AMD" {
    Price1 = "50";
    Price2 = "20";
} else  {
    Double.NaN;
}

I was able to accomplish something in the meantime, however I am not sure this is the most efficient way of defining the variables:

Code:
def Price1 = if (GetSymbol() == "AMD") then 50
     else if (GetSymbol() == "SPX") then 2900
     else if (GetSymbol() == "SPY") then 290
     else if (GetSymbol() == "AAPL") then 286
     else if (GetSymbol() == "STZ") then 164
     else Double.NaN;

def Price2 = if (GetSymbol() == "AMD") then 20
     else if (GetSymbol() == "SPX") then 3000
     else if (GetSymbol() == "SPY") then 300
     else if (GetSymbol() == "AAPL") then 292
     else if (GetSymbol() == "STZ") then 168
     else Double.NaN;
 
Solution
Define the variables above your conditional statement. If you assign a value in an IF statement, you must make sure each ...IF, ELSE IF, ELSE assigns also.

Code:
def Price1;
def Price2;
if GetSymbol() == "SPX" {
    Price1 = 2900;
    Price2 = 3000;
} else if GetSymbol() == "AMD" {
    Price1 = 50;
    Price2 = 20;
} else  {
    Price1 = Double.NaN;
    Price2 = Double.NaN;
}

Also, the current symbol is the default when you use most Thinkscript functions (just use high, low, close, open, etc)
I prefer to make the numbers fully dynamic:
Code:
input percent = 1;
def Price3 = close * (1 - percent/100);
def Price4 = close * (1 + percent/100);
I had this working when it was a little simpler. Now that I've added other things, it's no longer plotting the lines or the label. Hopefully, someone with more experience will be able to help me out.

I'm trying to plot three lines (high, low, close) that are inputted manually at the beginning of the RTH and ETH sessions for SPX or ES futures. I also want to add a cloud around those three line plots. Depending on which symbol (SPX or ES) the chart is showing and which session is currently trading (RTH or ETH), I want the label to show the info specific to the symbol and the current session.

Code:
#
# Predicted High Low Close for RTH and ETH sessions #                                                   
# These levels are inputed manually and is only valid
# for the specified period!
#                                                                       
                                                                      
                    
def na = Double.NaN;
def timeStrRTH = (SecondsFromTime(930) > 0) and (SecondsFromTime(1700) < 0);
def timeStrETH = ((1700) > 0) and (SecondsFromTime(930) < 0);

input AlgoHigh = 2801;
input AlgoLow = 2689;
input AlgoClose = 2743;
input AlgoHighETH = 2801;
input AlgoLowETH = 2689;
input AlgoCloseETH = 2743;
input diff = 12;
input MarginErrorHigh = 10;
input MarginErrorLow = 14.5;
input MarginErrorClose = 0;

def AHRTH = AlgoHigh - diff;
def ALRTH = AlgoLow - diff;
def ACRTH = AlgoClose - diff;
def AHETH = AlgoHighETH - diff;
def ALETH = AlgoLowETH - diff;
def ACETH = AlgoCloseETH - diff;

plot AH;
plot AL;
plot AC;

if  GetSymbol() == "/ES:XCME" {
    AH = if timeStrRTH then AHRTH else if timeStrETH then AHETH else na ;
    AL = if timeStrRTH then ALRTH else if timeStrETH then ALETH else na ;
    AC = if timeStrRTH then ACRTH else if timeStrETH then ACETH else na ;
} else

if  GetSymbol() == "SPX" {
    AH = if timeStrRTH then AlgoHigh else na ;
    AL = if timeStrRTH then AlgoLow else na ;
    AC = if timeStrRTH then AlgoClose else na ;
} else  {
    AH = na;
    AL = na;
    AC = na;

}

def upperAH = AH + (MarginErrorHigh/2);
def lowerAH = AH - (MarginErrorHigh/2);
def upperAL = AL + (MarginErrorLow/2);
def lowerAL = AL - (MarginErrorLow/2);
def upperAC = AC + (MarginErrorClose/2);
def lowerAC = AC - (MarginErrorClose/2);

AddCloud(upperAH, lowerAH, Color.LIGHT_RED);
AddCloud(upperAL, lowerAL, Color.LIGHT_GREEN);
AddCloud(upperAC, lowerAC, Color.light_ORANGE);

AH.SetDefaultColor(Color.RED);
AH.SetLineWeight(2);

AL.SetDefaultColor(Color.GREEN);
AL.SetLineWeight(2);

AC.SetDefaultColor(Color.ORANGE);
AC.SetLineWeight(2);

AddLabel(GetSymbolPart()== "SPX", "Algo High: " + AH, Color.RED);
AddLabel(GetSymbol()== "SPX" and timeStrRTH, "Algo Low: " + AlgoLow, Color.GREEN);
AddLabel(GetSymbol()== "SPX" and timeStrRTH, "Algo Close: " + AlgoClose, Color.ORANGE);

AddLabel(GetSymbol()== "SPX" and timeStrETH, "Algo High: " + AlgoHighETH, Color.RED);
AddLabel(GetSymbol()== "SPX" and timeStrETH, "Algo Low: " + AlgoLowETH, Color.GREEN);
AddLabel(GetSymbol()== "SPX" and timeStrETH, "Algo Close: " + AlgoCloseETH, Color.ORANGE);
                                  
AddLabel(GetSymbolPart()== "/ES:XCME" and timeStrRTH, "Algo Low: " + ALRTH, Color.GREEN);
AddLabel(GetSymbol()== "/ES:XCME" and timeStrRTH, "Algo Close: " + ACRTH, Color.ORANGE);

AddLabel(GetSymbol()== "/ES:XCME" and timeStrETH, "Algo High: " + AHETH, Color.RED);
AddLabel(GetSymbol()== "/ES:XCME" and timeStrETH, "Algo Low: " + ALETH, Color.GREEN);
AddLabel(GetSymbol()== "/ES:XCME" and timeStrETH, "Algo Close: " + ACETH, Color.ORANGE);
 

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

Define the variables above your conditional statement. If you assign a value in an IF statement, you must make sure each ...IF, ELSE IF, ELSE assigns also.

Code:
def Price1;
def Price2;
if GetSymbol() == "SPX" {
    Price1 = 2900;
    Price2 = 3000;
} else if GetSymbol() == "AMD" {
    Price1 = 50;
    Price2 = 20;
} else  {
    Price1 = Double.NaN;
    Price2 = Double.NaN;
}

Also, the current symbol is the default when you use most Thinkscript functions (just use high, low, close, open, etc)
I prefer to make the numbers fully dynamic:
Code:
input percent = 1;
def Price3 = close * (1 - percent/100);
def Price4 = close * (1 + percent/100);
 
Solution

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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