Future Preview
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © DFS_trading

//@version=5
indicator("Future Preview", overlay=true)

typeS = input.string("Long","Order Type",["Long","Short"],confirm=true)
showTime = input.bool(true,"Show order open time",confirm=true)
openTime = input.time(timestamp("01 JUL 2022 00:00 -0000"), "open time",confirm=true)
if not showTime
    openTime := timestamp("01 JAN 2020 00:00 -0000")
openPrice = input.price(0,"Open Price (USDT)",confirm=true)
margin = input.float(100,"Cash (USDT)")
leverage = input.float(10,"leverage")
tradingFee = input.float(0.06,"commission taker(%)")
showNum = input.bool(false,"show Cash")

hasTargetP = input.bool(false,"Take Profit",inline="TP")
targetP = input.float(0,"| TP price",inline="TP")
hasTargetL = input.bool(false,"Stop Loss",inline="SL")
targetL = input.float(0,"| SL price",inline="SL")

winC = input.color(color.green,"profit color",inline="color",group="drawing")
lossC = input.color(color.red,"loss color",inline="color",group="drawing")
bgTrans = input.float(80,"background color transparency",minval=0.0, maxval=100.0, step=5.0,inline="color",group="drawing")
sizeS = input.string("Large","Font Size",["Large","Normal","Small"],group="drawing")
textsize = size.large
if str.contains(sizeS,"Normal")
    textsize := size.normal
if str.contains(sizeS,"Small")
    textsize := size.small
var long = true
openColor=winC
if str.contains(typeS,"Short")
    long := false
if not long
    openColor := lossC
rtColor=winC

var preview = box.new(0,0,0,0)
box.delete(preview)
var openB = box.new(bar_index+5,openPrice,bar_index+25,openPrice)
box.delete(openB)

oBtop = 0.0
oBbot = 0

openB := box.new(bar_index+5,openPrice,bar_index+25,openPrice)

if close > openPrice
    box.set_top(openB,openPrice)
    box.set_bottom(openB, openPrice * 0.9999)
    box.set_text_valign(openB,text.align_top)
else
    box.set_top(openB, openPrice * 1.0001)
    box.set_bottom(openB,openPrice)
    box.set_text_valign(openB,text.align_bottom)
    

priceChange = close - openPrice
priceChangePerc = math.round(priceChange / openPrice,5)
change = priceChange
if not long
    change := 0 - change

changePerc = math.round(change/openPrice,5)

openFee = margin * leverage * tradingFee * 0.01
newMargin = math.round(margin * (1+changePerc*leverage),2)
closeFee = newMargin * leverage  * tradingFee * 0.01
profit = math.round(newMargin - margin - closeFee - openFee,2)

if profit < 0
    rtColor := lossC


tpC= color.rgb(0,0,0,255) // transparent color

preview := box.new(bar_index+5,close*1.001,right=bar_index+25,bottom=close)
if close < openPrice
    box.set_top(preview,close)
    box.set_bottom(preview,close*0.999)
    box.set_text_valign(preview,text.align_top)
else
    box.set_text_valign(preview,text.align_bottom)
box.set_bgcolor(preview,tpC)
box.set_border_color(preview,tpC)
box.set_text_color(preview,rtColor)
st = typeS
if profit > 0
    st += "            Gaining profit"
else
    st += "            Loss"

if showNum
    st += "\nGain:   " + str.tostring(profit) +""
st += "\nGain percentage: " + str.tostring(math.round(profit/margin*100,2)) + "%"
st += "\nPrice Change： " 
st += priceChange > 0 ? "↑ " : "↓ " 
st += str.tostring(priceChangePerc*100) + "%"

box.set_text_halign(preview,text.align_left)
box.set_text(preview, st)
box.set_text_size(preview,textsize)


box.set_bgcolor(openB,tpC)
box.set_border_color(openB,tpC)
box.set_text_color(openB,rtColor)
ot = "Open Price：" + str.tostring(openPrice)
box.set_text(openB,ot)
box.set_text_size(openB,textsize)
box.set_text_halign(openB,text.align_left)


var openPriceL = line.new(x1=openTime,y1=openPrice, x2 = openTime+1, y2=openPrice, xloc=xloc.bar_time, extend=extend.right,color=openColor,width=2)


