DELAYED FIBO
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © drhakankilic

//@version=5
strategy("FIBONACCI BANDS Long Strategy", shorttitle="FBANDS L Strategy", overlay=true, pyramiding=1, initial_capital = 1000,  default_qty_type = strategy.cash, default_qty_value = 1000, 
 process_orders_on_close=true, commission_value=0.075, max_bars_back=500, max_lines_count=500, currency = 'USD')

// === Date === { 
//Backtest dates
fromDay = input.int(defval=1, title='From Day',minval=1,maxval=31)
fromMonth = input.int(defval=1, title='From Month',minval=1,maxval=12)
fromYear = input.int(defval=2020, title='From Year')
thruDay = input.int(defval=1, title='Thru Day',minval=1,maxval=31)
thruMonth = input.int(defval=1, title='Thru Month',minval=1,maxval=12)
thruYear = input.int(defval=2112, title='Thru Year')
showDate = true  // input(defval=true, title="Show Date Range")
start = timestamp(fromYear, fromMonth, fromDay, 00, 00)  // backtest start window
finish = timestamp(thruYear, thruMonth, thruDay, 23, 59)  // backtest finish window
window() =>  // create function "within window of time"
    time >= start and time <= finish ? true : false
// }

// === Line === { 
sumd(series, distance) =>
    sumd = 0.0
    for i = 0 to distance
        sumd := sumd + series[i]
    sumd

ldistance_level = input.int(title="long distance level",  defval=2)
sdistance_level = input.int(title="short distance level", defval=1)    
lsumV = sumd(volume,ldistance_level+sdistance_level)
ssumV = sumd(volume,sdistance_level) 
lsumV:=lsumV-ssumV
vrate = ssumV/lsumV
vrate_level = input.float(title="vrate level", defval=0.1)
vt = close*(vrate>vrate_level?1:0)

if(vt==0) 
    vt:=vt[1]
// plot(vt, color=color.blue, title="MPT")
// plot(close, color=color.red,title="PRICE")
// }

// === FIBONACCI BANDS === {
EMAperiod = input.int(14, title='EMAperiod', minval=1, maxval=500, group="Fibonacci")
ATRperiod = input.int(14, title='ATRperiod', minval=1, maxval=500, group="Fibonacci")
EMA = ta.ema(close, EMAperiod)
TR1 = math.max(high - low, math.abs(high - close[1]))
TR = math.max(TR1, math.abs(low - close[1]))
ATR = ta.sma(TR, ATRperiod)
F2 = input(defval=1.618, title='Fibonacci Ratio 2', group="Fibonacci")
F3 = input(defval=2.618, title='Fibonacci Ratio 3', group="Fibonacci")
F4 = input(defval=4.236, title='Fibonacci Ratio 4', group="Fibonacci")
R1 = ATR
R2 = ATR * F2
R3 = ATR * F3
R4 = ATR * F4
FIBOTOP4 = EMA + R4
FIBOTOP3 = EMA + R3
FIBOTOP2 = EMA + R2
FIBOTOP1 = EMA + R1
FIBOBOT1 = EMA - R1
FIBOBOT2 = EMA - R2
FIBOBOT3 = EMA - R3
FIBOBOT4 = EMA - R4
plot(FIBOTOP4, title='FIBOTOP4', linewidth=1, color=color.new(color.orange, 0))
plot(FIBOTOP3, title='FIBOTOP3', linewidth=1, color=color.new(color.aqua, 20))
plot(FIBOTOP2, title='FIBOTOP2', linewidth=1, color=color.new(color.gray, 40))
plot(FIBOTOP1, title='FIBOTOP1', linewidth=1, color=color.new(color.purple, 40))

plot(FIBOBOT1, title='FIBOBOT1', linewidth=1, color=color.new(color.green, 40))
plot(FIBOBOT2, title='FIBOBOT2', linewidth=1, color=color.new(color.yellow, 40))
plot(FIBOBOT3, title='FIBOBOT3', linewidth=1, color=color.new(color.blue, 20))
plot(FIBOBOT4, title='FIBOBOT4', linewidth=1, color=color.new(color.aqua, 0))
// plot(EMA[1], style=plot.style_cross, title='EMA', color=color.new(color.red, 0))

prefmS = input.string(title="FiboLong", options=["cross_FIBOBOT1(green)", "cross_FIBOBOT2(yellow)", "cross_FIBOBOT3(blue)", "cross_FIBOBOT4(aqua)", "Disable"] , defval="cross_FIBOBOT2(yellow)", group="Long")
_prefmS = false 
if (prefmS == "cross_FIBOBOT1(green)" )
    _prefmS := ta.crossunder(vt, FIBOBOT1)

if (prefmS == "cross_FIBOBOT2(yellow)" )
    _prefmS := ta.crossunder(vt, FIBOBOT2)

if (prefmS == "cross_FIBOBOT3(blue)" )
    _prefmS := ta.crossunder(vt, FIBOBOT3)
  
if (prefmS == "cross_FIBOBOT4(aqua)" )
    _prefmS := ta.crossunder(vt, FIBOBOT4)

if (prefmS == "Disable" )
    _prefmS := low<low[1] or low>low[1]  

// }
longs= _prefmS
//

// === Bot Codes === { 
enterlong = input("Long Code", title='Long Enter', group="Long Code")
exitlong = input("Long Exit Code", title='Long Exit', group="Long Code")
// }

// === Extra === {
tpslacL=   input(true, title="TakeProfit StopLoss Aktif Et", group="ðŸ³Long")
// }

////////////////////////////////////////////////////////////////////////////////////////////TPSL

// === TP SL === {
per(pcnt) =>
       strategy.position_size > 0 and tpslacL  ? math.round(pcnt / 100 * strategy.position_avg_price / syminfo.mintick) : float(na)
trof      = 0
stoplossL  = input.float(title=" Stop loss", defval= 1.9, minval=0.1, step=0.1, group="ðŸ³Long")
tpL        = input.float(title=" Take profit", defval= 0.7, minval=0.1, step=0.1, group="ðŸ³Long")

// }
////////////////////////////////////////////////////////////////////////////////////////////

strategy.entry("AL", strategy.long, when= longs and window(), alert_message=enterlong, comment="Long")

strategy.close("AL",  when= ta.cross(vt, FIBOTOP3), alert_message = exitlong, comment="Cross")   

strategy.exit("AL",  profit = per(tpL), loss = per(stoplossL), trail_points=per(tpL+0.1) , trail_offset=trof, alert_message = exitlong, comment="TPorSL")
