Position Management sample

The source for Position Management

Position Management description

I hope previous article RangeTrader illustrated power of Event-driven programming.

This time we utilize events to accumulate our position size.

We operate hourly bars. If we get hourly bar that fulfill our trade criteria we open position. So the condition is that we do not have position open and in this case we have RSI smaller than 2. We set initial position size as target size divided by 4. When we get order confirm from broker we store our position status.


#region NewHourlyBar
public override void NewHourlyBar(object sender, SingleBarEventArgs args)
{
	IList pos = 
		SymbolScript.SystemData.PositionManager.GetOpenPositions(Symbol);

	if ( pos.Count == 0 )
	{
		if ( _rsi.Current < 2 )
		{
			PositionSettings _positionSettings = new PositionSettings();
			_positionSettings.PositionType = PositionType.Long;
			_positionSettings.Size = _targetSize / 4;
			_positionSettings.Description = "Open Long";
			OpenPosition(_positionSettings);
		}
	}
	else if ( pos.Count > 0 )
	{
		if ( pos[0].CurrentSize < _targetSize )
		{
			OrderSettings _orderSettings = new OrderSettings();
			_orderSettings.TransactionType = TransactionType.Buy;
			_orderSettings.Description = "Add to position";
			_orderSettings.Size = _targetSize / 4;
			pos[0].SubmitOrder(_orderSettings);
		}
	}
}
#endregion

The latter part of hourly bar function handles position accumulation. If we are below our target size we send buy order to broker. Again this time the size is target size divided by 4. This continues for next hourly bars until we have reached our target size for position.

Again remember that reception of new hourly bar can contain other filters if you choose not to apply position accumulation for every hourly bar.

After we have built our position to target size we can set our profit target as fixed or trailing as described in previous articles TimeTrigger and AdaptiveTrail.

The bottom line is that all accumulation orders are directed and tracked to same position. That is the difference between opening position and later submitting orders to that very same position. As our broker does not know which order belongs to which strategy we need to do that bookkeeping in our code.