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