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

//@version=5


// @description TODO: add library description here
library("TradersCustomLibrary", overlay=true)


export SelectOptimalTimeframeTrendlineSettings(string symbol) =>
    if (str.startswith(symbol, "ETH"))
        seconds = timeframe.in_seconds(timeframe.period)
        if (seconds <= 300)
            ['15', 17, '45', 14, '60', 11, '120', 7]
        else
            ['30', 15, '60', 25, '120', 25, '240', 20]

export calculateShortStopLoss(float ResistanceTrendLine_TF1, float ResistanceTrendLine_TF2, float ResistanceTrendLine_TF3, float ResistanceTrendLine_TF4, int tier, float minimumStopLoss) =>
    trendLinesAboveCurrentPrice = array.new_float()
    tf1Distance = math.abs(high - ResistanceTrendLine_TF1)/high * 100.0
    tf2Distance = math.abs(high - ResistanceTrendLine_TF1)/high * 100.0
    tf3Distance = math.abs(high - ResistanceTrendLine_TF1)/high * 100.0
    tf4Distance = math.abs(high - ResistanceTrendLine_TF1)/high * 100.0
    
    if (high < ResistanceTrendLine_TF1 and tier <= 1 and tf1Distance >= minimumStopLoss)
        array.push(trendLinesAboveCurrentPrice, ResistanceTrendLine_TF1)
    if (high < ResistanceTrendLine_TF2 and tier <= 2 and tf1Distance >= minimumStopLoss)
        array.push(trendLinesAboveCurrentPrice, ResistanceTrendLine_TF2)
    if (high < ResistanceTrendLine_TF3 and tier <= 3 and tf1Distance >= minimumStopLoss)
        array.push(trendLinesAboveCurrentPrice, ResistanceTrendLine_TF3)
    if (high < ResistanceTrendLine_TF4 and tier <= 4 and tf1Distance >= minimumStopLoss)
        array.push(trendLinesAboveCurrentPrice, ResistanceTrendLine_TF4)

    if (array.size(trendLinesAboveCurrentPrice) == 0)
        minimumResistanceTrendLine = 9999999999999999999999
    else 
        minimumResistanceTrendLine = array.min(trendLinesAboveCurrentPrice)
        
export calculateLongStopLoss(float SupportTrendLine_TF1, float SupportTrendLine_TF2, float SupportTrendLine_TF3, float SupportTrendLine_TF4, int tier, float minimumStopLoss) =>
    trendLinesBelowCurrentPrice = array.new_float()
    tf1Distance = math.abs(low - SupportTrendLine_TF1)/low * 100.0
    tf2Distance = math.abs(low - SupportTrendLine_TF2)/low * 100.0
    tf3Distance = math.abs(low - SupportTrendLine_TF3)/low * 100.0
    tf4Distance = math.abs(low - SupportTrendLine_TF4)/low * 100.0
    
    if (low > SupportTrendLine_TF1 and tier <= 1 and tf1Distance >= minimumStopLoss)
        array.push(trendLinesBelowCurrentPrice, SupportTrendLine_TF1)
    if (low > SupportTrendLine_TF2 and tier <= 2 and tf2Distance >= minimumStopLoss)
        array.push(trendLinesBelowCurrentPrice, SupportTrendLine_TF2)
    if (low > SupportTrendLine_TF3 and tier <= 3 and tf3Distance >= minimumStopLoss)
        array.push(trendLinesBelowCurrentPrice, SupportTrendLine_TF3)
    if (low > SupportTrendLine_TF4 and tier <= 4 and tf4Distance >= minimumStopLoss)
        array.push(trendLinesBelowCurrentPrice, SupportTrendLine_TF4)

    if (array.size(trendLinesBelowCurrentPrice) == 0)
        maximumSupportTrendLine = 0.0
    else 
        maximumSupportTrendLine = array.max(trendLinesBelowCurrentPrice)
        

export werdygerTrend(float lb) =>
    multiplier = timeframe.multiplier/5.
    tofPeriod = int(204./multiplier * lb)
    stPeriod = int(72./multiplier * lb)
    foPeriod = int(51./multiplier * lb)
    tof = ta.sma(close,tofPeriod)
    st = ta.ema(close,stPeriod)
    fo = ta.ema(close,foPeriod)
    werdygerDiff = math.abs(tof - st)
    werdygerBullish = (st > tof) and werdygerDiff > werdygerDiff[1]
    werdygerBearish = (st < tof) and werdygerDiff > werdygerDiff[1]
    [werdygerBullish, werdygerBearish, tof, st, fo,  (werdygerDiff<werdygerDiff[1])]

    
pine_rma(src, length) =>
	alpha = 1/length
	sum = 0.0
	sma = ta.sma(src, length)
	sum := na(sum[1]) ? sma : alpha * src + (1 - alpha) * nz(sum[1])

pine_atr(length) =>
    trueRange = na(high[1])? high-low : math.max(math.max(high - low, math.abs(high - close[1])), math.abs(low - close[1]))
    //true range can be also calculated with ta.tr(true)
    pine_rma(trueRange, length)
  
    
export trendLines(int length, float slopeModifier) =>
    upper = 0.,lower = 0.
    slope_ph = 0.,slope_pl = 0.
    ph = ta.pivothigh(length,length)
    pl = ta.pivotlow(length,length)

    slope = pine_atr(length)/length*slopeModifier
       
    slope_ph := ph ? slope : slope_ph[1]
    slope_pl := pl ? slope : slope_pl[1]
    
    upper := ph ? ph : upper[1] - slope_ph
    lower := pl ? pl : lower[1] + slope_pl
    [ph,pl,upper,lower]

export stoch(float multiplier) =>
    periodK = int(14*multiplier)//input.int(14, title="%K Length", minval=1)
    smoothK = int(3*multiplier)//input.int(1, title="%K Smoothing", minval=1)
    periodD = int(3*multiplier)//input.int(3, title="%D Smoothing", minval=1)
    _k = ta.sma(ta.stoch(close, high, low, periodK), smoothK)
    d = ta.sma(_k, periodD)
    [d]


export timeToString(int _t) =>
  str.tostring(year(_t), "0000") + "." + str.tostring(month(_t), "00") + "." + str.tostring(dayofmonth(_t), "00") + " " +
  str.tostring(hour(_t), "00") + ":" + str.tostring(minute(_t), "00") + ":" + str.tostring(second(_t), "00")
