TFlabOrderBlockRefiner_TradingFinder2.txt
Copy
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © TFlab
//@version=5
library("OrderBlockRefiner_TradingFinder", overlay = true)
export OBRefiner(string OBType, string OBRefine ,string RefineMethod, bool TriggerCondition ,int Index) =>
string oBType = OBType
string oBRefine = OBRefine
string refineMethod = RefineMethod
bool triggerCondition = TriggerCondition
int index = Index
ATR = ta.atr(55)
PosIndex = bar_index - index < 4999 ? bar_index - index : 2
Range = high - low
Body = close - open
//Zones Data
//Zone
//Proximal Line
var float Yp12 = 0.0
var int Xp1 = 0
var int Xp2 = 0
//Distal Line
var float Yd12 = 0.0
var int Xd1 = 0
var int Xd2 = 0
//Demand Candle Range /a : after , b : before , O : Order Block
var float DCRa1 = 0.0
var float DCRO = 0.0
var float DCRb1 = 0.0
//Supply Candle Range /a : after , b : before , O : Order Block
var float SCRa1 = 0.0
var float SCRO = 0.0
var float SCRb1 = 0.0
//Demand Min ohlc{Var}
var float DminOpen = 0.0
var float DminHigh = 0.0
var float DminLow = 0.0
var float DminClose = 0.0
//Demand Max ohl{Var}
var float DmaxOpen = 0.0
var float DmaxHigh = 0.0
var float DmaxLow = 0.0
var float DmaxClose = 0.0
//Supply Min ohlc
var float SminOpen = 0.0
var float SminHigh = 0.0
var float SminLow = 0.0
var float SminClose = 0.0
//Supply Max ohlc
var float SmaxOpen = 0.0
var float SmaxHigh = 0.0
var float SmaxLow = 0.0
var float SmaxClose = 0.0
//Demand Order Block Refiner
var float DDA = 0.0 //Demand Distal Aggressive
var float DPA = 0.0 //Demand Proximal Aggressive
//Supply Order Block Refiner
var float SDA = 0.0 //Supply Distal Aggressive
var float SPA = 0.0 //Supply Proximal Aggressive
// choose Type Refine {Aggressive Vs Defensive}
var float ProximalRefine = 0.0
var float DistalRefine = 0.0
//Calculate
//Demand Candle Range /a : after , b : before , O : Order Block
DCRa1 := Range[PosIndex+1]
DCRO := Range[PosIndex]
DCRb1 := Range[PosIndex-1]
//Supply Candle Range /a : after , b : before , O : Order Block
SCRa1 := Range[PosIndex+1]
SCRO := Range[PosIndex]
SCRb1 := Range[PosIndex-1]
if oBType == 'Demand' and bar_index > 4
if bar_index > index + 1
DminOpen := math.min(open[PosIndex - 2], open[PosIndex + 1],
open[PosIndex ], open[PosIndex - 1])
DminHigh := math.min(high[PosIndex - 2], high[PosIndex + 1],
high[PosIndex ], high[PosIndex - 1])
DminLow := math.min(low[PosIndex - 2], low[PosIndex + 1],
low[PosIndex ], low[PosIndex - 1])
DminClose := math.min(close[PosIndex - 2], close[PosIndex + 1],
close[PosIndex ], close[PosIndex - 1])
else if bar_index == index + 1
DminOpen := math.min( open[PosIndex + 1],
open[PosIndex ], open[PosIndex - 1])
DminHigh := math.min( high[PosIndex + 1],
high[PosIndex ], high[PosIndex - 1])
DminLow := math.min( low[PosIndex + 1],
low[PosIndex ], low[PosIndex - 1])
DminClose := math.min( close[PosIndex + 1],
close[PosIndex ], close[PosIndex - 1])
else if bar_index == index
DminOpen := math.min( open[PosIndex + 1],
open[PosIndex ])
DminHigh := math.min( high[PosIndex + 1],
high[PosIndex ])
DminLow := math.min( low[PosIndex + 1],
low[PosIndex ])
DminClose := math.min( close[PosIndex + 1],
close[PosIndex ])
//Demand Max ohlc{Find}
if bar_index > index + 1
DmaxOpen := math.max(open[PosIndex - 2], open[PosIndex + 1],
open[PosIndex ], open[PosIndex - 1])
DmaxHigh := math.max(high[PosIndex - 2], high[PosIndex + 1],
high[PosIndex ], high[PosIndex - 1])
DmaxLow := math.max(low[PosIndex - 2], low[PosIndex + 1],
low[PosIndex ], low[PosIndex - 1])
DmaxClose := math.max(close[PosIndex - 2], close[PosIndex + 1],
close[PosIndex ], close[PosIndex - 1])
else if bar_index == index + 1
DmaxOpen := math.max( open[PosIndex + 1],
open[PosIndex ], open[PosIndex - 1])
DmaxHigh := math.max( high[PosIndex + 1],
high[PosIndex ], high[PosIndex - 1])
DmaxLow := math.max( low[PosIndex + 1],
low[PosIndex ], low[PosIndex - 1])
DmaxClose := math.max( close[PosIndex + 1],
close[PosIndex ], close[PosIndex - 1])
else if bar_index == index
DmaxOpen := math.max( open[PosIndex + 1],
open[PosIndex ])
DmaxHigh := math.max( high[PosIndex + 1],
high[PosIndex ])
DmaxLow := math.max( low[PosIndex + 1],
low[PosIndex ])
DmaxClose := math.max( close[PosIndex + 1],
close[PosIndex ])
//Demand Order Block Refiner
if DCRO <= (ATR * 0.5)
DPA := high[PosIndex]
DDA := DminLow
else if DCRO > (ATR * 0.5) and DCRO <= ATR
if Body > 0 or Body == 0
DPA := close[PosIndex]
DDA := DminLow
else if Body < 0
DPA := open[PosIndex]
DDA := DminLow
else if DCRO > ATR
if bar_index > Index
if low[PosIndex + 1] < high[PosIndex ]
if low[PosIndex + 1] > low[PosIndex]
DPA := low[PosIndex + 1]
DDA := low[PosIndex]
else if low[PosIndex + 1] <= low[PosIndex]
if Body[PosIndex + 1] > 0 or Body[PosIndex + 1] == 0
if DCRa1 > ATR
DPA := DmaxOpen
DDA := DminLow
else if DCRa1 <= ATR
DPA := DmaxClose
DDA := DminLow
else if Body[PosIndex + 1] < 0
if DCRa1 > ATR
DPA := DmaxOpen
DDA := DminLow
else if DCRa1 <= ATR
DPA := DmaxOpen
DDA := DminLow
else
DPA := DmaxOpen
DDA := DminLow
else if not ((low[PosIndex + 1] < high[PosIndex ]))
if (DminLow - DminOpen ) > (DminLow - DminClose ) and ((DminLow - DminOpen ) <= ATR and (DminLow - DminOpen ) >= ( ATR*0.5))
DPA := DminOpen
DDA := DminLow
else if (DminLow - DminOpen ) < (DminLow - DminClose ) and ((DminLow - DminClose ) <= ATR and (DminLow - DminClose ) >= ( ATR*0.5))
DPA := DminClose
DDA := DminLow
else
DPA := DminClose
DDA := DminLow
else
DPA := DminClose
DDA := DminLow
else if DCRa1 > ATR
DPA := DmaxOpen
DDA := DminLow
else
DPA := DmaxClose
DDA := DminLow
else
if Body > 0
DPA := DmaxHigh
DDA := DminLow
else if Body < 0
DPA := DmaxHigh
DDA := DmaxLow
//Demand Order Block choose Type Refine
if refineMethod == 'Defensive'
if (DPA - DDA) >= ATR * 4
DistalRefine := DDA
ProximalRefine := DPA - (DPA - DDA) * 0.8
else if (DPA - DDA) >= ATR * 3 and (DPA - DDA) < ATR * 4
DistalRefine := DDA
ProximalRefine := DPA - (DPA - DDA) * 0.7
else if (DPA - DDA) < ATR * 3 and (DPA - DDA) >= ATR * 2
DistalRefine := DDA
ProximalRefine := DPA - (DPA - DDA) * 0.6
else if (DPA - DDA) < ATR * 2 and (DPA - DDA) >= ATR *1.6
DistalRefine := DDA
ProximalRefine := DPA - (DPA - DDA) * 0.5
else if (DPA - DDA) < ATR * 1.6 and (DPA - DDA) > ATR
DistalRefine := DDA
ProximalRefine := DPA - (DPA - DDA) * 0.25
else if (DPA - DDA) <= ATR
DistalRefine := DDA
ProximalRefine := DPA
else if refineMethod == 'Aggressive'
DistalRefine := DDA
ProximalRefine := DPA
if triggerCondition
Yp12 := OBRefine == 'On' ? ProximalRefine : high[PosIndex]
Xp1 := index
Xp2 := index + 1
Yd12 := OBRefine == 'On' ? DistalRefine : low[PosIndex]
Xd1 := index
Xd2 := index + 1
if oBType == 'Supply' and bar_index > 4
if bar_index > index + 1
SminOpen := math.min(open[PosIndex - 2], open[PosIndex + 1],
open[PosIndex ], open[PosIndex - 1])
SminHigh := math.min(high[PosIndex - 2], high[PosIndex + 1],
high[PosIndex ], high[PosIndex - 1])
SminLow := math.min(low[PosIndex - 2], low[PosIndex + 1],
low[PosIndex ], low[PosIndex - 1])
SminClose := math.min(close[PosIndex - 2], close[PosIndex + 1],
close[PosIndex ], close[PosIndex - 1])
else if bar_index == index + 1
SminOpen := math.min( open[PosIndex + 1],
open[PosIndex ], open[PosIndex - 1])
SminHigh := math.min( high[PosIndex + 1],
high[PosIndex ], high[PosIndex - 1])
SminLow := math.min( low[PosIndex + 1],
low[PosIndex ], low[PosIndex - 1])
SminClose := math.min( close[PosIndex + 1],
close[PosIndex ], close[PosIndex - 1])
else if bar_index == index
SminOpen := math.min( open[PosIndex + 1],
open[PosIndex ])
SminHigh := math.min( high[PosIndex + 1],
high[PosIndex ])
SminLow := math.min( low[PosIndex + 1],
low[PosIndex ])
SminClose := math.min( close[PosIndex + 1],
close[PosIndex ])
//Supply Max ohlc
if bar_index > index + 1
SmaxOpen := math.max(open[PosIndex - 2], open[PosIndex + 1],
open[PosIndex ], open[PosIndex - 1])
SmaxHigh := math.max(high[PosIndex - 2], high[PosIndex + 1],
high[PosIndex ], high[PosIndex - 1])
SmaxLow := math.max(low[PosIndex - 2], low[PosIndex + 1],
low[PosIndex ], low[PosIndex - 1])
SmaxClose := math.max(close[PosIndex - 2], close[PosIndex + 1],
close[PosIndex ], close[PosIndex - 1])
else if bar_index == index + 1
SmaxOpen := math.max( open[PosIndex + 1],
open[PosIndex ], open[PosIndex - 1])
SmaxHigh := math.max( high[PosIndex + 1],
high[PosIndex ], high[PosIndex - 1])
SmaxLow := math.max( low[PosIndex + 1],
low[PosIndex ], low[PosIndex - 1])
SmaxClose := math.max( close[PosIndex + 1],
close[PosIndex ], close[PosIndex - 1])
else if bar_index == index
SmaxOpen := math.max( open[PosIndex + 1],
open[PosIndex ])
SmaxHigh := math.max( high[PosIndex + 1],
high[PosIndex ])
SmaxLow := math.max( low[PosIndex + 1],
low[PosIndex ])
SmaxClose := math.max( close[PosIndex + 1],
close[PosIndex ])
//Supply Order Block Refiner
if SCRO <= (ATR * 0.5)
SPA := low[PosIndex]
SDA := SmaxHigh
else if SCRO > (ATR * 0.5) and SCRO <= ATR
if Body > 0 or Body == 0
SPA := close[PosIndex]
SDA := SmaxHigh
else if Body < 0
SPA := open[PosIndex]
SDA := SmaxHigh
else if SCRO > ATR
if bar_index > Index
if high[PosIndex - 1] > low[PosIndex ]
if high[PosIndex - 1] > high[PosIndex]
SPA := high[PosIndex - 1]
SDA := high[PosIndex]
else if high[PosIndex - 1] <= high[PosIndex]
if Body[PosIndex - 1] > 0 or Body[PosIndex - 1] == 0
if SCRa1 > ATR
SPA := SminClose
SDA := SmaxHigh
else if SCRa1 <= ATR
SPA := SminOpen
SDA := SmaxHigh
else if Body[PosIndex - 1] < 0
if SCRa1 > ATR
SPA := SminOpen
SDA := SmaxHigh
else if SCRa1 <= ATR
SPA := SmaxClose
SDA := SmaxHigh
else
SPA := SminOpen
SDA := SmaxHigh
else if not ((high[PosIndex + 1] > low[PosIndex ]))
if (SmaxHigh - SminOpen ) > (SmaxHigh - SminClose ) and ((SmaxHigh - SminOpen ) <= ATR and (SmaxHigh - SminOpen ) >= ( ATR*0.5))
SPA := SminOpen
SDA := SmaxHigh
else if (SmaxHigh - DminOpen ) < (SmaxHigh - DminClose ) and ((SmaxHigh - DminClose ) <= ATR and (SmaxHigh - DminClose ) >= ( ATR*0.5))
SPA := SminClose
SDA := SmaxHigh
else
SPA := SminClose
SDA := SmaxHigh
else
SPA := SminClose
SDA := SmaxHigh
else if SCRa1 > ATR
SPA := SminClose
SDA := SmaxHigh
else
SPA := SminOpen
SDA := SmaxHigh
else
if Body > 0
SPA := SminLow
SDA := SmaxHigh
else if Body < 0
SPA := SminLow
SDA := SmaxHigh
//Supply Order Block choose Type Refine
if refineMethod == 'Defensive'
if (SDA - SPA) >= ATR * 4
DistalRefine := SDA
ProximalRefine := SPA + (SDA - SPA) * 0.8
else if (SDA - SPA) >= ATR * 3 and (SDA - SPA) < ATR * 4
DistalRefine := SDA
ProximalRefine := SPA + (SDA - SPA) * 0.7
else if (SDA - SPA) < ATR * 3 and (SDA - SPA) >= ATR * 2
DistalRefine := SDA
ProximalRefine := SPA + (SDA - SPA) * 0.6
else if (SDA - SPA) < ATR * 2 and (SDA - SPA) >= ATR * 1.6
DistalRefine := SDA
ProximalRefine := SPA + (SDA - SPA) * 0.5
else if (SDA - SPA) < ATR * 1.6 and (SDA - SPA) > ATR
DistalRefine := SDA
ProximalRefine := SPA + (SDA - SPA) * 0.25
else if (SDA - SPA) <= ATR
DistalRefine := SDA
ProximalRefine := SPA
else if refineMethod == 'Aggressive'
DistalRefine := SDA
ProximalRefine := SPA
if triggerCondition
Yp12 := OBRefine == 'On' ? ProximalRefine : low[PosIndex]
Xp1 := index
Xp2 := index + 1
Yd12 := OBRefine == 'On' ? DistalRefine : high[PosIndex]
Xd1 := index
Xd2 := index + 1
[Xd1 ,Xd2, Yd12, Xp1 ,Xp2, Yp12]
Show all 396 lines