Alright, let's break down the steps for coding your indicator:
Define your input variables: The first step is to define the input variables like entry price and quantity that the user can input in the settings of the indicator.
Plot the stop loss and take profit levels on the chart:
pine
// Plot the lines on the chart
plot(stop_loss, color = color.red, style = plot.style_linebr)
plot(tp1, color = color.green, style = plot.style_linebr)
plot(tp2, color = color.green, style = plot.style_linebr)
plot(tp3, color = color.green, style = plot.style_linebr)
plot(tp4, color = color.green, style = plot.style_linebr)
Add labels to the stop loss and take profit levels:
pine
// Add labels to the lines on the chart
tp1label = label.new(bar_index, tp1, text = "TP1", color = color.green, style = label.style_label_left, size = size.tiny)
tp2label = label.new(bar_index, tp2, text = "TP2", color = color.green, style = label.style_label_left, size = size.tiny)
tp3label = label.new(bar_index, tp3, text = "TP3", color = color.green, style = label.style_label_left, size = size.tiny)
tp4label = label.new(bar_index, tp4, text = "TP4", color = color.green, style = label.style_label_left, size = size.tiny)
stoplabel = label.new(bar_index, stop_loss, text = "SL", color = color.red, style = label.style_label_left, size = size.tiny)
So, the complete code for your indicator will look like this:
pine
//@version=5
indicator("My Stop Loss & Take Profit Indicator", shorttitle = "SLTP", overlay = true)
// Define input variables
entry = input(title = "Entry Price", type = input.float, defval = 0.00, minval = 0.00, step = 0.01)
qty = input(title = "Qty", type = input.float, defval = 0.00, minval = 0.00, step = 0.01)
// Calculate stop loss and take profit levels
stop_loss = entry - (entry * 0.20)
tp1 = entry + (entry * 0.40)
tp2 = entry + (entry * 0.60)
tp3 = entry + (entry * 0.80)
tp4 = entry + (entry * 1.00)
// Plot the lines on the chart
plot(stop_loss, color = color.red, style = plot.style_linebr)
plot(tp1, color = color.green, style = plot.style_linebr)
plot(tp2, color = color.green, style = plot.style_linebr)
plot(tp3, color = color.green, style = plot.style_linebr)
plot(tp4, color = color.green, style = plot.style_linebr)
// Add labels to the lines on the chart
tp1label = label.new(bar_index, tp1, text = "TP1", color = color.green, style = label.style_label_left, size = size.tiny)
tp2label = label.new(bar_index, tp2, text = "TP2", color = color.green, style = label.style_label_left, size = size.tiny)
tp3label = label.new(bar_index, tp3, text = "TP3", color = color.green, style = label.style_label_left, size = size.tiny)
tp4label = label.new(bar_index, tp4, text = "TP4", color = color.green, style = label.style_label_left, size = size.tiny)
stoplabel = label.new(bar_index, stop_loss, text = "SL", color = color.red, style = label.style_label_left, size = size.tiny)
You can copy and paste this code in the Pine Editor of TradingView and save it as an indicator. You can then apply it to any chart and change the input variables as per your entry price and quantity.