Page 1 of 1

Risk/Reward tool

Posted: Mon Oct 19, 2020 7:03 am
by Gaz1892a
Hi all, (hey Matthew, loving your content btw :up: )

Just wondering if there's a way to draw the risk:reward tool off a signal I have in my script?

Getting myself in all sorts of script chaos here and starting to get frustrating.

Also is it possible to track price within the signal and R:R tool, so say it hits stop then can get an alert, similarly goes on to hit tp1 then back to break even and so on....

If this isn't even possible I'll give up the quest :laughing:

I guess maybe going down the strategy route and convert to study may be a way to go??

Regards

Gaz

Re: Risk/Reward tool

Posted: Mon Oct 19, 2020 3:14 pm
by ocaptain
Hey Gaz!

It doesn't look like at this point we can use that particular tool... We can draw lines, though... so maybe you can simulate the Long/Short tool by having your script draw a grey line at entry, a red line at stop, and a green line at target.

They keep adding functionality to PineScript so maybe we can do this in the future!

-Anthony

Re: Risk/Reward tool

Posted: Mon Oct 19, 2020 5:21 pm
by Gaz1892a
Thanks for the reply Anthony

Yeah been banging my head on the wall working with the solution you suggested lol

problem I'm having is the logic around tracking price. if this then this etc etc

I'll figure it out eventually :-(

Re: Risk/Reward tool

Posted: Mon Oct 19, 2020 8:40 pm
by ocaptain
Good luck! And maybe Matt can throw in his $.02 soon 😆

Re: Risk/Reward tool

Posted: Thu Nov 12, 2020 2:07 am
by Matthew
Hey Gaz! Welcome to the forum mate :)

What you're trying to achieve is definitely possible, I've done it with a couple of my scripts.

The easiest way I've found to do it is using "var" variables which are persistent across all the bars on your chart.

So some pseudo-code would look something like this:

Code: Select all

//@version=4
study(title="Stops & Targets", overlay=true)

// Detect long setup and get ATR
longSignal = close > open
atr = atr(14)

// Declare trade variables (eg. for longs)
var stopLossPrice = 0.0
var targetPrice = 0.0
var inLongTrade = false

// Declare trade management variables
stoppedOut = false
targetHit = false
initiatedLong = false

// Check if we have a valid long setup
if longSignal and not inLongTrade and not na(atr)
    stopLossPrice := low - atr
    targetPrice := close + atr
    inLongTrade := true
    initiatedLong := true

// Check if long stop has been hit
if inLongTrade and low <= stopLossPrice
    stoppedOut := true
    inLongTrade := false

// Check if long target has been hit
if inLongTrade and high >= targetPrice
    targetHit := true
    inLongTrade := false
   
// Draw stops & targets 
plot(inLongTrade ? stopLossPrice : na, color=color.red, style=plot.style_linebr, title="Stop Loss")
plot(inLongTrade ? targetPrice : na, color=color.green, style=plot.style_linebr, title="Target")

// Trigger alerts
alertcondition(initiatedLong, title="Long trade was placed", message="Opened new long position")
alertcondition(stoppedOut, title="Long trade was stopped out", message="Stop loss hit for {{ticker}}")
alertcondition(targetHit, title="Long target was hit", message="Take profit hit for {{ticker}}")
That's how I usually store my stop loss & targets for trade management alerts and drawing onto the chart and whatnot.

Obviously it's not perfect as it doesn't account for spread etc., but it's the best we can do with Pine. You could add in a fixed spread amount to the entry price / SL / target checks if your broker uses a fixed spread, otherwise there's nothing that can be done about that.

I hope that helps man, unfortunately I don't have time to dive deep into your script but let me know if you have any further questions. I'll try to record a YouTube video on this subject when I get a spare couple of hours :)

Good luck with your coding!

Re: Risk/Reward tool

Posted: Sat Nov 14, 2020 7:50 pm
by Gaz1892a
Hi Matt, Very interesting.

Will have a look into that.

Would love to see you work through it on a vid, I've got all my stops and targets off my signals sorted but getting the plots to react around price and those specific levels is becoming frustrating.

I can get it to react off the actual close of the signal. valuewhen (openbuy, close, 0) etc but I can't seem to be able to "valuewhen" the stops and targets so obviously the plots are only virtual (even though the draw correctly) and don't seem to hold any value once price gets to them when nth bar gets to them etc etc. If that makes sense.

I think line.new may be the way to go but that also has limitations of its own. grrrrrrr

Thanks for the reply, much appreciated

Gaz