Wait for x bars before entry

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #171194 quote
    ProRealP
    Participant
    Senior

    Hello!

    I’ve been using PRT  and reading the forums for about 7 months, but haven’t posted anything before now.

    My question is quite simple. I would like to wait for x bars before taking an entry when my conditions are met.

    Let’s say my system is something like this

    defparam cumulateorders = false
    
    positionsize = 1
    
    Indicator1 = average[100]
    indicator2 = average[50]
    c1 = indicator2 crosses over indicator1
    
    If c1 then 
    buy positionsize contract at market
    endif
    

    How can i get the buy function to first wait x bars before taking position?
    I’ve searched the forums, but haven’t been able to find anything like this.

    Thanks!

    #171196 quote
    robertogozzi
    Moderator
    Master

    Not tested:

    defparam cumulateorders = false
     N           = 3  //wait 3 bars
    positionsize = 1
    Once count = 0
    Count      = count - 1
    
    Indicator1 = average[100]
    indicator2 = average[50]
    c1 = indicator2 crosses over indicator1
     
    If c1 then
       Cond  = 1
       Count = N
    endif
    
    If cond and count = 0 then
       buy positionsize contract at market
       Cond = 0
    endif
    ProRealP thanked this post
    #171223 quote
    ProRealP
    Participant
    Senior

    Works like a charm!

    Thanks for your help and all the good work you do here, Roberto!

    Appreciate it!

    #171224 quote
    ProRealP
    Participant
    Senior

    I’m experiencing a strange issue. The code works, but sometimes it reverses the position compared to an identical system that doesn’t wait x bars before taking position. The code should only count and then buy/sell, yes? I made it like this to buy and short. Can you see a problem with this, or a reason why it would take a different position?

    N = 3 //wait 3 bars
    Once count = 0
    Count = count - 1
    
    If c5 then
    CondBuy = 1
    Count = N
    endif
    
    If c6 then
    CondSell = 1
    Count = N
    endif
    
    If CondBuy and count = 0 and not onmarket then
    buy positionsize contract at market
    CondBuy = 0
    endif
    
    If CondSell and count = 0 and not onmarket then
    sellshort positionsize contract at market
    CondSell = 0
    endif
    #171225 quote
    robertogozzi
    Moderator
    Master

    Try replacing line 5 with:

    If c5 and Count < 0 then

    and line 10 with:

    If c6 and Count < 0 then
    #171226 quote
    ProRealP
    Participant
    Senior

    It still reverses some positions. It’s quite odd since the conditions are identical..

    #171230 quote
    robertogozzi
    Moderator
    Master

    What exactly do you mean by “it reverses the position” because it can’t reverse as checking if NOT ON MARKET bans  from entering if a trade is open?

    #171235 quote
    ProRealP
    Participant
    Senior

    Quite! It was poorly explained by me. What I mean is that the system without this code takes a long position, and the one with the code then takes a short position instead, but 3 candles later. I believe this picture will make it clearer.

    Capture.jpg Capture.jpg
    #171242 quote
    robertogozzi
    Moderator
    Master

    Try this one:

    N = 3 //wait 3 bars
    Once count = 0
    Count = count - 1
     
    If c5 then
    CondBuy = 1
    CondSell = 0
    Count = N
    endif
     
    If c6 then
    CondSell = 1
    CondBuy = 0
    Count = N
    endif
     
    If CondBuy and count = 0 and not onmarket then
    buy positionsize contract at market
    CondBuy = 0
    endif
     
    If CondSell and count = 0 and not onmarket then
    sellshort positionsize contract at market
    CondSell = 0
    endif
    ProRealP thanked this post
    #171252 quote
    ProRealP
    Participant
    Senior

    Yes, now it’s working flawlessly! Thanks again, Roberto. Much appreciated

Viewing 10 posts - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.

Wait for x bars before entry


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
ProRealP @prorealpierre Participant
Summary

This topic contains 9 replies,
has 2 voices, and was last updated by ProRealP
4 years, 8 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 06/05/2021
Status: Active
Attachments: 1 files
Logo Logo
Loading...