Limit to the number of trades open in a direction

Moderator: admin

Limit to the number of trades open in a direction

Postby d10gen3 » Sat Jan 29, 2011 6:23 pm

Hello, can someone help me with this issue?
I want to put a limit to the number of positions open in the same direction, say for example i have a limit of 2 open positions and my strategy gives me a signal to go short, then it gives me a second signal to go short, a few minutes later he wants to go short again but it is already short 2.
This is the code I have so far:

in the Init():
Code: Select all
   strategy.parameters:addInteger("maxOpenTrades", " Allowed trades at the same time", "", 1, 0, 5);


outside in the parameters block:
Code: Select all
local MaxOpenOrders;


then in the Prepare() (After the check for canTrade) :
Code: Select all
      MaxOpenOrders = instance.parameters.MaxOpenOrders;


in the Update() function:
Code: Select all
   if((havetrades(decision) < MaxOpenOrders)) then


this is my havetrades(decision) function:

Code: Select all
function haveTrades(BuySell)
   local enum, row;
   local found = 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
         found = found + 1;
      end
      row = enum:next();
   end
   return found;
end


for some reason, the program is not taking care of the limit when i choose 1, anyone sees the mistake? please help, thanks in advance

Andres
d10gen3
 
Posts: 11
Joined: Wed Jan 26, 2011 8:18 pm

Re: Limit to the number of trades open in a direction

Postby Timon55 » Sun Jan 30, 2011 1:46 pm

Hello, d10gen3!

Code: Select all
strategy.parameters:addInteger("maxOpenTrades", " Allowed trades at the same time", "", 1, 0, 5);

VS.
Code: Select all
MaxOpenOrders = instance.parameters.MaxOpenOrders;


In the Init function you add a parameter named "maxOpenTrades" but in the Prepare function you try to get a parameter with name "MaxOpenOrders".
Timon55
FXCodeBase: Confirmed User
 
Posts: 43
Joined: Thu Jul 15, 2010 5:35 am
Location: Omsk

Re: Limit to the number of trades open in a direction

Postby d10gen3 » Sun Jan 30, 2011 1:57 pm

Actually that was a typo mistake, i used maxOpenTrades in all parts.
The strategy is still taking more positions than allowed.

Thanks
Andres
d10gen3
 
Posts: 11
Joined: Wed Jan 26, 2011 8:18 pm

Re: Limit to the number of trades open in a direction

Postby Timon55 » Mon Jan 31, 2011 10:58 am

I tried to make a same strategy and all works as expected. Probably if you can provide a complete code of your strategy I'll try to find root of this issue. I sent you my email address by private message
Timon55
FXCodeBase: Confirmed User
 
Posts: 43
Joined: Thu Jul 15, 2010 5:35 am
Location: Omsk


Return to Indicator Development

Who is online

Users browsing this forum: No registered users and 52 guests