Profit target, Stop loss window

Vorlauf

Member
Here I have a study that will draw a horizontal line from purchase date of average purchase press. Line only moves to re-average when new shares are bought or sold. So if we can have that, trading view has a Drawing tool that will show the area of profit/loss towards profit target and stop loss. Is it possible from this code to add profit/loss horizontal line from purchase price (based on ATR,%,$) to somewhat match the drawing tool on tradingview?

# plot a horizontal line
# at the average price of your open position
def open_position_avg_price = GetAveragePrice();
# color the plot line based on position side
# quantity will be negative if short
def open_position_qty = GetQuantity();
def PnL_Line_Color = if open_position_qty < 0 then -1 else 1;
#debug
#plot PnL_Line_Color__plot = PnL_Line_Color;
# apply long or short logic to line color
plot open_position_avg_price__plot = if (open_position_avg_price <= 0) then Double.NaN else open_position_avg_price;
open_position_avg_price__plot.AssignValueColor(if PnL_Line_Color > 0 then Color.GREEN else Color.RED);
open_position_avg_price__plot.SetLineWeight(3);
 
Solution
% is good, atr multiple would also be helpful


EDIT, FIX TYPO. 11/1 6am

this one can determine the risk amount, with 2 methods, price % or atr factor .

i rushed through this. just realized i didn't save the atr number when a buy is placed, so the risk/reward lines will vary a little with different atr numbers, bar to bar.


Code:
# profit_loss_region_02

# https://usethinkscript.com/threads/profit-target-stop-loss-window.13190/
# Profit target, Stop loss window
# Vorlauf  10/30


# Here
# http://tos.mx/UFajlgB
# I have a study that will draw a horizontal line
#https://imgur.com/a/qU6A3kI
#from purchase date of average purchase press. Line only moves to re-average when new shares are bought or sold. So if we can have that...
# plot a horizontal line
# at the average price of your open position
def open_position_avg_price = GetAveragePrice();
# color the plot line based on position side
# quantity will be negative if short
def open_position_qty = GetQuantity();
def PnL_Line_Color = if open_position_qty < 0 then -1 else 1;
#debug
#plot PnL_Line_Color__plot = PnL_Line_Color;
# apply long or short logic to line color
plot open_position_avg_price__plot = if (open_position_avg_price <= 0) then Double.NaN else open_position_avg_price;
open_position_avg_price__plot.AssignValueColor(if PnL_Line_Color > 0 then Color.GREEN else Color.RED);
open_position_avg_price__plot.SetLineWeight(3);

this is untested

you didn't describe how you wanted to determine the upper and lower levels so,
i made this , based on a risk % and calculates a risk and reward price level.


Code:
# profit_loss_region_0

# https://usethinkscript.com/threads/profit-target-stop-loss-window.13190/
# Profit target, Stop loss window
# Vorlauf  10/30


addlabel(1, "profit_loss_region_0");

# Here
# http://tos.mx/UFajlgB
# I have a study that will draw a horizontal line
#https://imgur.com/a/qU6A3kI
#from purchase date of average purchase press. Line only moves to re-average when new shares are bought or sold. So if we can have that, trading view has a Drawing tool
#https://imgur.com/a/T4sMRhV
# that will show the area of profit/loss towards profit target and stop loss. Is it possible from this code to add profit/loss horizontal line from purchase price (based on ATR,%,$) to somewhat match the drawing tool on tradingview?



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

def buy_price = close;

# plot a horizontal line
# at the average price of your open position
def open_position_avg_price = GetAveragePrice();
# color the plot line based on position side
# quantity will be negative if short
def open_position_qty = GetQuantity();
def PnL_Line_Color = if open_position_qty < 0 then -1 else 1;

def buy_first_bar = if open_position_qty[1] == 0 and open_position_qty != 0 then 1 else 0;

def entry = if bn == 1 then na
  else if open_position_qty == 0 then na
  else if buy_first_bar then buy_price
  else entry[1];

input risk_percent = 1.5;
def risk_amount = round(entry * (risk_percent/100),2);

def risk = entry - risk_amount;
input reward_factor = 3;
def reward = round(entry + (reward_factor * risk_amount),2);

plot zentry = entry;
zentry.SetDefaultColor(Color.cyan);
zentry.SetLineWeight(3);

plot zrisk = risk;
zrisk.SetDefaultColor(Color.red);

