ProRealCode - Trading & Coding with ProRealTime™
Hello,
Can anyone please help translating the code below from TradingView? I don’t need the alerts. I just need the order blocks boxes to be drawn.
Thanks in advance.
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © ClayeWeight
// Sonarlabs – Order Block Finder
//
//
// ENJOY THE FREE SCRIPT
//@version=5
indicator(title=’Sonarlab – Order Blocks’, shorttitle=’Sonarlab – OB’, overlay=true, max_boxes_count=20)
_v = input.string(“1.0.2″, title=”Version”, options=[“1.0.2″], group=”Sonarlab.io”, tooltip=”This is a free script based on our premium Smart Money Concepts (SMC) indicator. Get a free trial of our SMC indicator by visiting our website.\nhttps://www.sonarlab.io”)
sens = input.int(28, minval=1, title=’Sensitivity’, group=’Order Block’, tooltip=’Lower the sensitivity to show more order blocks. A higher sensitivity will show less order blocks.’)
sens /= 100
// OB
OBMitigationType = input.string(“Close”, title=”OB Mitigation Type”, options=[“Close”, “Wick”], group=”Order Block”, tooltip=”Choose how Order Blocks are mitigated”)
OBBullMitigation = OBMitigationType==”Close” ? close[1] : low
OBBearMitigation = OBMitigationType==”Close” ? close[1] : high
//OB Colors
col_bullish = input.color(#5db49e, title=”Bullish OB Border”, inline=”a”, group=”Order Block”)
col_bullish_ob = input.color(color.new(#64C4AC, 85), title=”Background”, inline=”a”, group=”Order Block”)
col_bearish = input.color(#4760bb, title=”Bearish OB Border”, inline=”b”, group=”Order Block”)
col_bearish_ob = input.color(color.new(#506CD3, 85), title=”Background”, inline=”b”, group=”Order Block”)
// Alerts
buy_alert = input.bool(title=’Buy Signal’, defval=true, group=’Alerts’, tooltip=’An alert will be sent when price goes below the top of a bullish order block.’)
sell_alert = input.bool(title=’Sell Signal’, defval=true, group=’Alerts’, tooltip=’An alert will be sent when price goes above the bottom of a bearish order block.’)
// Delacring Variables
bool ob_created = false
bool ob_created_bull = false
var int cross_index = na
// Declaring Box Arrays
var box drawlongBox = na
var longBoxes = array.new_box()
var box drawShortBox = na
var shortBoxes = array.new_box()
// Custom Rate of Change (ROC) calculation. This is to calculate high momentum moves in the market.
pc = (open – open[4]) / open[4] * 100
// If the ROC crossover our Sensitivty input – Then create an Order Block
// Sensitivty is negative as this is a Bearish OB
if ta.crossunder(pc, -sens)
ob_created := true
cross_index := bar_index
cross_index
// If the ROC crossover our Sensitivty input – Then create an Order Block
if ta.crossover(pc, sens)
ob_created_bull := true
cross_index := bar_index
cross_index
// ——————————-
// Bearish OB Creation
// ——————————-
// Check if we should create a OB, Also check if we haven’t created an OB in the last 5 candles.
if ob_created and cross_index – cross_index[1] > 5
float last_green = 0
float highest = 0
// Loop through the most recent candles and find the first GREEN (Bullish) candle. We will place our OB here.
for i = 4 to 15 by 1
if close[i] > open[i]
last_green := i
break
// Draw our OB on that candle – then push the box into our box arrays.
drawShortBox := box.new(left=bar_index[last_green], top=high[last_green], bottom=low[last_green], right=bar_index[last_green], bgcolor=col_bearish_ob, border_color=col_bearish, extend=extend.right)
array.push(shortBoxes, drawShortBox)
// ——————————-
// Bullish OB Creation
// ——————————-
// Check if we should create a OB, Also check if we haven’t created an OB in the last 5 candles.
if ob_created_bull and cross_index – cross_index[1] > 5
float last_red = 0
float highest = 0
// Loop through the most recent candles and find the first RED (Bearish) candle. We will place our OB here.
for i = 4 to 15 by 1
if close[i] < open[i]
last_red := i
break
// Draw our OB on that candle – then push the box into our box arrays.
drawlongBox := box.new(left=bar_index[last_red], top=high[last_red], bottom=low[last_red], right=bar_index[last_red], bgcolor=col_bullish_ob, border_color=col_bullish, extend=extend.right)
array.push(longBoxes, drawlongBox)
// —————– Bearish Order Block ——————-
// Clean up OB boxes and place alerts
if array.size(shortBoxes) > 0
for i = array.size(shortBoxes) – 1 to 0 by 1
sbox = array.get(shortBoxes, i)
top = box.get_top(sbox)
bot = box.get_bottom(sbox)
// If the two last closes are above the high of the bearish OB – Remove the OB
if OBBearMitigation > top
array.remove(shortBoxes, i)
box.delete(sbox)
// Alerts
if high > bot and sell_alert
alert(‘Price inside Bearish OB’, alert.freq_once_per_bar)
// —————– Bullish Clean Up ——————-
// Clean up OB boxes and place alerts
if array.size(longBoxes) > 0
for i = array.size(longBoxes) – 1 to 0 by 1
sbox = array.get(longBoxes, i)
bot = box.get_bottom(sbox)
top = box.get_top(sbox)
// If the two last closes are below the low of the bullish OB – Remove the OB
if OBBullMitigation < bot
array.remove(longBoxes, i)
box.delete(sbox)
// Alerts
if low < top and buy_alert
alert(‘Price inside Bullish OB’, alert.freq_once_per_bar)
Better with the screenshot
Anyone can help PLEASE ! It will benefit all the PRT community.
Thank you 🙂
I can’t translate the code but maybe this is a beginning:
It looks like a SAR with a custom ROC calculation:
pc = (open – open[4]) / open[4] * 100
The trigger lines are set with “Sensitivty” (sens = 28/100)
At a positive crossing (Crosses Over) “Bullish OB” are drawn:
If pc crosses over sens then
As a starting point, the last red candle is sought
For i = 4 to 15
If Close[i] < Open[i] then
LastRed = i
Break
At a negative crossing (Crosses Under) “Bearish OB” are drawn:
If pc crosses under -sens then
with the last green candle as the starting point.
For i = 4 to 15
If Close[i] > Open[i] then
LastGreen = i
Break
obbull=0
obbear=0
Sensi = 30
pc = (open - open[4]) / open[4] * 10000
if pc crosses over Sensi then
obbull = 1
bullcrossindex = barindex
endif
if pc crosses under -Sensi then
obbear = 1
bearcrossindex = barindex
endif
if obbull=1 and bullcrossindex - bullcrossindex[1] > 5 then
floatlastgreen = 0
floathighest = 0
For i = 4 to 15
If Close[i] > Open[i] then
LastGreen = i
endif
//Break
next
DRAWRECTANGLE(barindex[i], close, bullcrossindex, open)coloured("blue")
endif
if obbear=1 and bearcrossindex - bearcrossindex[1] > 5 then
floatlastred = 0
floathighest = 0
For i = 4 to 15
If Close[i] < Open[i] then
LastRed = i
endif
//Break
next
DRAWRECTANGLE(barindex[LastRed], open, bearcrossindex, close)coloured("red")
endif
return
obbull=0
obbear=0
bullcrossindex=0
bearcrossindex=0
Lookback = 4
Sensi = 30
pc = (open - open[Lookback]) / open[Lookback] * 10000
if pc crosses over Sensi then
obbull = 1
bullcrossindex = barindex
endif
if pc crosses under -Sensi then
obbear = 1
bearcrossindex = barindex
endif
if obbull=1 and bullcrossindex - bullcrossindex[1] > 5 then
floatlastred = 0
floathighest = 0
For i = 4 to 15
If Close[i] < Open[i] then
LastRed = i
endif
//Break
next
DRAWRECTANGLE(bullcrossindex, high, bullcrossindex+40, low)coloured("red")
endif
if obbear=1 and bearcrossindex - bearcrossindex[1] > 5 then
floatlastgreen = 0
floathighest = 0
For i = 4 to 15
If Close[i] > Open[i] then
LastGreen = i
endif
//Break
next
DRAWRECTANGLE(bearcrossindex, high, bearcrossindex+40, low)coloured("green")
endif
return
obbull=0
obbear=0
OBBullMitigation=1
OBBearMitigation=1
boxdrawlongBox = 0
boxdrawShortBox = 0
Long=0
Short = 0
$longboxes[i]=0
$shortboxes[i]=0
$drawlongbox[i]=0
$drawshortbox[i]=0
// Variable Definition
Sensi = 30
Lookback = 4
Loop = 15
pc = (open - open[Lookback]) / open[Lookback] * 10000
if pc crosses over Sensi then
obbull = 1
bullcrossindex = barindex
endif
if pc crosses under -Sensi then
obbear = 1
bearcrossindex = barindex
endif
if OBBullMitigation = 1 then
OBBullMitiValue = close[1]
endif
if OBBearMitigation = 1 then
OBBearMitiValue = close[1]
endif
// -------------------------------
// Bullish OB Creation
// -------------------------------
if obbull=1 and (bullcrossindex - bullcrossindex[1]) > 5 then
lastred = 0
ihighest = 0
For i = 4 to Loop do
If Close[i] < Open[i] then
LastRed = i
endif
Break
next
//openobbull= open[LastRed]
highobbull = high[LastRed]
lowobbull = low[LastRed]
//closeobbull = close[LastRed]
$longboxes[i]=1
$drawlongbox[i]=1
endif
// -------------------------------
// Bearish OB Creation
// -------------------------------
if obbear=1 and (bearcrossindex - bearcrossindex[1]) > 5 then
lastgreen = 0
ihighest = 0
For i = 4 to Loop do
If Close[i] > Open[i] then
LastGreen = i
endif
Break
next
//openobbear= open[LastGreen]
highobbear = high[LastGreen]
lowobbear = low[LastGreen]
//closeobbear = close[LastGreen]
$shortboxes[i]=1
$drawshortbox[i]=1
endif
// ----------------- Bearish Order Block -------------------
// Clean up OB boxes and place alerts
// -------------------------------
if $shortboxes[i] > 0 then
for i = $shortboxes[i] -1 to 0 do
sbox = $shortboxes[i]
ibot = lowobbear
itop = highobbear
next
if OBBearMitiValue > itop then
$shortboxes[i]=0
$drawshortbox[i]=0
elsif OBBearMitiValue < itop then
DRAWRECTANGLE(bearcrossindex[LastGreen], highobbear, bearcrossindex+150, lowobbear)coloured(80,108,211)//style(line,2)
//COLORBETWEEN(lowobbear,highobbear,"RED")
endif
if high > ibot then
Short=-1
endif
endif
// ----------------- Bullish Clean Up -------------------
// Clean up OB boxes and place alerts
// -------------------------------
if $longboxes[i] > 0 then
for i = $longboxes[i] -1 to 0 do
sbox = $longboxes[i]
ibot = lowobbull
itop = highobbull
next
if OBBullMitiValue < ibot then
$longboxes[i]=0
$drawlongbox[i]=0
elsif OBBullMitiValue > ibot then
DRAWRECTANGLE(bullcrossindex[LastRed], highobbull, bullcrossindex+150, lowobbull)coloured(196,102,100)
//COLORBETWEEN(lowobbull,highobbull,"blue")
endif
if low < itop then
Long=1
endif
endif
return
defparam drawonlastbaronly=true
//sens = 28
// -----------------
sens = sens/100
OBBearMitigation = close
// Custom Rate of Change (ROC) calculation. This is to calculate high momentum moves in the market.
pc = (open - open[4]) / open[4] * 100
// If the ROC crossover our Sensitivty input - Then create an Order Block
// Sensitivty is negative as this is a Bearish OB
if pc crosses under -sens then //ta.crossunder(pc, -sens)
obcreated = 1
crossindex = barindex
endif
// If the ROC crossover our Sensitivty input - Then create an Order Block
if pc crosses over sens then //ta.crossover(pc, sens)
obcreatedbull = 1
crossindex = barindex
endif
// -------------------------------
// Bearish OB Creation
// -------------------------------
// Check if we should create a OB, Also check if we haven't created an OB in the last 5 candles.
if obcreated and crossindex - crossindex[1] > 5 then
lastgreen = 0
hhighest = 0
// Loop through the most recent candles and find the first GREEN (Bullish) candle. We will place our OB here.
for i = 4 to 15
if close[i] > open[i] then
lastgreen = i
//populate the arrays of order block to draw them later
$left[plot]= barindex[lastgreen]
$top[plot]=high[lastgreen]
$bottom[plot]=low[lastgreen]
$right[plot]=barindex[lastgreen]
plot=plot+1 //increase the array column for next data
break
endif
next
endif
// Clean up OB boxes
if plot>0 then
for j = 0 to plot-1
// If the two last closes are above the high of the bearish OB - Remove the OB
if $left[j]>0 then //check if the zone still exist
itop = $top[j]
breakout = summation[max(1,barindex-$left[j])](OBBearMitigation>itop)>=2
if breakout then
$left[j]=0
endif
endif
next
endif
//plot the boxes
if islastbarupdate and plot>0 then
for j = 0 to plot-1
if $left[j]>0 then
drawrectangle($left[j],$top[j],barindex,$bottom[j]) coloured("grey",50) bordercolor("grey",50)
endif
next
endif
return
Please – Translation of Order Blocks from TV
This topic contains 21 replies,
has 5 voices, and was last updated by foxxcrypto
3 years, 10 months ago.
| Forum: | ProBuilder: Indicators & Custom Tools |
| Language: | English |
| Started: | 08/22/2022 |
| Status: | Active |
| Attachments: | 6 files |
The information collected on this form is stored in a computer file by ProRealCode to create and access your ProRealCode profile. This data is kept in a secure database for the duration of the member's membership. They will be kept as long as you use our services and will be automatically deleted after 3 years of inactivity. Your personal data is used to create your private profile on ProRealCode. This data is maintained by SAS ProRealCode, 407 rue Freycinet, 59151 Arleux, France. If you subscribe to our newsletters, your email address is provided to our service provider "MailChimp" located in the United States, with whom we have signed a confidentiality agreement. This company is also compliant with the EU/Swiss Privacy Shield, and the GDPR. For any request for correction or deletion concerning your data, you can directly contact the ProRealCode team by email at privacy@prorealcode.com If you would like to lodge a complaint regarding the use of your personal data, you can contact your data protection supervisory authority.