Exit Only Strategy
Author: thodder
Creation Date: 7/8/2009 1:29 PM
profile picture

thodder

#1
I feel comfortable entering new positions with Fidelity.com, ATP or WLP (manually); however, I'm not good at following through with my exit plan. I'd like to write a strategy for exiting positions only; however, my understanding from Wealth-Lab support is that a single strategy can only by used to exit its own positions - not positions created by another strategy or ones manually entered.

I have a couple of concerns:
1) It would be nice to automate my exit strategy without creating new positions by WLP.
2) What happens if I decide to remove a security from the Strategy DataSet in WLP because I do not want to open a new position? The strategy will no longer be run against that security to test for exiting it. This appears to be a dangling position that will need to be exited manually -- just what I want to automate.

Here is example code that I tried to sell off existing securities in my account:
CODE:
Please log in to see this code.
profile picture

Cone

#2
QUOTE:
my understanding from Wealth-Lab support is that a single strategy can only by used to exit its own positions - not positions created by another strategy or ones manually entered.
Not entirely correct. It's true that a Strategy has to create a theoretical position in order to exit it. But, in the Trading Preferences (F12), select the option "Exit Orders (Sell and Cover) should always exit the full Position currently held". That will cause the number of shares in the Alert to match the number in your account when the order is Staged or Placed.

QUOTE:
1) It would be nice to automate my exit strategy without creating new positions by WLP.
Sorry, there cannot be another way. You just have to create a dummy Position for the day and then let your exit logic work with it.

QUOTE:
2) What happens if I decide to remove a security from
You mean, "why would I shoot myself in the foot if I still want to walk?" This makes no sense to me.
profile picture

Cone

#3
To beat you to the next question, here's an "Exit Script" that you can start with (assumes long Position). You need only to change the exit condition to do what you want...and don't forget to use the Trading Preference.

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

thodder

#4
Creating a dummy order seems straight forward. Your assumption above is that I only have long positions in my account (which I do at the moment), but it doesn't look like this would exit my short positions properly. I tried a quick test to ShortAtMarket (instead of BuyAtMarket), and the ExitAtMarket did not exit my long positions.

Since the dummy order must match the position type of the existing order for this to work, I would need a DataSet for longs and another for shorts that I hold. I would have separate strategies to handle exiting long and short positions and assign them to the appropriate DataSet.

(I suppose I could use both BuyAtMarket() and ShortAtMarket() to generate dummy orders for both position types and then let orders that are invalid fail when they are placed. I haven't tested this but I assume it would work. It doesn't seem like a clean solution, but it might be simpler to manage the DataSet.)

