# Plotting trendline with if else condition

#### Prosperity

##### New member
I am working on a script to plot a trendline based on the following 5 conditions. I've made some progress but I am stuck on getting a conditional expression to work. Any help is appreciated.

CODE:
Code:
``````#Trend Line

#Higher Highs, Higher Lows
def HH = high[0]>high[1] AND low[0]>=low[1];
#Lower Highs, Lower Lows
def LL = high[0]<=high[1] AND low[0]<low[1];
#Inside Day
def ID = high[0]<=high[1] AND low[0]>=low[1];
#Outside Day Up
def ODU=high[0]>high[1] AND low[0]<low[1] AND close[0]>open[0];
#Outside Day Down
def ODD=high[0]>high[1] AND low[0]<low[1] AND close[0]<open[0];

#higher highs higher lows
plot up = if HH then high else double.NaN;

#lower highs lower lows
plot down = if LL then high else double.NaN;``````

This code works and I am able to plot trend lines, except on inside or outside days.

The next step in the process requires an "if-else" conditional expression. Specifically, on an inside day (ID), if the previous day were a "plot up" day (meaning the trendline connects to the high of the previous day), the inside day would be a "plot down" day (line connects to the low of the ID). On the other hand, if the last day were a "plot down" day (meaning the line connects to the low of the previous day), the inside day would be a "plot up" day (trend line connects to the ID's high). I am struggling to find a way to define the previous day as either "plot up" or "plot down." I am also unsure which syntax to use to extend the trend line. For example, can one study have multiple conditions to draw the line, or is it better to do separate studies for each condition since they should all connect in theory?

Last edited:
i've read your post a few times, but it's too confusing. maybe it's too early for me.
get a piece of paper and create a true/false truth table of what you want. on the left, have a column for each input, in the middle a column for the t/f output. in the right column, write a formula for each row, = a and b and not c.
use a[1] to reference the previous candle.
use !a to reference the inverse of a.
when you have formulas, try to join them together.

to draw a horizontal line from 1 trigger, you need to pass a previous price level onto the current candle.

Code:
``````def trigger = ( some formula that is true once in awhile)

def linelevel = if trigger then close else linelevel[1];

plot a= linelevel;``````

#### I would like to work on this T/L creator wtih you because I like and use Trendlines all the time.​

thinkScript is not an issue, I can code it.

First thing - is this line of your code correct?
"plot down = if LL then high else double.NaN;"

Or should it be written as
"plot down = if LL then low else double.NaN;"

because when I plot your script with this change, I get the begining of the trendlines.

So if you could get me a clear explanation, I wold be willing to work this with you.
An image would be great to help me understand, if you can crate one.
Is there a published artical you found and are trying to program from that article?

Bruce

Thanks for your reply. I'm sorry it wasn't clear. What I am trying to create with thinkscript is a swingline study used by a CBOE trader named Ira Epstein. The “Swingline Study” is an indicator devised by Ira Epstein, that you can see in his YouTube videos (his YT channel).

You are correct. The code should've been written:
"plot down = if LL then low else double.NaN;"

That was copy and paste typo. I updated my study, and it is drawing some of the trend lines. At this point, the study does not draw lines for inside and outside days. A trader attempted to create the code, but they did not use thinkscript. That code can be found here:
The code reveals what to do on inside and outside days.

Ira explains swinglines here:
Ira does not explain how swinglines are created in his videos, only how he uses them. He offers a course where he explains his complete system of trading in detail, and I would highly recommend it.

This is a screencap of my TOS platform with the partially completed study. I connected the missing parts of the trendline in white and noted what type of day occurred. You'll see three occasions of an inside day. The trendline in the first connected to the low of the day and the next two connected to the high. So for example, if the previous bar on an inside day was a higher high, then thinkscript needs to draw to the low. That's the type of conditional statement I want to make.

Last edited:
Thanks for the TOS screen shot. It helps a lot.
I'll look at the YouTube Video this afternoon and see what I make of it.
I looked at the code provide by the trader, and it is straight forward and will be a great help, if I decide to code this in thinkScript.

I told you I like the use of T/L's, because they make money.
So I'll watch the video and if it looks like we can make a buck using his process, I ll work on the thinkScript code.

Give me a day if two and reply what my next step is.

Write to you by Friday 5:00 PM EDT because I'm in CT, where are you?
Bruce

Thanks for the TOS screen shot. It helps a lot.
I'll look at the YouTube Video this afternoon and see what I make of it.
I looked at the code provide by the trader, and it is straight forward and will be a great help, if I decide to code this in thinkScript.

I told you I like the use of T/L's, because they make money.
So I'll watch the video and if it looks like we can make a buck using his process, I ll work on the thinkScript code.

Give me a day if two and reply what my next step is.

Write to you by Friday 5:00 PM EDT because I'm in CT, where are you?
Bruce
Thanks. I am just north of Chicago. I can generally plot the line in my head, but this would make it a lot easier to determine trend. It would be even better with a toggle for high/low bubbles.

Sorry for the delay, but I'm back at work, so not much time to do programming now.

I copied the code you pointed me to, and I'll play with it when I have some time, but no promises when I get to it.

I copied the code you pointed me to, and I'll play with it when I have some time, but no promises when I get to it.
I am working on a script to plot a trendline based on the following 5 conditions. I've made some progress but I am stuck on getting a conditional expression to work. Any help is appreciated.

CODE:
Code:
``````#Trend Line

#Higher Highs, Higher Lows
def HH = high[0]>high[1] AND low[0]>=low[1];
#Lower Highs, Lower Lows
def LL = high[0]<=high[1] AND low[0]<low[1];
#Inside Day
def ID = high[0]<=high[1] AND low[0]>=low[1];
#Outside Day Up
def ODU=high[0]>high[1] AND low[0]<low[1] AND close[0]>open[0];
#Outside Day Down
def ODD=high[0]>high[1] AND low[0]<low[1] AND close[0]<open[0];

#higher highs higher lows
plot up = if HH then high else double.NaN;

#lower highs lower lows
plot down = if LL then high else double.NaN;``````

This code works and I am able to plot trend lines, except on inside or outside days.

The next step in the process requires an "if-else" conditional expression. Specifically, on an inside day (ID), if the previous day were a "plot up" day (meaning the trendline connects to the high of the previous day), the inside day would be a "plot down" day (line connects to the low of the ID). On the other hand, if the last day were a "plot down" day (meaning the line connects to the low of the previous day), the inside day would be a "plot up" day (trend line connects to the ID's high). I am struggling to find a way to define the previous day as either "plot up" or "plot down." I am also unsure which syntax to use to extend the trend line. For example, can one study have multiple conditions to draw the line, or is it better to do separate studies for each condition since they should all connect in theory?
Hello, were you able to get the codes for Ira Epstein swinglines? I'm also looking to get his swinglines as an indicator. Thanks

Last edited by a moderator:
Hi ! I would like to know if you have an update for the correct script? I have been wanting this on TOS ever since I heard of Ira's swing line studies.

Thanks!

87k+ Posts
293 Online

## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

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?