Horizontal Lines In ThinkOrSwim

Looking for this exact indicator, except it's plotted live and not only to the right: https://researchtrade.com/forum/read.php?7,2258,page=65

Thanks in advance.
your request is confusing. you want that exact study, only different...
if you want a live plot, use any of the premade studies that draw average lines, of live data.

going to take a guess ,
do you want to plot a horizontal line, across the whole chart, with a price level based on the average value from the last candle ?
 
@xleb27
So I am pretty new to ThinkScript but this put a line on my options trade I did Friday morning:
(The price line is the cyan line on the bottom)

Code:
plot intAvgCost = if GetAveragePrice() == 0 then double.nan else GetAveragePrice();
AvgCost.png
Hi @Lexureyesed , I copied and paste the code but the line doesn't show on the chart. can you please paste the entire code? thanks
 
your request is confusing. you want that exact study, only different...
if you want a live plot, use any of the premade studies that draw average lines, of live data.

going to take a guess ,
do you want to plot a horizontal line, across the whole chart, with a price level based on the average value from the last candle ?
Yes, you are correct.
 
Yes, you are correct.

The code you requested modified to plot the lines across the chart is done below with select yes for input lines_across_chart and insertion of highestall() for the averages

Capture.jpg
Ruby:
#Mntman @funwiththinkscript
# Plot longer moving average support lines
# 20211004 added option ot plot horizontal lines across chart per @usthinkscript request

input lines_across_chart = yes;

def lastBar = HighestAll(if !IsNaN(close) then BarNumber() else 0);
def mostRecentClose = HighestAll(if BarNumber() == lastBar then close else 0);
def barNumber = BarNumber();
def barCount = HighestAll(If(IsNaN(close), 0, barNumber));

input shtAvg = 50;
input medAvg = 100;
input lngAvg = 200;
input avgType = {default "simple", "exponential"};

def avg1;
def avg2;
def avg3;
switch (avgType) {
case "simple":
    avg1 = Average(close, shtAvg);
    avg2 = Average(close, medAvg);
    avg3 = Average(close, lngAvg);
case "exponential":
    avg1 = ExpAverage(close, shtAvg);
    avg2 = ExpAverage(close, medAvg);
    avg3 = ExpAverage(close, lngAvg);
}

input proximity = 10;  #ref 0.5 = 1/2%

def value1 = GetValue(avg1, BarNumber() - HighestAll(lastBar));
def value2 = GetValue(avg2, BarNumber() - HighestAll(lastBar));
def value3 = GetValue(avg3, BarNumber() - HighestAll(lastBar));
def inRange1 = mostRecentClose > (value1 * (1 - proximity / 100)) and mostRecentClose < (value1 * (1 + proximity / 100));
def inRange2 = mostRecentClose > (value2 * (1 - proximity / 100)) and mostRecentClose < (value2 * (1 + proximity / 100));
def inRange3 = mostRecentClose > (value3 * (1 - proximity / 100)) and mostRecentClose < (value3 * (1 + proximity / 100));

def avg1Line = if lines_across_chart then highestall(value1) else if barNumber == 1 then Double.NaN else if barNumber == barCount then avg1 else if barNumber == barCount then Double.NaN else avg1Line[1];
def avg2Line = if lines_across_chart then highestall(value2) else if barNumber == 1 then Double.NaN else if barNumber == barCount then avg2 else if barNumber == barCount then Double.NaN else avg2Line[1];
def avg3Line = if lines_across_chart then highestall(value3) else if barNumber == 1 then Double.NaN else if barNumber == barCount then avg3 else if barNumber == barCount then Double.NaN else avg3Line[1];

plot sandline1 = avg1Line;
     sandline1.AssignValueColor(globalColor("sand" ));
     sandline1.SetLineWeight(1);
     sandline1.SetHiding(!inRange1);
AddChartBubble(yes, if inRange1 and BarNumber() == HighestAll(Lastbar+3) then sandline1 else double.nan, shtAvg + (if avgType == avgType.simple then "sma" else "ema" ), globalColor("sand" ), 0);
plot sandline2 = avg2Line;
     sandline2.AssignValueColor(globalColor("sand" ));
     sandline2.SetLineWeight(1);
     sandline2.SetHiding(!inRange2);
