Log in to see Cloud of Tags

Wealth-Lab Wiki

WealthScript Techniques | Splitting a Position

RSS
The WealthScript language includes fine control of multiple positions in a trading system. There are a wide variety of Position Management functions that allow you to gather information on open and closed positions (trades) and manipulate them. One of these is the ability to split an existing position into two. You might want to do this in some cases to take partial profits, for example.

SplitPosition

Splitting a position into two is accomplished via the SplitPosition function. The format of SplitPosition is:

Position SplitPosition(Position position, double percentToRetain);

The Position parameter contains the position object that you want to split. WealthScript maintains a list of positions (actually, even 2 in WL6 - the usual list and a separated ActivePositions list which is self-descriptive), and whenever a new trade is completed a new position is added to this list. You access positions by "index number", which is the index into this list. The index starts at zero and ends at Positions.Count – 1.

The percentToRetain parameter controls how the position is split. After SplitPosition is called, a new position is added to the end of the position list. percentToRetain determines what percentage of the shares/contracts will be retained in the original position. After the call to SplitPosition the original position has an index of Positions.Count – 2, while the newly added position has an index of Positions.Count – 1.

Example

The following example enters short position if there is a shooting star candle pattern and places the stop at 3 ticks above the top of the shooting star.

In the following example we want to split a position to create 2 profit taking levels:

  1. 50% of the position = selling level - (2 times (stop - open position)) (to get a Risk reward of 2)
  2. 50% of the position = selling level - (4 times (stop - open position)) (to get a Risk reward of 4)

The following call accomplishes this:

Position s = SplitPosition( LastPosition, 49.99 );

SplitPosition returns a new Position object that contains the remaining shares. This new Position object is also added to the end of the Positions list.

Conclusion

This is a simple example of using SplitPosition. Managing multiple positions, especially when splitting them, can be tricky. But WealthScript provides to tools necessary to implement strategies that take partial profits, or split positions into multiple parts for any other reason.

Code

using System; using System.Drawing; using WealthLab; using WealthLab.Indicators; using WealthLab.Rules.Candlesticks;

namespace WealthLab.Strategies { public class ShootingStarStrategy3 : WealthScript { protected override void Execute() { bool[] ShootingStar; CandlePattern.BearishShootingStar(this, "ShootingStar", true, out ShootingStar); double limit2 = 0; double limit4 = 0; bool canSplit = false; bool limit2hit = false; bool limit4hit = false; for(int bar = 25; bar < Bars.Count; bar++) { if( ActivePositions.Count > 0 ) { if( bar < Bars.Count-1 ) { limit2hit = Lowbar+1 < limit2; limit4hit = Lowbar+1 < limit4; } if( !CoverAtStop( bar+1, Position.AllPositions, RiskStopLevel, "Stop" ) ) { if( canSplit ) { if( limit2hit ) { Position s = SplitPosition( LastPosition, 49.99 ); if( s!= null ) canSplit = false; CoverAtLimit( bar+1, s, limit2, "R/R 2" ); } } else if( limit4hit ) CoverAtLimit( bar+1, LastActivePosition, limit4, "R/R 4" ); } } else { if( ShootingStarbar ) { int ssBar = bar; double ssPrice = Highbar; bool cond1 = ( (ssPrice > HighssBar-1) & (HighssBar-1 > HighssBar-2) ); bool cond2 = ( (ssPrice > HighssBar-1) & (ssPrice > HighssBar-3) ); bool cond3 = ( (ssPrice > HighssBar-2) & (ssPrice > HighssBar-3) ); RiskStopLevel = ssPrice + Bars.SymbolInfo.Tick * 3; if( cond1 || cond2 || cond3 ) { if( ShortAtMarket(bar + 1) != null ) { Position p = LastPosition; double ep = p.EntryPrice; double er = RiskStopLevel; limit2 = ep - Math.Abs( er - ep ) * 2; limit4 = ep - Math.Abs( er - ep ) * 4; canSplit = true; } } } } } } } }

Important Disclaimer: The information provided by Wealth-Lab is strictly for informational purposes and is not to be construed as advice or solicitation to buy or sell any security.  The owner of Wealth-Lab.com assumes no liability resulting from the use of the material contained herein for investment purposes. By using this web site, you agree to the terms of this disclaimer and our Terms of Use.


ScrewTurn Wiki. Some of the icons created by FamFamFam.