Sell at a Loss

Tomahawk6117

Member
Plus
Hi @halcyonguy, you were very helpful in starting the code below for me, thank you very much. I have made some edits so that it sells when the price crosses below a 20% loss from the market open price. May you please tell me, is there a way to edit the code so that the stock will sell at the close of any candle where the price is below 20% from the market open, even if it has not just crossed this price? Thank you very much for your time and your help, I really appreciate it.

Code:
input sell_percent_gain = -20.0;
input price1 = close;
input start = 0930;
def daystart = secondstillTime(start) == 0;
def dayopen = if daystart then open else dayopen[1];
def daydiff = round(price1 - dayopen,2);
def pergain = round(100*daydiff/dayopen,1);

def sell1 = if daystart and pergain < sell_percent_gain then 1 else if pergain crosses below sell_percent_gain then 1 else 0;

plot z1 = sell1;
z1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
z1.SetDefaultColor(Color.red);
z1.setlineweight(3);
z1.hidebubble();
 
Last edited:
Hi @halcyonguy, you were very helpful in starting the code below for me, thank you very much. I have made some edits so that it sells when the price crosses below a 20% loss from the market open price. May you please tell me, is there a way to edit the code so that the stock will sell at the close of any candle where the price is below 20% from the market open, even if it has not just crossed this price? Thank you very much for your time and your help, I really appreciate it.

Code:
input sell_percent_gain = -20.0;
input price1 = close;
input start = 0930;
def daystart = secondstillTime(start) == 0;
def dayopen = if daystart then open else dayopen[1];
def daydiff = round(price1 - dayopen,2);
def pergain = round(100*daydiff/dayopen,1);

def sell1 = if daystart and pergain < sell_percent_gain then 1 else if pergain crosses below sell_percent_gain then 1 else 0;

plot z1 = sell1;
z1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
z1.SetDefaultColor(Color.red);
z1.setlineweight(3);
z1.hidebubble();


i am not sure what you are asking for.
this version has an option to draw arrows on every bar that is lower than the stop line.
trigger_on_all_prices_below_stop = yes;

Code:
# stop_20per_drop_sell_00b

#https://usethinkscript.com/threads/sell-at-a-loss.15243/
#Sell at a Loss
#Tomahawk6117  4/20

#Hi @halcyonguy, you were very helpful in starting the code below for me, thank you very much. I have made some edits so that it sells when the price crosses below a 20% loss from the market open price. May you please tell me, is there a way to edit the code so that the stock will sell at the close of any candle where the price is below 20% from the market open, even if it has not just crossed this price? Thank you very much for your time and your help, I really appreciate it.

def na = double.nan;
def bn = barnumber();

input sell_percent_gain = -20.0;
input price1 = close;
input start = 0930;
def daystart = secondstillTime(start) == 0;
def dayopen = if daystart then open else dayopen[1];
def daydiff = round(price1 - dayopen,2);
def pergain = round(100*daydiff/dayopen,1);

input trigger_on_all_prices_below_stop = yes;
def sell1 = if (daystart or trigger_on_all_prices_below_stop) and pergain <= sell_percent_gain then 1 else if pergain crosses below sell_percent_gain then 1 else 0;

plot z1 = if sell1 then high * 1.001 else na;
z1.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
z1.SetDefaultColor(Color.red);
z1.setlineweight(3);
z1.hidebubble();

#---------------------------------------
# plot lines , at open and % offset

input show_lines = yes;
plot zo = if show_lines then dayopen else na;
zo.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
zo.SetDefaultColor(Color.light_gray);
zo.hidebubble();

def stop1 = dayopen * (1 + (sell_percent_gain/100));

plot zs = if show_lines then stop1 else na;
zs.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
zs.SetDefaultColor(Color.light_gray);
zs.hidebubble();


#--------------------------
addchartbubble(0, low*0.99,
 sell_percent_gain + "  sell\n" +
 pergain + "  per" 
, color.yellow, no);

#
 
Hi @halcyonguy, this is awesome, thank you so much. This is exactly what I was looking for in my question, thank you. May you please tell me in the following code you wrote, why did you multiple the high price of the candle by 1.001? Thank you again for your help, I really appreciate it.

Code:
plot z1 = if sell1 then high * 1.001 else na;
 
Hi @halcyonguy, this is awesome, thank you so much. This is exactly what I was looking for in my question, thank you. May you please tell me in the following code you wrote, why did you multiple the high price of the candle by 1.001? Thank you again for your help, I really appreciate it.

Code:
plot z1 = if sell1 then high * 1.001 else na;

personal preference. for me , arrows stand out and are easier to see if they are a small distance away from a candle.
 

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

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
335 Online
Create Post

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