I created a strategy that opens 2 positions with 2 different targets (in BAR mode). I wish that when the 1st target is hit, the stop on the remaining position move automatically to breakeven.
For this first wanted to count the number of opens positions for the instrument with a tradesCount () and if the result equals 1, then change the value of the stop position.
But everything I've tried has not worked until now.
Code exemple :
- Code: Select all
...
function Prepare(nameOnly)
...
Source = ExtSubscribe(1, nil, instance.parameters.TF, instance.parameters.Type == "Bid", "bar");
gTickB = ExtSubscribe(2, nil, "t1", instance.parameters.Type == "Bid", "close");
...
end
...
function ExtUpdate(id, source, period)
if id == 1 then
if cond1 == true then
enter("B", stop, target1, Lots1);
enter("B", stop, target2, Lots2);
stopbe_b = false;
end
if cond2 == true then
enter("S", stop, target1, Lots1);
enter("S", stop, target2, Lots2);
stopbe_s = false;
end
elseif id == 2 then
if tradesCount("B") == 1 and stopbe_b == false then
BuyOpenPrice = core.host:findTable("trades"):find("AccountID", Account).Open;
stopBE("S", BuyOpenPrice);
stopbe_b = true;
elseif tradesCount("S") == 1 and stopbe_s == false then
SellOpenPrice = core.host:findTable("trades"):find("AccountID", Account).Open;
stopBE("B", SellOpenPrice);
stopbe_s = true;
end
end
function stopBE(stopSide, stopValue)
if stopSide == "S" then
if stopValue >= instance.bid[NOW] then
return ;
end
elseif stopSide == "B" then
if stopValue <= instance.ask[NOW] then
return ;
end
end
-- try to find the order
local enum, row;
local order = nil;
local rate;
local enum, row;
local buy, sell;
enum = core.host:findTable("orders"):enumerator();
row = enum:next();
while (row ~= nil) do
if row.OfferID == OfferID and
row.AccountID == AccountID and
row.BS == stopSide and
row.NetQuantity and
row.Type == "SE" then
order = row.OrderID;
rate = row.Rate;
end
row = enum:next();
end
if order == nil then
return;
else
if math.abs(stopValue - rate) >= minChange then
-- stop exists
valuemap = core.valuemap();
valuemap.Command = "EditOrder";
valuemap.OfferID = OfferID;
valuemap.AcctID = AccountID;
valuemap.OrderID = order;
valuemap.Rate = stopValue;
--valuemap.RateStop = stopValue;
executing = true;
success, msg = terminal:execute(200, valuemap);
--print("Stop Moved");
if not(success) then
executing = false;
terminal:alertMessage(instance.bid:instrument(), instance.bid[NOW], "Failed change stop " .. msg, instance.bid:date(NOW));
end
end
end
end
Can anyone please help me to breakeven the stop ?
Thank you.