Buy/Sell Multiple Position
Author: Assenzio
Creation Date: 2/19/2011 8:05 AM
profile picture

Assenzio

#1
Could someone help me with a brief code example on how to build a system that buy double (triple and so on) fixed dollar position on a condition A and a single position on a condition B ? For example, when the close cross the SMA at 21 buy Double position (2000$) and sell after 10 days (Condition A). Whe the close cross SMA at 10 buy one position (1000$) and sell it after 5 day.
profile picture

Cone

#2
I'm assuming that "SMA at 21" means the 21-period SMA.

This is the most direct way using WealthScript Override (SetShareSize) in the Position Sizing control. You can also easily size entries differently in percentage terms using the Position Priority PosSizer and assigning an appropriate range of priorities.
CODE:
Please log in to see this code.
profile picture

Humanbean

#3
Would an example of adding shares to an open position fit easily into this example?
Something that I havent a handle on yet.
For example ...if the 10 day sma crosses over the 21 sma then add 3000 shares to the open position.
The strategy may not make sense but I just would like to see a code example of how that might be done.

Thanks
profile picture

Cone

#4
Wealth-Lab is Position-based. Sure you can add shares (i.e., add a Position), but you can't add shares to an active Position. Instead, you'll end up with more than one Position, and you have to manage both separately.

Going the other way, note that you can SplitPosition(), but there is no such thing as "MergePositions()".
profile picture

Assenzio

#5
Thanks Cone. Since WLD is not able to work with leveraged position, in this way I came up with many "insufficient capital" alert.

How to start with 1000$ capital and buy a double position with rule A (200% of equity suppose to use a 2X leverage product) and a single position (100% equity) with rule B so using the available capital at that moment ?
profile picture

Cone

#6
QUOTE:
not able to work with leveraged position
If you mean margin trading for stocks, sure you can. Just select the margin ratio at the bottom of the Portfolio Simulation Position Sizing.

QUOTE:
...and buy a double position with rule A (200% of equity suppose to use a 2X leverage product) and a single position (100% equity)...
You guys and your 100% equity trading. Do people really do that?

I mentioned the answer for using % of Equity sizing this way - use the Position Priority PosSizer. However, it appears the PosSizer is programmed to limit to 100% of equity sizing in the options, so we'll have to ask Eugene if he can remove that limit. Ideally the PosSizer could get the margin setting and limit the percentage accordingly as is done in the standard sizer.
profile picture

Eugene

#7
QUOTE:
However, it appears the PosSizer is programmed to limit to 100% of equity sizing in the options, so we'll have to ask Eugene if he can remove that limit.

Sure I'll remove the limit. An update to MS123 PosSizers is coming out early March.
profile picture

Assenzio

#8
So, if I have well understood, with the next update of March will be possibile to write a PosSizer that is able to do what I was asking before ? If so, since it involves the use of a development tool, I will need your support again ... :) For the moment, where I can found examples of PosSizer scripts ?
profile picture

Eugene

#9
QUOTE:
For the moment, where I can found examples of PosSizer scripts ?

In the Wealth-Lab Wiki, there's an open source demo PosSizer. PosSizers are not scripts though: they are compiled libraries like indicators, strategies, providers etc.
QUOTE:
So, if I have well understood, with the next update of March will be possibile to write a PosSizer that is able to do what I was asking before ?

I'm going to update the MS123 PosSizer library to remove the artificial position size limitation (capped at 100%) from the Priority Adjustment PosSizer, enabling it to take e.g. 200% of equity positions (using margin).
profile picture

Eugene

#10
Please update your MS123 PosSizer extension to v.2011.03 to be able to enter position sizes greater than 100% in the Priority Adjustment PosSizer.
profile picture

sf631

#11
I am trying to build a trading system that maintains a running measure of current quantity (cq) and target quantity (tq) and follows the simple rule of selling shares if cq>tq (i.e., I want less) and the opposite if I want more.

I've right now got code using the SetShareSize method and a fairly complex series of if statements to take into account all six cases (cq>=0 && tq >0, cq <0 && tq>0 etc...) to control whether to create a BuyAtMarket or CoverAtMarket, or both, etc... It's still buggy, and I'm trying to trace what I'm doing wrong, but my task would be much easier if there were a way to simply place a single type of buy that would create or increase a long position if one existed, reduce or close a short position, or close a short and then create a long depending.

Is there any means of doing this or would I just end up with a huge list of open Positions that offset one another? If this isn't possible, then I'll keep trying to trace the error in my logic. Should I be able to use the SetShareSize method to control either opening or closing orders (e.g., BuyAt or CoverAt)? Does anything weird happen to the Position object when partially closing (such as does it become a new Position object)?
profile picture

Eugene

#12
QUOTE:
Should I be able to use the SetShareSize method to control either opening or closing orders (e.g., BuyAt or CoverAt)?

Yes for opening but definitely not for closing - instead, look up SplitPosition in the QuickRef for one of the possible ways. (The other way is to create and close positions individually, one by one.)
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).