I have some problems with the closing of position.
What I'm trying to do is open a position (Market order, OM) with a stop.
Then when there is a signal (not close on opposite) exit with a (CM) and the stop must be cancelled with the close of the position. Allowed multiple positions in a direction, not presence of long and short trades at the same moment (but hedging is allowed on my account). Trades independent.
This is the code:
- Code: Select all
if conditions
BUY(stop);
end
if conditions
SELL(stop)
end
if Exit then
if (Indicator.ST:colorI(period)==core.rgb(255,0,0) then
if haveTrades("B") then
exitSpecific("B");
Signal("Close Long");
end
end
if (Indicator.ST:colorI(period)==core.rgb(0,0,255) then
if haveTrades("S") then
exitSpecific("S");
Signal("Close Short");
end
end
end
-------------------------------------------------------
------------------------------------------------------
--------------------------------------------------------
function BUY(Stop, Limit)
if AllowTrade and AllowMultiple then
if haveTrades("S") then
exitSpecific("S");
Signal ("Close Short");
end
if (not(haveTrades("B")) or (haveTrades("B"))) and (not(haveTrades("S"))) and AllowMultiple then
enter("B", Stop);
Signal ("Open Long");
end
elseif ShowAlert then
Signal ("Up Trend");
end
end
function SELL(Stop, Limit)
if AllowTrade and AllowMultiple then
if haveTrades("B") then
exitSpecific("B");
Signal ("Close Long");
end
if (not(haveTrades("S")) or (haveTrades("S"))) and (not(haveTrades("B"))) and AllowMultiple then
enter("S", Stop);
Signal ("Open Short");
end
elseif ShowAlert then
Signal ("Down Trend");
end
end
function haveTrades(BuySell)
local enum, row;
local found = false;
enum = core.host:findTable("trades"):enumerator();
row = enum:next();
while (not found) and (row ~= nil) do
if row.AccountID == Account and
row.OfferID == Offer and
(row.BS == BuySell or BuySell == nil) then
found = true;
end
row = enum:next();
end
return found;
end
-- enter into the specified direction
function enter(BuySell, Stop, Limit)
if ( not(AllowTrade) and not(AllowMultiple) ) then
return true;
end
local valuemap, success, msg;
valuemap = core.valuemap();
valuemap.OrderType = "OM";
valuemap.OfferID = Offer;
valuemap.AcctID = Account;
valuemap.Quantity = Amount * BaseSize;
valuemap.BuySell = BuySell;
valuemap.RateStop=Stop;
if TrailingStop then
valuemap.TrailStepStop = 1;
end
if (not CanClose) then
valuemap.EntryLimitStop = "Y";
end
success, msg = terminal:execute(200, valuemap);
if not(success) then
terminal:alertMessage(instance.bid:instrument(), instance.bid[instance.bid:size() - 1], "Open order failed" .. msg, instance.bid:date(instance.bid:size() - 1));
return false;
else
end
return true;
end
-- exit from the specified trade using the direction as a key
function exitSpecific(BuySell)
-- we have to loop through to exit all trades in each direction instead
-- of using the net qty flag because we may be running multiple strategies on the same account.
local enum, row;
local found = false;
enum = core.host:findTable("trades"):enumerator();
row = enum:next();
while (not found) and (row ~= nil) do
-- for every trade for this instance.
if row.AccountID == Account and row.OfferID == Offer and (row.BS == BuySell or BuySell == nil) then
exitTrade(row);
end
row = enum:next();
end
end
-- exit from the specified direction
function exitTrade(tradeRow)
if ( not(AllowTrade) and not(AllowMultiple) ) then
return true;
end
local valuemap, success, msg;
if tradesCount(BuySell) > 0 then
valuemap = core.valuemap();
-- switch the direction since the order must be in oppsite direction
if BuySell == "B" then
BuySell = "S";
else
BuySell = "B";
end
valuemap.OrderType = "CM";
valuemap.OfferID = Offer;
valuemap.AcctID = Account;
if (CanClose) then
-- Non-FIFO can close each trade independantly
valuemap.TradeID = tradeRow.TradeID;
valuemap.Quantity = tradeRow.Lot;
else -- FIFO
valuemap.NetQtyFlag = "Y";
end
valuemap.BuySell = BuySell;
success, msg = terminal:execute(201, valuemap);
if not(success) then
terminal:alertMessage(instance.bid:instrument(), instance.bid[instance.bid:size() - 1], "Open order failed" .. msg, instance.bid:date(instance.bid:size() - 1));
return false;
end
return true;
end
return false;
end
dofile(core.app_path() .. "\\strategies\\standard\\include\\helper.lua");
The strategy doesn't close the positions.
EX:
Open Short with a OM and a stop
Open another Short with a OM and a stop
Signal of Close Short Trades
So Close all shorts with a CM and cancelled the stops
Wait till another signal of open a trade
Another signal for open a trade is given
Signal of open a trade (long) is possible in the same period of close a short but not always because the conditions that follow the signals are different. (condition of opening and closing different).