edgregor
edgregor

Reputation: 19

How do I create a simple black line that connects to candle body lows & highs? [pinescript]

Example of Stoploss Black Line on Chart

My question today relates to plotting a simple black line, which will act as a visual aid for when to manually take a stop loss. My strategy utilizes stop losses, it does so by also using plot shape to plot a red or green arrow signaling entries. The stop loss comes into play when an arrow is plotted... it then takes the candle that it is plotted on and draws a horizontal line connecting to the candle body low (when going long) and candle body high (when going short). Once the strategy plots the opposite arrow it will drawn an angled line up to the designated candle low/high.

If this extra bit of info is necessary, lets say the signal arrow is plotted on the chart when

bullPoints = 2 // bullPoints receives 2 when conditions are met ideally signaling long

bullPlot = if bullPoints == 2
    true
else
    false

plotshape(series=bullPlot, shape=style.triangleup, location=location.belowbar, color=color.green)

How can I plot this basic concept of a stop loss?

https://www.tradingview.com/x/FzUQbruS/

Upvotes: 0

Views: 749

Answers (1)

PineCoders-LucF
PineCoders-LucF

Reputation: 8789

You need to watch for the events triggering a change in the stop level and change it when they occur. A var keyword is used to initialize the stop variable so that its value automatically propagates bar to bar.

An example of avoiding transitions when plotting the stop is also shown:

//@version=4
study("Stop", "", true)
// Need to have your conditions here.
bullPoints = bar_index % 22 == 0
bearPlot = bar_index % 33 == 0
bullPlot = bullPoints and not bearPlot

// This code should not require changing.
var stopLevel = 0.
if bullPlot and not bullPlot[1]
    // bullPlot just became true; save low.
    stopLevel := low
else
    if bearPlot and not bearPlot[1]
        // bearPlot just became true; save high.
        stopLevel := high

plotshape(series=bullPlot, style=shape.triangleup, location=location.belowbar, color=color.green)
plotshape(series=bearPlot, style=shape.triangledown, location=location.abovebar, color=color.maroon)
plot(stopLevel, "Stop")
// This plot doesn't show transitions.
plot(stopLevel, "Stop No transitions", change(stopLevel) ? na : color.blue, 15, transp = 80)

enter image description here

Upvotes: 1

Related Questions