ProRealCode - Trading & Coding with ProRealTime™
Hello guys! Just like to get your help with the backtesting codes I have created recently. It is based from the MACD/RSI divergence that I’ve read months before on one of the threads here (by Nicholas I believe). I slowly coded an indicator that would show the price divergence versus the MACD indicator based on my own conditions.
I am now very satisfied with the indicator I created and also uses it for screening stocks with divergences. I placed my entry conditions and added trend lines in my codes to provide exit signals. I tried running it in Probacktest but it is giving me parse error. I saw a post on how to workaround it by changing platform timezones but I have no luck.
I have here my program for everyone to check and give a feedback on. I am new in trading and automatic trading. This is the strategy that makes sense to me at the moment and I hope that you all could help me bring this to work to see how profitable it would be to use in realtime.
Much thanks and appreciation to those who would be so kind to check this post and help! <3
//BACKTESTING DiverLineBull Part3// ######################################################################################################
//Variable Declarations
obLevel = 65 //20210308
oslevel = 40 //20210308
//Indicator Declarations
indMACDLn = MACDline[12,26,9](close)
indMACDSign = MACDSignal[12,26,9](close)
indMACDHis = MACD[12,26,9](close)
indRSI = RSI[14](close)
indRSI30 = RSI[30](close)
//Conditions to Reset
ob = indRSI>obLevel //20210308
oS = indRSI<osLevel //20210308
osTL = indRSI < 30
condMACDLnOver0 = indMACDLn crosses over 0
condMACDLnUnder0 = indMACDLn crosses under 0
//BULLISH MACD RSI DIVERGENCE //###################################################
//Conditions to Start
if barindex = 11 then
InterPriceMin=100000
InterMACDLnMin =100000
oldPriceMin = 100000
oldMACDLnMin=100000
InterBIMin = 0
endif
If condMACDLnUnder0 then
PriceMin=100000
MACDLnMin =100000
BIStartBull = barindex
EntrySignalBull =0
SCANBull = 1
ospassed =0
SCANBULLTL =1
endif
if SCANBull = 1 and barindex >= BIStartBull and os then
//Get min PriceMin, MACDLn
PriceMin = min(PriceMin, Low)
if PriceMin <> PriceMin[1] then
PriceMinBI = barindex
PriceMinMACDLn = indMACDLn
endif
MACDLnMin =min(MACDLnMin,indMACDLn)
if MACDLnMin <> MACDLnMin[1] then
BIMin = barindex
endif
if MACDLnMin < PriceMinMACDLn then
BIMin = PriceMinBI
endif
ospassed = 1
endif
// Check for bullish crossover and divergences
If SCANBull=1 and condMACDLnOver0 and ospassed = 1 then
oldPriceMin = InterPriceMin
InterPriceMin =PriceMin
oldMACDLnMin = InterMACDLnMin
InterMACDLnMin =MACDLnMin
oldBIMin = InterBImin
InterBImin = BIMin
If BullPosition = 0 then
//declare divergence condition
BullishDivMACD = (InterPriceMin < oldPriceMin ) AND (InterMACDLnMin > oldMACDLnMin)AND (BIMin > BIStartBull)
if BullishDivMACD then
BIDivBull = barindex
//drawsegment(oldBIMin,oldMACDLnMin,BIMin,MACDLnMin) coloured(0,100,0)//green
//drawarrowup(BIMin,MACDLnMin) coloured(0,100,0)//green
SCANBull=0
x1orig = InterBImin
y1orig = InterPriceMin
y1MACDorig = InterMACDLnMin
//drawarrowdown(x1orig,y1MACDorig) coloured(0,255,255)//aquamarine
TrendlineStart = 1
InterPriceMin=100000
InterMACDLnMin =100000
oldPriceMin = 100000
oldMACDLnMin=100000
mslopeorig = 1000000
mslopeorig1 = 1000000
mslopeorig2 = 1000000
mslopeimmediate= 1000000
mslopeimmediateold= 1000000
VerifyEntryPoint =1
EnterAPosition =1
ScanExitBull =0
ValidTP = 0
TPcounter =0
endif
ospassed = 0
endif
endif
//#############################################################################################
//Trendline tracking start here
If condMACDLnUnder0 AND BullPosition = 1 AND barindex >= EntrySignalBIBull then
PriceMin=100000
MACDLnMin =100000
BIStartBull = barindex
osTLpassed =0
SCANBULLTL =1
endif
IF SCANBULLTL =1 AND TrendlineStart = 1 AND BIStartBull >= BIDivBull AND osTL then
//Get min PriceMin, MACDLn
PriceMin = min(PriceMin, Low)
if PriceMin <> PriceMin[1] then
PriceMinBI = barindex
PriceMinMACDLn = indMACDLn
endif
MACDLnMin =min(MACDLnMin,indMACDLn)
if MACDLnMin <> MACDLnMin[1] then
BIMin = barindex
endif
if MACDLnMin < PriceMinMACDLn then
BIMin = PriceMinBI
endif
osTLpassed =1
ENDIF
//counting bearish divergences and X2 plotting
If barindex >= EntrySignalBIBull AND BullPosition = 1 AND condMACDLnOver0 AND osTLpassed =1 then
oldPriceMin = InterPriceMin
InterPriceMin =PriceMin
oldMACDLnMin = InterMACDLnMin
InterMACDLnMin =MACDLnMin
oldBIMin = InterBImin
InterBImin = BIMin
if (oldPriceMin > InterPriceMin) AND (InterMACDLnMin > OldMACDLnMin) then
//drawarrowdown(OldBIMin,OldMACDLnMin) coloured(0,255,255)//aquamarine
checkTP = 1
endif
IF CountBearDiver <1 then
mslopeorigold= mslopeorig
x1diver =x1orig
y1diver = y1orig
y1MACDLndiver =y1MACDorig
oldx2 =oldBIMin //previous scan under macdln
oldy2= oldPriceMin
oldy2MACDLn = oldMACDLnMin
newx2=InterBImin
newy2=InterPriceMin
newy2MACDLn=InterMACDLnMin
mslopeorig1 = (oldy2-y1diver) / (oldx2-x1diver)
mslopeorig2 = (newy2-y1diver) / (newx2-x1diver)
mslopeorigold = mslopeorig
if mslopeorig2 < mslopeorig1 AND mslopeorig1 > 0 then //if new diver slope is less than the original
mslopeorig =mslopeorig2
x2orig = newx2
y2orig = newy2
y2MACDLnorig=newy2MACDLn
//TrendlineEnd = 0
mslope =mslopeorig
TrendlineEnd = 0
runningBI = barindex
else
//if new diver slope is less than the original slope then calculate the immediate slope, choose the lowest slope
mslopeorig =min(mslopeorig2,mslopeorigold)
x1imm =x2orig //previous scan under macdln with os
y1imm = y2orig
y1MACDLnimm =y2MACDLnorig
x2imm= InterBImin
y2imm=InterPriceMin
y2MACDLnimm =InterMACDLnMin
mslopeimmediate =(y2imm-y1imm) / (x2imm-x1imm) //to check orientation of slope (neg or pos)
mslope =MIN(mslopeimmediate,mslopeorig)
checkTP = 1
if TPcounter > 1 then
checkTP = 1
ValidTP = 1
endif
if barindex > TPProjectioncheckBI and TPProjectioncheck = 1 then
//drawarrowdown(barindex,indMACDLn) coloured(210, 105, 30) //chocolate for TP
TPProjectioncheck = 0
TPcounter = TPcounter + 1
endif
endif
x1anchor = x1imm
y1anchor = y1imm
y1MACDLnanchor =y1MACDLnimm
//TrendlineEnd = 0
endif
//If barindex >= EntrySignalBIBull AND BullPosition = 1 AND CountBearDiver =1 AND condMACDLnOver0 AND osTLpassed =1 then
IF CountBearDiver >=1 then
If TrendlineEnd = 0 then
//getting the immediate slope, mslopeimmediate
mslopeimmediateold = mslope
x1imm =x1anchor //previous scan under macdln with os
y1imm = y1anchor
y1MACDLnimm =y1MACDLnanchor
x2imm= InterBImin
y2imm=InterPriceMin
y2MACDLnimm =InterMACDLnMin
mslopeimmediate =(y2imm-y1imm) / (x2imm-x1imm) //to check orientation of slope (neg or pos)
//drawtext ("mslopeimmediateold = #mslopeimmediateold#",x2imm,y2MACDLnimm) //
//drawtext ("mslopeimmediate = #mslopeimmediate#",x2imm,y2MACDLnimm) //
//drawarrowdown(x1,y1MACDLn) coloured(0,255,255)//aquamarine
//drawarrowdown(x2imm,y2MACDLnimm) coloured(250,235,215)//antiquewhite
mslope = min(mslopeimmediate,mslopeimmediateold)
//drawtext ("mslopeimmediateold = #mslopeimmediateold#",x2imm,y2MACDLnimm) //
x1 = x1anchor
y1= y1anchor
y1MACDLn= y1MACDLnanchor
TrendlineEnd = 1
TrendlineEndBI = barindex
//drawarrowdown(x1,y1MACDLn) coloured(0,255,255)//aquamarine
//drawtext ("mslopeimmediate = #mslopeimmediate#",x1,y1MACDLn) //-0.0012
//drawsegment(x1,y1MACDLn,x2,y2MACDLn) coloured(0,255,255)// aquamarine
endif//end of first CountBearDiver =1
//start of CountBearDiver =1 with trendlineend=1 condition
IF TrendlineEnd = 1 AND barindex >= TrendlineEndBI then
mslopeimmediateold = mslopeimmediate
//getting the immediate slope, mslopeimmediate
x1 = x1anchor
y1= y1anchor
y1MACDLn= y1MACDLnanchor
x2 = InterBImin
y2 = InterPriceMin
y2MACDLn = InterMACDLnMin
mslopeimmediate = (y2-y1)/(x2-x1)
//drawarrowdown(x1anchor,y1MACDLnanchor) coloured(250,235,215)//antiquewhite
//drawtext ("mslopeimmediateold = #mslopeimmediateold#",x1,y1MACDLn)
//mslopetransferold = mslopetransfer
if mslopeimmediate < mslopeimmediateold AND mslopeimmediateold > 0 then //
mslope = mslopeimmediate
mslopetransferBI = barindex
//drawarrowdown(x2,y2MACDLn) coloured(0,255,255)//aquamarine
x1transfer = x1imm
y1transfer = y1imm
y1MACDLntransfer =y1MACDLnimm
x2transfer = x2imm
y2transfer = y2imm
y2MACDLntransfer=y2MACDLnimm
//drawtext ("mslopetransfer = #mslopetransfer#",x2,y2MACDLn)
//drawsegment(x1transfer,y1MACDLntransfer,x2transfer,y2MACDLntransfer) coloured(0,255,255)// aquamarine
checkTP = 1
else
if mslopeimmediate > mslopeimmediateold AND barindex >=TPProjectioncheckBI then
checkTP = 1
ValidTP = 1
mslope = mslopeimmediateold
endif
endif
//checkTP = 1
endif
endif
TPIntercept = HighestDetected - mslope*(BIHighestDetected)
TPProjection = mslope*(barindex) + TPIntercept
//tryBI = barindex
//drawtext ("TPIntercept = #TPIntercept#",BIHighestDetected,y2MACDLnimm)
//drawtext ("TPProjection = #TPProjection#",x2imm,y2MACDLnimm)
//drawarrowdown(BIHighestDetected,y2MACDLnimm) coloured(0,255,255)//aquamarine
endif
//#########################################################################################3
//conditions to enter LONG POSITION
//verify entry point
If (barindex >= BIDivBull) and VerifyEntryPoint=1 then
if LOW < y1orig then
VerifyEntryPoint = 0
SCANBULLTL =0
endif
endif
//endif
//If (barindex >= BIDivBull) and (VerifyEntryPoint =1 AND TrendlineStart = 1) AND (indRSI crosses over 50 AND indRSI < 56) AND EnterAPosition =1 Then
If (barindex >= BIDivBull) and (VerifyEntryPoint =1 AND TrendlineStart = 1) AND ScanExitBull =0 AND (indRSI crosses over 50 AND indRSI < 56) AND EnterAPosition =1 Then
// Conditions to enter long positions
IF NOT LongOnMarket THEN
BUY 2 CONTRACTS AT MARKET
ENDIF
EntrySignalBull= ABS(indMACDLn)
EntrySignalBIBull = barindex
//drawarrowup(EntrySignalBIBull,EntrySignalBull) coloured(255,192,203)//flesh colour
BullPosition = 1
BullishDivMACD =0
RemoveSignalBull = 1
ScanExitBull = 1
ENDIF
if barindex > EntrySignalBIBull and RemoveSignalBull = 1 then
EntrySignalBull = 0
RemoveSignalBull = 0
EnterAPosition =0
endif
//######################################################
//####################################################################################
//Conditions to Start Bearish
if barindex = 11 then
InterPriceMax=0
InterMACDLnMax =0
oldPriceMax = 0
oldMACDLnMax=0
InterBIMax = 0
HighestDetected =0
endif
If condMACDLnOver0 then
PriceMax=0
MACDLnMax =0
BIStartBear = barindex
EntrySignalBear =0
SCANBear = 1
obpassed =0
endif
if SCANBear = 1 and barindex >BIStartBear and ob then
//Get max of Price
PriceMax = max(PriceMax, High)
if PriceMax <> PriceMax[1] then
PriceMaxBI = barindex
PriceMaxMACDLn = indMACDLn
endif
//Get max MACDLn
MACDLnMax =max(MACDLnMax,indMACDLn)
if MACDLnMax <> MACDLnMax[1] then
BIMax = barindex
endif
if MACDLnMax > PriceMaxMACDLn then
BIMax = PriceMaxBI
endif
obpassed = 1
if TrendlineStart = 1 then
HighestDetected = max(HighestDetected, High)
if HighestDetected <> HighestDetected[1] then
BIHighestDetected = barindex
endif
endif
endif
// Check for bearish crossover and divergences
If SCANBear=1 and condMACDLnUnder0 and obpassed = 1 then
oldPriceMax = InterPriceMax
InterPriceMax =PriceMax
oldMACDLnMax = InterMACDLnMax
InterMACDLnMax =MACDLnMax
oldBIMax = InterBImax
InterBImax = BIMax
if barindex >= BIHighestDetected and InterPriceMax >= HighestDetected then
BearishDivMACD = (InterPriceMax >= oldPriceMax) AND (InterMACDLnMax < oldMACDLnMax) AND (BIMax > BIStartBear)
endif
if BearishDivMACD then
BIDivBear = BImax
SCANBear=0
InterPriceMax=0
InterMACDLnMax =0
oldPriceMax = 0
oldMACDLnMax=0
endif
obpassed = 0
endif
If (barindex >= BIDivBear) and BearishDivMACD THEN
EntrySignalBear = ABS(indMACDLn)
EntrySignalBIBear = barindex
if barindex >= EntrySignalBIBear and BullPosition = 1 then
CountBearDiver = CountBearDiver +1
//drawtext ("C = #CountBearDiver#" , barindex, indMACDLn)
endif
ExecutedBearishDivMACD = 1
ExecutedBearishBI = barindex
BearishDivMACD =0
RemoveSignalBear = 1
ENDIF
if barindex > EntrySignalBIBear and RemoveSignalBear = 1 then
EntrySignalBear = 0
RemoveSignalBear = 0
endif
//#############################################################################
//TRENDLINE SCAN FOR BREAK
//If barindex > BIDivBear AND TrendlineEnd = 1 AND TrendlineStart = 1 AND VerifyEntryPoint = 1 AND BullPosition = 1 then
If barindex > TrendlineEndBI AND TrendlineEnd=1 AND TrendlineStart = 1 AND VerifyEntryPoint = 1 AND BullPosition = 1 then
yintercept = (y1 - (mslope*x1))
ybreak = (mslope*barindex) + yintercept
if CLOSE crosses under ybreak OR CLOSE < ybreak Then
// Conditions to exit long positions
If LongOnMarket THEN
SELL AT MARKET
ENDIF
ScanTrendline = 0
TrendlineEnd = 0
TrendlineStart = 0
ScanExitBull=0
ScanEndPoint =0
CountBearDiver =0
BullPosition = 0
HighestDetected =0
SCANBULLTL =0
checkTP = 0
endif
endif
If barindex > TrendlineEndBI AND TrendlineStart = 1 AND VerifyEntryPoint = 1 AND BullPosition = 1 AND checkTP = 1 AND ValidTP = 1 then
if CLOSE crosses over TPProjection or CLOSE > TPProjection Then
// Conditions to exit long positions
If LongOnMarket THEN
SELL AT MARKET
ENDIF
ScanTrendline = 0
TrendlineEnd = 0
TrendlineStart = 0
ScanExitBull=0
ScanEndPoint =0
CountBearDiver =0
BullPosition = 0
HighestDetected =0
SCANBULLTL =0
checkTP = 0
ValidTP =0
endif
endif
///TPProjection status
if CLOSE crosses over TPProjection or CLOSE > TPProjection Then
TPProjectioncheck = 1
TPProjectioncheckBI = barindex
endif
//if barindex > TPProjectioncheckBI and TPProjectioncheck = 1 then
////drawarrowdown(barindex,indMACDLn) coloured(210, 105, 30) //chocolate for TP
//TPProjectioncheck = 0
//TPcounter = TPcounter + 1
//endif
if barindex > EntrySignalBIBull and ScanExitBull=1 then
if VerifyEntryPoint=0 then
ExitBullBI = barindex
ExitSignalBull = ABS(indMACDLn)
// Conditions to exit long positions
If LongOnMarket THEN
SELL AT MARKET
ENDIF
IF VerifyEntryPoint=0 THEN
x1 = x1orig //from bullish diver
y1 = y1orig
y1MACDLn = y1MACDorig
x2 =ExitBullBI
//y2 = InterPricemin
y2MACD = indMACDLn
ENDIF
ScanTrendline = 0
TrendlineEnd = 0
TrendlineStart = 0
ScanExitBull=0
ScanEndPoint =0
CountBearDiver =0
BullPosition = 0
ScanExitBull =0
HighestDetected =0
SCANBULLTL =0
checkTP = 0
ValidTP = 0
//endif
endif
endif
//
//
//return indMACDLn as "MACDLine",indMACDSign as "MACDSign",indMACDHis as "MACDHistogram", 0 coloured(168,168,168) style(line,2) as"0 level"//,EntrySignalBull coloured(255,192,203) style (histogram) as "Enter here bullish",EntrySignalBear coloured(160,75,180) style (histogram) as "Enter here bearish"
If it’s any consolation, I get the same Parsing Error! I tried a few things, but as yet no fix.
You have put a lot of work into the strategy / code … I hope others add their pearls of wisdom and experience and we can get a fix for you!
In the meantime send a Technical Report via Help > Help Center > I’m having a Technical Problem. Please let us know any fix you get.
You could try reading and trying some of the suggestions on the link below …
Hi @Grahal! Thanks so much for the advise. I will send a technical report for this ^^.
Hi,
I just like to update this query of mine.
The prorealtime help team got back to me and found the bug in my program.
They said that the variables starting with “REM” are making the parsing error. This “REM” is a reserved word in prorealtime as I understood from their reply.
Hope this helps everyone who are encountering the same issue. ^^
Regards!
Pls post code that works. Thanks
As requested:
//BACKTESTING DiverLineBull Part3// ######################################################################################################
//Variable Declarations
obLevel = 65 //20210308
oslevel = 40 //20210308
//Indicator Declarations
indMACDLn = MACDline[12,26,9](close)
indMACDSign = MACDSignal[12,26,9](close)
indMACDHis = MACD[12,26,9](close)
indRSI = RSI[14](close)
indRSI30 = RSI[30](close)
//Conditions to Reset
ob = indRSI>obLevel //20210308
oS = indRSI<osLevel //20210308
osTL = indRSI < 30
condMACDLnOver0 = indMACDLn crosses over 0
condMACDLnUnder0 = indMACDLn crosses under 0
//BULLISH MACD RSI DIVERGENCE //###################################################
//Conditions to Start
if barindex = 11 then
InterPriceMin=100000
InterMACDLnMin =100000
oldPriceMin = 100000
oldMACDLnMin=100000
InterBIMin = 0
endif
If condMACDLnUnder0 then
PriceMin=100000
MACDLnMin =100000
BIStartBull = barindex
EntrySignalBull =0
SCANBull = 1
ospassed =0
SCANBULLTL =1
endif
if SCANBull = 1 and barindex >= BIStartBull and os then
//Get min PriceMin, MACDLn
PriceMin = min(PriceMin, Low)
if PriceMin <> PriceMin[1] then
PriceMinBI = barindex
PriceMinMACDLn = indMACDLn
endif
MACDLnMin =min(MACDLnMin,indMACDLn)
if MACDLnMin <> MACDLnMin[1] then
BIMin = barindex
endif
if MACDLnMin < PriceMinMACDLn then
BIMin = PriceMinBI
endif
ospassed = 1
endif
// Check for bullish crossover and divergences
If SCANBull=1 and condMACDLnOver0 and ospassed = 1 then
oldPriceMin = InterPriceMin
InterPriceMin =PriceMin
oldMACDLnMin = InterMACDLnMin
InterMACDLnMin =MACDLnMin
oldBIMin = InterBImin
InterBImin = BIMin
If BullPosition = 0 then
//declare divergence condition
BullishDivMACD = (InterPriceMin < oldPriceMin ) AND (InterMACDLnMin > oldMACDLnMin)AND (BIMin > BIStartBull)
if BullishDivMACD then
BIDivBull = barindex
//drawsegment(oldBIMin,oldMACDLnMin,BIMin,MACDLnMin) coloured(0,100,0)//green
//drawarrowup(BIMin,MACDLnMin) coloured(0,100,0)//green
SCANBull=0
x1orig = InterBImin
y1orig = InterPriceMin
y1MACDorig = InterMACDLnMin
//drawarrowdown(x1orig,y1MACDorig) coloured(0,255,255)//aquamarine
TrendlineStart = 1
InterPriceMin=100000
InterMACDLnMin =100000
oldPriceMin = 100000
oldMACDLnMin=100000
mslopeorig = 1000000
mslopeorig1 = 1000000
mslopeorig2 = 1000000
mslopeimmediate= 1000000
mslopeimmediateold= 1000000
VerifyEntryPoint =1
EnterAPosition =1
ScanExitBull =0
ValidTP = 0
TPcounter =0
endif
ospassed = 0
endif
endif
//#############################################################################################
//Trendline tracking start here
If condMACDLnUnder0 AND BullPosition = 1 AND barindex >= EntrySignalBIBull then
PriceMin=100000
MACDLnMin =100000
BIStartBull = barindex
osTLpassed =0
SCANBULLTL =1
endif
IF SCANBULLTL =1 AND TrendlineStart = 1 AND BIStartBull >= BIDivBull AND osTL then
//Get min PriceMin, MACDLn
PriceMin = min(PriceMin, Low)
if PriceMin <> PriceMin[1] then
PriceMinBI = barindex
PriceMinMACDLn = indMACDLn
endif
MACDLnMin =min(MACDLnMin,indMACDLn)
if MACDLnMin <> MACDLnMin[1] then
BIMin = barindex
endif
if MACDLnMin < PriceMinMACDLn then
BIMin = PriceMinBI
endif
osTLpassed =1
ENDIF
//counting bearish divergences and X2 plotting
If barindex >= EntrySignalBIBull AND BullPosition = 1 AND condMACDLnOver0 AND osTLpassed =1 then
oldPriceMin = InterPriceMin
InterPriceMin =PriceMin
oldMACDLnMin = InterMACDLnMin
InterMACDLnMin =MACDLnMin
oldBIMin = InterBImin
InterBImin = BIMin
if (oldPriceMin > InterPriceMin) AND (InterMACDLnMin > OldMACDLnMin) then
//drawarrowdown(OldBIMin,OldMACDLnMin) coloured(0,255,255)//aquamarine
checkTP = 1
endif
IF CountBearDiver <1 then
mslopeorigold= mslopeorig
x1diver =x1orig
y1diver = y1orig
y1MACDLndiver =y1MACDorig
oldx2 =oldBIMin //previous scan under macdln
oldy2= oldPriceMin
oldy2MACDLn = oldMACDLnMin
newx2=InterBImin
newy2=InterPriceMin
newy2MACDLn=InterMACDLnMin
mslopeorig1 = (oldy2-y1diver) / (oldx2-x1diver)
mslopeorig2 = (newy2-y1diver) / (newx2-x1diver)
mslopeorigold = mslopeorig
if mslopeorig2 < mslopeorig1 AND mslopeorig1 > 0 then //if new diver slope is less than the original
mslopeorig =mslopeorig2
x2orig = newx2
y2orig = newy2
y2MACDLnorig=newy2MACDLn
//TrendlineEnd = 0
mslope =mslopeorig
TrendlineEnd = 0
runningBI = barindex
else
//if new diver slope is less than the original slope then calculate the immediate slope, choose the lowest slope
mslopeorig =min(mslopeorig2,mslopeorigold)
x1imm =x2orig //previous scan under macdln with os
y1imm = y2orig
y1MACDLnimm =y2MACDLnorig
x2imm= InterBImin
y2imm=InterPriceMin
y2MACDLnimm =InterMACDLnMin
mslopeimmediate =(y2imm-y1imm) / (x2imm-x1imm) //to check orientation of slope (neg or pos)
mslope =MIN(mslopeimmediate,mslopeorig)
checkTP = 1
if TPcounter > 1 then
checkTP = 1
ValidTP = 1
endif
if barindex > TPProjectioncheckBI and TPProjectioncheck = 1 then
//drawarrowdown(barindex,indMACDLn) coloured(210, 105, 30) //chocolate for TP
TPProjectioncheck = 0
TPcounter = TPcounter + 1
endif
endif
x1anchor = x1imm
y1anchor = y1imm
y1MACDLnanchor =y1MACDLnimm
//TrendlineEnd = 0
endif
//If barindex >= EntrySignalBIBull AND BullPosition = 1 AND CountBearDiver =1 AND condMACDLnOver0 AND osTLpassed =1 then
IF CountBearDiver >=1 then
If TrendlineEnd = 0 then
//getting the immediate slope, mslopeimmediate
mslopeimmediateold = mslope
x1imm =x1anchor //previous scan under macdln with os
y1imm = y1anchor
y1MACDLnimm =y1MACDLnanchor
x2imm= InterBImin
y2imm=InterPriceMin
y2MACDLnimm =InterMACDLnMin
mslopeimmediate =(y2imm-y1imm) / (x2imm-x1imm) //to check orientation of slope (neg or pos)
//drawtext ("mslopeimmediateold = #mslopeimmediateold#",x2imm,y2MACDLnimm) //
//drawtext ("mslopeimmediate = #mslopeimmediate#",x2imm,y2MACDLnimm) //
//drawarrowdown(x1,y1MACDLn) coloured(0,255,255)//aquamarine
//drawarrowdown(x2imm,y2MACDLnimm) coloured(250,235,215)//antiquewhite
mslope = min(mslopeimmediate,mslopeimmediateold)
//drawtext ("mslopeimmediateold = #mslopeimmediateold#",x2imm,y2MACDLnimm) //
x1 = x1anchor
y1= y1anchor
y1MACDLn= y1MACDLnanchor
TrendlineEnd = 1
TrendlineEndBI = barindex
//drawarrowdown(x1,y1MACDLn) coloured(0,255,255)//aquamarine
//drawtext ("mslopeimmediate = #mslopeimmediate#",x1,y1MACDLn) //-0.0012
//drawsegment(x1,y1MACDLn,x2,y2MACDLn) coloured(0,255,255)// aquamarine
endif//end of first CountBearDiver =1
//start of CountBearDiver =1 with trendlineend=1 condition
IF TrendlineEnd = 1 AND barindex >= TrendlineEndBI then
mslopeimmediateold = mslopeimmediate
//getting the immediate slope, mslopeimmediate
x1 = x1anchor
y1= y1anchor
y1MACDLn= y1MACDLnanchor
x2 = InterBImin
y2 = InterPriceMin
y2MACDLn = InterMACDLnMin
mslopeimmediate = (y2-y1)/(x2-x1)
//drawarrowdown(x1anchor,y1MACDLnanchor) coloured(250,235,215)//antiquewhite
//drawtext ("mslopeimmediateold = #mslopeimmediateold#",x1,y1MACDLn)
//mslopetransferold = mslopetransfer
if mslopeimmediate < mslopeimmediateold AND mslopeimmediateold > 0 then //
mslope = mslopeimmediate
mslopetransferBI = barindex
//drawarrowdown(x2,y2MACDLn) coloured(0,255,255)//aquamarine
x1transfer = x1imm
y1transfer = y1imm
y1MACDLntransfer =y1MACDLnimm
x2transfer = x2imm
y2transfer = y2imm
y2MACDLntransfer=y2MACDLnimm
//drawtext ("mslopetransfer = #mslopetransfer#",x2,y2MACDLn)
//drawsegment(x1transfer,y1MACDLntransfer,x2transfer,y2MACDLntransfer) coloured(0,255,255)// aquamarine
checkTP = 1
else
if mslopeimmediate > mslopeimmediateold AND barindex >=TPProjectioncheckBI then
checkTP = 1
ValidTP = 1
mslope = mslopeimmediateold
endif
endif
//checkTP = 1
endif
endif
TPIntercept = HighestDetected - mslope*(BIHighestDetected)
TPProjection = mslope*(barindex) + TPIntercept
//tryBI = barindex
//drawtext ("TPIntercept = #TPIntercept#",BIHighestDetected,y2MACDLnimm)
//drawtext ("TPProjection = #TPProjection#",x2imm,y2MACDLnimm)
//drawarrowdown(BIHighestDetected,y2MACDLnimm) coloured(0,255,255)//aquamarine
endif
//#########################################################################################3
//conditions to enter LONG POSITION
//verify entry point
If (barindex >= BIDivBull) and VerifyEntryPoint=1 then
if LOW < y1orig then
VerifyEntryPoint = 0
SCANBULLTL =0
endif
endif
//endif
//If (barindex >= BIDivBull) and (VerifyEntryPoint =1 AND TrendlineStart = 1) AND (indRSI crosses over 50 AND indRSI < 56) AND EnterAPosition =1 Then
If (barindex >= BIDivBull) and (VerifyEntryPoint =1 AND TrendlineStart = 1) AND ScanExitBull =0 AND (indRSI crosses over 50 AND indRSI < 56) AND EnterAPosition =1 Then
// Conditions to enter long positions
IF NOT LongOnMarket THEN
BUY 2 CONTRACTS AT MARKET
ENDIF
EntrySignalBull= ABS(indMACDLn)
EntrySignalBIBull = barindex
//drawarrowup(EntrySignalBIBull,EntrySignalBull) coloured(255,192,203)//flesh colour
BullPosition = 1
BullishDivMACD =0
SignalBullRemove = 1
ScanExitBull = 1
ENDIF
if barindex > EntrySignalBIBull and SignalBullRemove = 1 then
EntrySignalBull = 0
SignalBullRemove = 0
EnterAPosition =0
endif
//######################################################
//####################################################################################
//Conditions to Start Bearish
if barindex = 11 then
InterPriceMax=0
InterMACDLnMax =0
oldPriceMax = 0
oldMACDLnMax=0
InterBIMax = 0
HighestDetected =0
endif
If condMACDLnOver0 then
PriceMax=0
MACDLnMax =0
BIStartBear = barindex
EntrySignalBear =0
SCANBear = 1
obpassed =0
endif
if SCANBear = 1 and barindex >BIStartBear and ob then
//Get max of Price
PriceMax = max(PriceMax, High)
if PriceMax <> PriceMax[1] then
PriceMaxBI = barindex
PriceMaxMACDLn = indMACDLn
endif
//Get max MACDLn
MACDLnMax =max(MACDLnMax,indMACDLn)
if MACDLnMax <> MACDLnMax[1] then
BIMax = barindex
endif
if MACDLnMax > PriceMaxMACDLn then
BIMax = PriceMaxBI
endif
obpassed = 1
if TrendlineStart = 1 then
HighestDetected = max(HighestDetected, High)
if HighestDetected <> HighestDetected[1] then
BIHighestDetected = barindex
endif
endif
endif
// Check for bearish crossover and divergences
If SCANBear=1 and condMACDLnUnder0 and obpassed = 1 then
oldPriceMax = InterPriceMax
InterPriceMax =PriceMax
oldMACDLnMax = InterMACDLnMax
InterMACDLnMax =MACDLnMax
oldBIMax = InterBImax
InterBImax = BIMax
if barindex >= BIHighestDetected and InterPriceMax >= HighestDetected then
BearishDivMACD = (InterPriceMax >= oldPriceMax) AND (InterMACDLnMax < oldMACDLnMax) AND (BIMax > BIStartBear)
endif
if BearishDivMACD then
BIDivBear = BImax
SCANBear=0
InterPriceMax=0
InterMACDLnMax =0
oldPriceMax = 0
oldMACDLnMax=0
endif
obpassed = 0
endif
If (barindex >= BIDivBear) and BearishDivMACD THEN
EntrySignalBear = ABS(indMACDLn)
EntrySignalBIBear = barindex
if barindex >= EntrySignalBIBear and BullPosition = 1 then
CountBearDiver = CountBearDiver +1
//drawtext ("C = #CountBearDiver#" , barindex, indMACDLn)
endif
ExecutedBearishDivMACD = 1
ExecutedBearishBI = barindex
BearishDivMACD =0
SignalBearRemove = 1
ENDIF
if barindex > EntrySignalBIBear and SignalBearRemove = 1 then
EntrySignalBear = 0
SignalBearRemove = 0
endif
//#############################################################################
//TRENDLINE SCAN FOR BREAK
//If barindex > BIDivBear AND TrendlineEnd = 1 AND TrendlineStart = 1 AND VerifyEntryPoint = 1 AND BullPosition = 1 then
If barindex > TrendlineEndBI AND TrendlineEnd=1 AND TrendlineStart = 1 AND VerifyEntryPoint = 1 AND BullPosition = 1 then
yintercept = (y1 - (mslope*x1))
ybreak = (mslope*barindex) + yintercept
if CLOSE crosses under ybreak OR CLOSE < ybreak Then
// Conditions to exit long positions
If LongOnMarket THEN
SELL AT MARKET
ENDIF
ScanTrendline = 0
TrendlineEnd = 0
TrendlineStart = 0
ScanExitBull=0
ScanEndPoint =0
CountBearDiver =0
BullPosition = 0
HighestDetected =0
SCANBULLTL =0
checkTP = 0
endif
endif
If barindex > TrendlineEndBI AND TrendlineStart = 1 AND VerifyEntryPoint = 1 AND BullPosition = 1 AND checkTP = 1 AND ValidTP = 1 then
if CLOSE crosses over TPProjection or CLOSE > TPProjection Then
// Conditions to exit long positions
If LongOnMarket THEN
SELL AT MARKET
ENDIF
ScanTrendline = 0
TrendlineEnd = 0
TrendlineStart = 0
ScanExitBull=0
ScanEndPoint =0
CountBearDiver =0
BullPosition = 0
HighestDetected =0
SCANBULLTL =0
checkTP = 0
ValidTP =0
endif
endif
///TPProjection status
if CLOSE crosses over TPProjection or CLOSE > TPProjection Then
TPProjectioncheck = 1
TPProjectioncheckBI = barindex
endif
//if barindex > TPProjectioncheckBI and TPProjectioncheck = 1 then
////drawarrowdown(barindex,indMACDLn) coloured(210, 105, 30) //chocolate for TP
//TPProjectioncheck = 0
//TPcounter = TPcounter + 1
//endif
if barindex > EntrySignalBIBull and ScanExitBull=1 then
if VerifyEntryPoint=0 then
ExitBullBI = barindex
ExitSignalBull = ABS(indMACDLn)
// Conditions to exit long positions
If LongOnMarket THEN
SELL AT MARKET
ENDIF
IF VerifyEntryPoint=0 THEN
x1 = x1orig //from bullish diver
y1 = y1orig
y1MACDLn = y1MACDorig
x2 =ExitBullBI
//y2 = InterPricemin
y2MACD = indMACDLn
ENDIF
ScanTrendline = 0
TrendlineEnd = 0
TrendlineStart = 0
ScanExitBull=0
ScanEndPoint =0
CountBearDiver =0
BullPosition = 0
ScanExitBull =0
HighestDetected =0
SCANBULLTL =0
checkTP = 0
ValidTP = 0
//endif
endif
endif
//
//
//return indMACDLn as "MACDLine",indMACDSign as "MACDSign",indMACDHis as "MACDHistogram", 0 coloured(168,168,168) style(line,2) as"0 level"//,EntrySignalBull coloured(255,192,203) style (histogram) as "Enter here bullish",EntrySignalBear coloured(160,75,180) style (histogram) as "Enter here bearish"
Thanks alot for sharing.
I did a BT last 3 years. Seems good but very few trades
Divergence and Trend line automatic tradingBacktesting Error – Need Help Please!
This topic contains 6 replies,
has 3 voices, and was last updated by murre87
4 years, 6 months ago.
| Forum: | ProBuilder: Indicators & Custom Tools |
| Language: | English |
| Started: | 04/30/2021 |
| Status: | Active |
| Attachments: | 2 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.