Closing all positions each day at a certain time
Author: rdd75
Creation Date: 3/1/2016 8:11 PM
profile picture

rdd75

#1
Hi,

I am looking for a code to close all open positions (no matter of the P&L) each day at a specified time.

Any help is highly apprecoated.

Rouven
profile picture

Eugene

#2
Hi Rouven,

Here's a quick example that requires Community.Components. Run it on 5-minute data.

In a single-position strategy:
CODE:
Please log in to see this code.


In multi-position strategy:
CODE:
Please log in to see this code.
profile picture

rdd75

#3
Thanks very much Eugene
profile picture

rdd75

#4
Works very well when leaving
CODE:
Please log in to see this code.
out. otherwise CS0136 for ASCII with my local time

when using IQFeed (trading hours are labelled 02.00-1600 on the chat facing a Run time error. No error when using off trading hours, just in active trading hours.

CODE:
Please log in to see this code.




profile picture

Eugene

#5
QUOTE:
otherwise CS0136 for ASCII with my local time

That's expected: starting at bar #1 is only good for my example. To avoid an index out of bounds error (IOOB) in real code, you have to:

* use GetTradingLoopStartBar,
* care about indicators that don't have an input parameter named "period" (because GetTradingLoopStartBar is unable to sense such indicators),
* not exceed the largest indicator period by accessing a value N bars ago (e.g. suppose your largest indicator period is 5 bars but you're evaluating its value 20 bars ago),
* and use FirstActualBar if necessary.
profile picture

rdd75

#6
Call me a Muppet, but I can’t get it up and running, still the same issue.

Grateful for any help in the forum.
profile picture

Eugene

#7
Are you sure that the fast MA is 28-period and the slow one is 14-period? Otherwise feel free to unroll my change. Hope this helps:
CODE:
Please log in to see this code.
profile picture

rdd75

#8
Thank you very much for all you time and your help!
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).