Trend Indicator (dow trending) - Fontiramisu
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// Author : @Fontiramisu
// Pivot Calculation Pivot is from ©ZenAndTheArtOfTrading / PineScriptMastery.com
// Trend interpretation is from @Fontiramisu
// @version=5
indicator("Trend Indicator - Fontiramisu", overlay=true)

import Fontiramisu/fontilab/1 as fontLib


// ------------------------------------- Possibility to Use Other Timeframe -----------------------------------------------------------------
// oneHourClose = request.security(syminfo.tickerid, "60", close[1], barmerge.gaps_off, barmerge.lookahead_on)
// oneHourHigh = request.security(syminfo.tickerid, "60", high[1], barmerge.gaps_off, barmerge.lookahead_on)
// oneHourLow = request.security(syminfo.tickerid, "60", low[1], barmerge.gaps_off, barmerge.lookahead_on)


// ------------------------------------- Input Vars ------------------------------------------------------------------------------------------
// Get user input
var devTooltip              = "Deviation is a multiplier that affects how much the price should deviate from the previous pivot in order for the bar to become a new pivot."
var devTooltipUtSup         = "For UT Sup - Deviation is a multiplier that affects how much the price should deviate from the previous pivot in order for the bar to become a new pivot."
var depthTooltip            = "The minimum number of bars that will be taken into account when analyzing pivots."
var depthUtSupTooltip       = "For UT Sup - The minimum number of bars that will be taken into account when analyzing pivots."
thresholdMultiplier         = input.float(title="Deviation", defval=2.5, minval=0, tooltip=devTooltip, group="CUR. UT")
thresholdMultiplierUtSup    = input.float(title="Deviation", defval=2.5, minval=0, tooltip=devTooltip, group="UT SUP")
depth                       = input.int(title="Depth", defval=10, minval=1, tooltip=depthTooltip, group="CUR. UT") 
depthUtSup                  = input.int(title="Depth", defval=100, minval=1, tooltip=depthTooltip, group="UT SUP")
deleteLastLine              = input.bool(title="Delete Trend Lines", defval=true, group="CUR. UT")
deleteLastLineUtSup         = input.bool(title="Delete Trend Lines Ut Sup", defval=true, group="UT SUP")
isDrawBounds                = input.bool(title="Draw Trend Bounds", defval=true, group="CUR. UT")
isDrawBoundsUtSup           = input.bool(title="Draw Trend Bounds Ut Sup", defval=true, group="UT SUP")
breakingLineColor           = input.color(title="Breaking Line Color", defval=color.silver, group="CUR. UT")  
breakingLineColorUtSup      = input.color(title="Breaking Line Color Ut Sup", defval=color.gray, group="UT SUP")


// ------------------------------------- Current UT - Calculate Pivots - Trend From Pivots -----------------------------------------------------------------------------
// --- Find Dev Pivots ---
// Prepare pivot variables
var line lineLast = na
var int iLast = 0 // Index last
var int iPrev = 0 // Index previous
var float pLast = 0 // Price last
var float pLastHigh = 0 // Price last
var float pLastLow = 0 // Price last
var isHighLast = false // If false then the last pivot was a pivot low


// Get pivot information from dev pivot finding function
[dupLineLast, dupIsHighLast, dupIPrev, dupILast, dupPLast, dupPLastHigh, dupPLastLow] = 
     fontLib.getDeviationPivots(thresholdMultiplier, depth, lineLast, isHighLast, iLast, pLast, deleteLastLine, close, high, low)
    //  fontLib.getDeviationPivots(thresholdMultiplier, depth, isHighLast, iLast, pLast, close, high, low)

if not na(dupIsHighLast)
    lineLast := dupLineLast
    isHighLast := dupIsHighLast
    iPrev := dupIPrev
    iLast := dupILast
    pLast := dupPLast
    pLastHigh := dupPLastHigh
    pLastLow := dupPLastLow

// --- Find Trend Pivots ---
// - Init Vars and Inputs -
// Input
var trendPrecision = input.float(0.01, title="trend precision", tooltip="% move out of bound needed to validate a trend", group="CUR. UT") 
// Create Array to store the pivots
var trendBarIndexes     = array.new_int(0)
var trendPrices         = array.new_float(0)
var trendPricesIsHigh   = array.new_bool(0)
// Create inter Array to store hesitating moves
var interBarIndexes     = array.new_int(0)
var interPrices         = array.new_float(0)
var interPricesIsHigh   = array.new_bool(0)

