shows the profit and stop loss levels based on entry

Hello everyone, I'm seeking assistance with my current strategy. Currently, both the short entry profit/stop loss and the long entry profit/stop loss are displayed whenever an entry is executed. I'm looking for help to configure it in a way that only shows the profit and stop loss levels based on whether it's a short or long entry. Appreciate any guidance on this matter. see code below

short entry code
input contracts = 1;


def rsi = RSI(length = rsi_length);
def atr = ATR(length = atr_length);

def condition = (rsi > 80);

# Short
AddOrder(tickcolor = Color.GREEN, arrowcolor = Color.RED, name = "Short", tradeSize = contracts, condition = condition, type = OrderType.SELL_TO_OPEN);

# Profit
def target = EntryPrice() - ATR() * profit_mult;
AddOrder(type = OrderType.BUY_TO_CLOSE, low[-1] <= target, tickcolor = Color.GREEN, arrowcolor = Color.GREEN, name = "Target", price = target);

# Trailing stop
def stop = EntryPrice() + ATR() * stop_mult;
AddOrder(OrderType.BUY_TO_CLOSE, high[-1] >= stop, tickcolor = Color.GRAY, arrowcolor = Color.CYAN, name = "Stop", tradeSize = contracts);


plot Profit = target;
Profit.SetPaintingStrategy(PaintingStrategy.Horizontal);
Profit.SetDefaultColor(Color.BLUE);

plot loss= stop;
loss.SetPaintingStrategy(PaintingStrategy.Horizontal);
loss.SetDefaultColor(Color.MAGENTA);


here is my long entry code.

input contracts = 1;


def rsi = RSI(length = rsi_length);
def atr = ATR(length = atr_length);

def condition = (rsi < 30);

# Long
AddOrder(tickcolor = Color.GREEN, arrowcolor = Color.RED, name = "Long", tradeSize = contracts, condition = condition, type = OrderType.BUY_TO_OPEN);

# Profit
def target = EntryPrice() + ATR() * profit_mult;
AddOrder(type = OrderType.SELL_TO_CLOSE, high[-1] >= target, tickcolor = Color.GREEN, arrowcolor = Color.GREEN, name = "Target", price = target);

# Trailing stop
def stop = EntryPrice() - ATR() * stop_mult;
AddOrder(OrderType.SELL_TO_CLOSE, low[-1] <= stop, tickcolor = Color.GRAY, arrowcolor = Color.GRAY, name = "Stop", tradeSize = contracts);


plot ProfitTarget = target;
ProfitTarget.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ProfitTarget.SetDefaultColor(Color.GREEN);

plot stoploss = stop;
stoploss.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
stoploss.SetDefaultColor(Color.YELLOW);
 
Hello everyone, I'm seeking assistance with my current strategy. Currently, both the short entry profit/stop loss and the long entry profit/stop loss are displayed whenever an entry is executed. I'm looking for help to configure it in a way that only shows the profit and stop loss levels based on whether it's a short or long entry. Appreciate any guidance on this matter. see code below

short entry code
input contracts = 1;


def rsi = RSI(length = rsi_length);
def atr = ATR(length = atr_length);

def condition = (rsi > 80);

# Short
AddOrder(tickcolor = Color.GREEN, arrowcolor = Color.RED, name = "Short", tradeSize = contracts, condition = condition, type = OrderType.SELL_TO_OPEN);

# Profit
def target = EntryPrice() - ATR() * profit_mult;
AddOrder(type = OrderType.BUY_TO_CLOSE, low[-1] <= target, tickcolor = Color.GREEN, arrowcolor = Color.GREEN, name = "Target", price = target);

# Trailing stop
def stop = EntryPrice() + ATR() * stop_mult;
AddOrder(OrderType.BUY_TO_CLOSE, high[-1] >= stop, tickcolor = Color.GRAY, arrowcolor = Color.CYAN, name = "Stop", tradeSize = contracts);


plot Profit = target;
Profit.SetPaintingStrategy(PaintingStrategy.Horizontal);
Profit.SetDefaultColor(Color.BLUE);

plot loss= stop;
loss.SetPaintingStrategy(PaintingStrategy.Horizontal);
loss.SetDefaultColor(Color.MAGENTA);


here is my long entry code.

input contracts = 1;


def rsi = RSI(length = rsi_length);
def atr = ATR(length = atr_length);

def condition = (rsi < 30);

# Long
AddOrder(tickcolor = Color.GREEN, arrowcolor = Color.RED, name = "Long", tradeSize = contracts, condition = condition, type = OrderType.BUY_TO_OPEN);

# Profit
def target = EntryPrice() + ATR() * profit_mult;
AddOrder(type = OrderType.SELL_TO_CLOSE, high[-1] >= target, tickcolor = Color.GREEN, arrowcolor = Color.GREEN, name = "Target", price = target);

