Can anyone explain this in plain english?

rottentrade

Member
def BuyLine;

if close[1] > BuyLine[1] { <------ ???
BuyLine = Double.NaN;
} else if CBD == ConsecutiveBarsDown {
BuyLine = Trigger_Line;
} else {
BuyLine = BuyLine[1];
}

I found the above code in another thread and I was wondering if you can explain it for me:
https://usethinkscript.com/threads/intraday-scalping-strategy-for-thinkorswim.10698/


I don't understand what the logic is when it comes to BuyLine[1]. Don't you need to define what the BuyLine is first? There was no mention of BuyLine before this.
 
Solution
ByLine is defined as the first line in the code you have posted. Notice how it is not set with a value. This is what the if, else if, else statement is doing. Try removing the 'else' part and you will see that it throws an error, because for this to work it must always set the value.

Edit: Think of it like this, on the first iteration of the script, BuyLine is not set with a value. If the last close is greater than the last BuyLine value, it sets it to NaN. If that CBD condition is met, it sets it Trigger_Line, otherwise it sets it to the last value of itself, which on the first iteration would be NaN I believe (not too sure on this maybe it would be 0, still kinda new to thinkscipt myself) The end result now is that...
ByLine is defined as the first line in the code you have posted. Notice how it is not set with a value. This is what the if, else if, else statement is doing. Try removing the 'else' part and you will see that it throws an error, because for this to work it must always set the value.

Edit: Think of it like this, on the first iteration of the script, BuyLine is not set with a value. If the last close is greater than the last BuyLine value, it sets it to NaN. If that CBD condition is met, it sets it Trigger_Line, otherwise it sets it to the last value of itself, which on the first iteration would be NaN I believe (not too sure on this maybe it would be 0, still kinda new to thinkscipt myself) The end result now is that BuyLine is initialized with a value.
 
Solution

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

on the first iteration would be NaN I believe (not too sure on this maybe it would be 0, still kinda new to thinkscipt myself)

In this situation, it will be a zero. In other situations, primarily when dealing with series functions (open, close, etc.), it will actually fetch data from beyond the left edge of the chart, in which case BarNumber() will run negative. However, this assumes that the data does in fact exist. Data can not be fetched from prior to an IPO, for example. I assume it is also limited to the maximum selectable time frame of each respective aggregation, whether selected for or not. This is true of chart studies only, scans and columns are extremely limited when it comes to historic data.
 
Edit: Think of it like this, on the first iteration of the script, BuyLine is not set with a value. If the last close is greater than the last BuyLine value, it sets it to NaN. If that CBD condition is met, it sets it Trigger_Line, otherwise it sets it to the last value of itself, which on the first iteration would be NaN I believe (not too sure on this maybe it would be 0, still kinda new to thinkscipt myself) The end result now is that BuyLine is initialized with a value.
Thanks for the explanation. I follow what you're saying but I still can't wrap my head around the fact that you can assign a value to something that isn't defined yet. For example, suppose I exclude the middle part and just write

if close[1] > BuyLine[1]
BuyLine = Double.NaN;
} else {
BuyLine = BuyLine[1];
}

Would that work??
 
Thanks for the explanation. I follow what you're saying but I still can't wrap my head around the fact that you can assign a value to something that isn't defined yet. For example, suppose I exclude the middle part and just write

if close[1] > BuyLine[1]
BuyLine = Double.NaN;
} else {
BuyLine = BuyLine[1];
}

Would that work??
I believe so yes, see the other comment above by Joshua. In this instance it would either be set to 0 or the previous bar, the one before your timeframe. You can plot the BuyLine and/or use AddLabel to see what's it's doing.
 
I believe so yes, see the other comment above by Joshua. In this instance it would either be set to 0 or the previous bar, the one before your timeframe. You can plot the BuyLine and/or use AddLabel to see what's it's doing.
Thx for the reply. With your suggestion, I ran the following code on the chart. Unfortunately, I didn't see any "BuyLine" (as it should because it's not defined IMO) and only "N/A" was marked on the Labels.

Code:
def BuyLine;
if close[1] > BuyLine[1]{
BuyLine = Double.NaN;
} else {
BuyLine = BuyLine[1];
}

plot BuyLinePlot = BuyLine;
BuyLinePlot.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
BuyLinePlot.AssignValueColor(Color.MAGENTA);

AddLabel(yes, "BuyLine: " + BuyLine, Color.LIGHT_GRAY);
AddLabel(yes, "BuyLine[1]: " + BuyLine[1], Color.LIGHT_GRAY);
 
Thx for the reply. With your suggestion, I ran the following code on the chart. Unfortunately, I didn't see any "BuyLine" (as it should because it's not defined IMO) and only "N/A" was marked on the Labels.

Code:
def BuyLine;
if close[1] > BuyLine[1]{
BuyLine = Double.NaN;
} else {
BuyLine = BuyLine[1];
}

plot BuyLinePlot = BuyLine;
BuyLinePlot.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
BuyLinePlot.AssignValueColor(Color.MAGENTA);

AddLabel(yes, "BuyLine: " + BuyLine, Color.LIGHT_GRAY);
AddLabel(yes, "BuyLine[1]: " + BuyLine[1], Color.LIGHT_GRAY);
Ah, you need to add a condition though similar to what you had before with the:

} else if CBD == ConsecutiveBarsDown {
BuyLine = Trigger_Line;
}

Sorry I should have clarified that. As is stands with your code now, there is nothing putting a value other than Double.NaN into BuyLine. I'm not sure how the condition is triggered but it is possible it may not be triggered on the timeframe you are looking, or at all. The key part is that else if where a value other than NaN will be set and then you would see it on the plot. Without seeing the rest of the code it is hard to tell.
 
Just to clarify.

On the first iteration, BuyLine[1] will be Zero.
Close is greater than Zero, BuyLine will be set to Double.NaN.

On the second iteration, Close > Double.NaN will always resolve to Double.NaN
As will all similar comparisons regarding Double.NaN I believe.
Its basically a "soft" invalid statement, so to speak.

However, in thinkscript NaN natively converts to False in regard to Boolean Expressions.
So from there it resolves to - If No Then - which can never be true, leading to Else.
 
def BuyLine;

if close[1] > BuyLine[1] { <------ ???
BuyLine = Double.NaN;
} else if CBD == ConsecutiveBarsDown {
BuyLine = Trigger_Line;
} else {
BuyLine = BuyLine[1];
}

I found the above code in another thread and I was wondering if you can explain it for me:
https://usethinkscript.com/threads/intraday-scalping-strategy-for-thinkorswim.10698/


I don't understand what the logic is when it comes to BuyLine[1]. Don't you need to define what the BuyLine is first? There was no mention of BuyLine before this.

if you aren't sure how a formula will be evaluated, you can display variables in a bubble and see what the values are.
i don't remember the rules that joshua mentioned in post3, so i tend to add bubbles often, when i am working on a study. the \n command forces a new line.

addchartbubble(1, low*0.997,
close + " c\n" +
close[1] + " c1\n" +
buyline + " b0\n" +
buyline[1] + " b1\n" +
(close[1] > BuyLine[1]) + " c1>b1\n"
, color.yellow, no);

you can change the 4th parameter, from this, color.yellow, to something like this to change the color.
, (if (close[1] > BuyLine[1]) then color.yellow else color.gray)

you might have to use isnan( ) in order to evaluate some variables/expressions.
, (if isnan(close[1] > BuyLine[1]) then color.yellow else color.gray),
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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