Risk:Reward
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © sosso_bott

//@version=5
indicator("Risk:Reward", overlay=true)

// ——————————————————————————— \\
// ▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼ \\

// ▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲ \\
// ——————————————————————————— \\

tt_cstm_entry = "Disable this input to use selected entry source option and Make it Dynamic"
tt_cstm_exit = "Set BOTH Customs TP &&  custom SL  BEFORE enabling this input"


//  NOTE: ACTIVATE ONE MODE AT THE TIME
//      1. R:R MODE IS THE ONE BY DEFAULT
//      2. ALERT Mode is meant for SignalAutotraders who needs to figure out their exit strategy

// ------------------------ inputs \\
position        = input.string(title="Position",            options=["BUY", "SELL"],    defval="BUY",                               group="===== SETTINGS =====")
entry_source    = input.source(title="Entry source",                                    defval=close,                               group="===== SETTINGS =====")
box_length      = input.int(title="Left LookBack Length",                               defval=20,      minval=1,                   group="===== SETTINGS =====")
box_length2     = input.int(title="Right LookBack Length",                              defval=20,      minval=1,                   group="===== SETTINGS =====")
use_cstm_entry  = input.bool(title="Use custom Entry ?",    tooltip=tt_cstm_entry,      defval=true,                                group="===== SETTINGS =====")
custom_entry    = input.price(title="Entry Price: ",        confirm=true,               defval=0.0,                                 group="===== SETTINGS =====")
full_custom     = input.bool(title="Use custom TP && SL ?", tooltip=tt_cstm_exit,       defval=false,                               group="===== SETTINGS =====")
custom_tp       = input.float(title="Custom TP",                                        defval=000.0,                               group="===== SETTINGS =====")
custom_sl       = input.float(title="Custom SL",                                        defval=000.0,                               group="===== SETTINGS =====")


use_RR          = input.bool(title="Use Risk:Reward ?",                                 defval=true,                                group="=== RISK:REWARD ===")
risk            = input.float(title="Risk Value",                                       defval=1,       minval=0.5, step=0.5,       group="=== RISK:REWARD ===")
reward          = input.float(title="Reward Value",                                     defval=1,       minval=0.5, step=0.5,       group="=== RISK:REWARD ===")
rr_tp           = input.float(title='Percentage',                                       defval=0.5,     step=0.1,                   group="=== RISK:REWARD ===")


use_TPs         = input.bool(title="Use Take profit levels ?",                          defval=false,                               group="=== TAKEPROFIT LEVELS ===")
useTp1          = input.bool(title="Use TP1 ?",                                         defval=true,                                group="=== TAKEPROFIT LEVELS ===")
useTp2          = input.bool(title="Use TP2 ?",                                         defval=true,                                group="=== TAKEPROFIT LEVELS ===")
useTp3          = input.bool(title="Use TP3 ?",                                         defval=true,                                group="=== TAKEPROFIT LEVELS ===")
useTp4          = input.bool(title="Use TP4 ?",                                         defval=true,                                group="=== TAKEPROFIT LEVELS ===")
useTp5          = input.bool(title="Use TP5 ?",                                         defval=true,                                group="=== TAKEPROFIT LEVELS ===")
useTp6          = input.bool(title="Use TP6 ?",                                         defval=true,                                group="=== TAKEPROFIT LEVELS ===")
slx             = input.float(title="Percentage to SL",                                 defval=1.0,     step=0.1,                   group="=== TAKEPROFIT LEVELS ===")
tp1x            = input.float(title="Percentage to TP1",                                defval=0.5,     step=0.1,                   group="=== TAKEPROFIT LEVELS ===")
tp2x            = input.float(title="Percentage to TP2",                                defval=1.0,     step=0.1,                   group="=== TAKEPROFIT LEVELS ===")
tp3x            = input.float(title="Percentage to TP3",                                defval=1.5,     step=0.1,                   group="=== TAKEPROFIT LEVELS ===")
tp4x            = input.float(title="Percentage to TP4",                                defval=2.0,     step=0.1,                   group="=== TAKEPROFIT LEVELS ===")
tp5x            = input.float(title="Percentage to TP5",                                defval=2.5,     step=0.1,                   group="=== TAKEPROFIT LEVELS ===")
tp6x            = input.float(title="Percentage to TP6",                                defval=3.0,     step=0.1,                   group="=== TAKEPROFIT LEVELS ===")

