TrendLineScalping-Basic
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © TradeWiseWithEase

//@version=5
indicator(title = "TrendLineScalping-Basic", shorttitle = "TL-Basic",overlay = true)

import TradeWiseWithEase/BE_CustomFx_Library/2 as BEL
var GroupText1 = "Calcuation Inputs"
var GroupText2 = "Display Settings"
var GroupText3 = "Trade Settings"

LBC =    input.int(title="Past Candles to Consider", defval=4, minval=1, inline="1",group=GroupText1)
LBF = input.int(title="Future Candles to Consider", defval=2, minval=1, inline="1",group=GroupText1)
TrackTL = input.int(title='TL to Track from Past Candles', defval=50, minval=20, maxval=450, inline="2",group=GroupText1)


PlotOnType = input.string(defval = 'HL', title = 'Plot Lines On', options = ['HL','OC'], inline="2",group=GroupText2)
ColorUp=input.color(title='UpLine',defval=color.lime,inline='3',group=GroupText2)
ColorDown=input.color(title='DownLine',defval=color.red,inline='3',group=GroupText2)
Linetype = input.string(title='Line Style', options=['solid (─)', 'dotted (┈)', 'dashed (╌)', 'arrow left (←)', 'arrow right (→)', 'arrows both (↔)'], defval='dotted (┈)',group=GroupText2,inline='4')
lnSize=input.int(title='Width',defval=1, minval=1, maxval=3,inline='4',group=GroupText2)

Repaint = input.bool(defval = false, title = 'Calc on RealTime Bar ?', inline="5",group=GroupText3)
sess = input.session(defval = '0930-1510', title = 'Consider Trade During', inline="5",group=GroupText3)

SessionTrue = BEL.Chk_TradingTime(sess)

// ————— Converts current "timeframe.multiplier" plus the TF into minutes of type float.
GetCurrentResInMinutes() => 
    _resInMinutes = timeframe.multiplier * (
      timeframe.isseconds   ? 1. / 60.  :
      timeframe.isminutes   ? 1.        :
      timeframe.isdaily     ? 1440.     :
      timeframe.isweekly    ? 10080.    :
      timeframe.ismonthly   ? 43800.    : na)

ResBeingLoaded = GetCurrentResInMinutes()

// Initiating Temporary Variables which gets reset on each bar
Initial_PH = nz(ta.pivothigh(
     PlotOnType == 'HL' ? 
     BEL.G_CandleInfo("CandleHigh",0,true) : 
     math.max(BEL.G_CandleInfo("CandleOpen",0,true), BEL.G_CandleInfo("CandleClose",0,true)),
     LBC,
     LBF),0.0)
Initial_PH_Candle = nz(ta.valuewhen(Initial_PH, bar_index[LBF], 0),0)

Initial_PL = nz(ta.pivotlow(
     PlotOnType == 'HL' ? 
     BEL.G_CandleInfo("CandleLow",0,true) : 
     math.min(BEL.G_CandleInfo("CandleOpen",0,true), BEL.G_CandleInfo("CandleClose",0,true)),
     LBC,
     LBF),0.0)
Initial_PL_Candle = nz(ta.valuewhen(Initial_PL, bar_index[LBF], 0),0)

SlowEMA = BEL.G_Indicator_Val("EMA",8, BEL.G_CandleInfo("CandleClose",0,Repaint))
FastEMA = BEL.G_Indicator_Val("EMA",3, BEL.G_CandleInfo("CandleClose",0,Repaint))
StTermDelta = ((FastEMA - SlowEMA) / hlc3 ) * 100

R_Time_SlowEMA = BEL.G_Indicator_Val("EMA",8, BEL.G_CandleInfo("CandleClose",0,true))
R_Time_FastEMA = BEL.G_Indicator_Val("EMA",3, BEL.G_CandleInfo("CandleClose",0,true))
R_Time_StTermDelta = ((FastEMA - SlowEMA) / hlc3 ) * 100

float First_PH = na
float First_PL = na
int FPH_Bar = na
int FPL_Bar = na

if BEL.BarToStartYourCalculation(TrackTL) and ResBeingLoaded <= 5 and SessionTrue
    First_PH := Initial_PH
    First_PL := Initial_PL
    FPH_Bar := Initial_PH_Candle
    FPL_Bar := Initial_PL_Candle

var SeriesOf_PH=array.new_float(0)
var CandleInfoOf_PH=array.new_int(0)
var SeriesOf_PL=array.new_float(0)
var CandleInfoOf_PL=array.new_int(0)

var R_SeriesOf_PH=array.new_float(0)
var R_CandleInfoOf_PH=array.new_int(0)
var R_SeriesOf_PL=array.new_float(0)
var R_CandleInfoOf_PL=array.new_int(0)
var int SizeOfPH_Array = 0
var int SizeOfPL_Array = 0

