Moderator: admin
function close(side, offer)
local enum, row, valuemap;
enum = core.host:findTable("trades"):enumerator();
while true do
row = enum:next();
if row == nil then
terminal:alertMessage(instance.bid:instrument(), instance.bid[instance.bid:size() - 1], "close - 1", instance.bid:date(instance.bid:size() - 1));
break;
end
terminal:alertMessage(instance.bid:instrument(), instance.bid[instance.bid:size() - 1], "close - 2", instance.bid:date(instance.bid:size() - 1));
if row.AccountID == mAccount and
row.OfferID == offer and
row.BS == side and
row.QTXT == mCID then
terminal:alertMessage(instance.bid:instrument(), instance.bid[instance.bid:size() - 1], "close - 3", instance.bid:date(instance.bid:size() - 1));
-- if trade has to be closed
if canClose(row.Instrument) then
-- create a close market order when hedging is allowed
terminal:alertMessage(instance.bid:instrument(), instance.bid[instance.bid:size() - 1], "close - 4", instance.bid:date(instance.bid:size() - 1));
valuemap = core.valuemap();
valuemap.OrderType = "CM";
valuemap.OfferID = offer;
valuemap.AcctID = mAccount;
valuemap.Quantity = row.Lot;
valuemap.TradeID = row.TradeID;
valuemap.CustomID = mCID;
if row.BS == "B" then
valuemap.BuySell = "S";
else
valuemap.BuySell = "B";
end
success, msg = terminal:execute(200, valuemap);
assert(success, msg);
terminal:alertMessage(instance.bid:instrument(), instance.bid[instance.bid:size() - 1], "close - 5", instance.bid:date(instance.bid:size() - 1));
else
-- create an opposite market order when FIFO
terminal:alertMessage(instance.bid:instrument(), instance.bid[instance.bid:size() - 1], "close - 6", instance.bid:date(instance.bid:size() - 1));
valuemap = core.valuemap();
valuemap.OrderType = "OM";
valuemap.OfferID = offer;
valuemap.AcctID = mAccount;
valuemap.Quantity = row.Lot;
valuemap.CustomID = mCID;
if row.BS == "B" then
valuemap.BuySell = "S";
else
valuemap.BuySell = "B";
end
success, msg = terminal:execute(200, valuemap);
assert(success, msg);
terminal:alertMessage(instance.bid:instrument(), instance.bid[instance.bid:size() - 1], "close - 7", instance.bid:date(instance.bid:size() - 1));
end
end
end
end
Return to Indicator Development
Users browsing this forum: No registered users and 25 guests