// // Variable to store trend state
var bool isTrendUp = na // Is trend up or down
var bool isTrendHesitate = na // Is trend hesitate 

[dupTrendBarIndexes, dupTrendPrices, dupTrendPricesIsHigh, dupInterBarIndexes, dupInterPrices, dupInterPricesIsHigh, dupIsTrendHesitate, dupIsTrendUp] = 
     fontLib.getTrendPivots(trendBarIndexes, trendPrices, trendPricesIsHigh, interBarIndexes, interPrices, interPricesIsHigh, isTrendHesitate, isTrendUp, trendPrecision, pLast, iLast, isHighLast)      
                
if not na(dupTrendPrices)
    trendBarIndexes     := dupTrendBarIndexes
    trendPrices         := dupTrendPrices
    trendPricesIsHigh   := dupTrendPricesIsHigh
    interBarIndexes     := dupInterBarIndexes
    interPrices         := dupInterPrices
    interPricesIsHigh   := dupInterPricesIsHigh
    isTrendHesitate     := dupIsTrendHesitate
    isTrendUp           := dupIsTrendUp

// ------------------------------------- UT Sup - Calculate Pivots - Trend From Pivots -----------------------------------------------------------------------------
// --- Find Dev Pivots ---
var line lineLastUtSup = na
var int iLastUtSup = 0 // Index last
var int iPrevUtSup = 0 // Index previous
var float pLastLowUtSup = 0 // Price last
var float pLastHighUtSup = 0 // Price last
var float pLastUtSup = 0 // Price last
var isHighLastUtSup = false // If false then the last pivot was a pivot low

// Get pivot information from dev pivot finding function
[dupLineLastUtSup, dupIsHighLastUtSup, dupIPrevUtSup, dupILastUtSup, dupPLastUtSup, dupPLastLowUtSup, dupPLastHighUtSup] = 
    //  fontLib.getDeviationPivots(thresholdMultiplierUtSup, depthUtSup, lineLastUtSup, isHighLastUtSup, iLastUtSup, pLastUtSup, deleteLastLineUtSup, oneHourCloseUtSup, oneHourHighUtSup, oneHourLowUtSup)
     fontLib.getDeviationPivots(thresholdMultiplierUtSup, depthUtSup, lineLastUtSup, isHighLastUtSup, iLastUtSup, pLastUtSup, deleteLastLineUtSup, close, high, low)
    //  fontLib.getDeviationPivots(thresholdMultiplierUtSup, depthUtSup, isHighLastUtSup, iLastUtSup, pLastUtSup, close, high, low)

if not na(dupIsHighLastUtSup)
    lineLastUtSup       := dupLineLastUtSup
    isHighLastUtSup     := dupIsHighLastUtSup
    iPrevUtSup          := dupIPrevUtSup
    iLastUtSup          := dupILastUtSup
    pLastUtSup          := dupPLastUtSup
    pLastLowUtSup       := dupPLastLowUtSup
    pLastHighUtSup      := dupPLastHighUtSup
    
// --- Find Trend Pivots ---
// Input
var trendPrecisionUtSup = input.float(0.01, title="trend precision", tooltip="% move out of bound needed to validate a trend", group="UT SUP") 
// Create Array to store the pivots
var trendBarIndexesUtSup     = array.new_int(0)
var trendPricesUtSup         = array.new_float(0)
var trendPricesIsHighUtSup   = array.new_bool(0)
// Create inter Array to store hesitating moves
var interBarIndexesUtSup     = array.new_int(0)
var interPricesUtSup         = array.new_float(0)
var interPricesIsHighUtSup   = array.new_bool(0)

// Variable to store trend state
var bool isTrendUpUtSup = na // Is trend up or down
var bool isTrendHesitateUtSup = na // Is trend hesitate 

[dupTrendBarIndexesUtSup, dupTrendPricesUtSup, dupTrendPricesIsHighUtSup, dupInterBarIndexesUtSup, dupInterPricesUtSup, dupInterPricesIsHighUtSup, dupIsTrendHesitateUtSup, dupIsTrendUpUtSup] = 
     fontLib.getTrendPivots(trendBarIndexesUtSup, trendPricesUtSup, trendPricesIsHighUtSup, interBarIndexesUtSup, interPricesUtSup, interPricesIsHighUtSup, isTrendHesitateUtSup, isTrendUpUtSup, trendPrecisionUtSup, pLastUtSup, iLastUtSup, isHighLastUtSup)      
                