# Trailing stop
def stop = EntryPrice() - ATR() * stop_mult;
AddOrder(OrderType.SELL_TO_CLOSE, low[-1] <= stop, tickcolor = Color.GRAY, arrowcolor = Color.GRAY, name = "Stop", tradeSize = contracts);


plot ProfitTarget = target;
ProfitTarget.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ProfitTarget.SetDefaultColor(Color.GREEN);

plot stoploss = stop;
stoploss.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
stoploss.SetDefaultColor(Color.YELLOW);


this is a strategy.
after a trade opens, it plots lines for a target and a stop, until 1 of them is crossed.
it tracks profits for each trade with bubbles, and has labels for the totals.


i rearranged almost everthing,
..change entryprice to be a variable e, to trap out the errors

..change the buy and sell conditions, from >, to crosses. > will have many 'trues', while 'crosses' will have 1 true at the start of a series.
def long_open = (rsi crosses below 30);
def short_open = (rsi crosses above 80);

..your addorder layouts were inconsistant and missing input parameters. i added input parameters and put them in the correct order. i put each parameter on a new line. makes them easier to read. i used the common open[-1] for price for open.
# long - open
AddOrder(
type = OrderType.BUY_TO_OPEN,
condition = long_open,
price = open[-1],
tradeSize = contracts,
tickcolor = Color.GREEN,
arrowcolor = Color.green,
name = "LONG"
);

..add a variable , trade, to be 1 or -1, depending on the type of trade open (long or short). use this when plotting lines

..move trade open and trade close formulas, together and to the beginning of study.

..add bubbles to display the profit or loss on each trade. green = gain , red = loss.
..add labels to display the total gain for the long trades and total of short trades. green = gain , red = loss.


Code:
#profit_stop_loss_entry

#https://usethinkscript.com/threads/shows-the-profit-and-stop-loss-levels-based-on-entry.17790/
#shows the profit and stop loss levels based on entry

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

def e;
if isnan(EntryPrice()) then {
  e = 0;
} else {
  e = EntryPrice();
}


input contracts = 1;
input profit_mult = 2.4;
input stop_mult = 1.2;
input gain_bubbles = yes;
input rsi_length = 14;
input atr_length = 14;


def rsi = RSI(length = rsi_length);
def atr = ATR(length = atr_length);
#def a = ATR();


#========================================
# trade open

def long_open = (rsi crosses below 30);
def short_open = (rsi crosses above 80);

#=======================================

def trade1 = if bn == 1 then 0 
 else if long_open then 1
 else if short_open then -1 
 else trade1[1];

def apm = ATR() * profit_mult;
def asm = ATR() * stop_mult;

def long_target_lvl = if trade1 > 0 and e > 0 then (E + ATR() * profit_mult) else big;
def long_stop_lvl = if trade1 > 0 and e > 0 then (E - ATR() * stop_mult) else -big;

def short_target_lvl = if trade1 < 0 and e > 0 then (e - ATR() * profit_mult) else -big;
def short_stop_lvl = if trade1 < 0 and e > 0 then (E + ATR() * stop_mult) else big;


def long_t = (high[-1] >= long_target_lvl[-1]);
def long_s = (low[-1] <= long_stop_lvl[-1]);

def short_t = (low[-1] <= short_target_lvl[-1]);
def short_s = (high[-1] >= short_stop_lvl[-1]);

def trade2 = if bn == 1 then 0 
 else if trade1[0] > 0 and (long_t or long_s) then 0
 else if trade1[0] < 0 and (short_t or short_s) then 0
 else trade1;


def trade = trade2;
#def trade = trade1;



#========================================
# long entry

# long - open
AddOrder(
 type = OrderType.BUY_TO_OPEN, 
 condition = long_open, 
 price = open[-1], 
 tradeSize = contracts, 
 tickcolor = Color.GREEN, 
 arrowcolor = Color.green,
 name = "LONG"
);


#def long_t = (high[-1] >= long_target_lvl[-1]);
#def long_s = (low[-1] <= long_stop_lvl[-1]);

# long - target - gain
AddOrder(
 type = OrderType.SELL_TO_CLOSE,
 condition = long_t,
 price = long_target_lvl[-1],
 tradeSize = contracts, 
 tickcolor = Color.GREEN, 
 arrowcolor = Color.GREEN, 
 name = "Target");


# long - stop - loss
AddOrder(
 type = OrderType.SELL_TO_CLOSE,
 condition = long_s,
 price = long_stop_lvl[-1],
 tradeSize = contracts,
 tickcolor = Color.yellow,
 arrowcolor = Color.yellow,
 name = "Stop");

plot ProfitTarget = if trade > 0 and long_target_lvl[-1] < big then long_target_lvl[-1] else na;
ProfitTarget.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ProfitTarget.SetDefaultColor(Color.GREEN);

plot stoploss = if trade > 0 and long_stop_lvl[-1] > -big then long_stop_lvl[-1] else na;
stoploss.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
stoploss.SetDefaultColor(Color.YELLOW);