The Alerts in Strategy Monitor only reflect the Qty in my dummy order. When I stage the order, the quantity is adjusted to match the full positions held. This was confusing when I first tested it, so I am just making a note of it for anyone reading this post. (You stated that above, but I'm repeating it for clarity.)

To keep the sell off code simple, this is what I have for testing exiting the long positions and it appears to generate the correct orders:
CODE:
Please log in to see this code.

profile picture

Eugene

#5
QUOTE:
I tried a quick test to ShortAtMarket (instead of BuyAtMarket), and the ExitAtMarket did not exit my long positions.

Position.AllPositions is a shortcut used to sell/cover only long or short active positions. Since your requirement is to have both at the same time, you can loop over active positions and test for position type:
CODE:
Please log in to see this code.
profile picture

Cone

#6
thodder, the script I gave was an example, but a good one. In your rewrite (that doesn't have any condition for exiting) you ignored the logic of entering the dummy position prior to the current day and using the loop to exit. If you're trading an intraday script, you'll find your logic is a bad idea because it's going to try to exit the Position on every bar - not just the first time the exit condition triggers.

Anyway, it sounds like you're trying to manage discretionary positions that all have very different discretionary conditions to exit. You can do it, but that's not what the product is designed for.
profile picture

thodder

#7
Cone, This is just a TEST script to see how I could exit an existing position. I would not run this as intraday, or even run this on a daily schedule. For this test I expected the outcome of my script to be a single sell order for each security I hold with a quantity that matches my holding size. I needed a clear outcome of the test to see how WLP behaved.

I'm not critisizing your script. I just wanted to keep it as simple as possible for this test. Yours does look like a more realistic script to run in a live environment which also handles intraday trading.

Perhaps a better example of a DAILY script that I might run in Strategy Monitor would be something like this (generate protective stops):
CODE:
Please log in to see this code.


One problem that I see right away is that LastPosition.EntryPrice would never reflect that actual entry price of my position in my account -- it is the hypothetical entry price. If I used EntryPrice in my example above, it would not represent my holding price.

I have done some back testing with this tool, and I find it very powerful. I like it. However I realized after adding a strategy script to Strategy Monitor that it appears to have a flaw -- the WealthScript looks at hypothetical open positions to determine what to exit. It never looks at my actual positions in my account to exit. This means the script may be looking for exit points when I don't even own the security; or worse, it is looking to enter when I need it to be looking for exits. My main purpose of automating my trades is to have a non-emotional trading machine that will get me out of trades that turn bad.

I wish I had the ability to see the actual open tax lots in my account with the script code then I could make more accurate trades in a live environment.
profile picture

Cone

#8
Version 5 Strategies always run with hypothetical positions and don't have access to your account data. You can call it a flaw, but it's by design.

Using the EnterAtPrice method in Community.Components (get it in Extensions), you can read a file of entry dates and prices, and enter the dummy position precisely on the date and price at which you entered it in your account.
profile picture

thodder

#9
EnterAtPrice method just uses BuyAtLimit or BuyAtStop depending on which side of the current price you want the Position.EntryPrice to be. For this to work, I'd have to loop through all the Bars and try to find a bar that contained this price. This is only possible if the range in Strategy Monitor includes a bar that has a price that fits.

It looks like the SimTradeFile (or SimTradeFile2) method could be used to read the file of actual prices.

There is still a manual process to keep the WLP DataSet and SimTradeFile file synchronized with your live account, but this shouldn't be a big problem for someone that has a low volume of trades. However, an active trader that does more than 120 trades a year might find this very cumbersome.

Thanks for this information. It is something I can work with.
profile picture

kcbars

#10
Hello. In reference to #6 above, I have an intraday script that has a limit buy, then either a limit sell or an exit after so many bars. The problem I periodically run into is the script will think a limit sell has occurred, when it actually hasn't in "real life". As a result, the strategy will cycle through and buy more shares. My thinking was maybe I could create a dummy position that would automatically sell the original shares if this situation occurred. I think that might be possible based upon the above discussion, but I'm not sure how to execute it so that it would only happen if a limit sell had not really occurred and more shares were purchased. Any suggestions or input would be appreciated. Thanks.
profile picture

Eugene

#11
As a side note, is "Turn off limit/stop rounding entirely" activated in WLP Preferences > Backtest Settings?
profile picture

Cone

#12
QUOTE:
... the script will think a limit sell has occurred, when it actually hasn't in "real life".

Indeed, this is an issue for auto-trading limit-order scripts and even more so for intraday trading than EOD. The only ways I can think to work around it are to absorb a little slippage.

Manually....
Monitor limit orders. When your price is hit, make sure that you get filled and if not, take over and exit the position manually.

Automatically...
Add logic to check the range of the bar on which limit orders were active. Assuming a long position, if logic determines that the actual price "closely matched" the trigger price, create another dummy hypothetical position and sell it at market. If the live account does not have a position, the order will simply error out. If the Position still exists it will be sold - possibly at a non-desirable price.

Also, possibly....
Prerferences > Slippage and Round Lots > Activate Slippage for Limit Orders and enter a minimal percentage (like 0.05). Note, this will at times have the opposite of what you're currently experiencing: the live position may actually be sold at the trigger price, but the hypothetical position (script) remains active until price exceeds the trigger price by the slippage amount.

You have to decide which is better for your case!
profile picture

kcbars

#13
Great, thanks for the replies - I will keep experimenting. "Turn off limit/stop rounding entirely" is not activated. I don't think it should be, correct?
profile picture

Cone

#14
QUOTE:
I don't think it should be, correct?

"Should be" - no, maybe, sure. All answers are correct. It depends on what you want to accomplish. Generally you would NOT want this activated for live trading. But you have to understand what I wrote about it just above.