if not na(dupTrendPricesUtSup)
    trendBarIndexesUtSup := dupTrendBarIndexesUtSup
    trendPricesUtSup := dupTrendPricesUtSup
    trendPricesIsHighUtSup := dupTrendPricesIsHighUtSup
    interBarIndexesUtSup := dupInterBarIndexesUtSup
    interPricesUtSup := dupInterPricesUtSup
    interPricesIsHighUtSup := dupInterPricesIsHighUtSup
    isTrendHesitateUtSup := dupIsTrendHesitateUtSup
    isTrendUpUtSup := dupIsTrendUpUtSup

 
// ------------------------------------- Do Some Drawings------------------------------------------------------------------------------------------
// -- Draw boundlines UT SUP --
if isDrawBounds
    // Get starting and ending high/low price of the current pivot line
    startIndex          = fontLib.getElIntArrayFromEnd(trendBarIndexes, 1)
    startPrice          = fontLib.getElFloatArrayFromEnd(trendPrices, 1)
    endIndex            = fontLib.getElIntArrayFromEnd(trendBarIndexes, 0)
    endPrice            = fontLib.getElFloatArrayFromEnd(trendPrices, 0)
    breakingPivotIndex  = fontLib.getElIntArrayFromEnd(trendBarIndexes, 2)
    breakingPivotPrice  = fontLib.getElFloatArrayFromEnd(trendPrices, 2)
    
    // Draw boundlines
    fontLib.drawBoundLines(startIndex, startPrice, endIndex, endPrice, breakingPivotIndex , breakingPivotPrice, isTrendUp, breakingLineColor)


// -- Draw boundlines UT SUP --
if isDrawBoundsUtSup
    // Get starting and ending high/low price of the current pivot line
    startIndexUtSup          = fontLib.getElIntArrayFromEnd(trendBarIndexesUtSup, 1)
    startPriceUtSup          = fontLib.getElFloatArrayFromEnd(trendPricesUtSup, 1)
    endIndexUtSup            = fontLib.getElIntArrayFromEnd(trendBarIndexesUtSup, 0)
    endPriceUtSup            = fontLib.getElFloatArrayFromEnd(trendPricesUtSup, 0)
    breakingPivotIndexUtSup  = fontLib.getElIntArrayFromEnd(trendBarIndexesUtSup, 2)
    breakingPivotPriceUtSup  = fontLib.getElFloatArrayFromEnd(trendPricesUtSup, 2)
    
    // Draw boundlines
    fontLib.drawBoundLines(startIndexUtSup, startPriceUtSup, endIndexUtSup, endPriceUtSup, breakingPivotIndexUtSup, breakingPivotPriceUtSup, isTrendUpUtSup, breakingLineColorUtSup)
  

// -- bgcolor --
// Inputs
bgcolorChange = input.bool(title=" Show Color Trend", defval=false, group="Drawings")
offsetBG = -(depth / 2)
bgcolor(bgcolorChange ? isTrendHesitate ? na : isTrendUp ? color.new(color.green,88) : color.new(color.red,88) : na, offset=offsetBG)



// -- Display Trend and hesitate --
show_table = input(true,  "Show table with stats", group = "Drawings")

if show_table
    var table ATHtable = table.new(position.bottom_right, 10, 10, frame_color = color.gray, bgcolor = color.gray, border_width = 1, frame_width = 1, border_color = color.white)
    
    table.cell(ATHtable, 0, 0, text="Trend (D" + str.tostring(depth) + ")", bgcolor = isTrendUp ? color.green : color.red, tooltip="Green if pivot trend Up, red if down, for depth " + str.tostring(depth))

    table.cell(ATHtable, 0, 1, text="Hesit. (D" + str.tostring(depth) + ")", bgcolor = isTrendHesitate ? color.orange : color.black, tooltip="Orange if pivot trend hesitate, for depth " + str.tostring(depth))

    table.cell(ATHtable, 0, 2, text="Trend (D" + str.tostring(depthUtSup) + ")", bgcolor = dupIsTrendUpUtSup ? color.green : color.red, tooltip="Green if pivot trend up, red if down for depth " + str.tostring(depthUtSup))

    table.cell(ATHtable, 0, 3, text="Hesit. (D" + str.tostring(depthUtSup) + ")", bgcolor = isTrendHesitateUtSup ? color.orange : color.black, tooltip="Orange if pivot trend hesitate, for depth " + str.tostring(depthUtSup))


// ]