STRATEGY:RETRACEMENT
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © ponml
 
//@version=4 
strategy(title = "STRATEGY:RETRACEMENT", format = format.price, max_bars_back = 5000, precision = 2, overlay = true, pyramiding = 0, calc_on_order_fills = false, calc_on_every_tick = false, default_qty_type = strategy.percent_of_equity, default_qty_value = 100, initial_capital = 100, currency = "BTC", slippage = 0, commission_type = strategy.commission.percent, commission_value = 0.075)

//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
//Functions
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------

//Line Drawing Function 
drawLine(_y1, _y2, _period, _width, _text)=>

    int _x1 = nz(bar_index[_period], 0)
    int _x2 = bar_index
    var line _line = na
    var label _label = na
    if bar_index != nz(bar_index[1], 0)
        line.delete(_line)
        _line := line.new(x1 = _x1, y1 = _y1, x2 = _x2, y2 = _y2, xloc = xloc.bar_index, extend = extend.none, color = color.black, style = line.style_solid, width = _width)
        label.delete(_label)
        _label := label.new(bar_index - 3, _y2, _text, style = label.style_none)
 
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
//Inputs
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------

//Lookback Periods
high_lookback_period    = input(defval = 200,  type = input.integer,   title = "High Lookback Period", minval = 1,     step = 1)
low_lookback_period     = input(defval = 200,  type = input.integer,   title = "Low Lookback Period",  minval = 1,     step = 1)

//Backtest Range
from_month              = input(defval = 1,     type = input.integer,   title = "From Month",           minval = 1,     maxval = 12)
from_day                = input(defval = 1,     type = input.integer,   title = "From Day",             minval = 1,     maxval = 31)
from_year               = input(defval = 2019,  type = input.integer,   title = "From Year",            minval = 1970)
thru_month              = input(defval = 1,     type = input.integer,   title = "Thru Month",           minval = 1,     maxval = 12)
thru_day                = input(defval = 1,     type = input.integer,   title = "Thru Day",             minval = 1,     maxval = 31)
thru_year               = input(defval = 2021,  type = input.integer,   title = "Thru Year",            minval = 1970)

//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
//Definitions
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------

//Backtest Window
start   = timestamp(from_year, from_month, from_day, 00, 00)
finish  = timestamp(thru_year, thru_month, thru_day, 23, 59)
window()=> time >= start and time <= finish ? true : false

last_high    = 0.0
last_high    := na(last_high[1]) ? 0.0 : last_high[1]

//Highest High for Lookback Period
highest = highest(high, high_lookback_period)

//Lowest Low for Lookback Period
lowest  = lowest(low, low_lookback_period)

//Period for Lines
period  = max(-highestbars(high, high_lookback_period), -lowestbars(low, low_lookback_period))

//-----------------------------------------------------------------------------------------------------------------------------------------------------------------
//Plots
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------

//Plot Highest
if (window())

    drawLine(highest, highest, period, 2, "1.000")


//Plot Lowest
if (window())
    
    drawLine(lowest, lowest, period, 2, "0.000")


//Plot Fib
if (window())

    drawLine((highest - lowest) * 0.236 + lowest, (highest - lowest) * 0.236 + lowest, period, 1, "0.236")
    drawLine((highest - lowest) * 0.382 + lowest, (highest - lowest) * 0.382 + lowest, period, 1, "0.382")
    drawLine((highest - lowest) * 0.618 + lowest, (highest - lowest) * 0.618 + lowest, period, 1, "0.618")
    drawLine((highest - lowest) * 0.786 + lowest, (highest - lowest) * 0.786 + lowest, period, 1, "0.786")

