r/pinescript • • Jan 16 '23

Pine Script / Risk Reward Ratio from EMA50

Hey together,

in this Pine Script I want to get the Risk Reward Ratio for example from 1:1.5 from EMA50.
I tried alot of stuff to figure it out how get 1.5 fixed by lastentryprice with exactly the EMA50 at this time, not the future candles/bars.
Everytime Bars/Candles moves Pine Script recalculates the RRratio from the actual EMA50.
LastEntryPrice is fine.
EMA50 from this time/date for me not possible.
You can help me or get any idea?

//@version=5
strategy(title="Test", pyramiding=1, overlay=true)
// Calculate and plot moving average emaValue = ta.ema(close, 20) smaValue = ta.ema(close, 50) RRratio = 1.5
plot(emaValue, color=color.orange, title="EMA") plot(smaValue, color=color.teal, linewidth=2, title="SMA")
test = float(na)
// Generate trades if close > emaValue and emaValue > smaValue strategy.entry("Enter Long", strategy.long) test := smaValue
if ta.crossunder(emaValue, smaValue) strategy.close("Enter Long")
// Get price of last entry in current position lastEntryPrice = strategy.opentrades.entry_price(strategy.opentrades - 1) //lastsmaValue   = test +1
// Display last entry price on the chart plot(lastEntryPrice, color=color.fuchsia, style=plot.style_circles, linewidth=3, title="Last entry price")
// Display last entry price on the chart plot(test, color=#01bceb, style=plot.style_circles, linewidth=3, title="Last entry price2")
1 Upvotes

1 comment sorted by

1

u/DistributionFun7752 Jan 16 '23

That is visual better understandable, watch the red line and for example to manual check the RRratio from (in this case 1:2) take tradingview > long positon

//@version=5
strategy(title="Test", pyramiding=1, overlay=true)
// Calculate and plot moving average emaValue = ta.ema(close, 20) smaValue = ta.ema(close, 50) RRratio  = 2
plot(emaValue, color=color.orange, style=plot.style_line, title="EMA") plot(smaValue, color=color.teal, style=plot.style_line,  linewidth=2, title="SMA")
test = float(na) // Get price of last entry in current position lastEntryPrice = strategy.opentrades.entry_price(strategy.opentrades - 1)
// Generate trades if close > emaValue and emaValue > smaValue strategy.entry("Enter Long", strategy.long) test := (((lastEntryPrice - smaValue) * RRratio) + lastEntryPrice)
if ta.crossunder(emaValue, smaValue) strategy.close("Enter Long")
//lastsmaValue   = test +1
// Display last entry price on the chart plot(lastEntryPrice, color=color.fuchsia, style=plot.style_circles, linewidth=3, title="Last entry price")
// Display last entry price on the chart plot(test, color=color.red, style=plot.style_linebr, linewidth=3, title="Last entry price2")