Order Block Strategy
Forums › ProRealTime English forum › ProOrder support › Order Block Strategy
- This topic has 1 reply, 2 voices, and was last updated 2 weeks ago by
Iván.
Viewing 2 posts - 1 through 2 (of 2 total)
-
-
11/17/2025 at 3:10 PM #253698
Hi all,
I’m really interested in price action/volume trading and am trying to create an automated strategy based on order blocks with the following indicator code:
Order blocks123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533defparam drawonlastbaronly=true//--------------------------------------------------------------------------------////PRC_Smart Money Concepts//version = 1//29.07.2024//Iván González @ www.prorealcode.com//Sharing ProRealTime knowledge//--------------------------------------------------------------------------------////-----Inputs---------------------------------------------------------------------////length=50n=barindex//showswings=1//showHLswings=1//showinternals=1//showStructure=1//showOB=1//showiOB=1//showlastOB=5//ifilterConfluence=0//obFilter=1 //1=ATR 0=Cumulative mean Range//--------------------------------------------------------------------------------////--------------------------------------------------------------------------------//atr=averagetruerange[200](close)cmeanrange=summation[max(1,n)](high-low)/n//--------------------------------------------------------------------------------////-----Swings---------------------------------------------------------------------////---Swing lengthos=0upper=highest[length](high)lower=lowest[length](low)if high[length]>upper thenos=0elsif low[length]<lower thenos=1elseos=os[1]endifif os=0 and os[1]<>0 thenmytop=high[length]elsemytop=0endifif os=1 and os[1]<>1 thenmybot=low[length]elsemybot=0endif//---Swing 5os1=0upper1=highest[5](high)lower1=lowest[5](low)if high[5]>upper1 thenos1=0elsif low[5]<lower1 thenos1=1elseos1=os1[1]endifif os1=0 and os1[1]<>0 thenitop=high[5]elseitop=0endifif os1=1 and os1[1]<>1 thenibot=low[5]elseibot=0endif//--------------------------------------------------------------------------------////-----Pivot High-----------------------------------------------------------------//if mytop thentopcross=1if showswings thenif mytop>topy then$phy[z+1]=mytop$phx[z+1]=n-length$phtype[z+1]=1z=z+1else$phy[z+1]=mytop$phx[z+1]=n-length$phtype[z+1]=-1z=z+1endifendiftopy=mytoptopx=n-lengthtrailup=mytoptrailupx=n-lengthendifif itop thenitopcross=1itopy=itopitopx=n-5endif//Trailing maximuntrailup=max(high,trailup)if trailup=high thentrailupx=nelsetrailupx=trailupxendif//--------------------------------------------------------------------------------////-----Pivot Low------------------------------------------------------------------//if mybot thenbotcross=1if showswings thenif mybot<boty then$ply[k+1]=mybot$plx[k+1]=n-length$pltype[k+1]=1k=k+1else$ply[k+1]=mybot$plx[k+1]=n-length$pltype[k+1]=-1k=k+1endifendifboty=mybotbotx=n-lengthtraildn=mybottraildnx=n-lengthendifif ibot thenibotcross=1iboty=ibotibotx=n-5endif//Trailing maximuntraildn=min(low,traildn)if traildn=low thentraildnx=nelsetraildnx=traildnxendif//--------------------------------------------------------------------------------////-----Pivot High BOS/CHoCH-------------------------------------------------------//bullConcordant=1if ifilterConfluence thenbullConcordant=high-max(close,open)>min(close,open-low)endif//Detect internal bullish structureif close crosses over itopy and itopcross and topy<>itopy and bullConcordant thenchoch=undefinedif itrend<0 thenchoch=1bullichochalert=1elsebullibosalert=1endifif showinternals then$itopx[m+1]=itopx$itopy[m+1]=itopy$ichoch[m+1]=choch$iright[m+1]=barindexm=m+1endifitopcross=0itrend=1//Internal order blockif showiOB thenminim=9999999999maxim=0idx=1if obFilter=1 thenobthreshold=atrelseobthreshold=cmeanRangeendiffor i=1 to (n-itopx)-1 doif (high[i]-low[i])<obthreshold[i]*2 thenminim=min(low[i],minim)if minim=low[i] thenmaxim=high[i]idx=ielsemaxim=maximidx=idxendifendifnext$itargetTop[t+1]=maxim$itargetBot[t+1]=minim$itargetLeft[t+1]=barindex[idx]$itargetRight[t+1]=n$itargetType[t+1]=1t=t+1endifendif//Detect bullish Structureif close crosses over topy and topcross thenchoch=undefinedif trend<0 thenchoch=1endifif showStructure then$topx[g+1]=topx$topy[g+1]=topy$topchoch[g+1]=choch$right[g+1]=barindexg=g+1endif//order blockif showOB thenminim=9999999999maxim=0idx=1if obFilter=1 thenobthreshold=atrelseobthreshold=cmeanRangeendiffor i=1 to (n-topx)-1 doif (high[i]-low[i])<obthreshold[i]*2 thenminim=min(low[i],minim)if minim=low[i] thenmaxim=high[i]idx=ielsemaxim=maximidx=idxendifendifnext$targetTop[r+1]=maxim$targetBot[r+1]=minim$targetLeft[r+1]=barindex[idx]$targetRight[r+1]=n$targetType[r+1]=1r=r+1endiftopcross=0trend=1endif//--------------------------------------------------------------------------------////-----Pivot Low BOS/CHoCH--------------------------------------------------------//bearConcordant=1if ifilterConfluence thenbearConcordant=high-max(close,open)<min(close,open-low)endif//Detect internal bearish Structureif close crosses under iboty and ibotcross and boty<>iboty and bearConcordant thenchoch=0if itrend>0 thenchoch=1bearichochalert=1elsebearichochalert=1endifif showinternals then$ibotx[s+1]=ibotx$iboty[s+1]=iboty$ibotchoch[s+1]=choch$ibotright[s+1]=barindexs=s+1endifibotcross=0itrend=-1//Internal order blockif showiOB thenminim=9999999999maxim=0idx=1if obFilter=1 thenobthreshold=atrelseobthreshold=cmeanRangeendiffor i=1 to (n-ibotx)-1 doif (high[i]-low[i])<obthreshold[i]*2 thenmaxim=max(high[i],maxim)if maxim=high[i] thenminim=low[i]idx=ielseminim=minimidx=idxendifendifnext$itargetTop[t+1]=maxim$itargetBot[t+1]=minim$itargetLeft[t+1]=barindex[idx]$itargetRight[t+1]=n$itargetType[t+1]=-1t=t+1endifendif//Detect Bullish Strcutureif close crosses under boty and botcross thenchoch=undefinedif trend>0 thenchoch=1bearchochalert=1elsebearbosalert=1endifif showStructure then$botx[w+1]=botx$boty[w+1]=boty$botchoch[w+1]=choch$botright[w+1]=barindexw=w+1endif//order blockif showOB thenminim=9999999999maxim=0idx=1if obFilter=1 thenobthreshold=atrelseobthreshold=cmeanRangeendiffor i=1 to (n-botx)-1 doif (high[i]-low[i])<obthreshold[i]*2 thenmaxim=max(high[i],maxim)if maxim=high[i] thenminim=low[i]idx=ielseminim=minimidx=idxendifendifnext$targetTop[r+1]=maxim$targetBot[r+1]=minim$targetLeft[r+1]=barindex[idx]$targetRight[r+1]=n$targetType[r+1]=-1r=r+1endifbotcross=0trend=-1endif//--------------------------------------------------------------------------------////-----Order Blocks---------------------------------------------------------------////--------------------------------------------------------------------------------////Delete internal Order Blocksfor j=t downto 0 doif close < $itargetBot[j] and $itargetType[j]=1 then$itargetTop[j]=0$itargetBot[j]=0$itargetLeft[j]=0$itargetRight[j]=0$itargetType[j]=undefinedelsif close > $itargetTop[j] and $itargetType[j]=-1 then$itargetTop[j]=0$itargetBot[j]=0$itargetLeft[j]=0$itargetRight[j]=0$itargetType[j]=0endifnext//Delete structural Order Blocksfor j=r downto 0 doif close < $targetBot[j] and $targetType[j]=1 then$targetTop[j]=0$targetBot[j]=0$targetLeft[j]=0$targetRight[j]=0$targetType[j]=undefinedelsif close > $targetTop[j] and $targetType[j]=-1 then$targetTop[j]=0$targetBot[j]=0$targetLeft[j]=0$targetRight[j]=0$targetType[j]=0endifnext//Plot Order Blocks if not brokencountiOB=0countOB=0if islastbarupdate then//---Internal Order Blocksfor i=t downto 0 doif $itargetBot[i]=0 thencountiOB=countiOBelsecountiOB=countiOB+1if countiOB = showlastOB thenbreakelseif close > $itargetTop[i] thendrawrectangle($itargetLeft[i],$itargetBot[i],n+20,$itargetTop[i])coloured("green",0)fillcolor("green",50)elsif close <$itargetBot[i] thendrawrectangle($itargetLeft[i],$itargetBot[i],n+20,$itargetTop[i])coloured("red",0)fillcolor("red",50)elsedrawrectangle($itargetLeft[i],$itargetBot[i],n+20,$itargetTop[i])coloured("yellow",0)fillcolor("yellow",50)endifendifendifnext//---Structural Order Blocksfor i=r downto 0 doif $targetBot[i]=0 thencountOB=countOBelsecountOB=countOB+1if countOB = showlastOB thenbreakelseif close > $targetTop[i] thendrawrectangle($targetLeft[i],$targetBot[i],n+20,$targetTop[i])coloured("green",0)fillcolor("green",100)elsif close <$targetBot[i] thendrawrectangle($targetLeft[i],$targetBot[i],n+20,$targetTop[i])coloured("red",0)fillcolor("red",100)elsedrawrectangle($targetLeft[i],$targetBot[i],n+20,$targetTop[i])coloured("yellow",0)fillcolor("yellow",100)endifendifendifnext//-----------------------------------------------------------------------------////------Draw Pivot Points------------------------------------------------------////-----------------------------------------------------------------------------////---Pivot Highfor i=z downto 0 doif $phtype[i]=1 thendrawtext("HH",$phx[i],$phy[i]+atr*0.5)coloured("red")elsedrawtext("LH",$phx[i],$phy[i]+atr*0.5)coloured("red")endifnext//---Pivot Lowfor i=k downto 0 doif $pltype[i]=1 thendrawtext("LL",$plx[i],$ply[i]-atr*0.5)coloured("green")elsedrawtext("HL",$plx[i],$ply[i]-atr*0.5)coloured("green")endifnext//-----------------------------------------------------------------------------////------Draw Internal Structure------------------------------------------------////-----------------------------------------------------------------------------//for i=m downto 0 dodrawsegment($itopx[i],$itopy[i],$iright[i],$itopy[i])style(dottedline)coloured("green")if $ichoch[i]=1 thendrawtext("CHoCH",($itopx[i]+$iright[i])/2,$itopy[i]+0.25*atr)coloured("green")elsedrawtext("BOS",($itopx[i]+$iright[i])/2,$itopy[i]+0.25*atr)coloured("green")endifnext//-----------------------------------------------------------------------------//for i=s downto 0 dodrawsegment($ibotx[i],$iboty[i],$ibotright[i],$iboty[i])style(dottedline)coloured("red")if $ibotchoch[i]=1 thendrawtext("CHoCH",($ibotx[i]+$ibotright[i])/2,$iboty[i]-0.25*atr)coloured("red")elsedrawtext("BOS",($ibotx[i]+$ibotright[i])/2,$iboty[i]-0.25*atr)coloured("red")endifnext//-----------------------------------------------------------------------------////------Draw Structure---------------------------------------------------------////-----------------------------------------------------------------------------//for i=g downto 0 dodrawsegment($topx[i],$topy[i],$right[i],$topy[i])style(line)coloured("green")if $topchoch[i]=1 thendrawtext("CHoCH",($topx[i]+$right[i])/2,$topy[i]+0.25*atr)coloured("green")elsedrawtext("BOS",($topx[i]+$right[i])/2,$topy[i]+0.25*atr)coloured("green")endifnext//-----------------------------------------------------------------------------//for i=w downto 0 dodrawsegment($botx[i],$boty[i],$botright[i],$boty[i])style(line)coloured("red")if $botchoch[i]=1 thendrawtext("CHoCH",($botx[i]+$botright[i])/2,$boty[i]+0.25*atr)coloured("red")elsedrawtext("BOS",($botx[i]+$botright[i])/2,$boty[i]+0.25*atr)coloured("red")endifnext//-----------------------------------------------------------------------------////-----Top and Bottom extension------------------------------------------------////-----------------------------------------------------------------------------//if showhlswings thendrawsegment(traildnx,traildn,n+20,traildn)coloured("blue")drawsegment(trailupx,trailup,n+20,trailup)coloured("red")if trend>0 thendrawtext("Strong Low",(traildnx+n+20)/2,traildn-0.35*atr)coloured("blue")drawtext("Weak High",(trailupx+n+20)/2,trailup+0.35*atr)coloured("red")elsedrawtext("Weak Low",(traildnx+n+20)/2,traildn-0.35*atr)coloured("blue")drawtext("Strong High",(trailupx+n+20)/2,trailup+0.35*atr)coloured("red")endifendifendif//--------------------------------------------------------------------------------//returnIdeally, I’d like the system to buy on the next green heikin ashi candle after price enters a green order block. Stop loss would be set just below the green order block and Take profit would be 2:1 or the next red order block (whatever comes first). For selling the system would do the opposite.
Is this possible to do with this code or could anybody help?
Many thanks
Jim11/19/2025 at 1:36 PM #253758Here's an example to get you started.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133//------------------------------------// Estrategia SMC (Internal CHoCH)//------------------------------------DEFPARAM CumulateOrders = False//----- Variables / Parámetros -----length = 50ifilterConfluence = 0 // 1 = Activar filtro de confluencia//----- Inicialización de variables de estado -----ONCE os = 0ONCE os1 = 0ONCE itrend = 0 // 1 = Alcista, -1 = BajistaONCE topcross = 0ONCE botcross = 0ONCE itopcross = 0ONCE ibotcross = 0//----- Cálculo de Swings (Estructura Mayor) -----upper = highest[length](high)lower = lowest[length](low)IF high[length] > upper THENos = 0ELSIF low[length] < lower THENos = 1ELSEos = os[1]ENDIFIF os = 0 AND os[1] <> 0 THENmytop = high[length]ELSEmytop = 0ENDIFIF os = 1 AND os[1] <> 1 THENmybot = low[length]ELSEmybot = 0ENDIF//----- Cálculo de Swings (Estructura Interna - 5 periodos) -----upper1 = highest[5](high)lower1 = lowest[5](low)IF high[5] > upper1 THENos1 = 0ELSIF low[5] < lower1 THENos1 = 1ELSEos1 = os1[1]ENDIFIF os1 = 0 AND os1[1] <> 0 THENitop = high[5]ELSEitop = 0ENDIFIF os1 = 1 AND os1[1] <> 1 THENibot = low[5]ELSEibot = 0ENDIF//----- Gestión de Pivotes (Memoria del precio) -----// MayorIF mytop > 0 THENtopcross = 1topy = mytopENDIFIF mybot > 0 THENbotcross = 1boty = mybotENDIF// InternoIF itop > 0 THENitopcross = 1itopy = itopENDIFIF ibot > 0 THENibotcross = 1iboty = ibotENDIF//----- LÓGICA DE ESTRATEGIA (Internal CHoCH) -----// 1. Detección de ruptura Alcista InternabullConcordant = 1IF ifilterConfluence THENbullConcordant = high - max(close, open) > min(close, open - low)ENDIF// Condición: Cierre cruza por encima del último techo internoIF close CROSSES OVER itopy AND itopcross AND topy <> itopy AND bullConcordant THEN// Si la tendencia interna venía bajista (< 0), esto es un CHoCH AlcistaIF itrend < 0 THEN// ENTRADA LARGABUY 1 CONTRACT AT MARKETSET STOP %LOSS 5SET TARGET %PROFIT 5ENDIFitopcross = 0itrend = 1ENDIF// 2. Detección de ruptura Bajista InternabearConcordant = 1IF ifilterConfluence THENbearConcordant = high - max(close, open) < min(close, open - low)ENDIF// Condición: Cierre cruza por debajo del último suelo internoIF close CROSSES UNDER iboty AND ibotcross AND boty <> iboty AND bearConcordant THEN// Si la tendencia interna venía alcista (> 0), esto es un CHoCH BajistaIF itrend > 0 THEN// ENTRADA CORTASELLSHORT 1 CONTRACT AT MARKETSET STOP %LOSS 5SET TARGET %PROFIT 5ENDIFibotcross = 0itrend = -1ENDIF2 users thanked author for this post.
-
AuthorPosts
Viewing 2 posts - 1 through 2 (of 2 total)
Find exclusive trading pro-tools on 
Similar topics: