onOpenPosition

Here is the onOpenPosition function

bool onOpenPosition(Position pos, INDEX bar )
{
//**************************************
// Per position user defined exit strategies here
//**************************************

}

This function does not get called automatically by the application - the user must add the call to ON_EACH_OPEN_POSITION in the onBar function, and pass the current bar as parameter. This in turn will call onOpenPosition repeatedly for each open position in the default positions collection.

The purpose of this function is to perform per open position exit strategy processing.

This function takes 2 parameters: an open position, pos, and the current bar index. The function returns a boolean: if true the processing of subsequent open positions will continue (the usual case), or it will stop if false.

The simple trading system sample shows a typical use of this function:

bool onOpenPosition(Position pos, INDEX bar )
{
//**************************************
// Apply user defined exit strategies here
// for each open position at the current bar
//**************************************
    if( fastSMAHigh.crossOver( bar, slowSMAHigh ) )
        SELL_AT_MARKET( bar + 1, pos, "Sell at market on crossunder" );
    return
true;
}

The exit logic in this case is: if the fast SMA of highs crosses over the slow SMA of highs at the current bar, then close the position by selling at market at the open of next bar.

There are several assumptions made: all positions received in the onOpenPosition are still open, and in this particular case it is known that the position is long, so it will be closed by selling it. Other trading systems may open both long and short positions, in which case a test will have to be made to determine the position type by calling isLong or isShort


© 2006-2010 Tradery.com
info@tradery.com