DD/RP Calculator
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © jam_chen1025

//@version=5
indicator("DD/RP Calculator", overlay=true)

// input
show_se_lbl = input.bool(false,"Show Entry/Exit Label")
show_se_li = input.bool(true,"Show Entry/Exit Line")
ddrpc_is_open = input.bool(false,"Is Open")
ddrpc_order_type = input.string("Buy","OrderType",options=["Buy","Sell"])
ddrpc_t_s = input.time(timestamp("1 Jan 1990 00:00 +0000"),"S Time",confirm=true,inline="Order Enter Point")
ddrpc_p_s = input.price(0,title="Price",confirm=true,inline="Order Enter Point")
ddrpc_t_e = input.time(timestamp("1 Jan 1990 00:00 +0000"),"E Time",confirm=true,inline="Order Exit Point")
ddrpc_p_e = input.price(0,title="Price",confirm=true,inline="Order Exit Point")
ddrpc_p_s := math.round_to_mintick(ddrpc_p_s)
ddrpc_p_e := math.round_to_mintick(ddrpc_p_e)
ddrpc_t_e := ddrpc_is_open ? time : ddrpc_t_e

// calculator
var float pts = syminfo.mintick
var float ddrpc_dd = na // draw down
var float ddrpc_rp = na // run up
var float ddrpc_hh = na // highest high
var float ddrpc_ll = na // lowest low
if time[0] <= ddrpc_t_s
    ddrpc_dd := 0
    ddrpc_rp := 0

    ddrpc_hh := ddrpc_p_s
    ddrpc_ll := ddrpc_p_s

else if time[0] > ddrpc_t_e
    ddrpc_hh := na
    ddrpc_ll := na

else
    ddrpc_hh := math.max(high, ddrpc_hh)
    ddrpc_ll := math.min(low, ddrpc_ll)

    ddrpc_dd := int((ddrpc_ll - ddrpc_p_s) / pts)
    ddrpc_rp := int((ddrpc_hh - ddrpc_p_s) / pts)

// draw input object
ddrpc_p_hh = plot((time[0] >= ddrpc_t_s and time[0] <= ddrpc_t_e) ? ddrpc_hh : na,"High Track",color=color.new(color.green,0))
ddrpc_p_ll = plot((time[0] >= ddrpc_t_s and time[0] <= ddrpc_t_e) ? ddrpc_ll : na,"Low Track",color=color.new(color.red,0))

if show_se_lbl
    ddrpc_lbl_s = label.new(ddrpc_t_s,ddrpc_p_s,"S",xloc=xloc.bar_time,color=color.new(color.gray,0))
    ddrpc_lbl_e = label.new(ddrpc_t_e,ddrpc_p_e,"E",xloc=xloc.bar_time,color=color.new(color.gray,0))
    label.delete(ddrpc_lbl_s[1])
    label.delete(ddrpc_lbl_e[1])
if show_se_li
    ddrpc_li = line.new(ddrpc_t_s,ddrpc_p_s,ddrpc_t_e,ddrpc_p_e,xloc=xloc.bar_time,color=color.new(color.gray,0))
    line.delete(ddrpc_li[1])

float ddrpc_profit = 0
float ddrpc_dd_final = 0
float ddrpc_rp_final = 0
if ddrpc_order_type == "Buy"
    ddrpc_profit := (ddrpc_p_e - ddrpc_p_s) / pts
    ddrpc_dd_final := ddrpc_dd
    ddrpc_rp_final := ddrpc_rp
else
    ddrpc_profit := (ddrpc_p_s - ddrpc_p_e) / pts
    ddrpc_dd_final := -ddrpc_rp
    ddrpc_rp_final := -ddrpc_dd

ddrpc_str_order_info = ddrpc_order_type + ": " + str.tostring(ddrpc_p_s) + " -> " + str.tostring(ddrpc_p_e) + "\n"
     + "P(" + str.tostring(int(ddrpc_profit)) + "), D(" + str.tostring(str.format("{0,number,#.##}",ddrpc_dd_final)) + "), R(" + str.tostring(str.format("{0,number,#.##}",ddrpc_rp_final)) + ")"
ddrpc_lbl_order_info = label.new(ddrpc_t_e,ddrpc_p_e,ddrpc_str_order_info,xloc=xloc.bar_time,color=color.new(color.gray,0),style=label.style_label_left,textcolor=color.new(color.white,0),textalign=text.align_left)
label.delete(ddrpc_lbl_order_info[1])

