Coding Help: Stop Loss, Take Profit with ECN Broker

 
This website is amazing. It is in fact where I'm learning to code by trial and error.

That said, I'm a newbie to MQL4 programming and need help attaching a SL & TP to an ORDERSEND with an ECN broker.

Initially, I couldn't understand why the SL & TPs were making my order result in an error until someone pointed out that one has to set the StopLoss & TakeProfit field within the ORDERSEND to "0" or else an ECN/STP broker will not execute the order.

So, my question is: How can I execute an order at Market (using ORDERSEND) and then immediately (or within a second or two) attach a SL/TP to that ticket?

Here's a piece of the code pertinent to my question:

if(CurrentPriceBuy>UpperPriceTrigger)
{
OrderSend(Sym,OP_BUY,Lots,CurrentPriceBuy,0,0,0,Co mm,0,0,CLR_NONE);
}

Can anyone help me insert code right after the ORDERSEND within the if { } that I can attach a TP/SL of say arbitrarily 25pips to that specific ticket?

Any help would be greatly appreciated. Thanks in advance.

Regards,
Kasio
 
 

Hi Ubzen,

Thanks for the swift reply. I took a look at your code as you suggested, obviously it was well above my understanding.

I did however extract from one of your codes what I thought would work for me and this is what I've done:



if(CurrentPriceBuy>UpperPriceTrigger)
{
TakeProfit = UpperPriceTrigger + (TP_Pips/1000);
StopLoss = UpperPriceTrigger - (SL_Pips/1000);
Ticket=OrderSend(Sym,OP_BUY,Lots,CurrentPriceBuy,0,0,0,"",0,0,CLR_NONE);
Sleep(4000);
if(Ticket>-1) OrderModify(Ticket,OrderOpenPrice(),TakeProfit,StopLoss,0,CLR_NONE);

}

It complied properly and the order opened properly however, the order was not modified to include the TP and SL.

I even added a wait of about 4 seconds to give the dealer a chance to catch up and it didn't work.

Can you please help me out. Thanks.

 

You have to Order-Select before you can Order-Modify.

        if(Ticket>-1){
            if(OrderSelect(Ticket, SELECT_BY_TICKET)){
                OrderModify(Ticket,OrderOpenPrice(),Sl,Tp,0,Color);
}   }   }   }
 

I just did that, still not working. Can you please take a look at this code and tell me why it's not modifying SL/TP. Thanks:


if(CurrentPriceBuy>UpperPriceTrigger)
{
TakeProfit = UpperPriceTrigger + (TP_Pips/1000);
StopLoss = UpperPriceTrigger - (SL_Pips/1000);
Ticket=OrderSend(Sym,OP_BUY,Lots,CurrentPriceBuy,0,0,0,"",0,0,CLR_NONE);
if(Ticket>-1) { if(OrderSelect(Ticket, SELECT_BY_TICKET)){
OrderModify(Ticket,OrderOpenPrice(),TakeProfit,StopLoss,0,CLR_NONE);}
}
}

 

There can be a million things wrong.

You'll want to print the Error. And use the SRC buttton above when you Submit Code. See if the below compiles and gets the last error. Plus it looks like your Sl and Tp are in the wrong place. I'm not going to code this for you, so you'll have to pay attention to detail.

bool OrderModify( int ticket, double price, double stoploss, double takeprofit, datetime expiration, color arrow_color=CLR_NONE)

if(CurrentPriceBuy>UpperPriceTrigger)
 {
 TakeProfit = UpperPriceTrigger + (TP_Pips/1000);
 StopLoss = UpperPriceTrigger - (SL_Pips/1000);
 Ticket=OrderSend(Sym,OP_BUY,Lots,CurrentPriceBuy,0,0,0,"",0,0,CLR_NONE);
if(Ticket>-1) { 
    if(OrderSelect(Ticket, SELECT_BY_TICKET) ){
      if(OrderModify(Ticket,OrderOpenPrice(),StopLoss,TakeProfit,0,CLR_NONE)==false){
         Print( "Order_Modify_Failed_Last_Error=" + GetLastError() );
      }
    }
}
 

Ok,


I keep getting "Order Modified Failed Last Error 130" which I guess translates into invalid stops. I've tried experimenting with different stops and still get the same error. Any thoughts on that?


Again, I do appreciate taking your time to help me with this.

 

Well there's a good chance that your stops too close. In that case I'll recommend you create a function for this.

It'll need to try processing the Sl and Tp on the next tick.

It'll need to search for the market order, select it, and try to modify if Sl==0.

It'll need to check the Stop_Level and Freeze_Levels.

It'll need to make sure the Sl and Tp that you are trying to modify it to is No-Closer then the Brokers Sl and Tp levels in Points/Pips.

 

Ubzen,

Your help has been greatly appreciated but what you are discussing is way beyond my head.

Thanks for all your help, I'm gonna sleep on it and try to figure it out tomorrow. It's past midnight my time.

Again, much appreciated.

 
//++++ These are adjusted for 5 digit brokers.
int     pips2points;    // slippage  3 pips    3=points    30=points
double  pips2dbl;       // Stoploss 15 pips    0.015      0.0150
int     Digits.pips;    // DoubleToStr(dbl/pips2dbl, Digits.pips)
int     init(){
     if (Digits % 2 == 1){      // DE30=1/JPY=3/EURUSD=5 forum.mql4.com/43064#515262
                pips2dbl    = Point*10; pips2points = 10;   Digits.pips = 1;
    } else {    pips2dbl    = Point;    pips2points =  1;   Digits.pips = 0; }
    // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl
//---- These are adjusted for 5 digit brokers.
    /* On ECN brokers you must open first and THEN set stops
    int ticket = OrderSend(..., 0,0,...)
    if (ticket < 0)
       Alert("OrderSend failed: ", GetLastError());
    else if (!OrderSelect(ticket, SELECT_BY_TICKET))
       Alert("OrderSelect failed: ", GetLastError());
    else if (!OrderModify(OrderTicket(), OrderOpenPrice(), SL, TP, 0)
       Alert("OrderModify failed: ", GetLastError());
     */
Reason: