## Maximum Allowable Number of Positions

Section for discussions related to indicators, use of indicators, and building of trading stategies using indicators.

### Maximum Allowable Number of Positions

When coding a strategy, is there a way to set a maximum allowable number of positions? Say I'm using a trend following strategy and using a pyramiding system to add more positions on pullbacks. Is there a way to make the strategy only add a certain number of additional positions? My goal is to do this in order to keep from becoming over-leveraged.

Many thanks.

Outside_The_Box

Posts: 31
Joined: Tue Apr 02, 2013 5:33 pm

### Re: Maximum Allowable Number of Positions

No, you should count number of opened trades manually. Like that:
Code: Select all
`function tradesCount(BuySell)    local enum, row;    local count = 0;    enum = core.host:findTable("trades"):enumerator();    row = enum:next();    while row ~= nil do        if row.AccountID == Account and           row.OfferID == Offer and           (row.BS == BuySell or BuySell == nil) then           count = count + 1;        end        row = enum:next();    end    return count;endfunction enter(BuySell)    if tradesCount(BuySell) >= MAX_POSITION_COUNT then        return;    end    ... open a positionend`
“There are only three sports: bullfighting, motor racing, and mountaineering; all the rest are merely games.” (c) Ernest Hemingway
Victor.Tereschenko
FXCodeBase: Confirmed User

Posts: 122
Joined: Fri Nov 19, 2010 8:55 am

### Re: Maximum Allowable Number of Positions

Victor.Tereschenko wrote:No, you should count number of opened trades manually. Like that:
Code: Select all
`function tradesCount(BuySell)    local enum, row;    local count = 0;    enum = core.host:findTable("trades"):enumerator();    row = enum:next();    while row ~= nil do        if row.AccountID == Account and           row.OfferID == Offer and           (row.BS == BuySell or BuySell == nil) then           count = count + 1;        end        row = enum:next();    end    return count;endfunction enter(BuySell)    if tradesCount(BuySell) >= MAX_POSITION_COUNT then        return;    end    ... open a positionend`

Thank you. Where in the code should I copy/paste that? Sorry, I have next to no experience coding. The extent of my code writing education was writing HTML in notepad and checking it with Netscape Navigator back in the 90's. LOL

Outside_The_Box

Posts: 31
Joined: Tue Apr 02, 2013 5:33 pm

### Re: Maximum Allowable Number of Positions

Code: Select all
` if tradesCount(BuySell) >= MAX_POSITION_COUNT then        return;    end`

U can past at the beginning of enter function.
Or try something like this.
Code: Select all
`if tradesCount (BuySell) <MAX_POSITION_COUNT thenenter ("Long");end`

Apprentice
FXCodeBase: Confirmed User

Posts: 20628
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

### Re: Maximum Allowable Number of Positions

Thanks Apprentice. I'll see what I can do with it and report back.

Outside_The_Box

Posts: 31
Joined: Tue Apr 02, 2013 5:33 pm

### Re: Maximum Allowable Number of Positions

I figured out how to set a de facto max positions in strategy wizard. I just added this to the end of the activation point logic:

.and. countLongPositions( symbol ) < 4

Outside_The_Box

Posts: 31
Joined: Tue Apr 02, 2013 5:33 pm

### Re: Maximum Allowable Number of Positions

...and the same goes for shorts. Just add countShortPositions( symbol ) instead of countLongPositions( symbol ).

Outside_The_Box

Posts: 31
Joined: Tue Apr 02, 2013 5:33 pm