# long gain/loss
def long_pl;
if trade == -1 then {
  long_pl = 0;
}  else if long_t then {
  long_pl = (ATR() * profit_mult);
} else if long_s then {
  long_pl = (-ATR() * stop_mult);
} else {
  long_pl = 0;
};

def long_pl_ttl = if bn == 1 then 0 else round(long_pl_ttl[1] + long_pl,4);

input show_labels = yes;
addlabel( show_labels, "LONG PL " + long_pl_ttl, (if long_pl_ttl > 0 then color.green else color.red));


addchartbubble(gain_bubbles and trade[0] > 0 and long_pl[1] > 0, high,
long_pl[1]
, color.green, yes);

addchartbubble(gain_bubbles and trade[0] > 0 and long_pl[1] < 0, low,
long_pl[1]
, color.red, no);


addchartbubble(0, low,
trade[0] + " T\n" +
long_pl[0] + " pl\n" +
 long_pl_ttl 
, color.green, no);


#========================================
# short entry

# short - open
AddOrder(
 type = OrderType.SELL_TO_OPEN,
 condition = short_open,
 price = open[-1],
 tradeSize = contracts,
 tickcolor = Color.red, 
 arrowcolor = Color.RED, 
 name = "Short");

#def short_t = (low[-1] <= short_target_lvl[-1]);
#def short_s = (high[-1] >= short_stop_lvl[-1]);

# short - target - gain
AddOrder(
 type = OrderType.BUY_TO_CLOSE,
 condition = short_t, 
 price = short_target_lvl[-1],
 tradeSize = contracts,
 tickcolor = Color.red, 
 arrowcolor = Color.red, 
 name = "Target"
);

# short - stop - loss
AddOrder(
 type = OrderType.BUY_TO_CLOSE, 
 condition = short_s,
 price = short_stop_lvl[-1],
 tradeSize = contracts,
 tickcolor = Color.yellow, 
 arrowcolor = Color.yellow, 
 name = "Stop");


plot Profit = if trade < 0 and short_target_lvl[-1] > -big then short_target_lvl[-1] else na;
Profit.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Profit.SetDefaultColor(Color.BLUE);

plot loss = if trade < 0 and short_stop_lvl[-1] < big then short_stop_lvl[-1] else na;
loss.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
loss.SetDefaultColor(Color.MAGENTA);


#def short_t = (low[-1] <= short_target_lvl[-1]);
#def short_s = (high[-1] >= short_stop_lvl[-1]);

# short gain/loss
def short_pl;
if trade == -1 then {
  short_pl = 0;
} else if short_t then {
  short_pl = (ATR() * profit_mult);
} else if short_s then {
  short_pl = (-ATR() * stop_mult);
} else {
  short_pl = 0;
};

def short_pl_ttl = if bn == 1 then 0 else round(short_pl_ttl[1] + short_pl,4);



addlabel( show_labels, "SHORT PL " + short_pl_ttl, (if short_pl_ttl > 0 then color.green else color.red));


addchartbubble(gain_bubbles and trade[0] < 0 and short_pl[1] > 0, high,
short_pl[1]
, color.green, yes);

addchartbubble(gain_bubbles and trade[0] < 0 and short_pl[1] < 0, low,
short_pl[1]
, color.red, no);



#====================================


#AddOrder ( type, condition, price = open[-1], tradeSize, tickColor, arrowColor, name); 

AddChartBubble(0, low*0.998,
 #e + "\n" +
 #a + "\n" +
 e + "\n" +
 long_open + "\n" +
 short_open + "\n" +
 trade1 + "\n" +
 trade2 + "\n" +
"---\n" +
long_t + "\n" +
long_s + "\n" +
short_t + "\n" +
short_s + "\n" +
"---\n" +
 long_target_lvl + "\n" +
 long_stop_lvl  + "\n" +
 short_target_lvl + "\n" +
 short_stop_lvl  + "\n" 
#, (if long_open then Color.green else if short_open then color.red else color.gray), no);
#, (if trade > 0 then Color.green else if trade < 0 then color.red else color.gray), no);
, Color.YELLOW, no);
#


CVX 5min
target # = 2.4 , stop # = 1.2
first trade is short. green bubble , a profit of $0.51.
second trade is long. red bubble , a loss of $0.19.
labels at top, 1 for total of all long trades, 1 for all short trades. both are green, both types of trades had profits over the period of the chart.
 

Attachments

  • 00f- cvx 5min 2.4-1.2.JPG
    00f- cvx 5min 2.4-1.2.JPG
    129.4 KB · Views: 295

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

I created/downloaded the script (both the 1st & 2nd study), and I'm not seeing the Labels in the top left corner of your screenshot, nor the short/target/long/stops or bubbles and lines am I missing something or are the addition indicators? I also used different time frames to see if that made a difference, but it didn't.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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