Maximum Allowable Number of Positions

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

Moderator: admin

Maximum Allowable Number of Positions

Postby Outside_The_Box » Wed Nov 06, 2013 7:29 pm

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.
User avatar
Outside_The_Box
 
Posts: 31
Joined: Tue Apr 02, 2013 5:33 pm

Re: Maximum Allowable Number of Positions

Postby Victor.Tereschenko » Thu Nov 07, 2013 2:27 am

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;
end

function enter(BuySell)
    if tradesCount(BuySell) >= MAX_POSITION_COUNT then
        return;
    end
    ... open a position
end
“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: 113
Joined: Fri Nov 19, 2010 8:55 am

Re: Maximum Allowable Number of Positions

Postby Outside_The_Box » Thu Nov 07, 2013 2:37 am

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;
end

function enter(BuySell)
    if tradesCount(BuySell) >= MAX_POSITION_COUNT then
        return;
    end
    ... open a position
end


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
User avatar
Outside_The_Box
 
Posts: 31
Joined: Tue Apr 02, 2013 5:33 pm

Re: Maximum Allowable Number of Positions

Postby Apprentice » Fri Nov 08, 2013 4:04 am

U can past tradesCount(BuySell) function anywhere, outside existing functions.
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 then
enter ("Long");
end
User avatar
Apprentice
FXCodeBase: Confirmed User
 
Posts: 18101
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

Re: Maximum Allowable Number of Positions

Postby Outside_The_Box » Fri Nov 08, 2013 4:26 am

Thanks Apprentice. I'll see what I can do with it and report back.
User avatar
Outside_The_Box
 
Posts: 31
Joined: Tue Apr 02, 2013 5:33 pm

Re: Maximum Allowable Number of Positions

Postby Outside_The_Box » Mon Nov 11, 2013 3:41 am

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
User avatar
Outside_The_Box
 
Posts: 31
Joined: Tue Apr 02, 2013 5:33 pm

Re: Maximum Allowable Number of Positions

Postby Outside_The_Box » Mon Nov 11, 2013 3:42 am

...and the same goes for shorts. Just add countShortPositions( symbol ) instead of countLongPositions( symbol ).
User avatar
Outside_The_Box
 
Posts: 31
Joined: Tue Apr 02, 2013 5:33 pm


Return to Discussions

Who is online

Users browsing this forum: No registered users and 3 guests