not taking any more trade in one direction until another condition is met?
Forums › ProRealTime English forum › ProOrder support › not taking any more trade in one direction until another condition is met?
- This topic has 9 replies, 2 voices, and was last updated 3 years ago by Oliviertrader2020.
Tagged: divergences
-
-
01/16/2021 at 1:11 AM #158013
Hello
When a condition is met, how to prevent his algo from not taking any more trade in one direction until another condition is met?
I have an algo that takes trades (buy and sell) with different filters and a trigger on a TF15min, so :
IF the daily RSI is > 70 and indicates a bearish divergence …
The algo must not take any more buy (but it can sell) AS SOON AS …
The daily RSI is > 30The difficulty is that when the bearish divergence is no longer active the algo resumes buy trades.
During and after the divergence, how to prevent it from taking buy trades as long as the RSI is above 30?01/16/2021 at 5:04 AM #158015Usually a divergence is detected once, not every bar, so it shouldn’t be true for more than one bar!
Post an example, your description is not clear enough. AS SOON AS doesn’t help much.
01/16/2021 at 6:10 AM #158016Hello @robertogozzi, here is an example:
1. At 1:45 pm, the indicator PRC_AnotherRSIdivergence signals by a red bar a bearish divergence.
2. From this signal, I want my robot to stop buying as long as the RSI is > 30 (but the robot can sellBut the robot must be able to sell if there is a sell signal)In the image you can see the gray area between the beginning of the signal and the moment when the RSI is < 30. If it can be useful I attach the code of the indicator:
PRC_AnotherRSIdivergence1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768//PRC_AnotherRSIdivergences | indicator//25.02.2019//Nicolas @ www.prorealcode.com//Sharing ProRealTime knowledge// — settingsRSIp=10obLevel=70osLevel=30// — end of settingsirsi = rsi[RSIp]ob = irsi>obLevelos = irsi<osLevelif ob thenif not ob[1] thenmaxrsi = 0maxprice = 0endifmaxrsi=max(maxrsi,irsi)maxprice=max(maxprice,high)if maxrsi<>maxrsi[1] thenmaxrsibar=barindexendifendifif os thenif not os[1] thenminrsi = 100minprice = close*100endifminrsi=min(minrsi,irsi)minprice=min(minprice,low)if minrsi<>minrsi[1] thenminrsibar=barindexendifendifdivsell=0if irsi crosses under obLevel then//verif divergencediv = maxprice>oldmaxprice and maxrsi<oldmaxrsiif div thendrawsegment(oldmaxrsibar,oldmaxrsi,maxrsibar,maxrsi) coloured(200,0,0)drawarrowdown(maxrsibar,maxrsi) coloured(200,0,0)divsell=osLevelendifoldmaxrsi = maxrsioldmaxprice = maxpriceoldmaxrsibar = maxrsibarendifdivbuy=0if irsi crosses over osLevel then//verif divergencediv = minprice<oldminprice and minrsi>oldminrsiif div thendrawsegment(oldminrsibar,oldminrsi,minrsibar,minrsi) coloured(0,200,0)drawarrowup(minrsibar,minrsi) coloured(0,200,0)divbuy=osLevelendifoldminrsi = minrsioldminprice = minpriceoldminrsibar = minrsibarendifreturn irsi style(line,2),obLevel coloured(168,168,168) style(dottedline,1), osLevel coloured(168,168,168) style(dottedline,1), divsell coloured(200,0,0) style(histogram), divbuy coloured(0,200,0) style(histogram)01/16/2021 at 9:26 AM #158021Your post is not clear enough and you posted the indicator, instead of the code of the strategy.
As you can see from your pic:
- the bearish divergence only lasts one bar, why buying the next bars?
- it’s a bearish divergence, why buying instead of going short?
- what has RSI (< 30 or > 30? your two posts have two different operators) to do exactly with the divergence?
Post the code of your strategy.
01/16/2021 at 9:30 AM #158022DO NOT use @ to reference me, I know, it’s just the two of us!
Use that sign with care and only when there are many people involved.
Thank you 🙂
01/16/2021 at 10:17 AM #158026Ok, I’ll try to explain again:
– The goal is to create a filter that prevents taking purchases on a smaller TF (15min) as long as there is a downward divergence on a larger TF (daily).
– This filter will be used in different strategies, so there is no specific strategy, but there is a specific filter:
.
– This filter is :
1. If bearish divergence detected by the indicator PRC_AnotherRSIdivergence (on TF daily)
2. THEN do not take any more purchase (on TF 15min) AS…
3. the daily RSI is higher than > 30 (so yes, we will resume purchases when the RSI is lower than 30, it’s logical 😉 )For example :
First picture: filter on large time frame that starts with the report of the downward divergence and ends when the RSI is < 30. Second picture : on a small time frame, the purpose of this filter is to prevent the robot to take the purchases (signal with green arrows) while letting it take the sales (signal with red candles). If you need an example of strategy to code the filter with (I don't use this strategy but it's to have a simple example) :
Simple Stratégy1234567891011121314151617181920212223242526272829303132333435// Définition des paramètres du codeDEFPARAM CumulateOrders = False // Cumul des positions désactivé// Conditions pour ouvrir une position acheteuseindicator1 = Average[7](close)c1 = (close < indicator1)indicator2 = Average[7](close)indicator3 = Average[20](close)c2 = (indicator2 < indicator3)indicator4 = Average[20](close)indicator5 = Average[50](close)c3 = (indicator4 < indicator5)c4 = (close > high[1])IF c1 AND c2 AND c3 AND c4 THENBUY 1 SHARES AT MARKETENDIF// Conditions pour ouvrir une position vendeuseindicator6 = Average[7](close)indicator7 = Average[20](close)c5 = (indicator6 > indicator7)indicator8 = Average[20](close)indicator9 = Average[50](close)c6 = (indicator8 > indicator9)indicator10 = Average[7](close)c7 = (close > indicator10)c8 = (close < low[1])IF c5 AND c6 AND c7 AND c8 THENSELLSHORT 1 LOT AT MARKETENDIFSET STOP $LOSS 1000SET TARGET $PROFIT 100001/16/2021 at 1:02 PM #158059Ok, you don’t like working examples, so I will post some general suggestions.
Use a flag (a variable that signals something), in this case TRADEON, which defaults to 1, then is cleared whenever a trade is entered, until the next day or a different divergence occurs. This willmake your 15-minute TF tp enter only once per divergence.
1234567891011121314151617181920Timeframe(Daily,UpdateOnClose)BullishDiv,BearishDiv = CALL "MyDivergenceIndicator" //returns 1 or 0 for either typeTimeframe(15 minute,UpdateOnClose)ONCE TradeON = 1 //1 = trading allowedIF (IntraDayBarIndex = 0) THENTradeON = 1 //1 = trading allowed again each new dayENDIFIF (BullishDiv <> BullishDiv[1]) OR (BearishDiv <> BearishDiv[1]) THENTradeON = 1 //1 = trading allowed whenever the indicator changes returned valuesENDIFIF BullishDiv AND Not OnMarket AND TradeON THEN //check also that TradeON is 1..TradeON = 0 //0 = trading not allowed after the first entry of the day or with the same divergenceENDIFIF BearishDiv AND Not OnMarket AND TradeON THEN //check also that TradeON is 1..TradeON = 0 //0 = trading not allowed after the first entry of the day or with the same divergenceENDIFYou can also use BullishDiv and BearishDiv to not enter a trade the opposite direction.
1 user thanked author for this post.
01/16/2021 at 3:06 PM #158072Thank you for your suggestions, they help me understand the process.
I have coded an algo that uses the purchase conditions of the example above (I have removed the sales to simplify) in TF 15min.
I added a condition to prevent new trades from being taken, as soon as a bearish divergence is detected (on a TF H1) and until the RSI is below 30 (also on a TF H1).
But as you can see in the pictures, the robot takes trades in the gray areas, but it shouldn’t take any.
Can you tell me the mistakes I made in the code?
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849DEFPARAM CumulateOrders = False// FILTRE ANTI DIVERGENCETimeframe(60minutes, UpdateOnClose)divbuy, divsell = CALL "PRC_AnotherRSIdivergences"Indicateur1 = RSI[14](close)Timeframe(15minutes, UpdateOnClose)ONCE TradeOn = 1IF divsell AND Not OnMarket THENTRADEON = 0EndifIF (TradeOn = 0) AND (Indicateur1 < 30) THENTradeON = 1ENDIF// Conditions pour ouvrir une position acheteuseindicator1 = Average[7](close)c1 = (close < indicator1)indicator2 = Average[7](close)indicator3 = Average[20](close)c2 = (indicator2 < indicator3)indicator4 = Average[20](close)indicator5 = Average[50](close)c3 = (indicator4 < indicator5)c4 = (close > high[1])ConditionsAchat = (c1 AND c2 AND c3 AND c4)IF ConditionsAchat THENBUY 1 SHARES AT MARKETENDIF// Conditions pour fermer une position acheteuseindicator6 = Average[20](close)c5 = (high >= indicator6)ClotureAchat = c5If LongOnMarket AND ClotureAchat THENSELL AT MARKETENDIF01/16/2021 at 3:12 PM #158076Add AND TradeON to line 36:
1ConditionsAchat = (c1 AND c2 AND c3 AND c4) AND TradeON1 user thanked author for this post.
01/16/2021 at 4:59 PM #158085Thank you very much for your help!
I added TradeON to line 36, and as you can see, there are no more trades in the gray areas.
I will create new filters and improve my algorithms with your suggestions. Have a nice day 🙂1 user thanked author for this post.
-
AuthorPosts