var openTimeL = line.new(x1=openTime,y1=openPrice,x2=openTime,y2=openPrice*1.001,xloc=xloc.bar_time,extend=extend.both,color=openColor)
var markL = line.new(x1=bar_index+5,y1=close,x2=bar_index+25,y2=close)
line.delete(markL)
markL := line.new(x1=bar_index+5,y1=close,x2=bar_index+25,y2=close,color=rtColor,width=2)
var markLv = line.new(x1=bar_index+15,y1=close,x2=bar_index+15,y2=openPrice)
line.delete(markLv)
markLv := line.new(x1=bar_index+15,y1=close,x2=bar_index+15,y2=openPrice,color=rtColor,width=2,xloc=xloc.bar_index)

var markB = box.new(openTime,openPrice,time,close,xloc=xloc.bar_time)
box.delete(markB)
markB := box.new(openTime,openPrice,openTime+1,close,xloc=xloc.bar_time,border_color=tpC,border_width=0,extend=extend.right,bgcolor=color.new(rtColor,bgTrans))


// target profit
var targetPL = line.new(x1=bar_index,y1=targetP,x2=bar_index+5,y2=targetP)
line.delete(targetPL)
var targetPB = box.new(left=bar_index+25,top=targetP*1.05,right=bar_index+40,bottom=targetP)
box.delete(targetPB)
if hasTargetP
    tk = 1.05
    bk = 1.0
    if not long
        tk := 1.0
        bk := 0.95
    targetPB := box.new(bar_index+25,targetP*tk,bar_index+45,targetP*bk,bgcolor=tpC,border_color=tpC)
    targetPT = "TP Price：" + str.tostring(targetP)
    tpc = math.abs(targetP-openPrice)
    tpcs = tpc
    if not long
        tpcs := - tpcs
    tpcsp = tpcs / openPrice
    tpcsp := math.round(tpcsp,5)
    
    tpcp = tpc / openPrice
    tpnm = math.round(margin * (1+tpcp*leverage),2)
    tpp = tpnm - margin - openFee - tpnm*leverage*tradingFee * 0.01
    tpp := math.round(tpp,2)
    
    if showNum
        targetPT += "\nProfit：" + str.tostring(tpp)
    tppp = math.round(tpp / margin,5) // target profit profit percentage
    targetPT += "\nProfit Rate：" + str.tostring(tppp*100) + "%"
    targetPT += "\nPrice Change："
    targetPT += tpcsp > 0 ? "↑ " : "↓ "
    targetPT += str.tostring(tpcsp*100) + "%"
    box.set_text(targetPB,targetPT)
    box.set_text_color(targetPB,color.new(winC,30))
    box.set_text_valign(targetPB,text.align_bottom)
    if not long
        box.set_text_valign(targetPB,text.align_top)
    box.set_text_size(targetPB,textsize)
    targetPL := line.new(x1=bar_index+25,y1=targetP,x2=bar_index+45,y2=targetP, color=winC,extend=extend.both)

// target loss
var targetLL = line.new(x1=bar_index+25,y1=targetL,x2=bar_index+40,y2=targetL)
line.delete(targetLL)
var targetLB = box.new(left=bar_index+25,top=targetL*1.05,right=bar_index+40,bottom=targetL)
box.delete(targetLB)
if hasTargetL
    tk = 1.0
    bk = 0.95
    if not long
        tk := 1.05
        bk := 1.0
    targetLB := box.new(bar_index+25,targetL*tk,bar_index+45,targetL*bk,bgcolor=tpC,border_color=tpC)
    targetLT = "SL Price：" + str.tostring(targetL)
    tlc = math.abs(targetL-openPrice)
    tlcs = tlc
    if long
        tlcs := - tlcs
    tlcsp = tlcs / openPrice
    tlcsp := math.round(tlcsp,5)
    
    tlcp = tlc / openPrice
    tlnm = math.round(margin * (1+tlcp*leverage),2)
    tlp = margin - tlnm - openFee - tlnm*leverage*tradingFee * 0.01
    tlp := math.round(tlp,2)
    
    if showNum
        targetLT += "\nLoss：" + str.tostring(tlp)
    tlpp = math.round(tlp / margin,5)
    targetLT += "\nLoss Rate：" + str.tostring(tlpp*100) + "%"
    targetLT += "\nPrice Change："
    targetLT += tlcsp > 0 ? "↑ " : "↓ "
    targetLT += str.tostring(tlcsp*100) + "%"
    box.set_text(targetLB,targetLT)
    box.set_text_color(targetLB,color.new(lossC,30))
    box.set_text_valign(targetLB,text.align_top)
    if not long
        box.set_text_valign(targetLB,text.align_bottom)
    box.set_text_size(targetLB,textsize)
    targetLL := line.new(x1=bar_index-5,y1=targetL,x2=bar_index+ 40,y2=targetL, color=color.new(lossC,30),extend=extend.both)