plot zreward = reward;
zentry.SetDefaultColor(Color.green);

addcloud(zreward, zentry, color.green);
addcloud(zentry, zrisk, color.red);



#debug
#plot PnL_Line_Color__plot = PnL_Line_Color;
# apply long or short logic to line color
#plot open_position_avg_price__plot = if (open_position_avg_price <= 0) then Double.NaN else open_position_avg_price;
#open_position_avg_price__plot.AssignValueColor(if PnL_Line_Color > 0 then Color.GREEN else Color.RED);
#open_position_avg_price__plot.SetLineWeight(3);


#
 

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

% is good, atr multiple would also be helpful


EDIT, FIX TYPO. 11/1 6am

this one can determine the risk amount, with 2 methods, price % or atr factor .

i rushed through this. just realized i didn't save the atr number when a buy is placed, so the risk/reward lines will vary a little with different atr numbers, bar to bar.


Code:
# profit_loss_region_02

# https://usethinkscript.com/threads/profit-target-stop-loss-window.13190/
# Profit target, Stop loss window
# Vorlauf  10/30


# Here
# http://tos.mx/UFajlgB
# I have a study that will draw a horizontal line
#https://imgur.com/a/qU6A3kI
#from purchase date of average purchase press. Line only moves to re-average when new shares are bought or sold. So if we can have that, trading view has a Drawing tool
#https://imgur.com/a/T4sMRhV
# that will show the area of profit/loss towards profit target and stop loss. Is it possible from this code to add profit/loss horizontal line from purchase price (based on ATR,%,$) to somewhat match the drawing tool on tradingview?


# % is good, atr multiple would also be helpful

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

def buy_price = close;


#----------------
# ATR
input atr_length = 14;
input atr_averageType = AverageType.WILDERS;
def ATR = MovingAverage(atr_averageType, TrueRange(high, close, low), atr_length);
# --------------------

input risk_type = {buy_price_percent , default atr_factor };


# plot a horizontal line
# at the average price of your open position
def open_position_avg_price = GetAveragePrice();
# color the plot line based on position side
# quantity will be negative if short
def open_position_qty = GetQuantity();
def PnL_Line_Color = if open_position_qty < 0 then -1 else 1;

def buy_first_bar = if open_position_qty[1] == 0 and open_position_qty != 0 then 1 else 0;

def entry = if bn == 1 then na
  else if open_position_qty == 0 then na
  else if buy_first_bar then buy_price
  else entry[1];


input risk_price_percent = 1.5;
input risk_atr_factor = 1.0;


def risk_amount;
switch (risk_type) {
case buy_price_percent:
  risk_amount = round(entry * (risk_price_percent/100), 2);
case atr_factor:
  risk_amount = round(entry * risk_atr_factor, 2);
}


#def risk_amount = round(entry * (risk_percent/100),2);

def risk = entry - risk_amount;
input reward_factor = 3;
def reward = round(entry + (reward_factor * risk_amount),2);

plot zentry = entry;
zentry.SetDefaultColor(Color.cyan);
zentry.SetLineWeight(3);

plot zrisk = risk;
zrisk.SetDefaultColor(Color.red);


#EDIT, FIX TYPO.  wrong plot variable
plot zreward = reward;
#zentry.SetDefaultColor(Color.green);
zreward.SetDefaultColor(Color.green);

addcloud(zreward, zentry, color.green);
addcloud(zentry, zrisk, color.red);


#debug
#plot PnL_Line_Color__plot = PnL_Line_Color;
# apply long or short logic to line color
#plot open_position_avg_price__plot = if (open_position_avg_price <= 0) then Double.NaN else open_position_avg_price;
#open_position_avg_price__plot.AssignValueColor(if PnL_Line_Color > 0 then Color.GREEN else Color.RED);
#open_position_avg_price__plot.SetLineWeight(3);

#
 
Last edited:
Solution
Amazing. Thanks so much. In picture, green line is the average price but somehow doesn't match up with the correct color. Think the P/L code is using close, not the average price. Easy fix? Picture of the difference

fixed, sorry about that, had wrong variable in a plot statement.

------
i don't like adding a lot of shapes/shading that covers up the candles, so i try to draw things away from candles.

here is an example of shading after the last candle.
https://usethinkscript.com/threads/help-with-relative-range.10482/#post-92839
 
Last edited:

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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