Divergence and Trend line automatic tradingBacktesting Error – Need Help Please!
Forums › ProRealTime English forum › ProBuilder support › Divergence and Trend line automatic tradingBacktesting Error – Need Help Please!
- This topic has 6 replies, 3 voices, and was last updated 3 years ago by murre87.
-
-
04/30/2021 at 11:01 AM #168458
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
MACD RSI divergence with trendlines123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550//BACKTESTING DiverLineBull Part3// ######################################################################################################//Variable DeclarationsobLevel = 65 //20210308oslevel = 40 //20210308//Indicator DeclarationsindMACDLn = 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 Resetob = indRSI>obLevel //20210308oS = indRSI<osLevel //20210308osTL = indRSI < 30condMACDLnOver0 = indMACDLn crosses over 0condMACDLnUnder0 = indMACDLn crosses under 0//BULLISH MACD RSI DIVERGENCE //###################################################//Conditions to Startif barindex = 11 thenInterPriceMin=100000InterMACDLnMin =100000oldPriceMin = 100000oldMACDLnMin=100000InterBIMin = 0endifIf condMACDLnUnder0 thenPriceMin=100000MACDLnMin =100000BIStartBull = barindexEntrySignalBull =0SCANBull = 1ospassed =0SCANBULLTL =1endifif SCANBull = 1 and barindex >= BIStartBull and os then//Get min PriceMin, MACDLnPriceMin = min(PriceMin, Low)if PriceMin <> PriceMin[1] thenPriceMinBI = barindexPriceMinMACDLn = indMACDLnendifMACDLnMin =min(MACDLnMin,indMACDLn)if MACDLnMin <> MACDLnMin[1] thenBIMin = barindexendifif MACDLnMin < PriceMinMACDLn thenBIMin = PriceMinBIendifospassed = 1endif// Check for bullish crossover and divergencesIf SCANBull=1 and condMACDLnOver0 and ospassed = 1 thenoldPriceMin = InterPriceMinInterPriceMin =PriceMinoldMACDLnMin = InterMACDLnMinInterMACDLnMin =MACDLnMinoldBIMin = InterBIminInterBImin = BIMinIf BullPosition = 0 then//declare divergence conditionBullishDivMACD = (InterPriceMin < oldPriceMin ) AND (InterMACDLnMin > oldMACDLnMin)AND (BIMin > BIStartBull)if BullishDivMACD thenBIDivBull = barindex//drawsegment(oldBIMin,oldMACDLnMin,BIMin,MACDLnMin) coloured(0,100,0)//green//drawarrowup(BIMin,MACDLnMin) coloured(0,100,0)//greenSCANBull=0x1orig = InterBIminy1orig = InterPriceMiny1MACDorig = InterMACDLnMin//drawarrowdown(x1orig,y1MACDorig) coloured(0,255,255)//aquamarineTrendlineStart = 1InterPriceMin=100000InterMACDLnMin =100000oldPriceMin = 100000oldMACDLnMin=100000mslopeorig = 1000000mslopeorig1 = 1000000mslopeorig2 = 1000000mslopeimmediate= 1000000mslopeimmediateold= 1000000VerifyEntryPoint =1EnterAPosition =1ScanExitBull =0ValidTP = 0TPcounter =0endifospassed = 0endifendif//#############################################################################################//Trendline tracking start hereIf condMACDLnUnder0 AND BullPosition = 1 AND barindex >= EntrySignalBIBull thenPriceMin=100000MACDLnMin =100000BIStartBull = barindexosTLpassed =0SCANBULLTL =1endifIF SCANBULLTL =1 AND TrendlineStart = 1 AND BIStartBull >= BIDivBull AND osTL then//Get min PriceMin, MACDLnPriceMin = min(PriceMin, Low)if PriceMin <> PriceMin[1] thenPriceMinBI = barindexPriceMinMACDLn = indMACDLnendifMACDLnMin =min(MACDLnMin,indMACDLn)if MACDLnMin <> MACDLnMin[1] thenBIMin = barindexendifif MACDLnMin < PriceMinMACDLn thenBIMin = PriceMinBIendifosTLpassed =1ENDIF//counting bearish divergences and X2 plottingIf barindex >= EntrySignalBIBull AND BullPosition = 1 AND condMACDLnOver0 AND osTLpassed =1 thenoldPriceMin = InterPriceMinInterPriceMin =PriceMinoldMACDLnMin = InterMACDLnMinInterMACDLnMin =MACDLnMinoldBIMin = InterBIminInterBImin = BIMinif (oldPriceMin > InterPriceMin) AND (InterMACDLnMin > OldMACDLnMin) then//drawarrowdown(OldBIMin,OldMACDLnMin) coloured(0,255,255)//aquamarinecheckTP = 1endifIF CountBearDiver <1 thenmslopeorigold= mslopeorigx1diver =x1origy1diver = y1origy1MACDLndiver =y1MACDorigoldx2 =oldBIMin //previous scan under macdlnoldy2= oldPriceMinoldy2MACDLn = oldMACDLnMinnewx2=InterBIminnewy2=InterPriceMinnewy2MACDLn=InterMACDLnMinmslopeorig1 = (oldy2-y1diver) / (oldx2-x1diver)mslopeorig2 = (newy2-y1diver) / (newx2-x1diver)mslopeorigold = mslopeorigif mslopeorig2 < mslopeorig1 AND mslopeorig1 > 0 then //if new diver slope is less than the originalmslopeorig =mslopeorig2x2orig = newx2y2orig = newy2y2MACDLnorig=newy2MACDLn//TrendlineEnd = 0mslope =mslopeorigTrendlineEnd = 0runningBI = barindexelse//if new diver slope is less than the original slope then calculate the immediate slope, choose the lowest slopemslopeorig =min(mslopeorig2,mslopeorigold)x1imm =x2orig //previous scan under macdln with osy1imm = y2origy1MACDLnimm =y2MACDLnorigx2imm= InterBIminy2imm=InterPriceMiny2MACDLnimm =InterMACDLnMinmslopeimmediate =(y2imm-y1imm) / (x2imm-x1imm) //to check orientation of slope (neg or pos)mslope =MIN(mslopeimmediate,mslopeorig)checkTP = 1if TPcounter > 1 thencheckTP = 1ValidTP = 1endifif barindex > TPProjectioncheckBI and TPProjectioncheck = 1 then//drawarrowdown(barindex,indMACDLn) coloured(210, 105, 30) //chocolate for TPTPProjectioncheck = 0TPcounter = TPcounter + 1endifendifx1anchor = x1immy1anchor = y1immy1MACDLnanchor =y1MACDLnimm//TrendlineEnd = 0endif//If barindex >= EntrySignalBIBull AND BullPosition = 1 AND CountBearDiver =1 AND condMACDLnOver0 AND osTLpassed =1 thenIF CountBearDiver >=1 thenIf TrendlineEnd = 0 then//getting the immediate slope, mslopeimmediatemslopeimmediateold = mslopex1imm =x1anchor //previous scan under macdln with osy1imm = y1anchory1MACDLnimm =y1MACDLnanchorx2imm= InterBIminy2imm=InterPriceMiny2MACDLnimm =InterMACDLnMinmslopeimmediate =(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)//antiquewhitemslope = min(mslopeimmediate,mslopeimmediateold)//drawtext ("mslopeimmediateold = #mslopeimmediateold#",x2imm,y2MACDLnimm) //x1 = x1anchory1= y1anchory1MACDLn= y1MACDLnanchorTrendlineEnd = 1TrendlineEndBI = 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)// aquamarineendif//end of first CountBearDiver =1//start of CountBearDiver =1 with trendlineend=1 conditionIF TrendlineEnd = 1 AND barindex >= TrendlineEndBI thenmslopeimmediateold = mslopeimmediate//getting the immediate slope, mslopeimmediatex1 = x1anchory1= y1anchory1MACDLn= y1MACDLnanchorx2 = InterBIminy2 = InterPriceMiny2MACDLn = InterMACDLnMinmslopeimmediate = (y2-y1)/(x2-x1)//drawarrowdown(x1anchor,y1MACDLnanchor) coloured(250,235,215)//antiquewhite//drawtext ("mslopeimmediateold = #mslopeimmediateold#",x1,y1MACDLn)//mslopetransferold = mslopetransferif mslopeimmediate < mslopeimmediateold AND mslopeimmediateold > 0 then //mslope = mslopeimmediatemslopetransferBI = barindex//drawarrowdown(x2,y2MACDLn) coloured(0,255,255)//aquamarinex1transfer = x1immy1transfer = y1immy1MACDLntransfer =y1MACDLnimmx2transfer = x2immy2transfer = y2immy2MACDLntransfer=y2MACDLnimm//drawtext ("mslopetransfer = #mslopetransfer#",x2,y2MACDLn)//drawsegment(x1transfer,y1MACDLntransfer,x2transfer,y2MACDLntransfer) coloured(0,255,255)// aquamarinecheckTP = 1elseif mslopeimmediate > mslopeimmediateold AND barindex >=TPProjectioncheckBI thencheckTP = 1ValidTP = 1mslope = mslopeimmediateoldendifendif//checkTP = 1endifendifTPIntercept = 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)//aquamarineendif//#########################################################################################3//conditions to enter LONG POSITION//verify entry pointIf (barindex >= BIDivBull) and VerifyEntryPoint=1 thenif LOW < y1orig thenVerifyEntryPoint = 0SCANBULLTL =0endifendif//endif//If (barindex >= BIDivBull) and (VerifyEntryPoint =1 AND TrendlineStart = 1) AND (indRSI crosses over 50 AND indRSI < 56) AND EnterAPosition =1 ThenIf (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 positionsIF NOT LongOnMarket THENBUY 2 CONTRACTS AT MARKETENDIFEntrySignalBull= ABS(indMACDLn)EntrySignalBIBull = barindex//drawarrowup(EntrySignalBIBull,EntrySignalBull) coloured(255,192,203)//flesh colourBullPosition = 1BullishDivMACD =0RemoveSignalBull = 1ScanExitBull = 1ENDIFif barindex > EntrySignalBIBull and RemoveSignalBull = 1 thenEntrySignalBull = 0RemoveSignalBull = 0EnterAPosition =0endif//######################################################//####################################################################################//Conditions to Start Bearishif barindex = 11 thenInterPriceMax=0InterMACDLnMax =0oldPriceMax = 0oldMACDLnMax=0InterBIMax = 0HighestDetected =0endifIf condMACDLnOver0 thenPriceMax=0MACDLnMax =0BIStartBear = barindexEntrySignalBear =0SCANBear = 1obpassed =0endifif SCANBear = 1 and barindex >BIStartBear and ob then//Get max of PricePriceMax = max(PriceMax, High)if PriceMax <> PriceMax[1] thenPriceMaxBI = barindexPriceMaxMACDLn = indMACDLnendif//Get max MACDLnMACDLnMax =max(MACDLnMax,indMACDLn)if MACDLnMax <> MACDLnMax[1] thenBIMax = barindexendifif MACDLnMax > PriceMaxMACDLn thenBIMax = PriceMaxBIendifobpassed = 1if TrendlineStart = 1 thenHighestDetected = max(HighestDetected, High)if HighestDetected <> HighestDetected[1] thenBIHighestDetected = barindexendifendifendif// Check for bearish crossover and divergencesIf SCANBear=1 and condMACDLnUnder0 and obpassed = 1 thenoldPriceMax = InterPriceMaxInterPriceMax =PriceMaxoldMACDLnMax = InterMACDLnMaxInterMACDLnMax =MACDLnMaxoldBIMax = InterBImaxInterBImax = BIMaxif barindex >= BIHighestDetected and InterPriceMax >= HighestDetected thenBearishDivMACD = (InterPriceMax >= oldPriceMax) AND (InterMACDLnMax < oldMACDLnMax) AND (BIMax > BIStartBear)endifif BearishDivMACD thenBIDivBear = BImaxSCANBear=0InterPriceMax=0InterMACDLnMax =0oldPriceMax = 0oldMACDLnMax=0endifobpassed = 0endifIf (barindex >= BIDivBear) and BearishDivMACD THENEntrySignalBear = ABS(indMACDLn)EntrySignalBIBear = barindexif barindex >= EntrySignalBIBear and BullPosition = 1 thenCountBearDiver = CountBearDiver +1//drawtext ("C = #CountBearDiver#" , barindex, indMACDLn)endifExecutedBearishDivMACD = 1ExecutedBearishBI = barindexBearishDivMACD =0RemoveSignalBear = 1ENDIFif barindex > EntrySignalBIBear and RemoveSignalBear = 1 thenEntrySignalBear = 0RemoveSignalBear = 0endif//#############################################################################//TRENDLINE SCAN FOR BREAK//If barindex > BIDivBear AND TrendlineEnd = 1 AND TrendlineStart = 1 AND VerifyEntryPoint = 1 AND BullPosition = 1 thenIf barindex > TrendlineEndBI AND TrendlineEnd=1 AND TrendlineStart = 1 AND VerifyEntryPoint = 1 AND BullPosition = 1 thenyintercept = (y1 - (mslope*x1))ybreak = (mslope*barindex) + yinterceptif CLOSE crosses under ybreak OR CLOSE < ybreak Then// Conditions to exit long positionsIf LongOnMarket THENSELL AT MARKETENDIFScanTrendline = 0TrendlineEnd = 0TrendlineStart = 0ScanExitBull=0ScanEndPoint =0CountBearDiver =0BullPosition = 0HighestDetected =0SCANBULLTL =0checkTP = 0endifendifIf barindex > TrendlineEndBI AND TrendlineStart = 1 AND VerifyEntryPoint = 1 AND BullPosition = 1 AND checkTP = 1 AND ValidTP = 1 thenif CLOSE crosses over TPProjection or CLOSE > TPProjection Then// Conditions to exit long positionsIf LongOnMarket THENSELL AT MARKETENDIFScanTrendline = 0TrendlineEnd = 0TrendlineStart = 0ScanExitBull=0ScanEndPoint =0CountBearDiver =0BullPosition = 0HighestDetected =0SCANBULLTL =0checkTP = 0ValidTP =0endifendif///TPProjection statusif CLOSE crosses over TPProjection or CLOSE > TPProjection ThenTPProjectioncheck = 1TPProjectioncheckBI = barindexendif//if barindex > TPProjectioncheckBI and TPProjectioncheck = 1 then////drawarrowdown(barindex,indMACDLn) coloured(210, 105, 30) //chocolate for TP//TPProjectioncheck = 0//TPcounter = TPcounter + 1//endifif barindex > EntrySignalBIBull and ScanExitBull=1 thenif VerifyEntryPoint=0 thenExitBullBI = barindexExitSignalBull = ABS(indMACDLn)// Conditions to exit long positionsIf LongOnMarket THENSELL AT MARKETENDIFIF VerifyEntryPoint=0 THENx1 = x1orig //from bullish divery1 = y1origy1MACDLn = y1MACDorigx2 =ExitBullBI//y2 = InterPriceminy2MACD = indMACDLnENDIFScanTrendline = 0TrendlineEnd = 0TrendlineStart = 0ScanExitBull=0ScanEndPoint =0CountBearDiver =0BullPosition = 0ScanExitBull =0HighestDetected =0SCANBULLTL =0checkTP = 0ValidTP = 0//endifendifendif//////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"04/30/2021 at 11:55 AM #168460If 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 …
2 users thanked author for this post.
04/30/2021 at 1:02 PM #168465Hi @Grahal! Thanks so much for the advise. I will send a technical report for this ^^.
08/14/2021 at 8:25 AM #175277Hi,
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!
1 user thanked author for this post.
08/15/2021 at 7:24 AM #17537608/15/2021 at 2:54 PM #175391As requested:
Working Code123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550//BACKTESTING DiverLineBull Part3// ######################################################################################################//Variable DeclarationsobLevel = 65 //20210308oslevel = 40 //20210308//Indicator DeclarationsindMACDLn = 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 Resetob = indRSI>obLevel //20210308oS = indRSI<osLevel //20210308osTL = indRSI < 30condMACDLnOver0 = indMACDLn crosses over 0condMACDLnUnder0 = indMACDLn crosses under 0//BULLISH MACD RSI DIVERGENCE //###################################################//Conditions to Startif barindex = 11 thenInterPriceMin=100000InterMACDLnMin =100000oldPriceMin = 100000oldMACDLnMin=100000InterBIMin = 0endifIf condMACDLnUnder0 thenPriceMin=100000MACDLnMin =100000BIStartBull = barindexEntrySignalBull =0SCANBull = 1ospassed =0SCANBULLTL =1endifif SCANBull = 1 and barindex >= BIStartBull and os then//Get min PriceMin, MACDLnPriceMin = min(PriceMin, Low)if PriceMin <> PriceMin[1] thenPriceMinBI = barindexPriceMinMACDLn = indMACDLnendifMACDLnMin =min(MACDLnMin,indMACDLn)if MACDLnMin <> MACDLnMin[1] thenBIMin = barindexendifif MACDLnMin < PriceMinMACDLn thenBIMin = PriceMinBIendifospassed = 1endif// Check for bullish crossover and divergencesIf SCANBull=1 and condMACDLnOver0 and ospassed = 1 thenoldPriceMin = InterPriceMinInterPriceMin =PriceMinoldMACDLnMin = InterMACDLnMinInterMACDLnMin =MACDLnMinoldBIMin = InterBIminInterBImin = BIMinIf BullPosition = 0 then//declare divergence conditionBullishDivMACD = (InterPriceMin < oldPriceMin ) AND (InterMACDLnMin > oldMACDLnMin)AND (BIMin > BIStartBull)if BullishDivMACD thenBIDivBull = barindex//drawsegment(oldBIMin,oldMACDLnMin,BIMin,MACDLnMin) coloured(0,100,0)//green//drawarrowup(BIMin,MACDLnMin) coloured(0,100,0)//greenSCANBull=0x1orig = InterBIminy1orig = InterPriceMiny1MACDorig = InterMACDLnMin//drawarrowdown(x1orig,y1MACDorig) coloured(0,255,255)//aquamarineTrendlineStart = 1InterPriceMin=100000InterMACDLnMin =100000oldPriceMin = 100000oldMACDLnMin=100000mslopeorig = 1000000mslopeorig1 = 1000000mslopeorig2 = 1000000mslopeimmediate= 1000000mslopeimmediateold= 1000000VerifyEntryPoint =1EnterAPosition =1ScanExitBull =0ValidTP = 0TPcounter =0endifospassed = 0endifendif//#############################################################################################//Trendline tracking start hereIf condMACDLnUnder0 AND BullPosition = 1 AND barindex >= EntrySignalBIBull thenPriceMin=100000MACDLnMin =100000BIStartBull = barindexosTLpassed =0SCANBULLTL =1endifIF SCANBULLTL =1 AND TrendlineStart = 1 AND BIStartBull >= BIDivBull AND osTL then//Get min PriceMin, MACDLnPriceMin = min(PriceMin, Low)if PriceMin <> PriceMin[1] thenPriceMinBI = barindexPriceMinMACDLn = indMACDLnendifMACDLnMin =min(MACDLnMin,indMACDLn)if MACDLnMin <> MACDLnMin[1] thenBIMin = barindexendifif MACDLnMin < PriceMinMACDLn thenBIMin = PriceMinBIendifosTLpassed =1ENDIF//counting bearish divergences and X2 plottingIf barindex >= EntrySignalBIBull AND BullPosition = 1 AND condMACDLnOver0 AND osTLpassed =1 thenoldPriceMin = InterPriceMinInterPriceMin =PriceMinoldMACDLnMin = InterMACDLnMinInterMACDLnMin =MACDLnMinoldBIMin = InterBIminInterBImin = BIMinif (oldPriceMin > InterPriceMin) AND (InterMACDLnMin > OldMACDLnMin) then//drawarrowdown(OldBIMin,OldMACDLnMin) coloured(0,255,255)//aquamarinecheckTP = 1endifIF CountBearDiver <1 thenmslopeorigold= mslopeorigx1diver =x1origy1diver = y1origy1MACDLndiver =y1MACDorigoldx2 =oldBIMin //previous scan under macdlnoldy2= oldPriceMinoldy2MACDLn = oldMACDLnMinnewx2=InterBIminnewy2=InterPriceMinnewy2MACDLn=InterMACDLnMinmslopeorig1 = (oldy2-y1diver) / (oldx2-x1diver)mslopeorig2 = (newy2-y1diver) / (newx2-x1diver)mslopeorigold = mslopeorigif mslopeorig2 < mslopeorig1 AND mslopeorig1 > 0 then //if new diver slope is less than the originalmslopeorig =mslopeorig2x2orig = newx2y2orig = newy2y2MACDLnorig=newy2MACDLn//TrendlineEnd = 0mslope =mslopeorigTrendlineEnd = 0runningBI = barindexelse//if new diver slope is less than the original slope then calculate the immediate slope, choose the lowest slopemslopeorig =min(mslopeorig2,mslopeorigold)x1imm =x2orig //previous scan under macdln with osy1imm = y2origy1MACDLnimm =y2MACDLnorigx2imm= InterBIminy2imm=InterPriceMiny2MACDLnimm =InterMACDLnMinmslopeimmediate =(y2imm-y1imm) / (x2imm-x1imm) //to check orientation of slope (neg or pos)mslope =MIN(mslopeimmediate,mslopeorig)checkTP = 1if TPcounter > 1 thencheckTP = 1ValidTP = 1endifif barindex > TPProjectioncheckBI and TPProjectioncheck = 1 then//drawarrowdown(barindex,indMACDLn) coloured(210, 105, 30) //chocolate for TPTPProjectioncheck = 0TPcounter = TPcounter + 1endifendifx1anchor = x1immy1anchor = y1immy1MACDLnanchor =y1MACDLnimm//TrendlineEnd = 0endif//If barindex >= EntrySignalBIBull AND BullPosition = 1 AND CountBearDiver =1 AND condMACDLnOver0 AND osTLpassed =1 thenIF CountBearDiver >=1 thenIf TrendlineEnd = 0 then//getting the immediate slope, mslopeimmediatemslopeimmediateold = mslopex1imm =x1anchor //previous scan under macdln with osy1imm = y1anchory1MACDLnimm =y1MACDLnanchorx2imm= InterBIminy2imm=InterPriceMiny2MACDLnimm =InterMACDLnMinmslopeimmediate =(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)//antiquewhitemslope = min(mslopeimmediate,mslopeimmediateold)//drawtext ("mslopeimmediateold = #mslopeimmediateold#",x2imm,y2MACDLnimm) //x1 = x1anchory1= y1anchory1MACDLn= y1MACDLnanchorTrendlineEnd = 1TrendlineEndBI = 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)// aquamarineendif//end of first CountBearDiver =1//start of CountBearDiver =1 with trendlineend=1 conditionIF TrendlineEnd = 1 AND barindex >= TrendlineEndBI thenmslopeimmediateold = mslopeimmediate//getting the immediate slope, mslopeimmediatex1 = x1anchory1= y1anchory1MACDLn= y1MACDLnanchorx2 = InterBIminy2 = InterPriceMiny2MACDLn = InterMACDLnMinmslopeimmediate = (y2-y1)/(x2-x1)//drawarrowdown(x1anchor,y1MACDLnanchor) coloured(250,235,215)//antiquewhite//drawtext ("mslopeimmediateold = #mslopeimmediateold#",x1,y1MACDLn)//mslopetransferold = mslopetransferif mslopeimmediate < mslopeimmediateold AND mslopeimmediateold > 0 then //mslope = mslopeimmediatemslopetransferBI = barindex//drawarrowdown(x2,y2MACDLn) coloured(0,255,255)//aquamarinex1transfer = x1immy1transfer = y1immy1MACDLntransfer =y1MACDLnimmx2transfer = x2immy2transfer = y2immy2MACDLntransfer=y2MACDLnimm//drawtext ("mslopetransfer = #mslopetransfer#",x2,y2MACDLn)//drawsegment(x1transfer,y1MACDLntransfer,x2transfer,y2MACDLntransfer) coloured(0,255,255)// aquamarinecheckTP = 1elseif mslopeimmediate > mslopeimmediateold AND barindex >=TPProjectioncheckBI thencheckTP = 1ValidTP = 1mslope = mslopeimmediateoldendifendif//checkTP = 1endifendifTPIntercept = 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)//aquamarineendif//#########################################################################################3//conditions to enter LONG POSITION//verify entry pointIf (barindex >= BIDivBull) and VerifyEntryPoint=1 thenif LOW < y1orig thenVerifyEntryPoint = 0SCANBULLTL =0endifendif//endif//If (barindex >= BIDivBull) and (VerifyEntryPoint =1 AND TrendlineStart = 1) AND (indRSI crosses over 50 AND indRSI < 56) AND EnterAPosition =1 ThenIf (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 positionsIF NOT LongOnMarket THENBUY 2 CONTRACTS AT MARKETENDIFEntrySignalBull= ABS(indMACDLn)EntrySignalBIBull = barindex//drawarrowup(EntrySignalBIBull,EntrySignalBull) coloured(255,192,203)//flesh colourBullPosition = 1BullishDivMACD =0SignalBullRemove = 1ScanExitBull = 1ENDIFif barindex > EntrySignalBIBull and SignalBullRemove = 1 thenEntrySignalBull = 0SignalBullRemove = 0EnterAPosition =0endif//######################################################//####################################################################################//Conditions to Start Bearishif barindex = 11 thenInterPriceMax=0InterMACDLnMax =0oldPriceMax = 0oldMACDLnMax=0InterBIMax = 0HighestDetected =0endifIf condMACDLnOver0 thenPriceMax=0MACDLnMax =0BIStartBear = barindexEntrySignalBear =0SCANBear = 1obpassed =0endifif SCANBear = 1 and barindex >BIStartBear and ob then//Get max of PricePriceMax = max(PriceMax, High)if PriceMax <> PriceMax[1] thenPriceMaxBI = barindexPriceMaxMACDLn = indMACDLnendif//Get max MACDLnMACDLnMax =max(MACDLnMax,indMACDLn)if MACDLnMax <> MACDLnMax[1] thenBIMax = barindexendifif MACDLnMax > PriceMaxMACDLn thenBIMax = PriceMaxBIendifobpassed = 1if TrendlineStart = 1 thenHighestDetected = max(HighestDetected, High)if HighestDetected <> HighestDetected[1] thenBIHighestDetected = barindexendifendifendif// Check for bearish crossover and divergencesIf SCANBear=1 and condMACDLnUnder0 and obpassed = 1 thenoldPriceMax = InterPriceMaxInterPriceMax =PriceMaxoldMACDLnMax = InterMACDLnMaxInterMACDLnMax =MACDLnMaxoldBIMax = InterBImaxInterBImax = BIMaxif barindex >= BIHighestDetected and InterPriceMax >= HighestDetected thenBearishDivMACD = (InterPriceMax >= oldPriceMax) AND (InterMACDLnMax < oldMACDLnMax) AND (BIMax > BIStartBear)endifif BearishDivMACD thenBIDivBear = BImaxSCANBear=0InterPriceMax=0InterMACDLnMax =0oldPriceMax = 0oldMACDLnMax=0endifobpassed = 0endifIf (barindex >= BIDivBear) and BearishDivMACD THENEntrySignalBear = ABS(indMACDLn)EntrySignalBIBear = barindexif barindex >= EntrySignalBIBear and BullPosition = 1 thenCountBearDiver = CountBearDiver +1//drawtext ("C = #CountBearDiver#" , barindex, indMACDLn)endifExecutedBearishDivMACD = 1ExecutedBearishBI = barindexBearishDivMACD =0SignalBearRemove = 1ENDIFif barindex > EntrySignalBIBear and SignalBearRemove = 1 thenEntrySignalBear = 0SignalBearRemove = 0endif//#############################################################################//TRENDLINE SCAN FOR BREAK//If barindex > BIDivBear AND TrendlineEnd = 1 AND TrendlineStart = 1 AND VerifyEntryPoint = 1 AND BullPosition = 1 thenIf barindex > TrendlineEndBI AND TrendlineEnd=1 AND TrendlineStart = 1 AND VerifyEntryPoint = 1 AND BullPosition = 1 thenyintercept = (y1 - (mslope*x1))ybreak = (mslope*barindex) + yinterceptif CLOSE crosses under ybreak OR CLOSE < ybreak Then// Conditions to exit long positionsIf LongOnMarket THENSELL AT MARKETENDIFScanTrendline = 0TrendlineEnd = 0TrendlineStart = 0ScanExitBull=0ScanEndPoint =0CountBearDiver =0BullPosition = 0HighestDetected =0SCANBULLTL =0checkTP = 0endifendifIf barindex > TrendlineEndBI AND TrendlineStart = 1 AND VerifyEntryPoint = 1 AND BullPosition = 1 AND checkTP = 1 AND ValidTP = 1 thenif CLOSE crosses over TPProjection or CLOSE > TPProjection Then// Conditions to exit long positionsIf LongOnMarket THENSELL AT MARKETENDIFScanTrendline = 0TrendlineEnd = 0TrendlineStart = 0ScanExitBull=0ScanEndPoint =0CountBearDiver =0BullPosition = 0HighestDetected =0SCANBULLTL =0checkTP = 0ValidTP =0endifendif///TPProjection statusif CLOSE crosses over TPProjection or CLOSE > TPProjection ThenTPProjectioncheck = 1TPProjectioncheckBI = barindexendif//if barindex > TPProjectioncheckBI and TPProjectioncheck = 1 then////drawarrowdown(barindex,indMACDLn) coloured(210, 105, 30) //chocolate for TP//TPProjectioncheck = 0//TPcounter = TPcounter + 1//endifif barindex > EntrySignalBIBull and ScanExitBull=1 thenif VerifyEntryPoint=0 thenExitBullBI = barindexExitSignalBull = ABS(indMACDLn)// Conditions to exit long positionsIf LongOnMarket THENSELL AT MARKETENDIFIF VerifyEntryPoint=0 THENx1 = x1orig //from bullish divery1 = y1origy1MACDLn = y1MACDorigx2 =ExitBullBI//y2 = InterPriceminy2MACD = indMACDLnENDIFScanTrendline = 0TrendlineEnd = 0TrendlineStart = 0ScanExitBull=0ScanEndPoint =0CountBearDiver =0BullPosition = 0ScanExitBull =0HighestDetected =0SCANBULLTL =0checkTP = 0ValidTP = 0//endifendifendif//////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"08/16/2021 at 5:51 AM #175422 -
AuthorPosts