How to get plots removed from this study once a security sells???

number9

New member
Code:
input length = 14;
plot ADR = MovingAverage(AverageType.wilders, high-low, length);

def plBuy = GetAveragePrice();
def plStraightLine = highestall(plBuy);

plot x=plStraightLine;
x.SetDefaultColor(Color.white);
x.SetLineWeight(1);

plot pl1 = x * 1.015;
pl1.SetDefaultColor(Color.GREEN);
pl1.SetLineWeight(1);

plot pl2 = x - ADR;
pl1.SetDefaultColor(Color.RED);
pl1.SetLineWeight(1);

I have the above study plot three lines on my upper chart once a security is bought. It plots a line for average price, an upper line for 1.5% above average price (my price target) and a lower line that equals average price minus ADR (my stop). However, once I sell and completely liquidate the security, the lines remain on the chart. Is there a way to add code so the lines/plots are removed once the security is sold/liquidated and I no longer hold shares? If so, any help with adding that code to what I already have would be greatly appreciated. Thank you.
 
Solution
@number9

Ruby:
input length = 14;
def ADR = MovingAverage(AverageType.wilders, high-low, length);

def OpenPosCount = CompoundValue(1,if GetAveragePrice()[1] <> GetAveragePrice() then OpenPosCount[1]+1 else OpenPosCount[1],0);
def LastOpen = OpenPosCount == HighestAll(OpenPosCount) and GetAveragePrice() <> 0;

plot x = if LastOpen then GetAveragePrice() else Double.NaN;
x.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
x.SetDefaultColor(Color.white);
x.SetLineWeight(1);

plot pl1 = if LastOpen then (x * 1.015) else Double.NaN;
pl1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
pl1.SetDefaultColor(Color.GREEN);
pl1.SetLineWeight(1);

plot pl2 = If LastOpen then x - ADR else Double.NaN...
@number9

Ruby:
input length = 14;
def ADR = MovingAverage(AverageType.wilders, high-low, length);

def OpenPosCount = CompoundValue(1,if GetAveragePrice()[1] <> GetAveragePrice() then OpenPosCount[1]+1 else OpenPosCount[1],0);
def LastOpen = OpenPosCount == HighestAll(OpenPosCount) and GetAveragePrice() <> 0;

plot x = if LastOpen then GetAveragePrice() else Double.NaN;
x.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
x.SetDefaultColor(Color.white);
x.SetLineWeight(1);

plot pl1 = if LastOpen then (x * 1.015) else Double.NaN;
pl1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
pl1.SetDefaultColor(Color.GREEN);
pl1.SetLineWeight(1);

plot pl2 = If LastOpen then x - ADR else Double.NaN;
pl2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
pl2.SetDefaultColor(Color.RED);
pl2.SetLineWeight(1);

def xExt = if !IsNaN(close[1]) and IsNaN(close) then x[1] else xExt[1];
plot xExtP = if xExt then xExt else Double.NaN;
xExtP.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
xExtP.SetDefaultColor(Color.white);
xExtP.SetLineWeight(1);

def pl1Ext = if !IsNaN(close[1]) and IsNaN(close) then pl1[1] else pl1Ext[1];
plot pl1ExtP = if xExt then pl1Ext else Double.NaN;
pl1ExtP.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
pl1ExtP.SetDefaultColor(Color.GREEN);
pl1ExtP.SetLineWeight(1);

def pl2Ext = if !IsNaN(close[1]) and IsNaN(close) then pl2[1] else pl2Ext[1];
plot pl2ExtP = if xExt then pl2Ext else Double.NaN;
pl2ExtP.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
pl2ExtP.SetDefaultColor(Color.RED);
pl2ExtP.SetLineWeight(1);
 
Last edited:
Solution

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

@number9

Ruby:
input length = 14;
def ADR = MovingAverage(AverageType.wilders, high-low, length);

def plBuy = GetAveragePrice();
def plStraightLine = HighestAll(plBuy);
def OpenPosCount = CompoundValue(1,if GetAveragePrice()[1] <> GetAveragePrice() then OpenPosCount[1]+1 else OpenPosCount[1],0);
def LastOpen = OpenPosCount == HighestAll(OpenPosCount) and GetAveragePrice() <> 0;