AddChartBubble(yes, if inRange2 and BarNumber() == HighestAll(Lastbar+3) then sandline2 else double.nan, medAvg + (if avgType == avgType.simple then "sma" else "ema" ), globalColor("sand" ), 0);
plot sandline3 = avg3Line;
     sandline3.AssignValueColor(globalColor("sand" ));
     sandline3.SetLineWeight(1);
     sandline3.SetHiding(!inRange3);
AddChartBubble(yes, if inRange3 and BarNumber() == HighestAll(Lastbar+3) then sandline3 else double.nan, lngAvg + (if avgType == avgType.simple then "sma" else "ema" ), globalColor("sand" ), 0);

AddLabel(yes, "proximity: " + proximity + "%", color.white);
AddLabel(yes, shtAvg + (if avgType == avgType.simple then "sma: " else "ema: " ) + round(avg1,2), globalColor("sand" ));  #for reference only
AddLabel(yes, medAvg + (if avgType == avgType.simple then "sma: " else "ema: " ) + round(avg2,2), globalColor("sand" ));  #for reference only
AddLabel(yes, lngAvg + (if avgType == avgType.simple then "sma: " else "ema: " ) + round(avg3,2), globalColor("sand" ));  #for reference only

DefineGlobalColor("sand", CreateColor(255,204,102)); #sand
 
@xleb27 Hey, I am replying back on the thread you originally asked this:
That is the entire code. It's a one-liner. There are no variables to declare as they are taken care of inside of the "stock" TD Ameritrade function. From left to right, this reads "If the average price is nothing (i haven't taken a position) then don't draw anything, otherwise, draw my average cost on my chart"
Ruby:
plot intAvgCost = if GetAveragePrice() == 0 then double.nan else GetAveragePrice();


I double checked to make sure that the code works. If starts painting when i open, and stops when i close my position:


This is what my studies window looks like:




tl:dr
and if all else fails, click this link and follow the directions from TD Ameritrade :D
https://tos.mx/PTRAWlF
 
@xleb27 Hey, I am replying back on the thread you originally asked this:
That is the entire code. It's a one-liner. There are no variables to declare as they are taken care of inside of the "stock" TD Ameritrade function. From left to right, this reads "If the average price is nothing (i haven't taken a position) then don't draw anything, otherwise, draw my average cost on my chart"
Ruby:
plot intAvgCost = if GetAveragePrice() == 0 then double.nan else GetAveragePrice();


I double checked to make sure that the code works. If starts painting when i open, and stops when i close my position:


This is what my studies window looks like:
Got it. but when I add the indicator it doesn't show the line on the chart. I'm following the instructions as you showed in the screen shot.
 
Got it. but when I add the indicator it doesn't show the line on the chart. I'm following the instructions as you showed in the screen shot.
maybe I should mention I'm trading forex pairs on TOS and not options. maybe that's why?
 
maybe I should mention I'm trading forex pairs on TOS and not options. maybe that's why?
I would imagine so and makes a lot more sense why it's not working now (ToS functionality bounds, not @xleb27 ). I am by no means a ToS expert. I mean you can probably tell by my trade that I am pretty new, LOL.

