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!
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
Works like a charm!
Thanks for your help and all the good work you do here, Roberto!
Appreciate it!
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
Try replacing line 5 with:
If c5 and Count < 0 then
and line 10 with:
If c6 and Count < 0 then
It still reverses some positions. It’s quite odd since the conditions are identical..
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?
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.
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
Yes, now it’s working flawlessly! Thanks again, Roberto. Much appreciated