I'm having the same issue , here is how: I'm implementing more than one different strategies on the same instrument; this means that when a strategy closes one or more positions, it must close its own positions, not the other strategy's positions.
One of the strategies is the
Donchian Strategy I found in fxcodebase, but since I need to close only its own positions, I changed the closing function like this: instead of using
- Code: Select all
valuemap.NetQtyFlag = "Y";
I call a findTrade function which returns the first trade of the same
AccountID, OfferID, QTXT, Instrument and
BS , close this trade, and keep calling findTrade until it returns nil.
The problem is that I get the 30 sec error msg even when less than 10 trades are running on TSII. It is hard to believe that going ten times (max) through a ten-row table takes more than 30 seconds!!!
I would also really appreciate some help on this, because the error pauses the strategy and this has already costed me some money...
Here is the code:
- Code: Select all
function findTrade(BuySell)
local enum, row;
local foundRow = false;
enum = core.host:findTable("Trades"):enumerator();
row = enum:next();
while not(foundRow) and row~=nil do
if row.AccountID == Account and
row.OfferID == Offer and
row.QTXT=="DONCHIAN" and
row.Instrument == instance.bid:instrument() and
row.BS == BuySell then
foundRow = row;
end
row = enum:next();
end
return foundRow;
end
function exit(BuySell)
if not(AllowTrade) then
return true;
end
local enum, row, valuemap, success, message;
row = findTrade(BuySell);
if row then
valuemap = core.valuemap();
valuemap.Command = "CreateOrder";
valuemap.OrderType = "CM"; -- Close Market
valuemap.OfferID = Offer;
valuemap.AcctID = Account;
valuemap.Quantity = row.Lot;
valuemap.TradeID = row.TradeID;
if row.BS == "B" then
valuemap.BuySell = "S";
else
valuemap.BuySell = "B";
end
success, message = terminal:execute(101, valuemap);
if not success then
terminal:alertMessage(instance.bid:instrument(), instance.bid[NOW], "Close trade failed: " .. message, instance.bid:date(NOW));
end
exit(BuySell); -- executed only if row~=nil
end
end