https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Portfolio
says:
They cannot be used on charts with a price type other than LAST (i.e., they won't work with MARK, ASK, or BID price types).

It doesn't say anything about forex here but I think that because of the pricetype of forex pairs and the way the data is transmitted back to ToS client, it won't show. Honestly, it's probably better answered by some of the vets here or ToS support
I would guess that your pricetype on your charts are set to something other than LAST.

EDIT:
Just checked my FX chart settings and I don't trade FX and it was set to BID as default, so I would recommend checking the price type under the STYLES chart option > FX

FX is alllll teh way on the end in the style settings.

I also updated the share link that throws an error in case you want to keep using BID pricetype but you wanna check your avg cost mid session.
https://tos.mx/aa70Wl1

 
Last edited:
I would imagine so and makes a lot more sense why it's not working now (ToS functionality bounds, not @xleb27 ). I am by no means a ToS expert. I mean you can probably tell by my trade that I am pretty new, LOL.

https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Portfolio
says:


It doesn't say anything about forex here but I think that because of the pricetype of forex pairs and the way the data is transmitted back to ToS client, it won't show. Honestly, it's probably better answered by some of the vets here or ToS support
I would guess that your pricetype on your charts are set to something other than LAST.

EDIT:
Just checked my FX chart settings and I don't trade FX and it was set to BID as default, so I would recommend checking the price type under the STYLES chart option > FX

FX is alllll teh way on the end in the style settings.

I also updated the share link that throws an error in case you want to keep using BID pricetype but you wanna check your avg cost mid session.
https://tos.mx/aa70Wl1

oh that's why then. I trade forex so my candle shows MARK price not last. I'll change it and give it a try
 
Good day!
I’m wondering if someone is able to write a thinkscript that will plot a line at a daily simple moving average (example: 20 period) so it can be identified while viewing any other chart timeframe.
Thank you!
 
Hi @Lexureyesed , for some reason it still does not show the line on my chart.. I've changed the price from MARK to LAST and nothing.
@xleb27 I'm sorry to hear that. I know that the portfolio functions are a little weird and there are some other per-requisites:

Note also that Portfolio functions can only be used with the following aggregation periods: 1 min, 2 min, 3 min, 4 min, 5 min, 10 min, 15 min, 20 min, 30 min, 1h, or 1 day. Time period for the aggregation of 1 day is limited to 1 year.

I can easily imagine you using a tick chart for forex. Maybe they just don't work on forex. I dont trade fx or futures yet so I cannot test unfortunately.
 
Hello All,

I am trying to write a simple code with thinkscript to draw a horizonal line where the current 5 days moving average is.

Here's my code but it's the same as the moving average line, not a horizonal line at the current value, please help

input price = FundamentalType.CLOSE;
input aggregationPeriod = AggregationPeriod.DAY;
input averageType = AverageType.SIMPLE;

#Define of bars
def lastBar = BarNumber();
def currentClose = close[0];

input SMA5 = 5;
def value5 = simpleMovingAvg(Fundamental(price, period = aggregationPeriod), smA5);

plot myline = value5;
myline.SetLineWeight(1);
myline.setdefaultColor(color.YELLOW);

10-10-2021-10-12-52-AM.png
 
Ruby:
def MA =
    HighestAll(
        if !IsNaN(close) and IsNaN(close[-1])
        then SimpleMovingAvg(close,5)
        else Double.NaN
    )
;
plot MALine =
    MA
;
 
Ruby:
def MA =
    HighestAll(
        if !IsNaN(close) and IsNaN(close[-1])
        then SimpleMovingAvg(close,5)
        else Double.NaN
    )
;
plot MALine =
    MA
;
I am trying to understand and learn how to code, would you mind tell me why when I take highestall() off the code does not work anymore? I don't understand what's the highest value needs to return from the rest of the code for it to work
 
I am trying to understand and learn how to code, would you mind tell me why when I take highestall() off the code does not work anymore? I don't understand what's the highest value needs to return from the rest of the code for it to work

highestall( ) finds the biggest number , usualy in 1 variable.

it is possible to put an if-then inside of a highestall, to filter the values passed on to it.
in joshuas example, the first line,

if !IsNaN(close) and IsNaN(close[-1])

looks for the last bar with a valid close price. when it finds it, then it does the second line

then SimpleMovingAvg(close,5)

so, out of all the bars, only 1 value, from the last bar, is passed on to highestall( ).

this might help
https://usethinkscript.com/threads/...ering-the-aggregation-period.7523/#post-72687
 
highestall( ) finds the biggest number , usualy in 1 variable.

it is possible to put an if-then inside of a highestall, to filter the values passed on to it.
in joshuas example, the first line,

if !IsNaN(close) and IsNaN(close[-1])

looks for the last bar with a valid close price. when it finds it, then it does the second line

then SimpleMovingAvg(close,5)

so, out of all the bars, only 1 value, from the last bar, is passed on to highestall( ).

this might help
https://usethinkscript.com/threads/...ering-the-aggregation-period.7523/#post-72687
thank you for the detailed explanation, so the code if !IsNaN(close) and IsNaN(close[-1]) returns either 0 or 1, in this case since there's only one last bar, so the "highest" value is 1, then the code process the second line with the close price of last bar.

I wrote another similar code with labels to show the value of the variables, both "MA" and "something" and returning the same value, how come when I plot something, it does not give me the same results as MA? I don't see any lines being plotted unless I add highestall(something);

10-10-2021-2-47-11-PM.png
 

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
253 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