long_alert      = input.bool(title="Enable BUY Alerts",                                 defval=false,                               group="====== ALERTS ======")
short_alert     = input.bool(title="Enable SELL Alerts",                                defval=false,                               group="====== ALERTS ======")
order_type      = input.string(title="Alert Order Type",  options=["LIMIT", "MARKET"],  defval="LIMIT",                             group="====== ALERTS ======")
resop           = input.timeframe(title="timeFrame",                                    defval="",                                  group="====== ALERTS ======")
choice          = input.bool(title="Heikin Choice",                                     defval=false,                               group="====== ALERTS ======")


// ———————   LOGIC
round_price(x) =>
    xx = int(x / syminfo.mintick)
    xx * syminfo.mintick

Price_in = use_cstm_entry ? custom_entry : entry_source

var float tp1 = na
var float tp2 = na
var float tp3 = na
var float tp4 = na
var float tp5 = na
var float tp6 = na
var float sl = na
var float entry1 = na
var int et  = 0


line l11    = na
line l2     = na
line l3     = na
line l4     = na
line l5     = na
line l6     = na
line l7     = na
line l8     = na

label lb11  = na
label lb2   = na
label lb3   = na
label lb4   = na
label lb5   = na
label lb6   = na
label lb7   = na
label lb8   = na

label lb_buyTP1  = na
label lb_buyTP2  = na
label lb_buyTP3  = na
label lb_buyTP4  = na
label lb_buyTP5  = na
label lb_buyTP6  = na
label lb_buySL   = na

label lb_sellTP1 = na
label lb_sellTP2 = na
label lb_sellTP3 = na
label lb_sellTP4 = na
label lb_sellTP5 = na
label lb_sellTP6 = na
label lb_sellSL  = na


dt               = time - time[box_length]


if use_TPs
    if position == "BUY"
        entry1      := round_price(Price_in)
        et          := time[box_length2]
        tp1         := round_price(entry1  * (1 + (tp1x / 100)))
        tp2         := round_price(entry1  * (1 + (tp2x / 100)))
        tp3         := round_price(entry1  * (1 + (tp3x / 100)))
        tp4         := round_price(entry1  * (1 + (tp4x / 100)))
        tp5         := round_price(entry1  * (1 + (tp5x / 100)))
        tp6         := round_price(entry1  * (1 + (tp6x / 100)))
        sl          := round_price(entry1  * (1 - (slx  / 100))) 

    if position == "SELL"
        entry1      := round_price(Price_in)
        et          := time[box_length2]
        tp1         := round_price(entry1  * (1 - (tp1x / 100)))
        tp2         := round_price(entry1  * (1 - (tp2x / 100)))
        tp3         := round_price(entry1  * (1 - (tp3x / 100)))
        tp4         := round_price(entry1  * (1 - (tp4x / 100)))
        tp5         := round_price(entry1  * (1 - (tp5x / 100)))
        tp6         := round_price(entry1  * (1 - (tp6x / 100)))
        sl          := round_price(entry1  * (1 + (slx  / 100)))

if use_RR
    if position == "SELL"    
        entry1      := round_price(Price_in)
        et          := time[box_length2]
        tp1         := round_price(entry1  * ((1 - (rr_tp / 100)* reward)) ) 
        sl          := round_price(entry1  * ((1 + (rr_tp  / 100)* risk)) ) 

    if position == "BUY"
        entry1      := round_price(Price_in)
        et          := time[box_length2]
        tp1         := round_price(entry1  * ((1 + (rr_tp / 100)* reward)) )
        sl          := round_price(entry1  * ((1 - (rr_tp  / 100)* risk)) ) 
        
        
