Perform code on stop-loss or take-profit event

How to create strategies and indicators
Message
Author
amberwine
Posts: 17
Joined: Fri Jul 12, 2013 3:47 am

Perform code on stop-loss or take-profit event

#1 Postby amberwine » Sat Jul 13, 2013 6:06 am

When a buy or sell order is created with SendInstantOrder (or SendPendingOrder), is there a way to detect when the order has closed due to hitting its stop-loss or take-profit level?

I need to change some internal variables when the order has hit its stop-loss, to set up for the next order, so some kind of callback or event when this happens would be great. I'm using C++ to code my strategies.

What's the best way of handling this?

Phil_Trade
Posts: 94
Joined: Tue Jan 31, 2012 5:14 am
Contact:

Re: Perform code on stop-loss or take-profit event

#2 Postby Phil_Trade » Sat Jul 13, 2013 9:40 am

amberwine wrote:When a buy or sell order is created with SendInstantOrder (or SendPendingOrder), is there a way to detect when the order has closed due to hitting its stop-loss or take-profit level?

I need to change some internal variables when the order has hit its stop-loss, to set up for the next order, so some kind of callback or event when this happens would be great. I'm using C++ to code my strategies.

What's the best way of handling this?


Hi

you have to analyse history trade and check SL >= ClosePrice + delta pips for Buy and SL >= ClosePrice - delta pips for sell

here is an example of code to read history. You will have to manage in a global variable the last ticket analyse, so you can break the loop if ticket Number > Your last ticket analyse

Code: Select all

function ReturnHighOpenPriceFromHistory(PaireAtrouver : string; MonTicket : integer) :double;
var
i : integer;
LastOpenPrice : double;
begin
LastOpenPrice := 0;

for i:=HistoryTotal - 1 downTo 0 do
begin
   if OrderSelect(i, SELECT_BY_POS, MODE_HISTORY) then
   begin

        if (OrderSymbol = PaireAtrouver)  then
        begin

///  your code here... ////
        end;
   end;

end;   //for i:=0 to OrdersTotal - 1 do

// Repositionne boucle appelante sur bon trade
if MonTicket>0 then OrderSelect(MonTicket, SELECT_BY_TICKET, MODE_TRADES);

result := LastOpenPrice;
end;   //function ReturnHighOpenPriceFromHistory(PaireAtrouver : string ) :double;
my live account - 8 based pairs with optimized parameters : +188%
http://www.myfxbook.com/members/Philipp ... jpg/519044
TradeSlide : http://bit.ly/14R9Nf6

to be informed about Windev/MT4 services : a04486-tradingsignal@yahoo.fr

amberwine
Posts: 17
Joined: Fri Jul 12, 2013 3:47 am

#3 Postby amberwine » Sat Jul 13, 2013 12:28 pm

Thanks for the suggestion.

After some experimentation I found that the following (C++) also seems to work for capturing the profit (or rather loss) from the last automatically closed trade, due to stop loss. This was all I needed:

Code: Select all

double lastProfit = 0;

if (OrdersTotal() == 0) {

   if (OrderHandle != -1)
   {
      if (OrderClosed(OrderHandle))
      {
         if (OrderSelect(OrderHandle, SELECT_BY_TICKET, MODE_TRADES))
            lastProfit = OrderProfit();
         OrderHandle = -1;
      }
   }
}

I also tried GetOrderInfo() in place of OrderClosed(), thus:

Code: Select all

TTradePosition* lastTrade = new TTradePosition();
if (GetOrderInfo(OrderHandle, *lastTrade))
{
   // set breakpoint here...
   int i=0;
   i++;
}
delete lastTrade;

However, it doesn't seem to work, and never returned true to trigger the breakpoint. Not sure why. Maybe it only works for open trades?


Return to “FT API”

Who is online

Users browsing this forum: No registered users and 23 guests