WealthScript Techniques | Splitting a Position

Modified on 2018/10/14 15:40 by Eugene — Categorized as: Knowledge Base

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 = Low[bar+1] < limit2; limit4hit = Low[bar+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( ShootingStar[bar] ) { int ssBar = bar; double ssPrice = High[bar]; bool cond1 = ( (ssPrice > High[ssBar-1]) & (High[ssBar-1] > High[ssBar-2]) ); bool cond2 = ( (ssPrice > High[ssBar-1]) & (ssPrice > High[ssBar-3]) ); bool cond3 = ( (ssPrice > High[ssBar-2]) & (ssPrice > High[ssBar-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; } } } } } } } }