Sending orders to ECN

 

Just wondering if someone already worked in the code to send an order with stops to an ecn. can you please share it? I do not seem to make it work on my end

the idea is to send the order without any stops, verify that the deal is executed and then modify it adding the stops.

 
emforex:

Just wondering if someone already worked in the code to send an order with stops to an ecn. can you please share it? I do not seem to make it work on my end

the idea is to send the order without any stops, verify that the deal is executed and then modify it adding the stops.

Example of Buy trade. Requires global variable for TakeProfit, StopLoss, Lots, MagicNumber and Slippage values.

//---------- Open Buy Position ---------------------------------
void OpenBuy()
{
  double SL=0, TP=0;
  int err=0;

  MqlTradeRequest Request;
  Request.action = TRADE_ACTION_DEAL;
  Request.magic = MagicNumber;
  Request.symbol = _Symbol;
  Request.volume = Lots;
  Request.price = NormalizeDouble(SymbolInfoDouble(Symbol(), SYMBOL_ASK), _Digits);
  Request.type = ORDER_TYPE_BUY; 
  Request.sl = 0; // No StopLoss Market Stop
  Request.tp = 0; // No TakeProfit Market Stop
  Request.type_filling = ORDER_FILLING_AON;
  Request.type_time = ORDER_TIME_GTC;
  Request.deviation = Slippage;
  Request.comment = "Buy Trade";
   
  MqlTradeResult Result;
   
  bool result = OrderSend(Request, Result); 
  if (result == false)  { err=GetLastError(); Print("Buy Trade Failed. error ",err); ResetLastError(); return;} // If trade fails, do not proceed to trade Modify.
  else Print("Result Code: ", Result.retcode, ", ", "Deal: ", Result.deal, ", ", "Volume: ", Result.volume, ", ", 
  "Price: ", Result.price, ", ", "Requote Bid: ", Result.bid, ", ", "Requote Ask: ", Result.ask);

        
//----- Modify SL and TP -------------------
   if (TakeProfit > 0) TP = SymbolInfoDouble(Symbol(), SYMBOL_ASK) + TakeProfit * _Point ; else TP = 0;
   if (StopLoss > 0) SL = SymbolInfoDouble(Symbol(), SYMBOL_ASK) - StopLoss * _Point ; else SL = 0;
                
  Request.action = TRADE_ACTION_SLTP;
  Request.symbol = _Symbol;
  Request.sl = NormalizeDouble(SL, _Digits);
  Request.tp = NormalizeDouble(TP, _Digits);
  Request.deviation = Slippage;
               
  result = OrderSend(Request, Result); 
  if (result == false)  { err=GetLastError(); Print("Modify Buy Trade Failed, Error ",err); ResetLastError(); }
  else Print("Result Code: ", Result.retcode, ", ", "Deal: ", Result.deal, ", ", "Volume: ", Result.volume, ", ", 
  "Price: ", Result.price, ", ", "Requote Bid: ", Result.bid, ", ", "Requote Ask: ", Result.ask);
      
  return;
}
//------------ End of OpenBuy() -----------------------------


 

I've got the following code to add the SL and TP onto a position after a market order has successfuly entered a new deal:

      // Submit deal
      MqlTradeRequest Request;
      MqlTradeResult Result;
      ZeroMemory(Request);
      Request.action = TRADE_ACTION_DEAL;
      Request.magic = MagicNumber;
      Request.symbol = _Symbol;
      Request.volume = NormalizeDouble(NumLots,LotDecimal);
      Request.price = NormalizeDouble(Price,_Digits);
      Request.sl = 0.0;
      Request.tp = 0.0;
      Request.deviation = gSlippage;
      Request.type = Type;
      Request.type_filling = ORDER_FILLING_AON;
      Request.type_time = ORDER_TIME_GTC;
      Request.comment = EA_Comment;
      ResetLastError();
      OrderSend(Request,Result);
      // Modify SL and Tp
      if (Result.retcode == TRADE_RETCODE_DONE) {
         ulong Ticket = Result.order;
         HistoryOrderSelect(Ticket);
         double SL = HistoryOrderGetDouble(Ticket,ORDER_PRICE_OPEN),

This code works in Strategy Tester.  I haven't shown the code that actually adjusts the SL/TP with number of pips and appends the SL/TP onto the position, my question has to do with obtaining the order open price using HistoryDealSelect() and HistoryDealGetDouble().  If I use the following code after the "if" statement, it doesn't work:

         ulong Ticket = Result.deal;
         HistoryDealSelect(Ticket);
         double SL = HistoryDealGetDouble(Ticket,DEAL_PRICE),

I receive a price of 0.00000

Why doesn't this work?  What exactly is "DEAL_PRICE" showing? Also, if there is slippage, will the price returned by HistoryOrderGetDouble() differ from the actual execution price?  I want the SL and TP to be based on the exact execution price.  Other code I have that checks to set the SL to break-even successfully uses the HistoryDealGetDouble(Ticket,DEAL_PRICE) after the deal is selected.

 

*** EDIT 2012.01.17 *** 

The problem appears to be a timing issue.  Although the original code using the HistoryOrder functions worked in ST, it intermittenty failed on demo account.  Even though the Result.retcode returned a "done" status, the execution of the deal had not completed to the point where the built-in data elements obtained from the HistoryDeal functions were avaialble.  I got it to work by using the OnTrade() event, that event isn't triggered until the deal is completely done.