plot x = if LastOpen then plStraightLine else Double.NaN;
x.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
x.SetDefaultColor(Color.white);
x.SetLineWeight(1);

plot pl1 = if LastOpen then (x * 1.015) else Double.NaN;
pl1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
pl1.SetDefaultColor(Color.GREEN);
pl1.SetLineWeight(1);

plot pl2 = If LastOpen then x - ADR else Double.NaN;
pl2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
pl2.SetDefaultColor(Color.RED);
pl2.SetLineWeight(1);

def xExt = if !IsNaN(close[1]) and IsNaN(close) then x[1] else xExt[1];
plot xExtP = if xExt then xExt else Double.NaN;
xExtP.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
xExtP.SetDefaultColor(Color.white);
xExtP.SetLineWeight(1);

def pl1Ext = if !IsNaN(close[1]) and IsNaN(close) then pl1[1] else pl1Ext[1];
plot pl1ExtP = if xExt then pl1Ext else Double.NaN;
pl1ExtP.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
pl1ExtP.SetDefaultColor(Color.GREEN);
pl1ExtP.SetLineWeight(1);

def pl2Ext = if !IsNaN(close[1]) and IsNaN(close) then pl2[1] else pl2Ext[1];
plot pl2ExtP = if xExt then pl2Ext else Double.NaN;
pl2ExtP.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
pl2ExtP.SetDefaultColor(Color.RED);
pl2ExtP.SetLineWeight(1);
Thank you very much! It works exactly as I wanted!
 
@number9

Ruby:
input length = 14;
def ADR = MovingAverage(AverageType.wilders, high-low, length);

def plBuy = GetAveragePrice();
def plStraightLine = HighestAll(plBuy);
def OpenPosCount = CompoundValue(1,if GetAveragePrice()[1] <> GetAveragePrice() then OpenPosCount[1]+1 else OpenPosCount[1],0);
def LastOpen = OpenPosCount == HighestAll(OpenPosCount) and GetAveragePrice() <> 0;

plot x = if LastOpen then plStraightLine else Double.NaN;
x.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
x.SetDefaultColor(Color.white);
x.SetLineWeight(1);

plot pl1 = if LastOpen then (x * 1.015) else Double.NaN;
pl1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
pl1.SetDefaultColor(Color.GREEN);
pl1.SetLineWeight(1);

plot pl2 = If LastOpen then x - ADR else Double.NaN;
pl2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
pl2.SetDefaultColor(Color.RED);
pl2.SetLineWeight(1);

def xExt = if !IsNaN(close[1]) and IsNaN(close) then x[1] else xExt[1];
plot xExtP = if xExt then xExt else Double.NaN;
xExtP.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
xExtP.SetDefaultColor(Color.white);
xExtP.SetLineWeight(1);

def pl1Ext = if !IsNaN(close[1]) and IsNaN(close) then pl1[1] else pl1Ext[1];
plot pl1ExtP = if xExt then pl1Ext else Double.NaN;
pl1ExtP.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
pl1ExtP.SetDefaultColor(Color.GREEN);
pl1ExtP.SetLineWeight(1);

def pl2Ext = if !IsNaN(close[1]) and IsNaN(close) then pl2[1] else pl2Ext[1];
plot pl2ExtP = if xExt then pl2Ext else Double.NaN;
pl2ExtP.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
pl2ExtP.SetDefaultColor(Color.RED);
pl2ExtP.SetLineWeight(1);
I ran into a little snag with this script and was wondering if you (or anyone else) could help again, please? I just purchased KO at $58.86 for a swing trade. However, instead of the target lines plotting based on this current average price, they plotted based on a previous average price from when I purchased KO back in July. That purchase was a swing trade, as well. I liquidated those shares a couple weeks later, and therefore was holding no shares until purchasing the security again today. Any help in modifying the code so the lines will only plot based on the current average price (and not previous average prices) would be greatly appreciated. Thanks!
 
Last edited by a moderator:

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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