if full_custom
    if position == "SELL"    
        entry1      := round_price(Price_in)
        et          := time[box_length2]
        tp1         := round_price(custom_tp)
        sl          := round_price(custom_sl) 

    if position == "BUY"
        entry1      := round_price(Price_in)
        et          := time[box_length2]
        tp1         := round_price(custom_tp)
        sl          := round_price(custom_sl) 


if position == "BUY" 
    if barstate.islast
        l11         := line.new(et, entry1, time + dt * 2, entry1,  color=color.purple, style=line.style_solid,xloc = xloc.bar_time)
        l2          := line.new(et, sl,     time + dt * 2,  sl,     color=color.red,    style=line.style_solid,xloc = xloc.bar_time)
        l3          := line.new(et, tp1,    time + dt * 2, tp1,     color=color.green,  style=line.style_solid,xloc = xloc.bar_time)
        
        lb11        := label.new(time + dt * 4, entry1, text="Entry:" + str.tostring(entry1),   color=color.blue,   textcolor=color.white,style=label.style_label_left,xloc = xloc.bar_time,textalign=text.align_left,size=size.normal)
        lb2         := label.new(time + dt * 4, sl,     text="Stop:" + str.tostring(sl),        color=color.red,    textcolor=color.white,style=label.style_label_left,xloc = xloc.bar_time,textalign=text.align_left,size=size.normal)
        lb3         := label.new(time + dt * 4, tp1,    text="TP1:" + str.tostring(tp1),        color=color.green,  textcolor=color.white,style=label.style_label_left,xloc = xloc.bar_time,textalign=text.align_left,size=size.normal)
        
        linefill3   = linefill.new(l11, l3, color = color.new(color.green, 85))
        linefill4   = linefill.new(l2, l11, color = color.new(color.red, 85))
        
        if not use_RR
            l3      := line.new(useTp1 ? et : na, useTp1 ? tp1 : na,useTp1 ?time + dt * 2:na,useTp1 ? tp1 : na,color=color.green,style=line.style_solid,xloc = xloc.bar_time)
            l4      := line.new(useTp2 ? et : na, useTp2 ? tp2 : na,useTp2 ?time + dt * 2:na,useTp2 ? tp2 : na,color=color.green,style=line.style_solid,xloc = xloc.bar_time)
            l5      := line.new(useTp3 ? et : na, useTp3 ? tp3 : na,useTp3 ?time + dt * 2:na,useTp3 ? tp3 : na,color=color.green,style=line.style_solid,xloc = xloc.bar_time)
            l6      := line.new(useTp4 ? et : na, useTp4 ? tp4 : na,useTp4 ?time + dt * 2:na,useTp4 ? tp4 : na,color=color.green,style=line.style_solid,xloc = xloc.bar_time)
            l7      := line.new(useTp5 ? et : na, useTp5 ? tp5 : na,useTp5 ?time + dt * 2:na,useTp5 ? tp5 : na,color=color.green,style=line.style_solid,xloc = xloc.bar_time)
            l8      := line.new(useTp6 ? et : na, useTp6 ? tp6 : na,useTp6 ?time + dt * 2:na,useTp6 ? tp6 : na,color=color.green,style=line.style_solid,xloc = xloc.bar_time)
    
            lb4     := label.new(useTp2 ? time + dt * 4 : na, tp2,text="TP2:" + str.tostring(tp2),color=color.green,textcolor=color.white,style=label.style_label_left,xloc = xloc.bar_time,textalign=text.align_left,size=size.normal)
            lb5     := label.new(useTp3 ? time + dt * 4 : na, tp3,text="TP3:" + str.tostring(tp3),color=color.green,textcolor=color.white,style=label.style_label_left,xloc = xloc.bar_time,textalign=text.align_left,size=size.normal)
    		lb6     := label.new(useTp4 ? time + dt * 4 : na, tp4,text="TP4:" + str.tostring(tp4),color=color.green,textcolor=color.white,style=label.style_label_left,xloc = xloc.bar_time,textalign=text.align_left,size=size.normal)
    		lb7     := label.new(useTp5 ? time + dt * 4 : na, tp5,text="TP5:" + str.tostring(tp5),color=color.green,textcolor=color.white,style=label.style_label_left,xloc = xloc.bar_time,textalign=text.align_left,size=size.normal)
    		lb8     := label.new(useTp6 ? time + dt * 4 : na, tp6,text="TP6:" + str.tostring(tp6),color=color.green,textcolor=color.white,style=label.style_label_left,xloc = xloc.bar_time,textalign=text.align_left,size=size.normal)

            linefill3 = linefill.new(l11, l8, color = color.new(color.green, 85))
            linefill4 = linefill.new(l2, l11, color = color.new(color.red, 85))
            
