Hi Guys, hope everyone is well.. loving how informative this platform is..
Was curious if anyone would kindly help me code a condition as part of a strategy.. and that is to prevent orders being opened when they are in close proximity to a pivot point..
e.g. No longs within 5 points of R1, R2, R3
e.g. No Shorts within 5 points of S1, S2, S3
These are key reversal points, or where lots of bids are stacked and I find that majority of my losses come from when the automated system enters close to these.
Simply put, the strategy would be superior if somehow it could determine these areas.
Would love some help, albeit I am very very basic, only just picked this up last week so apologies in advance.
The below code could work for this purpose, I let you analyze it, i did not test it, it is just a quick rough version:
DEFPARAM Cumulateorders=false
dist = 5 //distance from pivot points s/r
x = dist*pointsize
//find proximity of pivot points
Ht = DHigh(1)
Bs = DLow(1)
C = DClose(1)
Pivot = (Ht + Bs + C) / 3
Res3 = Res1 + (Ht - Bs)
Res2 = Pivot + Ht - Bs
Res1 = (2 * Pivot) - Bs
Sup1 = (2 * Pivot) - Ht
Sup2 = Pivot - (Ht - Bs)
Sup3 = Sup1 - (Ht - Bs)
prox=0
if close>pivot then
//** above Pivot **
i=1
while i<=2 do
if i=1 then
floor=res1
ceil=res2
if (close>floor-x and close<floor+x) or (close>ceil-x and close<ceil+x) then
prox=1
break
endif
elsif i=3 then
floor=res2
ceil=res3
if (close>floor-x and close<floor+x) or (close>ceil-x and close<ceil+x) then
prox=1
break
endif
endif
i=i+1
wend
elsif close<pivot then
//** below Pivot **
i=1
while i<=2 do
if i=1 then
floor=sup2
ceil=sup1
if (close>floor-x and close<floor+x) or (close>ceil-x and close<ceil+x) then
prox=1
break
endif
elsif i=2 then
floor=sup3
if (close>floor-x and close<floor+x) or (close>ceil-x and close<ceil+x) then
prox=1
break
endif
endif
i=i+1
wend
endif
// dummy strategy
a=rsi[14] crosses over 50 and not prox
IF a and not longonmarket THEN
BUY 1 CONTRACT AT market
set target pprofit 10
set stop ploss 5
ENDIF
graphonprice floor coloured(0,200,200)
graphonprice ceil coloured(200,0,0)
graph prox
Above added as Log 233 here …
Snippet Link Library
Thank you for beating me to it Nicolas! 🙂
Hello,
I just tried to use this code, but the system displays an error message of line 25 and suggests that I use endif rather than floor, which does not make sense.
Do you know if this code works?
Thanks !
Hi,
floor (and ceil too, ligne 26) were introduced recently as reserved keywords of the language, year of this code 2020 sounds old enough to be “before” they became reserved keywords, so they used to be ok as variable names but not anymore. They have to be changed to other names of your choice, for example you could replace them with “myfloor” and “myceil”.
Thank you indeed, but this code does not in fact give what I want.
Actually, I think my problem is more complex.
I am looking for a code that allows me to put a blocking condition so as not to enter position if my price is too close to a pivot point, say a distance of X points. I would like to consider the Daily Weekly and Monthly pivot points.
The code that I have just created attached does not work, because I do not know how to code how the different levels are positioned between them, and to determine those which frame my price at the moment.
Indeed, the order of the pivot points is then no longer at all obvious and my price can be found for example S1 Daily and S2 Weekly depending on the configuration of the moment.
I hope my question is clear, and if anyone can help me out that would be great.
Thanks!
//find proximity of pivot points journalier
Ht = High(1)
Bs = DLow(1)
C = DClose(1)
Pivot = (Ht + Bs + C) / 3
Res3 = Res1 + (Ht - Bs)
Res2 = Pivot + Ht - Bs
Res1 = (2 * Pivot) - Bs
Sup1 = (2 * Pivot) - Ht
Sup2 = Pivot - (Ht - Bs)
Sup3 = Sup1 - (Ht - Bs)
midres1 = (res1+pivot)/2
midres2= (res2+res1)/2
midres3= (res3+res2)/2
midsup1=(sup1+pivot)/2
midsup2=(sup2+sup1)/2
midsup3=(sup3+sup2)/2
if close > pivot and close < midres1 then
myceil=midres1
myfloor=pivot
elsif close > midres1 and close < res1 then
myceil=res1
myfloor=midres1
elsif close > res1 and close < midres2 then
myceil=midres2
myfloor=res1
elsif close > midres2 and close < res2 then
myceil=res2
myfloor=midres2
elsif close > res2 and close < midres3 then
myceil=midres3
myfloor=res2
elsif close > midres3 and close < res3 then
myceil=res3
myfloor=midres3
elsif close >sup3 and close < midsup3 then
myceil=midsup3
myfloor=sup3
elsif close >midsup3 and close < sup2 then
myceil=sup2
myfloor=midsup3
elsif close >sup2 and close < midsup2 then
myceil=midsup2
myfloor=sup2
elsif close >midsup2 and close < sup1 then
myceil=sup1
myfloor=midsup2
elsif close >sup1 and close < midsup1 then
myceil=midsup1
myfloor=sup1
elsif close >midsup1 and close < pivot then
myceil=pivot
myfloor=midsup1
endif
return myfloor, myceil
Much better approach with help of variables in array. We store all pivot points into an array, and the fetch it to check if the current price is near of the pivot point, if that’s the case we do not allow any order to be opened.
You can fill the array $p with any other pivot points from any other time horizons (such as weekly, monthly, ..)
DEFPARAM Cumulateorders=false
dist = 5 //distance from pivot points s/r
x = dist*pointsize
//find proximity of pivot points
Ht = DHigh(1)
Bs = DLow(1)
C = DClose(1)
Pivot = (Ht + Bs + C) / 3
Res3 = Res1 + (Ht - Bs)
Res2 = Pivot + Ht - Bs
Res1 = (2 * Pivot) - Bs
Sup1 = (2 * Pivot) - Ht
Sup2 = Pivot - (Ht - Bs)
Sup3 = Sup1 - (Ht - Bs)
//using arrays to store pivot points
$p[0]= Pivot
$p[1] = Res3
$p[2] = Res2
$p[3] = Res1
$p[4] = Sup1
$p[5] = Sup2
$p[6] = Sup3
//loop into the array to check if price is too near
tradeok = 1
for i = 0 to lastset($p)
if abs(close-$p[i])<=x then //near a pivot point
tradeok=0 //no trading allowed!!
break
endif
next
// dummy strategy
if tradeok then
a=rsi[14] crosses over 50
IF a and not longonmarket THEN
BUY 1 CONTRACT AT market
set target pprofit 10
set stop ploss 5
ENDIF
endif
graph tradeok as "no pivot within range"