Download MetaTrader 5

How to create a stoploss/takeprofit order

To add comments, please log in or register
Find out how to install MetaTrader on Linux
saltzmanjoelh
9
saltzmanjoelh 2012.03.15 18:18 

You can see the request had accurate values... Any ideas why it's taking the order but not applying the stops?

 

2012.03.15 09:59:51 Core 1 2012.03.12 00:06:00   market sell 0.10 EURUSD (1.31175 / 1.31194 / 1.31175)//does this mean it didn't accept my stop loss and take profit values?

2012.03.15 09:59:51 Core 1 2012.03.12 00:06:00   ~~~~~~~~~~   Price: 1.311940 SL: 1.311890 TP: 1.312040 //from the MqlTradeRequest object


 

void newOrder(ENUM_ORDER_TYPE orderType)
{
      MqlTick latest_price;      // To be used for getting recent/latest price quotes
      MqlTradeRequest mrequest;  // To be used for sending our trade requests
      MqlTradeResult mresult;    // To be used to get our trade results
      MqlRates mrate[];          // To be used to store the prices, volumes and spread of each bar
      ZeroMemory(mrequest);      // Initialization of mrequest structure
      
      ArraySetAsSeries(mrate,true);
      
      //--- Get the last price quote using the MQL5 MqlTick Structure
      if(!SymbolInfoTick(_Symbol,latest_price))
     {
      Alert("Error getting the latest price quote - error:",GetLastError(),"!!");
      return;
     }
     
      if(orderType == ORDER_TYPE_SELL){
         printf("SELL %f", latest_price.ask);
      }else{
         printf("BUY %f", latest_price.ask);
      }
      ZeroMemory(mrequest);
      mrequest.action = TRADE_ACTION_DEAL;                                  // immediate order execution
      mrequest.price = NormalizeDouble(latest_price.ask,_Digits);           // latest ask price
      mrequest.sl = NormalizeDouble(latest_price.ask - STP*_Point,_Digits); // Stop Loss
      mrequest.tp = NormalizeDouble(latest_price.ask + TKP*_Point,_Digits); // Take Profit
      mrequest.symbol = _Symbol;                                            // currency pair
      mrequest.volume = Lot;                                                 // number of lots to trade
      mrequest.magic = EA_Magic;                                             // Order Magic Number
      mrequest.type = orderType;                                        // Buy Order
      mrequest.type_filling = ORDER_FILLING_AON;                             // Order execution type
      mrequest.deviation=5;                                                // Deviation from current price
      
      
      printf("~~~~~~~~~~ STP: %i Points: %f Digits: %i Total: %f", STP, _Point, _Digits,  STP*_Point);
      printf("~~~~~~~~~~   Price: %f SL: %f TP: %f", mrequest.price, mrequest.sl, mrequest.tp);
      
      //--- send order
      OrderSend(mrequest,mresult);
      //return;
      // get the result code
      if(mresult.retcode==10009 || mresult.retcode==10008) //Request is completed or order placed
        {
         Alert("A Buy order has been successfully placed with Ticket#:",mresult.order,"!!");
        }
      else
        {
         Alert("The Buy order request could not be completed -error:",GetLastError());
         ResetLastError();           
         return;
        }
        
}
johhny6
130
johhny6 2012.03.17 12:07  
saltzmanjoelh:

You can see the request had accurate values... Any ideas why it's taking the order but not applying the stops?

 

2012.03.15 09:59:51 Core 1 2012.03.12 00:06:00   market sell 0.10 EURUSD (1.31175 / 1.31194 / 1.31175)//does this mean it didn't accept my stop loss and take profit values?

2012.03.15 09:59:51 Core 1 2012.03.12 00:06:00   ~~~~~~~~~~   Price: 1.311940 SL: 1.311890 TP: 1.312040 //from the MqlTradeRequest object


 



i've found that some brokers don't let you submit the stop-loss or take-profit with the original order, and you therefore need to either sleep() for 3-4 seconds until the order is submitted (but not dealt), or make sure your EA goes into a trailing-stop mode or other code section that modify the order, and add stop-loss/tp to it.

To add comments, please log in or register