if position == "SELL"  
    if barstate.islast
        l11         := line.new(et, entry1, time + dt * 2,  entry1, color=color.purple, style=line.style_solid,xloc = xloc.bar_time)
        l2          := line.new(et, sl,     time + dt * 2,  sl,     color=color.red,    style=line.style_solid,xloc = xloc.bar_time)
        l3          := line.new(et, tp1,    time + dt * 2,  tp1,    color=color.green,  style=line.style_solid,xloc = xloc.bar_time)
        
        lb11        := label.new(time + dt * 4, entry1,text="Entry:" + str.tostring(entry1),    color=color.blue,   textcolor=color.white,style=label.style_label_left,xloc = xloc.bar_time,textalign=text.align_left,size=size.normal)
        lb2         := label.new(time + dt * 4, sl,text="Stop:" + str.tostring(sl),             color=color.red,    textcolor=color.white,style=label.style_label_left,xloc = xloc.bar_time,textalign=text.align_left,size=size.normal)
        lb3         := label.new(useTp1 ? time + dt * 4 : na, tp1,text="TP1:" + str.tostring(tp1),color=color.green,textcolor=color.white,style=label.style_label_left,xloc = xloc.bar_time,textalign=text.align_left,size=size.normal)
        linefill3   = linefill.new(l11, l3, color = color.new(color.green, 85))
        linefill4   = linefill.new(l2, l11, color = color.new(color.red, 85))
        if not use_RR
            l3      := line.new(useTp1 ? et : na, useTp1 ? tp1 : na,useTp1 ?time + dt * 2:na,useTp1 ? tp1 : na,color=color.green,style=line.style_solid,xloc = xloc.bar_time)
            l4      := line.new(useTp2 ? et : na, useTp2 ? tp2 : na,useTp2 ?time + dt * 2:na,useTp2 ? tp2 : na,color=color.green,style=line.style_solid,xloc = xloc.bar_time)
            l5      := line.new(useTp3 ? et : na, useTp3 ? tp3 : na,useTp3 ?time + dt * 2:na,useTp3 ? tp3 : na,color=color.green,style=line.style_solid,xloc = xloc.bar_time)
            l6      := line.new(useTp4 ? et : na, useTp4 ? tp4 : na,useTp4 ?time + dt * 2:na,useTp4 ? tp4 : na,color=color.green,style=line.style_solid,xloc = xloc.bar_time)
            l7      := line.new(useTp5 ? et : na, useTp5 ? tp5 : na,useTp5 ?time + dt * 2:na,useTp5 ? tp5 : na,color=color.green,style=line.style_solid,xloc = xloc.bar_time)
            l8      := line.new(useTp6 ? et : na, useTp6 ? tp6 : na,useTp6 ?time + dt * 2:na,useTp6 ? tp6 : na,color=color.green,style=line.style_solid,xloc = xloc.bar_time)
    
            lb4     := label.new(useTp2 ? time + dt * 4 : na, tp2,text="TP2:" + str.tostring(tp2),color=color.green,textcolor=color.white,style=label.style_label_left,xloc = xloc.bar_time,textalign=text.align_left,size=size.normal)
            lb5     := label.new(useTp3 ? time + dt * 4 : na, tp3,text="TP3:" + str.tostring(tp3),color=color.green,textcolor=color.white,style=label.style_label_left,xloc = xloc.bar_time,textalign=text.align_left,size=size.normal)
            lb6     := label.new(useTp4 ? time + dt * 4 : na, tp4,text="TP4:" + str.tostring(tp4),color=color.green,textcolor=color.white,style=label.style_label_left,xloc = xloc.bar_time,textalign=text.align_left,size=size.normal)
    		lb7     := label.new(useTp5 ? time + dt * 4 : na, tp5,text="TP5:" + str.tostring(tp5),color=color.green,textcolor=color.white,style=label.style_label_left,xloc = xloc.bar_time,textalign=text.align_left,size=size.normal)
    		lb8     := label.new(useTp6 ? time + dt * 4 : na, tp6,text="TP6:" + str.tostring(tp6),color=color.green,textcolor=color.white,style=label.style_label_left,xloc = xloc.bar_time,textalign=text.align_left,size=size.normal)
            
            linefill3 = linefill.new(l11, l8, color = color.new(color.green, 85))
            linefill4 = linefill.new(l2, l11, color = color.new(color.red, 85))
            