sizeH = array.size(SeriesOf_PH)
sizeL = array.size(SeriesOf_PL)
//========================== PIVOT HIGH CALCULATION ===============================
if Initial_PH != 0.00 and BEL.BarToStartYourCalculation(TrackTL) and ResBeingLoaded <= 5 and SessionTrue
    if sizeH == 0
        array.push(SeriesOf_PH,Initial_PH)
        array.push(CandleInfoOf_PH,Initial_PH_Candle)
    else
        if Initial_PH_Candle - array.get(CandleInfoOf_PH, sizeH - 1) > 4 and array.get(SeriesOf_PH, sizeH - 1) >= Initial_PH
            array.push(SeriesOf_PH,Initial_PH)
            array.push(CandleInfoOf_PH,Initial_PH_Candle)
        else
            array.pop(SeriesOf_PH)
            array.pop(CandleInfoOf_PH)
            array.push(SeriesOf_PH,Initial_PH)
            array.push(CandleInfoOf_PH,Initial_PH_Candle)            

//========================== PIVOT LOW CALCULATION ===============================
if Initial_PL != 0.00 and BEL.BarToStartYourCalculation(TrackTL) and ResBeingLoaded <= 5 and SessionTrue
    if sizeL == 0
        array.push(SeriesOf_PL,Initial_PL)
        array.push(CandleInfoOf_PL,Initial_PL_Candle)
    else
        if Initial_PL_Candle - array.get(CandleInfoOf_PL, sizeL - 1) > 4 and array.get(SeriesOf_PL, sizeL - 1) <= Initial_PL
            array.push(SeriesOf_PL,Initial_PL)
            array.push(CandleInfoOf_PL,Initial_PL_Candle)
        else
            array.pop(SeriesOf_PL)
            array.pop(CandleInfoOf_PL)
            array.push(SeriesOf_PL,Initial_PL)
            array.push(CandleInfoOf_PL,Initial_PL_Candle)            

SizeOfPH_Array:= array.size(SeriesOf_PH)
SizeOfPL_Array:= array.size(SeriesOf_PL)
var line[] trend_line=array.new_line(2)
var float UpTrendlftval = 0
var float UpTrendrgtval = 0
var int UpLft = 0
var int Uprgt = 0
var float DnTrendlftval = 0
var float DnTrendrgtval = 0
var int DnLft = 0
var int Dnrgt = 0
var bool SetUpPriceLineScan = false
var bool SetDnPriceLineScan = false

if SizeOfPL_Array == 2
    SetUpPriceLineScan:= true
    UpTrendlftval := array.get(SeriesOf_PL, 0)
    UpTrendrgtval := array.get(SeriesOf_PL,1)
    UpLft := array.get(CandleInfoOf_PL, 0)
    Uprgt := array.get(CandleInfoOf_PL, 1)
    array.shift(SeriesOf_PL)
    array.shift(CandleInfoOf_PL)    
    if UpTrendlftval <= UpTrendrgtval
        line.delete(array.get(trend_line, 0))
        array.set(trend_line, 0,
                  line.new(x1 = UpLft, 
                          y1 = UpTrendlftval, 
                          x2 = Uprgt, 
                          y2 = UpTrendrgtval,
                          color = ColorUp,
                          style = BEL.G_Reg_LineType(Linetype),
                          width = lnSize,
                          extend = extend.right))

if SizeOfPH_Array == 2
    SetDnPriceLineScan:= true
    DnTrendlftval := array.get(SeriesOf_PH, 0)
    DnTrendrgtval := array.get(SeriesOf_PH,1)
    DnLft := array.get(CandleInfoOf_PH, 0)
    Dnrgt := array.get(CandleInfoOf_PH, 1)
    array.shift(SeriesOf_PH)
    array.shift(CandleInfoOf_PH)    
    if DnTrendlftval >= DnTrendrgtval
        line.delete(array.get(trend_line, 1))
        array.set(trend_line, 1,
                  line.new(x1 = DnLft, 
                          y1 = DnTrendlftval, 
                          x2 = Dnrgt, 
                          y2 = DnTrendrgtval,
                          color = ColorDown,
                          style = BEL.G_Reg_LineType(Linetype),
                          width = lnSize,
                          extend = extend.right))

line UpTrndLine = array.get(trend_line, 0)
line DnTrndLine = array.get(trend_line, 1)
var float UpTrackPrice = na
var float DnTrackPrice = na
UpTrackPrice:= SetUpPriceLineScan ? BEL.G_TradableValue(line.get_price(UpTrndLine, bar_index)) : na
DnTrackPrice:= SetDnPriceLineScan ? BEL.G_TradableValue(line.get_price(DnTrndLine, bar_index)) : na

var bool InTheTrade = false
var TradeNo = array.new_int(0)
var int TradeNoCounter = 0
var int SizeOfTrades_Array = 0
var TradeEntryCandle = array.new_int(0)
var TradeExitCandle = array.new_int(0)
var TradeEntryPrice = array.new_float(0)
var TradeExitPrice = array.new_float(0)
var Trade_Types = array.new_string(0)
var string TradeType = ""
var int GapCounter = 0

if BEL.G_CandleInfo("CandleClose",0,Repaint) < UpTrackPrice and not InTheTrade and StTermDelta < 0.00
    if GapCounter == 0
        InTheTrade:= true
        TradeNoCounter += 1
        TradeType := "BrDn -Short"
        array.push(TradeNo,TradeNoCounter)
        array.push(TradeEntryCandle,bar_index)
        array.push(TradeEntryPrice,BEL.G_CandleInfo("CandleClose",0,Repaint))
        array.push(Trade_Types,TradeType)    
    else if GapCounter >= 3
        InTheTrade:= false
        GapCounter:= 0
    else if BEL.G_CandleInfo("CandleClose",0,Repaint) > DnTrackPrice
        InTheTrade:= false
        GapCounter:= 0
    else
        GapCounter += 1
        if BEL.isStar() or BEL.isBearishEC()
            InTheTrade:= true
            TradeNoCounter += 1
            array.push(TradeNo,TradeNoCounter)
            array.push(TradeEntryCandle,bar_index)
            array.push(TradeEntryPrice,BEL.G_CandleInfo("CandleClose",0,Repaint))
            array.push(Trade_Types,TradeType)

if BEL.G_CandleInfo("CandleClose",0,Repaint) > DnTrackPrice and not InTheTrade and StTermDelta > 0.00
    if GapCounter == 0
        InTheTrade:= true
        TradeNoCounter += 1        
        TradeType := "BrOut -Long"
        array.push(TradeNo,TradeNoCounter)
        array.push(TradeEntryCandle,bar_index)
        array.push(TradeEntryPrice,BEL.G_CandleInfo("CandleClose",0,Repaint))
        array.push(Trade_Types,TradeType)   
    else if GapCounter >= 3
        InTheTrade:= false
        GapCounter:= 0
    else if BEL.G_CandleInfo("CandleClose",0,Repaint) < UpTrackPrice
        InTheTrade:= false
        GapCounter:= 0
    else
        GapCounter += 1
        if BEL.isStar() or BEL.isBearishEC()
            InTheTrade:= true
            TradeNoCounter += 1        
            TradeType := "BrOut -Long"
            array.push(TradeNo,TradeNoCounter)
            array.push(TradeEntryCandle,bar_index)
            array.push(TradeEntryPrice,BEL.G_CandleInfo("CandleClose",0,Repaint))
            array.push(Trade_Types,TradeType)

highestDelta = ta.highest(StTermDelta,5)
lowestDelta = ta.lowest(StTermDelta,5)
smaDelta = BEL.G_Indicator_Val("SMA",5,StTermDelta)
if InTheTrade and TradeType == "BrOut -Long"
    if ta.crossunder(R_Time_StTermDelta,smaDelta)
        InTheTrade := false
        array.push(TradeExitCandle,bar_index)
        array.push(TradeExitPrice,BEL.G_CandleInfo("CandleClose",0,Repaint))
else
    if InTheTrade and TradeType == "BrDn -Short"
        if ta.crossover(R_Time_StTermDelta,smaDelta)
            InTheTrade := false
            array.push(TradeExitCandle,bar_index)
            array.push(TradeExitPrice,BEL.G_CandleInfo("CandleClose",0,Repaint))

SizeOfTrades_Array:= array.size(TradeNo)
var table MyTable = table.new(position.bottom_left,5,40, border_width = 1, border_color=color.gray, frame_width = 1, frame_color=color.gray)
var int NoOfCandles = 0
NoOfCandles += 1 
if bar_index == (NoOfCandles - 1)
    table.cell(MyTable,0,0,"Trade No",text_size=size.normal, text_color= color.red)
    table.cell(MyTable,1,0,"En Val | Candle",text_size=size.normal, text_color= color.red)
    table.cell(MyTable,2,0,"Ex Val | Candle",text_size=size.normal, text_color= color.lime)
    table.cell(MyTable,3,0,"Trade",text_size=size.normal, text_color= color.lime)
    if SizeOfTrades_Array > 0 
        for cell = SizeOfTrades_Array - 1 to 0
            table.cell(MyTable, 0, cell + 1, str.tostring(array.get(TradeNo, cell)),text_size=size.auto, text_color= color.orange)
            table.cell(MyTable, 1, cell + 1, str.tostring(array.get(TradeEntryPrice, cell)) + "|" + str.tostring(array.get(TradeEntryCandle, cell)),text_size=size.auto, text_color= color.gray)
            table.cell(MyTable, 3, cell + 1, str.tostring(array.get(Trade_Types, cell)),text_size=size.auto, text_color= color.red)
        for cellExit = array.size(TradeExitPrice) - 1 to 0  
            if array.size(TradeExitPrice) > 0
                table.cell(MyTable, 2, cellExit + 1, str.tostring(array.get(TradeExitPrice, cellExit)) + "|" + str.tostring(array.get(TradeExitCandle, cellExit)),text_size=size.auto, text_color= color.gray)
