Avoid Intraday Reentry
Author: Supersol
Creation Date: 10/1/2015 2:48 AM
profile picture

Supersol

#1
Hy all,

I am currently working on an intraday code which should invest in 5 different stocks. After it has entered these 5 positions it should not invest further.

This does work with a simple MarketOnClose Order at the end of the day, but does not work if a stopp loss is implemented.

When the stop loss is triggered, the system makes new trades on the same day.

Is there a code snippet to prevent this?

Kind regards
Supersol
profile picture

Eugene

#2
No need to code. Here's the solution: Position Options PosSizer > Cap maximum entries per day. Type in "5" near "Max pos. per day" and press the button.
profile picture

Supersol

#3
Dear Eugene,

thank you for the tipp. Max open Positions does work, but the option "Max open in Symbol" does not do anything (see the attached screenshot). Although I activated and restricted the "Max open in symbol" to 1 - it enters in this example 4 Times PDCO on the same day.



Kind regards
profile picture

Eugene

#4
Dear Mathias,

You pressed an unrelated option instead of "Max pos. per day". Neither of the chosen options apply within the same day. No wonder you're not getting the desired result.

P.S. As to "Max open in symbol", it works as intended with multi-position systems in MSB mode.
profile picture

Supersol

#5
ok - I do not really understand your point...


Let me change the question:

I want to backtest an intraday strategy which only enters up to 5 open positions per day where each position should only be in one symbol.

How can I achieve this? The settings as mentioned before did not work (as you can see)

Kind regards
Matthias
profile picture

Eugene

#6
QUOTE:
The settings as mentioned before did not work (as you can see)

Of course. Isn't it what I was trying to explain? You pressed the wrong button. "Max Open Positions" imposes a global limit (not per day). It should be "Max pos. PER DAY" if you wish to limit the number of positions taken in a trading session to 5. Activating it together with "Max open in symbol" set to 1 should do the job:

profile picture

Supersol

#7
Aaaaah. Thumbs up - now I know what you mean - Thank you very much Eugene!
profile picture

Supersol

#8
No, sorry Eugene, does not work as expected and still takes more than one trade in one symbol

profile picture

Eugene

#9
Matthias, I positively can not reproduce this. The PosSizer works as expected on both daily and intraday data:



As can be seen, the conditions have been met: symbols are unique, only 5 trades in a day, one trade per symbol per day.
profile picture

Cone

#10
Matthias said,
QUOTE:
working on an intraday code which should invest in 5 different stocks

This is the key. The PosSizer prevents multiple simultaneous positions, but if you enter and exit, you can still buy the same symbol again. To prevent trading multiple times in the same day for the the same symbol, you need Strategy code. Here's the design pattern:

CODE:
Please log in to see this code.


Eugene, unless we already have it somewhere else, I'll add this to the Intraday | Building blocks of Intraday trading strategies wiki.

p.s. In Community Rules, there's a Position-Based Rule "On entry per day" that uses this code.
profile picture

Eugene

#11
Robert, please add the design pattern to the Wiki. Thanks.
profile picture

Eugene

#12
Guys, I'm still curious to find out why the PosSizer doesn't work for you. Here's my code, adapted from the MP template in the WealthScript Guide:

CODE:
Please log in to see this code.


As can be seen, it is set to mindlessly enter and exit (and re-enter and so on) on every bar through the day. Now I run this in MSB mode on 5-minute bars of the Dow 30 DataSet and get only 5 trades a day - each in a unique stock symbol:



What am I doing wrong?
profile picture

Cone

#13
Eugene, just run it on one symbol and you'll get 5 trades, right? Run it on 2 symbols, and you'll get 3 trades on one symbol and 2 on the other... it all depends on the sequence.
profile picture

Eugene

#14
QUOTE:
just run it on one symbol and you'll get 5 trades, right?

Nope, I get exactly 1 trade per day as advertised.
profile picture

Cone

#15
Then we're definitely doing something different. Here's the complete script:
CODE:
Please log in to see this code.


And my results with one symbol:
profile picture

Eugene

#16
After restarting WLD, now I see it. You're right. No idea why it worked for me (once) in single-symbol mode. Should have enclosed a screenshot next to my post #14.
profile picture

Cone

#17
Matthias, in case you missed it, please see post #10 above for the solution for 1 trade per day per symbol.
profile picture

Supersol

#18
So, I finally tried both of your solutions but unfortunately with each of them, I don't make any trade (although I did with the previous code)

The main difference between my code and yours (from Nr. 10) , is that my

CODE:
Please log in to see this code.


is located in the if Loop
CODE:
Please log in to see this code.


and yours in the for Loop
CODE:
Please log in to see this code.


Any idea on that?

Kind regards
profile picture

Cone

#19
Probably it didn't trade because there's a compile error when including the "Position p = LastPosition;" statement twice.

I gave you a design pattern in post #10, so you have to follow the pattern... remove the "Position p = LastPosition;" statement from inside your exit logic.

In this case, it has to be where I put it because there are conditions below it that need that p variable.
This website uses cookies to improve your experience. We'll assume you're ok with that, but you can opt-out if you wish (Read more).