Exit Positions at End of Day using Rules?
Author: marty n
Creation Date: 3/25/2009 1:34 AM
profile picture

marty n

#1
Just getting started with Wealth Lab...

Is it possible to close all open positions at end of day using Rules or does it have to be coded?

Thanks (if this was covered previously, post the link - thanks)
profile picture

Eugene

#2
The Rules wizard does not have this rule, so it has to be coded. This was covered many times before on the forums.

For instance, here's a couple of Wiki examples that do this one way or another:
Intraday / Multi-Time Frame | Keep from holding positions overnight
Mixing intraday and daily data
Intraday strategy based on time of day

In your particular case, the method call should be referencing Position.AllPositions to close all postitions simultaneously (more details in the QuickRef -- just hit F11).
profile picture

marty n

#3
Thanks, I'll try these.
Marty
profile picture

marty n

#4
Having Trouble with Code... 2 Problems

I tried inserting the code from one of the links you suggested but I'm having trouble getting it to compile. I keep getting the CS1513 error.
profile picture

Eugene

#5
1. Please use the CODE tags when posting code.
2. Please don't cross post. I've answered your request for Why No Shorts in a SAR System?, so the duplcating code and request here were removed.

QUOTE:
I tried inserting the code from one of the links you suggested but I'm having trouble getting it to compile.

Can you show us the code?
QUOTE:
I keep getting the CS1513 error.

Compiler Error CS1513
profile picture

marty n

#6
Thank you and truly sorry about the cross posting. I thought the no shorts issue was more of a strategy issue - won't happen again.

Also, I don't know how to use Code Tags. I looked it up on the MSDN Visual C# Developer Center, but didn't understand it.

Here is the code. Using the corrected code for a SAR strategy you provided -- THANKS AGAIN -- I added the code from one of the links you suggested (http://www2.wealth-lab.com/WL5Wiki/kbIntradayOvernight.ashx):

CODE:
Please log in to see this code.

I tried several corrections to eliminate the CS1513 put since I have almost zero - maybe less than zero - knowledge of Wealthscript, I couldn't get to compile.

Thanks again for your help. (It's really not my attention to get a lot of free help from you and others. I had used Tradestation for several years and was getting used to their "Easylanguage" but programming is definitely not my talent and this language really has me stumped.)
profile picture

Eugene

#7
1. The CODE tags are right here on the forum -- just click the CODE button (among the Bold, Italic, Quote etc.) and it will look like the one above (I edited your post).
2. You're trying to paste a class inside another class, it's not going to work. Please consider reviewing this tip:
How do I start with C# ?

I'll try to get this code done for you but first please tell more about the rules. I see it's a cross over/under strategy from another forum thread; so what you want to add is to close an open position at end of day and start looking for new opportunities the next day?
profile picture

marty n

#8
Eugene,

Thanks so much for your help. I appreciate your taking the extra effort to understand what I'm trying to do. I hope I can return the favor someday.

1. I think I understand the code tags. Thanks.

2. Yes. That's correct. I want to close any open position around 15 minutes before the close of the day and start fresh the next trading day. I liked the idea of being able to specify the time the market closes in case of a shortened trading day.

Thanks gain for your help.

Marty
profile picture

Eugene

#9
QUOTE:
2. Yes. That's correct. I want to close any open position around 15 minutes before the close of the day and start fresh the next trading day. I liked the idea of being able to specify the time the market closes in case of a shortened trading day.

Here's a quick try:
CODE:
Please log in to see this code.
profile picture

marty n

#10
Eugene,

Thank you! It works fine and I'll try to learn from what you did so I don't have to ask you for so much help in the future.

I'd like to ask you something which is not totally relevant to this forum... can you give me your e-mail, or e-mail me at rotors88-stocks@yahoo.com

Thanks again.
profile picture

Eugene

#11
Yes, feel free to contact me here.
profile picture

marty n

#12
Hi Eugene,

I noticed that some positions don't close at the end of the day when backtesting in a chart.

E.g., on a 15 minute chart of AA, a Buy was generated on 3/26 at the open of the 3:45 bar -- pprice = 8.06. This position wasn't sold until 3/27 at the open of the 10:00 bar.

Similarly, a Buy was generated on 3/20 at the open of the 3:45 bar, which didn't get sold until 3/24 at the open of the 10:00 bar.

I see the Slider to enter the closing time in the Strategy Monitor. Will closing positions at the end of the day be implemented only in the Strategy Monitor, but not in a chart?

Thanks,
Marty
profile picture

Eugene

#13
Thanks for pointing this out. Here's corrected code.
CODE:
Please log in to see this code.
profile picture

marty n

#14
Thank you Eugene. Like some other Wealth Lab users, I'm trying to learn more by looking at scripts generated by Rules and yourself, and trying different modifications - but it's a slow go and I usually don't get it right. Thanks for helping!

On an unrelated question, I noticed that on some symbols, e.g. SKF, 15 minute and daily data is available, but not 30 or 60 minute data. Am I doing something wrong?

Thanks again,
Marty
profile picture

marty n

#15
Hi Eugene,

I tried to adapt your code to a strategy from rules; All I wanted to add was the end of day exit.

I'm not sure where my mistake is, but I'm down to one CS1513 error and I can't figure what's wrong. I would appreciate your help, if you have the time.

Thanks.

Here's the code:

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

marty n

#16
Sorry about the extra post. In the above code, I commented out the code from the SAR strategy you wrote so I could see the code and how your strategy works.

Thanks again.
profile picture

Eugene

#17
Sorry but that's a real mess. You can't just glue things that easily, including one trading loop into another, adding the ExitAtMarket line that uses a variable (p) outside its scope and in an inappropriate place etc. C# is real programming language.
CODE:
Please log in to see this code.
profile picture

marty n

#18
"C# is a real programming language"

That's my problem. I wish Rules had a few more options or parameters that would enable some things - like exiting all positions at Market Close - without requiring code writing. What I thought might be a simple cut and paste was obviously much more complex.

But like I said, that's my problem. I'm grateful for your assistance and your generosity in devoting so much time to helping this forum.

Thank you!!!
Marty
profile picture

marty n

#19
Thanks for all your help.

I modified the strategy slightly. Although I want to exit positions at the end of the day, I want to get back in shortly after the open if there are no major price changes at the next open.

I changed the strategy's entry requirements to enter again the next day if MACD is still above the required value but what happens now is that after a position exits at MOC, the strategy sometimes enters again at the end of the same day.

What I'd like to do is prevent the strategy from entering ANY trades after 15:45 (or 15 minutes before market close and certainly after the MOC exit has been triggered). I'm not sure how best to do this without messing up this strategy all over again.

Also, and this is not very important, is it possible to vary the time the positions are closed, for example at 15:55 although I'm using 15 minute bars. If the latest I can exit using 15-miute bars id 1:45, that's fine.

Thanks gain for your help.

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

Eugene

#20
QUOTE:
I changed the strategy's entry requirements to enter again the next day if MACD is still above the required value but what happens now is that after a position exits at MOC, the strategy sometimes enters again at the end of the same day.

What you have done is replacing the CrossOver condition with a requirement to enter any time whenever MACD is above 0 - and as long as it holds true, a re-entry can follow on any bar.
profile picture

marty n

#21
I thought that might be the case. But is it possible to code a restriction that prevents any entry after a certain time.
profile picture

Eugene

#22
Yes, it's possible -

FAQ: How do I code a rule for time of the day?

Or you can re-use that LastBar boolean condition by checking that it's not valid on the bar preceding the entry.
profile picture

marty n

#23
Thanks - I'll try my best to code it without ruining the strategy.
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).