Morning,
I’m very new to PRT coding (though not the markets in general) and have been trying to work on a 15-minute trend reversal strategy for the US 500.
I’ve pulled several examples of very useful code (mostly from Nicolas) but tonight I’ve become stuck trying to limit the system to just taking one trade per day. I’ve found multiple examples of how to fix the problem in the forums but when I try to institute each and every one of these (which are all similar), the strategy still occasionally takes 2 trades per day when I backtest (via IG) over the past 6 months and I can’t work out what I’m doing wrong.
Any help would be much appreciated.
Here is the code I’ve cobbled together so far (in the attachments).
Mark
* Accidentally uploaded the first pic twice – apologies as it didn’t seem to work initially
There you go:
Once TradeON = 1
If IntraDayBarIndex = 0 then
TradeON = 1
Endif
If OnMarket or StrategyProfit <> StrategyProfit[1] Then
TradeON = 0
Endif
If MyConditions and TradeON and Not OnMarket Then
BUY/SELLSHORT.......
Endif
use TradeON as an additional condition to enter a trade.
Many thanks for your swift reply Roberto!
I’ll give that a whirl later this morning.
Greatly appreciated.
Mark
@robertogozzi
Hi Roberto,
I’ve just integrated that nice piece of code you sent me this am but frustratingly the order list indicates the strategy is still occasionally taking 2 trades in the same day (12 June, 1st July and 8th July). See attached pics with updated code.
Any idea how to get around this?
Mark
Please paste the code using the “Insert PRT code” button, not pics of it, otherwise I cannot use it.
Thank you 🙂
Moreover, use “@” only when there are so many users posting and you absolutely need to reference one of them. In this case it’s just the two of us. Because everytime you put that sign in front of a username, that user will receive an email.
Thank you 🙂
Hi Roberto,
Thanks for letting me know how things work and apologies for using ‘@’ . I hadn’t even realised there was an ‘insert PRT code’ button!
As requested, please find the code attached.
Any ideas how to ensure the strategy only takes 1 trade/day?
Many thanks
Mark
//US 500
//15-min TF
DEFPARAM CumulateOrders=false
//UK(London) time
DefParam FlatBefore = 100000
DefParam FlatAfter = 230000
IF Time >= 100000 AND Time <= 170000 OR Time >= 180000 AND Time <= 203000 THEN
Tradetime = 1
else
Tradetime = 0
endif
Once TradeON = 1
If IntraDayBarIndex = 0 then
TradeON = 1
Endif
If OnMarket or StrategyProfit <> StrategyProfit[1] Then
TradeON = 0
Endif
PositionSize = 50
CorrectVol = high-low[1]>5 AND high-low[2]>5 and high-low[3]>5 //volatility of preceding 3 candles is sufficient for trade to occur
sto = stochastic[8,3]
c1 = close[1]<open[1] and close>open
c2 = close>open[1] AND close >= (high-low)*0.60 + low
c3 = lowest[3](low)<lowest[50](low)[1] or lowest[3](low)<lowest[50](low)[2] or lowest[3](low)<lowest[50](low)[3]
c4 = summation[3](sto<20)>0
c5 = close[1]>open[1] and close<open
c6 = close<open[1] AND close <= (high-low)*0.40 + low
c7 = highest[3](high)>highest[50](high)[1] or highest[3](high)>highest[50](high)[2] or highest[3](high)>highest[50](high)[3]
c8 = summation[3](sto>80)>0
buyCondition = c1 and c2 and c3 and c4
sellCondition = c5 and c6 and c7 and c8
// BUY order
IF NOT OnMarket AND CorrectVol AND buyCondition AND Tradetime and TradeON THEN
BUY PositionSize CONTRACTS AT MARKET
SET STOP LOSS abs(close - (lowest[3](low) - 1 * pipsize))
set target $profit 600
ENDIF
// SELL order
IF NOT OnMarket AND CorrectVol AND sellCondition AND Tradetime and TradeON then
SELLSHORT PositionSize CONTRACTS AT MARKET
SET STOP LOSS abs(close - (highest[3](high) + 1 * pipsize))
set target $profit 600
ENDIF
If LongonMarket and (high-low[1]) <3 AND (high-low[2]) < 3 THEN //if volatility very low intra-day then exit positions
sell positionSize contracts at market
Endif
If ShortOnMarket AND (high-low[1]) <3 AND (high-low[2]) < 3 THEN
buy positionSize contracts at market
Endif
It’s because at line 62 you are using BUY to exit a Short trade. No, you’ll have to replace it with EXITSHORT.
To recap:
- BUY opens a Long trade
- SELL exits a Long trade
- SELLSHORT opens a Short trade
- EXITSHORT exits a Short trade
using BUY at line 62 implies a Stop & Reverse, since the Short trade is first closed and new Long one is subsequently opened.
Ahhh silly me. I knew it had to be be something fundamental I was doing wrong.
Have now corrected this and it works perfectly.
Hugely appreciate your help with this Roberto!
Many thanks again
Mark
is there a different way to limit to ONE trade a day then: IntraDayBarIndex
When my position ends before the first bar, it does not work. if my position closes at 0 bars.. it takes a new position.
Have a look how this code is used
OTD = Barindex - TradeIndex(1) > IntradayBarIndex // limits the (opening) trades till 1 per day (OTD One Trade per Day)
if time >= StartE And time <= StartL and OTD then //and not onmarket then
IF conditionsmet then
buy N shares AT MARKET
endif
Taken from this strategy here:
Optimization moving average crossing strategy with “machine learning”