Close Out Positions At End Of Day
Author: kcbars
Creation Date: 11/15/2010 4:18 PM
profile picture

kcbars

#1
I'm trying to create, either via rules or code, a way to close out positions at the end of every day. I figured out how to do it via code by using the total minute bars for a given day:

if (LastActivePosition != null && LastActivePosition.Active)
if (bar - LastActivePosition.EntryBar >= 387 - LastActivePosition.EntryBar)
CoverAtMarket(bar + 1, p, "Group1")

But this will only work for one day at a time. I'm wondering if there is an easier way that I'm missing, or a way to reset the count at the end of each day or something? Thank you.
profile picture

Eugene

#2
Checking out the Wiki Knowledge Base proves to be a good idea:

KB > Intraday / Multi-Time Frame | Keep from holding positions overnight
profile picture

kcbars

#3
Eugene, thank you for the tip.
profile picture

kcbars

#4
I'm trying to close open multiple positions at the end of the day. The code below seems to work for some symbols, but for some I get a bar number error. (Runtime Error - Bar number must be 390 or greater). Any help or ideas would be greatly appreciated. Thanks.


CODE:
Please log in to see this code.
profile picture

Eugene

#5
QUOTE:
(Runtime Error - Bar number must be 390 or greater).

It happens when a CrossUnder occurs at the last bar of day. Try this code:
CODE:
Please log in to see this code.
profile picture

kcbars

#6
Thank you for this - it works well. I noticed you changed the loop to 421. Is this to cycle through an entire day up front? Also, if I were to ever start auto trading, if I understand correctly you should not use AtClose functions? In this case, could you use CoverAtMarket instead of CoverAtClose?
profile picture

Eugene

#7
That's a remnant (just fixed), feel free to use "21" as it was.
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).