"Value never assigned" to variable defined. Involves 'if' statement.

ADM1992

New member
I'm not exactly sure what is going on here as I've seen near identical code working (different variable names) elsewhere in my script.

For context, I am trying to detect when a trendline (MidpointLine) crosses over an offset line (Offset1) and then assigning some values to variables associated with the date the line crosses over the offset.

With the code below, I'm receiving a 'Value never assigned to c1Date at...' error. I'm new to writing TS code and I would appreciate the help.

Code:
def c1Date;

if MidpointLine > Offset1 && MidpointLine[1] <= offset[1] {
    c1Date = GetYYYYMMDD();
} else {}
 
Solution
@ADM1992 just have it carry the previous value.
Ruby:
def c1Date;

if MidpointLine > Offset1 && MidpointLine[1] <= offset[1] {
    c1Date = GetYYYYMMDD();
} else {
    c1Date = c1Date[1];
}
@ADM1992 you have to provide a condition or value assign to c1Date if your IF statement is false.

Ruby:
def c1Date;

if MidpointLine > Offset1 && MidpointLine[1] <= offset[1] {
    c1Date = GetYYYYMMDD();
} else {
    c1Date = <Need Something Here>;
}
 
@ADM1992 you have to provide a condition or value assign to c1Date if your IF statement is false.

Ruby:
def c1Date;

if MidpointLine > Offset1 && MidpointLine[1] <= offset[1] {
    c1Date = GetYYYYMMDD();
} else {
    c1Date = <Need Something Here>;
}
Thank you for your answer. While it did resolve the error mentioned above, I've now run into another issue. Pretty much, I am looking to just save the most recent crossing date into a variable to reference at a later time. But if I need to declare the value of that same variable under the 'else', it will overwrite the stored variable.

Writing the value as shown breaks the label I am using for testing:
Code:
def c1Date;

if MidpointLine > Offset1 && MidpointLine[1] <= offset[1] {
    c1Date = GetYYYYMMDD();
} else {
    c1Date = c1Date;
}
 
Perhaps I am going about this the wrong way. Could you point me to any code that accomplishes what I'm doing?

Just looking to pull the date and price of the daily midpoint value of a stock when it crosses over my trendline.
 
@ADM1992 just have it carry the previous value.
Ruby:
def c1Date;

if MidpointLine > Offset1 && MidpointLine[1] <= offset[1] {
    c1Date = GetYYYYMMDD();
} else {
    c1Date = c1Date[1];
}
 
Solution
@ADM1992 just have it carry the previous value.
Ruby:
def c1Date;

if MidpointLine > Offset1 && MidpointLine[1] <= offset[1] {
    c1Date = GetYYYYMMDD();
} else {
    c1Date = c1Date[1];
}
Thank you for this! I was able to start from here and modify it exactly where I needed it to be.

I'm now well on my way to recreating Hurst's market cycle projections.
 

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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