label.delete(lb11[1])
line.delete(l11[1])
line.delete(l2[1])
label.delete(lb2[1])
line.delete(l3[1])
label.delete(lb3[1])
line.delete(l4[1])
label.delete(lb4[1])
line.delete(l5[1])
label.delete(lb5[1])
line.delete(l6[1])
label.delete(lb6[1])
line.delete(l7[1])
label.delete(lb7[1])
line.delete(l8[1])
label.delete(lb8[1])




///////////// ******----- ALERTS ---------******** \\\\\\\\\\\\\\\\\\\\\
message = ""
message += syminfo.ticker

if position == "BUY"
    message += "\nlong"
    
if position == "SELL"
    message += "\nshort"
    
if order_type == "LIMIT"
    message += "\nEntry: " + str.tostring(Price_in)

if use_TPs
    if useTp1
        message += "\nTP1: " + str.tostring(tp1)
        
    if useTp2
        message += "\nTP2: " + str.tostring(tp2)
        
    if useTp3
        message += "\nTP3: " + str.tostring(tp3)
         
    if useTp4
        message += "\nTP4: " + str.tostring(tp4)
        
    if useTp5
        message += "\nTP5: " + str.tostring(tp5)
        
    if useTp6
        message += "\nTP6: " + str.tostring(tp6)
         
else
    message += "\nTP1: " + str.tostring(tp1)

message += "\nSL: " + str.tostring(sl)


no_rep(_symbol, _res, _src) =>
    request.security(_symbol, _res, _src[barstate.isrealtime ? 1:0], barmerge.gaps_off, barmerge.lookahead_off)


ha_top = choice ? ticker.heikinashi(syminfo.tickerid) : syminfo.tickerid
ha_close= no_rep(ha_top, resop, close)
ha_open = no_rep(ha_top, resop, open)


longCondition = ha_close > ha_open and ha_open[1] > ha_close[1]
shortCondition = ha_close < ha_open and ha_open[1] < ha_close[1]

if long_alert and longCondition
    alert(message, alert.freq_once_per_bar_close)
    
if short_alert and shortCondition
    alert(message, alert.freq_once_per_bar_close)

plotshape(long_alert ? longCondition : na,      title="Buy",    location=location.belowbar, transp=0, color=color.blue,     textcolor=color.blue,    text="Buy",     style=shape.triangleup,     size=size.normal)
plotshape(short_alert ? shortCondition: na,     title="Sell",   location=location.abovebar, transp=0, color=color.orange,   textcolor=color.orange,  text="Sell",    style=shape.triangledown,   size=size.normal)


//////////////// ******----- ALERTS END ---------******** \\\\\\\\\\\\\\\\\\\\\


