Hi all,
Quite an unusual request for code review here.
I tried to replicate a script which works very well on TV platform.
Though I understand the highlevel algo, I am unable to understand rational behind each step in the code. Mainly the exit criterias.
I have done my best with my rookie PRT coding knowledge to replicate the same.
I request you to please check the following –
- Is the code conversion correct?
- Are there any better ways to make the code concise?
- Why does the trade close within 1 candle?
Things to note –
- Zigzag indicator is not supported in ProOrder to had to code it ( not sure if that’s correct )
- Entry and exist criterias are based on Fibo levels.
DefParam cumulateOrders = true
isUp = close >=open
isDown = close <=open
//direction
if isUp[1] and isDown then
direction = -1
elsif isDown[1] and isUp then
direction = 1
//else
//direction = direction[1]
endif
//zigzag
if isUp[1] and isDown and direction[1] <> -1 then
zigzagF = high[2]
elsif isDown[1] and isUp and direction[1] <> 1 then
zigzagF = low[2]
endif
x = zigzagF[4]
a = zigzagF[3]
b = zigzagF[2]
c = zigzagF[1]
d = zigzagF[0]
xab = (abs(b-a)/abs(x-a))
xad = (abs(a-d)/abs(x-a))
abc = (abs(b-c)/abs(a-b))
bcd = (abs(c-d)/abs(b-c))
bc = d < c
sc = d > c
// isBAT
Batxab = xab >= 0.382 and xab <= 0.5
Batabc = abc >= 0.382 and abc <= 0.886
Batbcd = bcd >= 1.618 and bcd <= 2.618
Batxad = xad <= 0.618 and xad <= 1.000 // 0.886
isBat = Batxab and Batabc and Batbcd and Batxad
// isAntiBat
AntiBatxab = xab >= 0.500 and xab <= 0.886 // 0.618
AntiBatabc = abc >= 1.000 and abc <= 2.618 // 1.13 --> 2.618
AntiBatbcd = bcd >= 1.618 and bcd <= 2.618 // 2.0 --> 2.618
AntiBatxad = xad >= 0.886 and xad <= 1.000 // 1.13
isAntiBat = AntiBatxab and AntiBatabc and AntiBatbcd and AntiBatxad
// isAltBat
AltBatxab = xab <= 0.382
AltBatabc = abc >= 0.382 and abc <= 0.886
AltBatbcd = bcd >= 2.0 and bcd <= 3.618
AltBatxad = xad <= 1.13
isAltBat = AltBatxab and AltBatabc and AltBatbcd and AltBatxad
// isButterfly
Butterflyxab = xab <= 0.786
Butterflyabc = abc >= 0.382 and abc <= 0.886
Butterflybcd = bcd >= 1.618 and bcd <= 2.618
Butterflyxad = xad >= 1.27 and xad <= 1.618
isButterfly = Butterflyxab and Butterflyabc and Butterflybcd and Butterflyxad
// isAntiButterfly
AntiButterflyxab = xab >= 0.236 and xab <= 0.886 // 0.382 - 0.618
AntiButterflyabc = abc >= 1.130 and abc <= 2.618 // 1.130 - 2.618
AntiButterflybcd = bcd >= 1.000 and bcd <= 1.382 // 1.27
AntiButterflyxad = xad >= 0.500 and xad <= 0.886 // 0.618 - 0.786
isAntiButterfly = AntiButterflyxab and AntiButterflyabc and AntiButterflybcd and AntiButterflyxad
// isABCD
ABCDabc = abc >= 0.382 and abc <= 0.886
ABCDbcd = bcd >= 1.13 and bcd <= 2.618
isABCD = ABCDabc and ABCDbcd
// isGartley
Gartleyxab = xab >= 0.5 and xab <= 0.618 // 0.618
Gartleyabc = abc >= 0.382 and abc <= 0.886
Gartleybcd = bcd >= 1.13 and bcd <= 2.618
Gartleyxad = xad >= 0.75 and xad <= 0.875 // 0.786
isGartley = Gartleyxab and Gartleyabc and Gartleybcd and Gartleyxad
// isAntiGartley
AntiGartleyxab = xab >= 0.500 and xab <= 0.886 // 0.618 -> 0.786
AntiGartleyabc = abc >= 1.000 and abc <= 2.618 // 1.130 -> 2.618
AntiGartleybcd = bcd >= 1.500 and bcd <= 5.000 // 1.618
AntiGartleyxad = xad >= 1.000 and xad <= 5.000 // 1.272
isAntiGartley = AntiGartleyxab and AntiGartleyabc and AntiGartleybcd and AntiGartleyxad
// isCrab
Crabxab = xab >= 0.500 and xab <= 0.875 // 0.886
Crababc = abc >= 0.382 and abc <= 0.886
Crabbcd = bcd >= 2.000 and bcd <= 5.000 // 3.618
Crabxad = xad >= 1.382 and xad <= 5.000 // 1.618
isCrab = Crabxab and Crababc and Crabbcd and Crabxad
// isAntiCrab
AntiCrabxab = xab >= 0.250 and xab <= 0.500 // 0.276 -> 0.446
AntiCrababc = abc >= 1.130 and abc <= 2.618 // 1.130 -> 2.618
AntiCrabbcd = bcd >= 1.618 and bcd <= 2.618 // 1.618 -> 2.618
AntiCrabxad = xad >= 0.500 and xad <= 0.750 // 0.618
isAntiCrab = AntiCrabxab and AntiCrababc and AntiCrabbcd and AntiCrabxad
// isShark
Sharkxab = xab >= 0.500 and xab <= 0.875 // 0.5 --> 0.886
Sharkabc = abc >= 1.130 and abc <= 1.618 //
Sharkbcd = bcd >= 1.270 and bcd <= 2.240 //
Sharkxad = xad >= 0.886 and xad <= 1.130 // 0.886 --> 1.13
isShark = Sharkxab and Sharkabc and Sharkbcd and Sharkxad
// isAntiShark
AntiSharkxab = xab >= 0.382 and xab <= 0.875 // 0.446 --> 0.618
AntiSharkabc = abc >= 0.500 and abc <= 1.000 // 0.618 --> 0.886
AntiSharkbcd = bcd >= 1.250 and bcd <= 2.618 // 1.618 --> 2.618
AntiSharkxad = xad >= 0.500 and xad <= 1.250 // 1.130 --> 1.130
isAntiShark = AntiSharkxab and AntiSharkabc and AntiSharkbcd and AntiSharkxad
// is5o
Five0xab = xab >= 1.13 and xab <= 1.618
Five0abc = abc >= 1.618 and abc <= 2.24
Five0bcd = bcd >= 0.5 and bcd <= 0.625 // 0.5
Five0xad = xad >= 0.0 and xad <= 0.236 // negative?
is5o = Five0xab and Five0abc and Five0bcd and Five0xad
// isWolf
Wolfxab = xab >= 1.27 and xab <= 1.618
Wolfabc = abc >= 0 and abc <= 5
Wolfbcd = bcd >= 1.27 and bcd <= 1.618
Wolfxad = xad >= 0.0 and xad <= 5
isWolf = Wolfxab and Wolfabc and Wolfbcd and Wolfxad
// isHnS
HnSxab = xab >= 2.0 and xab <= 10
HnSabc = abc >= 0.90 and abc <= 1.1
HnSbcd = bcd >= 0.236 and bcd <= 0.88
HnSxad = xad >= 0.90 and xad <= 1.1
isHnS = HnSxab and HnSabc and HnSbcd and HnSxad
// isConTria
Contriaxab = xab >= 0.382 and xab <= 0.618
Contriaabc = abc >= 0.382 and abc <= 0.618
Contriabcd = bcd >= 0.382 and bcd <= 0.618
Contriaxad = xad >= 0.236 and xad <= 0.764
isConTria = Contriaxab and Contriaabc and Contriabcd and Contriaxad
// isExpTria
ExpTriaxab = xab >= 1.236 and xab <= 1.618
ExpTriaabc = abc >= 1.000 and abc <= 1.618
ExpTriabcd = bcd >= 1.236 and bcd <= 2.000
ExpTriaxad = xad >= 2.000 and xad <= 2.236
isExpTria = ExpTriaxab and ExpTriaabc and ExpTriabcd and ExpTriaxad
allPatterns = isBAT or isAntiBat or isAltBat or isButterfly or isAntiButterfly or isABCD or isGartley or isAntiGartley or isCrab or isAntiCrab or isShark or isAntiShark or is5o or isWolf or isHnS or isConTria or isExpTria
bullish = allPatterns and bc
bearish = allPatterns and sc
targetewrate = 0.236
targettprate = 0.618
targetslrate = 0.236
//fib calculation
fibrange = abs(d-c)
if sc then
flastfibew = d-(fibrange*targetewrate)
flastfibtp = d-(fibrange*targettprate)
flastfibsl = d-(fibrange*targetslrate)
else
flastfibew = d+(fibrange*targetewrate)
flastfibtp = d+(fibrange*targettprate)
flastfibsl = d+(fibrange*targetslrate)
endif
//
targetbuyentry = bullish and close <= flastfibew
targetbuyclose = high >= flastfibtp or low <= flastfibsl
targetselentry = bearish and close >= flastfibew
targetselclose = low <= flastfibtp or high >= flastfibsl
//// Conditions to enter long positions
IF NOT LongOnMarket AND targetbuyentry THEN
BUY 1 CONTRACTS AT MARKET
SET STOP %Loss 2
ENDIF
// Conditions to exit long positions
If LongOnMarket AND targetbuyclose THEN
SELL AT MARKET
ENDIF
//// Conditions to enter short positions
IF NOT ShortOnMarket AND targetselentry THEN
SELLSHORT 1 CONTRACTS AT MARKET
SET STOP %Loss 2
ENDIF
//// Conditions to exit short positions
IF ShortOnMarket AND targetselclose THEN
EXITSHORT AT MARKET
ENDIF