BreakEven when Target1 Filled

Section for discussions related to indicators, use of indicators, and building of trading stategies using indicators.

Moderator: admin

BreakEven when Target1 Filled

Postby SilverC » Wed Nov 18, 2015 8:02 am

Hi,

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.
SilverC
 
Posts: 3
Joined: Wed Nov 18, 2015 7:38 am

Re: BreakEven when Target1 Filled

Postby Julia CJ » Thu Nov 19, 2015 2:44 am

Hi SilverC,

Please follow these steps:

When you open positions, remember their IDs and check their presence in the trades table.
Once you notice that you cannot find any of these positions, you have to move stop.
In order not to move stop several times, you need to add a variable to the code.

For example, a variable like this:
local stop_moved = {};
...
if tradeClosed(trade_1_ID) and stop_moved[trade_2_ID] ~= true then
move_stop_to_breakeven(trade_2_ID);
stop_moved[trade_2_ID] = true;
end
Julia CJ
 

Re: BreakEven when Target1 Filled

Postby SilverC » Thu Nov 19, 2015 10:09 am

Hi Julia,

thank you for help but what the best way to check if id is in trades table ?

Code: Select all
local trade1id;
local trade2id;

function ExtUpdate(id, source, period)
    if id == 1 then
        enter("B", stop, target1, Lots1, msgb1, 1);
        enter("B", stop, target2, Lots2, msgb2, 2);

    elseif id == 2 then
        if haveTrades("B") then
            if --[[find trade1id]] then
                -- nothing to do
            else
                -- movestop()
            end
        end
    end
end


-- enter into the specified direction
function enter(BuySell, StopTrade, LimitTrade, NbLots, msgBS, number)
    if not(AllowTrade) then
        return true;
    end

    local valuemap, success, msg;
    valuemap = core.valuemap();

    valuemap.OrderType = "OM";
    valuemap.OfferID = Offer;
    valuemap.AcctID = Account;
    valuemap.Quantity = NbLots * BaseSize;
    valuemap.BuySell = BuySell;

   --local StopTrade = Stop;
   --local LimitTrade = Limit;

    -- add stop/limit

    valuemap.PegTypeStop = "O";
        if BuySell == "B" then
            valuemap.PegPriceOffsetPipsStop = -StopTrade;
        else
            valuemap.PegPriceOffsetPipsStop = StopTrade;
        end

 --   if TrailingStop then
 --       valuemap.TrailStepStop = 1;
 --    end

    valuemap.PegTypeLimit = "O";
        if BuySell == "B" then
            valuemap.PegPriceOffsetPipsLimit = LimitTrade;
        else
            valuemap.PegPriceOffsetPipsLimit = -LimitTrade;
        end

    if (not CanClose) then
        valuemap.EntryLimitStop = "Y";
    end


    success, msg = terminal:execute(200, valuemap);
    Signal(msgBS);

    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
        requestId = core.parseCsv(msg, ",")[0];
        terminal:alertMessage(instance.bid:instrument(), instance.bid[NOW], "order sent:" .. requestId, instance.bid:date(NOW));
        if number == 1 then
            print("OM 1 sent:" .. requestId);
            trade1id = requestId;
        elseif number == 2 then
            print("OM 2 sent:" .. requestId);
            trade2id = requestId;
        end

    end

    return true;
end


thank you for your time.
SilverC
 
Posts: 3
Joined: Wed Nov 18, 2015 7:38 am

Re: BreakEven when Target1 Filled

Postby Julia CJ » Mon Nov 23, 2015 6:20 am

Hi SilverC,

Please use this function:

http://www.fxcodebase.com/documents/Ind ... .find.html
Julia CJ
 

Re: BreakEven when Target1 Filled

Postby Stance » Tue Apr 26, 2016 9:31 pm

... ...
Last edited by Stance on Tue May 10, 2016 3:31 am, edited 1 time in total.
Stance
FXCodeBase: Initiate
 
Posts: 141
Joined: Mon Jun 01, 2015 4:34 pm

Re: BreakEven when Target1 Filled

Postby Julia CJ » Wed Apr 27, 2016 12:53 am

Hi Stance,

Could you please send the snapshot with this issue at e-mail: ycherepanova@gehtsoft.com?

Thank you.
Julia CJ
 

Re: BreakEven when Target1 Filled

Postby Stance » Wed Apr 27, 2016 1:28 am

Hi Julia,

I've sent you an email.

Regards,

Tony
Stance
FXCodeBase: Initiate
 
Posts: 141
Joined: Mon Jun 01, 2015 4:34 pm


Return to Discussions

Who is online

Users browsing this forum: No registered users and 4 guests