Adding a stoploss line that follows price to existing ThinkScript

azakusa

New member
Looking to add an additional plot line to the thinkscript below.

The desired plot line would follow the current price -4%. Effectively making two lines. One with the current price, and one at 4% below it.

The idea is to use it as a quick visual reference for where my stoploss would reside to help me speed up the decision process on my entries.

I was poking around on the site the closest thing (visually) I came up with was this.
https://usethinkscript.com/threads/bid-ask-spread-lines-indicator-for-thinkorswim.1140/post-8476

Any thoughts anyone?

---------------------
#Plots a Horizontal Line that follows the price value selected
input price=close;
input offset=0;
input length = 300;

def sma = SimpleMovingAvg(price, 1, length);
rec line = if IsNaN(sma) then line[1] else sma[offset];
plot priceline=if isnan(sma) then line else double.nan;
priceline.setpaintingStrategy(paintingStrategy.LINE);
priceline.setlineWeight(3);
priceline.setdefaultColor(color.green);
priceline.hideBubble();
------------------------
 

Pensar

Active member
VIP
Looking to add an additional plot line to the thinkscript below.

The desired plot line would follow the current price -4%. Effectively making two lines. One with the current price, and one at 4% below it.

The idea is to use it as a quick visual reference for where my stoploss would reside to help me speed up the decision process on my entries.

I was poking around on the site the closest thing (visually) I came up with was this.
https://usethinkscript.com/threads/bid-ask-spread-lines-indicator-for-thinkorswim.1140/post-8476

Any thoughts anyone?

---------------------
#Plots a Horizontal Line that follows the price value selected
input price=close;
input offset=0;
input length = 300;

def sma = SimpleMovingAvg(price, 1, length);
rec line = if IsNaN(sma) then line[1] else sma[offset];
plot priceline=if isnan(sma) then line else double.nan;
priceline.setpaintingStrategy(paintingStrategy.LINE);
priceline.setlineWeight(3);
priceline.setdefaultColor(color.green);
priceline.hideBubble();
------------------------
@azakusa Something like this?

Code:
input percentoffset = 4;
plot current_price = HighestAll(if IsNaN(close[-1]) then close else Double.NaN);
     current_price.setdefaultcolor(color.cyan);
plot offset_line = LowestAll(current_price - (percentoffset /100*close));
     offset_line.setdefaultcolor(color.red);
 
Last edited:

azakusa

New member
@Pensar
Hello and many thanks for the follow-up.

I have tried out this piece of code, and it seems to plot the price from the previous candles close. But doesnt plot price in realtime like the original code I posted. Also for some reason its drawing the stoploss line at around -3% as opposed to -4%. (?)

It would be quite amazing to see both lines follow realtime price. I'm using this with microcaps on 1 minute charts. So perhaps thats the issue?

Truly appreciate your help. If you have any further thoughts on this I am all ears.

Thanks again.
 

Pensar

Active member
VIP
@azakusa So with the math portion of the code I posted, the 4 / 100 = 0.04, or 4% in decimal form. then multiplying that 0.04 by the close ( aka the most current price) should return 4% of the current price's value, which is then subtracted from the priceline to find 4% below it. The code should be correct. That being said, I did just now plot it on a 1 minute microcap chart and noticed that the line is in the area of 3.85% - 4% below the current price. The only thing that I can think of why that is happening is that the calculations as performed by Thinkorswim are not 100% precise.

As for the line plotting at the previous candle's close, perhaps you could share a screenshot of the issue? I've used that version of priceline for some time, and havent had that ever happen on my charts. Given that you are using it on a microcap, perhaps low volume might be causing an issue as well.
 

azakusa

New member
Hello @Pensar

I restarted TOS and the price plot as well as the 4% line are both moving nicely in realtime with the PA now.

Thanks for explaining the code behind the 4% line -this much I get. :) However the percentage difference its plotting is quite off. Seems it doesnt matter what the volumes are. Not sure how you are even getting something close (3.85-4%), what instrument?

If I change the value to 10% its still way off (last screenshot). I will keep tweaking it until it registers something close to 4%.

Even as is, it brilliant, and will be extremely helpful for me. Thank you very much!

See these screenshots below.

amc-trailing4.png


gme-trailing4.png


amc-trailing-10.png
 

Pensar

Active member
VIP
@azakusa Thanks for the screenshots. After looking at the code, I believe I used a wrong function ( "HighestAll()" vs "LowestAll()" ), so I have updated the above code in post #2. See if it gives better results.
Also, here is what I checked it on, both before and after the code change -

/ES - 3.91% before, 4.09% after
AMC - 2.67% before, 4.5% after
AAPL - 3.94% before, 4.1% after
QQQ - 3.93% before, 4.04% after
ZM - 3.97% before, 4.12% after
GHSI - 3.63% before, 4.35% after
ASRT - 3.76% before, 4.35% after
CDEV - 3.32% before, 4.16% after
 

azakusa

New member
@Pensar

Seems using LowestAll creates too much % gap to use comfortably. Will stick with the HighestAll version.

Too bad this doesnt work accurately. It would be an amazingly helpful tool if it were precise.

Again, thanks for your help. I really appreciate it.
 

azakusa

New member
Hi @Pensar,

Got to thinking... would it be possible to script a dynamic plot line at -4% of the close of the previous candle (for use on the 1 min)?
No need for the script to contain the additional current price plot, as that can run in a separate script, and no need for it to move in real-time.

Might this work effectively?
 

Similar threads

Top