Trading System Auto Closing Position
Forums › ProRealTime English forum › ProOrder support › Trading System Auto Closing Position
- This topic has 2 replies, 2 voices, and was last updated 2 years ago by LuckyClover18.
-
-
08/14/2021 at 8:48 AM #175280
Good day!
I hope I am posting on the correct forum.
I recently created a program for my strategy. I ran it in proorder auto trading on a few stocks in my IG demo and CFD accounts.
It is automatically opening a position according to my conditions. But, the next day at the opening, it automatically closes. I am not sure where the bug is. I hope somebody can have a look and coach me in the proper way of executing a trading system. I am using daily timeframe for the strategy.
MACD Divergence and Bollinger Band Strategy123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456//-------------------------------------------------------------------------// Main code : SFBTradeMND//-------------------------------------------------------------------------//indicatorsindBullishWatcher, indEntrySignalBI = CALL DIVERoutputindALMA = CALL ALMA[9, 6, 0.85]indMACDLn = ExponentialAverage[12](close)-ExponentialAverage[26](close)indMACDSign = ExponentialAverage[9](indMACDLn)indMACDHis = indMACDLn - indMACDSignindRSI = RSI[30](close)//indBollHi, indBollLo, indBollMid = CALL BOLLINGERBAND[0.2, 50]ignored, indBollLo, indBollMid = CALL BOLLINGERBAND[0.2, 50]indEMA20 =ExponentialAverage[20](close)CutLossPercentage = 0.05if barindex = 26 thenBBLoSupportScan=1SCANRSI = 1SCANBoll = 0SCANMACDx = 0condParabolic =0condlong =0endif//initial conditionsEntrySignalBar = indEntrySignalBI//checking for bullish divergence, DIVERoutput = 1if indBullishWatcher crosses over 0.5 AND (EntrySignalBar <> EntrySignalBar[1]) then//ScanEntryDiver = 0condDIVERoutput = 1BBLoSupportScan=1endifif indBullishWatcher = 1 then//AND condDIVERoutput=1//scan for RSI30 crossing over 50if SCANRSI = 1 AND BBLoSupportScan=1 AND (indRSI crosses over 50 OR indRSI crosses over 55) AND condlong = 0 then //OR (indRSI >50 AND indRSI < 56))SCANRSI = 0SCANBoll = 1RSI50xBI = barindexBBLoSupportScan=0elseif SCANRSI = 1 AND BBLoSupportScan=1 AND (indRSI >50) AND condlong = 0 thencutlosslevel = indBollLocalcrisk = (Close - cutlosslevel)/Closeif calcrisk < (CutLossPercentage) thenSCANRSI = 0SCANBoll = 1RSI50xBI = barindexBBLoSupportScan=0endifendifendifif SCANBoll = 1 AND BBLoSupportScan=0 AND SCANRSI = 0 AND barindex => RSI50xBI AND (Close crosses over indBollMid or Close > indBollMid) AND condlong =0 thenSCANBoll = 0SCANMACDx = 1BOLLxBI = barindexendifif barindex > BOLLxBI AND (High < indBollLo OR (OPEN < indBollLo AND CLOSE < indBollLo)) AND condlong =0 thenBBLoSupportScan=1SCANRSI = 1SCANBoll = 1endifif (SCANMACDx = 1 and SCANRSI = 0 AND SCANBoll = 0 ) AND barindex => BOLLxBI AND ((indMACDLn >0 AND indMACDHis >0) OR (indMACDHis crosses over 0)) and BBLoSupportScan=0 AND condlong =0 then //OR (indMACDLn crosses over 0 AND indMACDHis > 0)cutlosslevel = indBollLocalcrisk = (Close - cutlosslevel)/Closeif calcrisk < (CutLossPercentage) then //Cut Loss Percentage//SCANMACDx = 0BBLoSupportScan = 1BuyingSignal = 1condDIVERoutput = 0//calc limit level for buying, LBLLBL = cutlosslevel/(1-CutLossPercentage)else//ALMASupportScan = 1BBLoSupportScan = 1SCANRSI = 1condDIVERoutput = 0BuyingSignal = 0endifendifIF NOT LongOnMarket and BuyingSignal = 1 THEN//BUY 1000 CASH ROUNDEDDOWN AT LBL LIMIT//BUY 500 CASH ROUNDEDDOWN AT MARKETBUY 100 SHARES AT LBL LIMIT//BUY 10 CONTRACTS AT MARKETcondLong = 1BuyingBI = barindexcondParabolic = 0BBLoSupportScan = 1condDIVERoutput = 0ENDIFENDif//sell settingsIf barindex => BuyingBI and condLong= 1 AND indRSI CROSSES OVER 70 thencondParabolic = 1parabolicBI = barindexendif//condBBlowExit= Low crosses under indBollLo//condEMA20Exit = Low CROSSES under indEMA20//condCutLoss = Close crosses under indBollLoIf condLong = 1 AND barindex > BuyingBI thencondBBlowExit= Low crosses under indBollLocondEMA20Exit = Low CROSSES under indEMA20condCutLoss = Close crosses under indBollLo//if condParabolic = 0 AND condALMAExit thenif condParabolic = 0 AND condBBlowExit thenBBSellSignal = 1//ALMASellSignal = 1//ALMASellSignalBI = barindexendifif barindex > parabolicBI AND condParabolic = 1 AND condEMA20Exit thenEMA20SellSignal = 1//EMA20SellSignalBI = barindexendif//if ALMASellSignal = 1 or EMA20SellSignal = 1 or condCutLoss thenif BBSellSignal = 1 or EMA20SellSignal = 1 or condCutLoss thencondEXIT =1endifIf LongOnMarket AND condEXIT =1 thenSELL AT MARKET//ALMASupportScan = 1BBLoSupportScan = 1SCANRSI = 1SCANBoll = 0SCANMACDx = 0condParabolic = 0condLong = 0BBSellSignal = 0 //ALMASellSignal =0EMA20SellSignal =0condEXIT =0BuyingSignal = 0//condDIVERoutput =0ActivateTrailStop = 0//ScanDiver = 1endifENDIFSET STOP pLOSS 173.0//-------------------------------------------------------------------------// Function : ALMA//-------------------------------------------------------------------------//ALMA Arnaud Legoux Moving Average// parameters// Window = 9// Sigma = 6// Offset = 0.85Price = Closem = (Offset * (Window - 1))s = Window/SigmaWtdSum = 0CumWt = 0for k = 0 to Window - 1 doWtd = Exp(-((k-m)*(k-m))/(2*s*s))WtdSum = WtdSum + Wtd * Price[Window - 1 - k]CumWt = CumWt + WtdnextALAverage = WtdSum / CumWtRETURN ALAverage//-------------------------------------------------------------------------// Function : BollingerBand//-------------------------------------------------------------------------REM Computes the Bollinger's moving average and the Standard DeviationBollingerMA = ExponentialAverage[p](close)REM Computes the Standard DeviationREM Please note that it is much faster to use the built-in function STDIF barindex >= p-1 THENsumy2 = 0sumy = 0FOR i = 0 TO p-1 dosumy2 = sumy2 + SQUARE(close[i])sumy = sumy + close[i]NEXTSTDDEV =SQRT(sumy2 / p - SQUARE(sumy / p))ELSESTDDEV = undefinedENDIFREM We can now compute the Bollinger BandsbollUP = BollingerMA + s * STDDEVbollDOWN = BollingerMA - s * STDDEVbollMID = BollingerMARETURN bollUP AS "Boll+", bollDOWN AS "Boll-", bollMID AS "BollMid"//-------------------------------------------------------------------------// Function : DIVERoutput//-------------------------------------------------------------------------//EMRSDIVERINTER // ######################################################################################################//BEARISH RSI DIVERGENCE//Variable DeclarationsobLevel = 65 //20210308oslevel = 40 //20210308//Indicator DeclarationsindMACDLn = MACDline[12,26,9](close)//indMACDLn = ExponentialAverage[12](close) -ExponentialAverage[26](close)//indMACDSign = MACDSignal[12,26,9](close)indMACDSign = ExponentialAverage[9](indMACDLn)indMACDHis = MACD[12,26,9](close)//indMACDHis = indMACDLn - indMACDSignindRSI = RSI[14](close)//histogram colourif indMACDHis>0 thenr=0g=100b=0elser=255g=0b=0endif//Conditions to Resetob = indRSI>obLevel //20210308oS = indRSI<osLevel //20210308condMACDLnOver0 = indMACDLn crosses over 0condMACDLnUnder0 = indMACDLn crosses under 0//Conditions to Startif barindex = 0 thenInterPriceMax=0InterMACDLnMax =0oldPriceMax = 0oldMACDLnMax=0InterBIMax = 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 = 1endif// Check for bearish crossover and divergencesIf SCANBear=1 and condMACDLnUnder0 and obpassed = 1 thenoldPriceMax = InterPriceMaxInterPriceMax =PriceMaxoldMACDLnMax = InterMACDLnMaxInterMACDLnMax =MACDLnMaxoldBIMax = InterBImaxInterBImax = BIMax//drawtext ("InterMACDLnMax = #InterMACDLnMax#", BIMax,MACDLnMax) coloured(200,0,0)//drawtext ("oldMACDLnMax = #oldMACDLnMax#", BIMax,MACDLnMax*1.1) coloured(200,0,0)//drawtext ("MACDLnMax = #MACDLnMax#", BIMax,MACDLnMax*1.2) coloured(200,0,0)BearishDivMACD = (InterPriceMax >= oldPriceMax) AND (InterMACDLnMax < oldMACDLnMax) AND (BIMax > BIStartBear)if BearishDivMACD thenBIDivBear = BImax//drawtext ("BearishDivMACD = #BearishDivMACD#", BIMax,MACDLnMax) coloured(200,0,0)//drawsegment(oldBIMax,oldMACDLnMax,BIMax,MACDLnMax) coloured(200,0,0)//drawarrowdown(BIMax,MACDLnMax) coloured(200,0,0)SCANBear=0InterPriceMax=0InterMACDLnMax =0oldPriceMax = 0oldMACDLnMax=0endifobpassed = 0endifIf (barindex >= BIDivBear) and BearishDivMACD AND condMACDLnUnder0 ThenEntrySignalBear = -1*(indMACDHis)EntrySignalBIBear = barindexBearishDivMACD =0ClearSignalBear = 1ENDIF//20210308if barindex > EntrySignalBIBear and ClearSignalBear= 1 thenEntrySignalBear = 0ClearSignalBear= 0endif// //20210308//BULLISH MACD RSI DIVERGENCE //###################################################//Conditions to Startif barindex = 0 thenInterPriceMin=100000InterMACDLnMin =100000oldPriceMin = 100000oldMACDLnMin=100000InterBIMin = 0endif//IF os <> os[1] then//20210308If condMACDLnUnder0 thenPriceMin=100000MACDLnMin =100000BIStartBull = barindexEntrySignalBull =0SCANBull = 1ospassed =0endif//endif//20210308//if SCANBull = 1 and barindex >BIStartBull and indMACDHis < 0 thenif 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 = 1WatcherOnBull = 0endif// Check for bearish crossover and divergencesIf SCANBull=1 and condMACDLnOver0 and ospassed = 1 thenoldPriceMin = InterPriceMinInterPriceMin =PriceMinoldMACDLnMin = InterMACDLnMinInterMACDLnMin =MACDLnMinoldBIMin = InterBIminInterBImin = BIMin//drawtext ("InterMACDLnMin = #InterMACDLnMin#", BIMin,MACDLnMin) coloured(200,0,0)//drawtext ("oldMACDLnMin = #oldMACDLnMin#", BIMin,MACDLnMin+20) coloured(200,0,0)//drawtext ("MACDLnMin = #MACDLnMin#", BIMin,MACDLnMin+40) coloured(200,0,0)//declare divergence conditionBullishDivMACD = (InterPriceMin < oldPriceMin ) AND (InterMACDLnMin > oldMACDLnMin)AND (BIMin > BIStartBull)if BullishDivMACD thenBIDivBull = BImin//drawsegment(oldBIMin,oldMACDLnMin,BIMin,MACDLnMin) coloured(200,0,0) //this is it//drawsegment(oldBIMin,oldMACDLnMin,InterBImin,InterMACDLnMin) coloured(200,0,0)//drawarrowup(BIMin,MACDLnMin) coloured(200,0,0) //this is it//drawarrowup(InterBImin,InterMACDLnMin) coloured(200,0,0)//drawtext ("MACDLnMin = #MACDLnMin#", BIMin, MACDLnMin) coloured(200,0,0)//RESET = 1SCANBull=0//drawarrowup(BIMin,indMACDLn) coloured(255,255,0)//EntrySignalBull= 1//indMACDLn//BullishDivMACD =0InterPriceMin=100000InterMACDLnMin =100000oldPriceMin = 100000oldMACDLnMin=100000//InterBIMin = 0endifospassed = 0endifIf (barindex >= BIDivBull) and BullishDivMACD AND condMACDLnOver0 Then//If indRSI < 51 then//drawarrowup(barindex,indMACDLn) coloured(255,255,0)EntrySignalBull= (indMACDHis)EntrySignalBIBull = barindexBullishDivMACD =0ClearSignalBull = 1WatcherOnBull = 1//endifENDIFif barindex > EntrySignalBIBull and ClearSignalBull= 1 thenEntrySignalBull = 0ClearSignalBull= 0endif//return indMACDLn coloured (255,255,0) style(line,1) as "MACDLine",indMACDSign coloured (100,100,100) style (line,1) as "MACDSign",indMACDHis coloured(r,g,b) style(histogram) as "MACDHistogram", 0 coloured(168,168,168) style(line,2) as "0 level",EntrySignalBull coloured(160,75,180) style (histogram) as "Enter here bullish",EntrySignalBear coloured(160,75,180) style (histogram) as "Enter here bearish", WatcherOnBullreturn WatcherOnBull as "WatcherOnBull", EntrySignalBIBull as "EntrySignalBIBull"08/14/2021 at 10:56 AM #175303No, the correct support forum is ProOrder, as it is a stratregy. I moved it.
Below you can read the basic rules highlighted in yellow.
I couldn’t spot any issue. You should post:
- instrument/asset traded
- TF
- date and time of entry of the trade closed incorrectly.
1 user thanked author for this post.
08/14/2021 at 11:40 AM #175309Thanks for correcting, robertogozzi!
-
AuthorPosts
Find exclusive trading pro-tools on