Below you will find a study that will plot entry, target and stop loss lines or zones. The lines/zones will stop plotting when either the stop or target are hit. To use, you only need to replace the short sample code with your own that should set a buy or sell signal variable to true. You may turn the cloud feature off and just the lines will appear.
The code currently uses an ATR multiplier to determine the target and stop loss but it should be simple for you to modify this to use whatever you like.
The picture shows the results when using a moving average crossover for the buy and sell signals and the zones created showing entry to target and stop loss based on ATR multipliers of 3.0 for the target and 1.0 for the stop loss.
Shared study link: http://tos.mx/StIcExL
MP_917 Trade Lines
The code currently uses an ATR multiplier to determine the target and stop loss but it should be simple for you to modify this to use whatever you like.
The picture shows the results when using a moving average crossover for the buy and sell signals and the zones created showing entry to target and stop loss based on ATR multipliers of 3.0 for the target and 1.0 for the stop loss.
Shared study link: http://tos.mx/StIcExL
MP_917 Trade Lines
Code:
#
# Trade Lines
# v1.0 by @Mark.917
#
# Trade Lines will plot lines with and optional cloud showing where you entered your
# position and the location of your stop loss and profit target.
#
# To use, your code will need to set "your_buy_signal" or "your_sell_signal" to true.
# The profit target and stop loss are based on an ATR multplier. You may modify this
# to be whatever you would like.
declare upper;
#######################
# YOUR CODE STARTS HERE
#######################
# sample using moving average crossover for signals
plot FastMA = MovingAverage(AverageType.EXPONENTIAL, close, 20);
plot SlowMA = MovingAverage(AverageType.EXPONENTIAL, close, 50);
FastMA.SetDefaultColor(GetColor(1));
SlowMA.SetDefaultColor(GetColor(2));
def buy_signal = FastMA crosses above SlowMA;
def sell_signal = FastMA crosses below SlowMA;
###################
# BEGIN TRADE LINES
###################
input show_trade_zone = yes; #hint show_trade_zone: Adds a cloud in the profit and loss zones
input ATRTargetMultiple = 3.0;
input ATRStopMultiple = 1.0;
def atr = ATR(14, averageType.WILDERS);
def long_signal = if buy_signal then 1 else 0;
def long_target = if long_signal == 1 then atr * ATRTargetMultiple else long_target[1];
def long_stop = if long_signal == 1 then atr * ATRStopMultiple else long_stop[1];
def long_entry = if long_signal == 1 then open else if (high[1] > long_entry[1] + long_target or low[1] < long_entry[1] - long_stop) then Double.NaN else long_entry[1];
def long_exit = if long_entry > 0 then 0 else 1;
plot long_pt = if show_trade_zone and !long_exit then long_entry + long_target else Double.NaN;
long_pt.SetDefaultColor(Color.GREEN);
plot long_ep = if show_trade_zone and !long_exit then long_entry else Double.NaN;
long_ep.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
long_ep.SetPaintingStrategy(PaintingStrategy.DASHES);
long_ep.SetDefaultColor(Color.WHITE);
plot long_sl = if show_trade_zone and !long_exit then long_ep - long_stop else Double.NaN;
long_sl.SetDefaultColor(Color.RED);
AddCloud(if show_trade_zone then long_pt else Double.NaN, long_ep, Color.GREEN);
AddCloud(if show_trade_zone then long_sl else Double.NaN, long_ep, Color.RED);
def short_signal = if sell_signal then 1 else 0;
def short_target = if short_signal == 1 then atr * ATRTargetMultiple else short_target[1];
def short_stop = if short_signal == 1 then atr * ATRStopMultiple else short_stop[1];
def short_entry = if short_signal == 1 then open else if (low[1] < short_entry[1] - short_target or high[1] > short_entry[1] + short_stop) then Double.NaN else short_entry[1];
def short_exit = if short_entry > 0 then 0 else 1;
plot short_pt = if show_trade_zone and !short_exit then short_entry - short_target else Double.NaN;
short_pt.SetDefaultColor(Color.GREEN);
plot short_ep = if show_trade_zone and !short_exit then short_entry else Double.NaN;
short_ep.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
short_ep.SetPaintingStrategy(PaintingStrategy.DASHES);
short_ep.SetDefaultColor(Color.WHITE);
plot short_sl = if show_trade_zone and !short_exit then short_ep + short_stop else Double.NaN;
short_sl.SetDefaultColor(Color.RED);
AddCloud(if show_trade_zone then short_ep else Double.NaN, short_pt, Color.GREEN);
AddCloud(if show_trade_zone then short_sl else Double.NaN, short_ep, Color.RED);
#################
# END TRADE LINES
#################
Last edited: