not taking any more trade in one direction until another condition is met?

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #158013 quote
    Oliviertrader2020
    Participant
    Average

    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 > 30

    The 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?

    #158015 quote
    robertogozzi
    Moderator
    Master

    Usually 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.

    #158016 quote
    Oliviertrader2020
    Participant
    Average

    Hello @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_AnotherRSIdivergences | indicator
    //25.02.2019
    //Nicolas @ www.prorealcode.com
    //Sharing ProRealTime knowledge
    
    // --- settings
    RSIp=10
    obLevel=70
    osLevel=30
    // --- end of settings
    
    irsi = rsi[RSIp]
    ob = irsi>obLevel
    os = irsi<osLevel
    
    if ob then
    if not ob[1] then
    maxrsi = 0
    maxprice = 0
    endif
    maxrsi=max(maxrsi,irsi)
    maxprice=max(maxprice,high)
    if maxrsi<>maxrsi[1] then
    maxrsibar=barindex
    endif
    endif
    
    if os then
    if not os[1] then
    minrsi = 100
    minprice = close*100
    endif
    minrsi=min(minrsi,irsi)
    minprice=min(minprice,low)
    if minrsi<>minrsi[1] then
    minrsibar=barindex
    endif
    endif
    
    divsell=0
    if irsi crosses under obLevel then
    //verif divergence
    div = maxprice>oldmaxprice and maxrsi<oldmaxrsi
    if div then
    drawsegment(oldmaxrsibar,oldmaxrsi,maxrsibar,maxrsi) coloured(200,0,0)
    drawarrowdown(maxrsibar,maxrsi) coloured(200,0,0)
    divsell=osLevel
    endif
    oldmaxrsi = maxrsi
    oldmaxprice = maxprice
    oldmaxrsibar = maxrsibar
    endif
    
    divbuy=0
    if irsi crosses over osLevel then
    //verif divergence
    div = minprice<oldminprice and minrsi>oldminrsi
    if div then
    drawsegment(oldminrsibar,oldminrsi,minrsibar,minrsi) coloured(0,200,0)
    drawarrowup(minrsibar,minrsi) coloured(0,200,0)
    divbuy=osLevel
    endif
    oldminrsi = minrsi
    oldminprice = minprice
    oldminrsibar = minrsibar
    endif
    
    return 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)
    
    NO-buy.png NO-buy.png
    #158021 quote
    robertogozzi
    Moderator
    Master

    Your 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.

    #158022 quote
    robertogozzi
    Moderator
    Master

    DO 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 🙂

    #158026 quote
    Oliviertrader2020
    Participant
    Average

    Ok, 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) :

    // Définition des paramètres du code
    DEFPARAM CumulateOrders = False // Cumul des positions désactivé
    
    // Conditions pour ouvrir une position acheteuse
    indicator1 = 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 THEN
    BUY 1 SHARES AT MARKET
    ENDIF
    
    // Conditions pour ouvrir une position vendeuse
    indicator6 = 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 THEN
    SELLSHORT 1 LOT AT MARKET
    ENDIF
    
    SET STOP $LOSS 1000
    SET TARGET $PROFIT 1000
    NO-buy-1.png NO-buy-1.png No-buy2.png No-buy2.png
    #158059 quote
    robertogozzi
    Moderator
    Master

    Ok, 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.

    Timeframe(Daily,UpdateOnClose)
    BullishDiv,BearishDiv = CALL "MyDivergenceIndicator"  //returns 1 or 0 for either type
    Timeframe(15 minute,UpdateOnClose)
    ONCE TradeON = 1                     //1 = trading allowed
    IF (IntraDayBarIndex = 0) THEN
       TradeON = 1                       //1 = trading allowed again each new day
    ENDIF
    IF (BullishDiv <> BullishDiv[1]) OR (BearishDiv <> BearishDiv[1]) THEN
       TradeON = 1                       //1 = trading allowed whenever the indicator changes returned values
    ENDIF
    IF 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 divergence
    ENDIF
    IF 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 divergence
    ENDIF

    You can also use BullishDiv and BearishDiv to not enter a trade the opposite direction.

    Oliviertrader2020 thanked this post
    #158072 quote
    Oliviertrader2020
    Participant
    Average

    Thank 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?

    DEFPARAM CumulateOrders = False
    
    // FILTRE ANTI DIVERGENCE
    Timeframe(60minutes, UpdateOnClose)
    
    divbuy, divsell = CALL "PRC_AnotherRSIdivergences"
    Indicateur1 = RSI[14](close)
    
    Timeframe(15minutes, UpdateOnClose)
    
    ONCE TradeOn = 1 
    
    IF divsell AND Not OnMarket THEN
    TRADEON = 0
    Endif
    
    IF (TradeOn = 0) AND (Indicateur1 < 30) THEN
    TradeON = 1                       
    ENDIF
    
    
    // Conditions pour ouvrir une position acheteuse
    indicator1 = 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 THEN
    BUY 1 SHARES AT MARKET
    ENDIF
    
    // Conditions pour fermer une position acheteuse
    
    indicator6 = Average[20](close)
    c5 = (high >= indicator6)
    
    ClotureAchat = c5
    If LongOnMarket AND ClotureAchat THEN
    SELL AT MARKET
    ENDIF
    No_trade_allowed_on_divergence.png No_trade_allowed_on_divergence.png
    #158076 quote
    robertogozzi
    Moderator
    Master

    Add AND TradeON to line 36:

    ConditionsAchat = (c1 AND c2 AND c3 AND c4) AND TradeON
    Oliviertrader2020 thanked this post
    #158085 quote
    Oliviertrader2020
    Participant
    Average

    Thank 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 🙂

    robertogozzi thanked this post
    No-trade_Done.png No-trade_Done.png
Viewing 10 posts - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.

not taking any more trade in one direction until another condition is met?


ProOrder: Automated Strategies & Backtesting

New Reply
Summary

This topic contains 9 replies,
has 2 voices, and was last updated by Oliviertrader2020
5 years, 1 month ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 01/16/2021
Status: Active
Attachments: 5 files
Logo Logo
Loading...