Page 1 of 1

Maximum Allowable Number of Positions

PostPosted: Wed Nov 06, 2013 7:29 pm
by Outside_The_Box
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.

Re: Maximum Allowable Number of Positions

PostPosted: Thu Nov 07, 2013 2:27 am
by Victor.Tereschenko
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

Re: Maximum Allowable Number of Positions

PostPosted: Thu Nov 07, 2013 2:37 am
by Outside_The_Box
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

Re: Maximum Allowable Number of Positions

PostPosted: Fri Nov 08, 2013 4:04 am
by Apprentice
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

Re: Maximum Allowable Number of Positions

PostPosted: Fri Nov 08, 2013 4:26 am
by Outside_The_Box
Thanks Apprentice. I'll see what I can do with it and report back.

Re: Maximum Allowable Number of Positions

PostPosted: Mon Nov 11, 2013 3:41 am
by Outside_The_Box
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

Re: Maximum Allowable Number of Positions

PostPosted: Mon Nov 11, 2013 3:42 am
by Outside_The_Box
...and the same goes for shorts. Just add countShortPositions( symbol ) instead of countLongPositions( symbol ).