Download MetaTrader 5

how to replace the stop orders into market orders ??

To add comments, please log in or register
Giorgio Gaudio
251
Giorgio Gaudio 2016.04.26 20:15 
Many EA that we can develop  are, undoubtedly, very high performance in testing
 
Especially when we talk about strategies developed with input orders "stop-orders" .. Obviously we know that within the other software have the opportunity to also test with a hypothetical slippage and despite this we can pull out beautiful strategies ... However ...
when we go to trade LIVE we encounter so many factors ;
 
  • variable spreads
  • speed of execution of the broker
  • Latency of the machine (VPS)
  • depth market
  • liquidity
  • volatility
        etc. etc
 
EA more sensitive to these issues are undoubtedly those who work in "BUY_STOP" and "SELL_STOP"
 
According to my studies, however, the EA less sensitive to these aspects are undoubtedly those that open positions directly At MARKET
For this reason I would like to have the opportunity to turn my EA working in BUY_STOP And SELL_STOP in OPENING MARKET orders.
 
There are PRO and AGAINST in changing the type of order and I am not here to list them, but 'surely, for many EA in live, may be the solution to many problems!
 
I'm no expert on mql codes, and wanted to ask through to change manually EA code, if I have the opportunity to replace the stop orders into market orders
I have already tried but I failed and I have no idea now how i can do 
 
someone can help me?
 
thank you so much
Alain Verleyen
Moderator
31134
Alain Verleyen 2016.04.27 09:14  
Giorgio Gaudio:
...
According to my studies, however, the EA less sensitive to these aspects are undoubtedly those that open positions directly At MARKET

For this reason I would like to have the opportunity to turn my EA working in BUY_STOP And SELL_STOP in OPENING MARKET orders.

I have some doubts about these statements. Why a market order would be less sensitive to latency or slippage than a STOP order ?
 
I'm no expert on mql codes, and wanted to ask through to change manually EA code, if I have the opportunity to replace the stop orders into market orders
I have already tried but I failed and I have no idea now how i can do 
Where is your code ?
Giorgio Gaudio
251
Giorgio Gaudio 2016.04.27 11:32  
Alain Verleyen:
I have some doubts about these statements. Why a market order would be less sensitive to latency or slippage than a STOP order ?
Where is your code ?
market order would be less sensitive to latency or slippage than a STOP order because (unfortunately) 90% of the brokers have a fraudulent handling of customer orders! so do not speak of a purely technical discussion of entry, but unfortunately if we make visible the order to the broker go "magically" encounter slippage even in low volatility phases .. is useless to deny that even the most serious broker has no doubt a job behind the scenes and only a few clients we are spared from this "game"
are a professional trader, I've also worked for most brokers and I know what I'm saying .. so make the "invisible" orders to the broker would be the solution
The entrance to the market (in part and not always) makes avoid manipulation and is a first step to have operational strategies operational.
Obviously this is true for those strategies a bit more aggressive and not for those of trend following or more soft ...

there are two solutions:

1 hide stop orders with a management inside of your machine (mt4) but with good chances to extend the latency of execution
2 transform pending orders in market orders

which may be the solution?

below a "code sample" of a constructed strategy stop_orders

(Can not be used in trading this strategy, it's just an example)

 

int start() {

   drawStats();
   
   if(!customStart()) return(0);

   if(manageTrades()) {
      // we are within trading range hours (if set)
   
      //-------------------------------------------
      // ENTRY RULES

      // LONG: ((Stoch(20, 20, 20) < 2) Or (RSI(2) < 2))
      if(TradeLong) {
         bool LongEntryCondition = ((iStochastic(NULL, 0, 20, 20, 20, MODE_SMA, 0, MODE_MAIN, 1) < 2) || (iRSI(NULL, 0, 2, PRICE_CLOSE, 1) < 2));
         if(LongEntryCondition == true) {
            openPosition(1);
         }
      }
   
      // SHORT: ((Stoch(20, 20, 20) > 2) Or (RSI(40) > 2))
      if(TradeShort) {
         bool ShortEntryCondition = ((iStochastic(NULL, 0, 20, 20, 20, MODE_SMA, 0, MODE_MAIN, 1) > 2) || (iRSI(NULL, 0, 40, PRICE_CLOSE, 1) > 2));
         if(ShortEntryCondition == true) {
            openPosition(-1);
         }
      }
   }
   
   if(getMarketPosition() != 0) {
      //-------------------------------------------
      // EXIT RULES
      if(isMarketLongPosition()) {
         // we are in LONG position
          // Exit Rule: ((Close(1) Closes Below Highest(93)) Or (Close(14) Closes Above Lowest(8)))
          if(OrderOpenTime() >= Time[0]) return; // New line of Code! Exit if the order was just opened
          if ((((Open[1] > getHighest(93, 1)) && (Close[1] < getHighest(93, 1))) || ((Open[14] < getLowest(8, 1)) && (Close[14] > getLowest(8, 1))))) {
             closePositionAtMarket();
         }
      }
   
      if(isMarketShortPosition()) {
         // we are in SHORT position
          // Exit Rule: ((Close(1) Closes Above Lowest(93)) Or (Close(14) Closes Below Highest(8)))
          if(OrderOpenTime() >= Time[0]) return; // New line of Code! Exit if the order was just opened
          if ((((Open[1] < getLowest(93, 1)) && (Close[1] > getLowest(93, 1))) || ((Open[14] > getHighest(8, 1)) && (Close[14] < getHighest(8, 1))))) {
             closePositionAtMarket();
         }
      }

      manageStop();
   }

   return(0);
}

//+------------------------------------------------------------------+

int init() {
   Log("--------------------------------------------------------");
   Log("Starting the EA");

   double realDigits;
   if(Digits < 2) {
      realDigits = 0;
   } else if (Digits < 4) {
      realDigits = 2;
   } else {
      realDigits = 4;
   }

   gPointPow = MathPow(10, realDigits);
   gPointCoef = 1/gPointPow;
                                                     
   double brokerStopDifferenceNumber = MarketInfo(Symbol(),MODE_STOPLEVEL)/MathPow(10, Digits);
   brokerStopDifference = gPointPow*brokerStopDifferenceNumber;

   eaStopDifferenceNumber = MinDistanceOfStopFromPrice/gPointPow;

   eaStopDifference = DoubleToStr(MinDistanceOfStopFromPrice, 2);
   Log("Broker Stop Difference: ",DoubleToStr(brokerStopDifference, 2),", EA Stop Difference: ",eaStopDifference);

   if(DoubleToStr(brokerStopDifference, 2) != eaStopDifference) {
      Log("WARNING! EA Stop Difference is different from real Broker Stop Difference, the backtest results in MT4 could be different from results of Genetic Builder!");
      
      if(eaStopDifferenceNumber < brokerStopDifferenceNumber) {
         eaStopDifferenceNumber = brokerStopDifferenceNumber;
      }      
   }

   string brokerSpread = DoubleToStr((Ask - Bid)*gPointPow, 2);
   string strGbSpread = DoubleToStr(gbSpread, 2);
   Log("Broker spread: ",brokerSpread,", Genetic Builder test spread: ",strGbSpread);

   if(strGbSpread != brokerSpread) {
      Log("WARNING! Real Broker spread is different from spread used in Genetic Builder, the backtest results in MT4 could be different from results of Genetic Builder!");
   }

   if(TimeStringToDateTime(TimeRangeTo) < TimeStringToDateTime(TimeRangeFrom)) {
      tradingRangeReverted = true;
      Log("Trading range s reverted, from: ", TimeRangeFrom," to ", TimeRangeTo);
   } else {
      tradingRangeReverted = false;
   }

   Log("--------------------------------------------------------");

   customInit();

   if(DisplayInfoPanel) {
      ObjectCreate("line1", OBJ_LABEL, 0, 0, 0);
      ObjectSet("line1", OBJPROP_CORNER, LabelCorner);
      ObjectSet("line1", OBJPROP_YDISTANCE, OffsetVertical + 0 );
      ObjectSet("line1", OBJPROP_XDISTANCE, OffsetHorizontal);
      ObjectSetText("line1", "Diana Strategy gbpusd _15min_", 9, "Tahoma", LabelColor);

      ObjectCreate("linec", OBJ_LABEL, 0, 0, 0);
      ObjectSet("linec", OBJPROP_CORNER, LabelCorner);
      ObjectSet("linec", OBJPROP_YDISTANCE, OffsetVertical + 16 );
      ObjectSet("linec", OBJPROP_XDISTANCE, OffsetHorizontal);
      ObjectSetText("linec", "Generated by StrategyQuant 3.8.1", 8, "Tahoma", LabelColor);

      ObjectCreate("line2", OBJ_LABEL, 0, 0, 0);
      ObjectSet("line2", OBJPROP_CORNER, LabelCorner);
      ObjectSet("line2", OBJPROP_YDISTANCE, OffsetVertical + 28);
      ObjectSet("line2", OBJPROP_XDISTANCE, OffsetHorizontal);
      ObjectSetText("line2", "------------------------------------------", 8, "Tahoma", LabelColor);

      ObjectCreate("lines", OBJ_LABEL, 0, 0, 0);
      ObjectSet("lines", OBJPROP_CORNER, LabelCorner);
      ObjectSet("lines", OBJPROP_YDISTANCE, OffsetVertical + 44);
      ObjectSet("lines", OBJPROP_XDISTANCE, OffsetHorizontal);
      ObjectSetText("lines", "Last Signal:  -", 9, "Tahoma", LabelColor);

      ObjectCreate("lineopl", OBJ_LABEL, 0, 0, 0);
      ObjectSet("lineopl", OBJPROP_CORNER, LabelCorner);
      ObjectSet("lineopl", OBJPROP_YDISTANCE, OffsetVertical + 60);
      ObjectSet("lineopl", OBJPROP_XDISTANCE, OffsetHorizontal);
      ObjectSetText("lineopl", "Open P/L: -", 8, "Tahoma", LabelColor);

      ObjectCreate("linea", OBJ_LABEL, 0, 0, 0);
      ObjectSet("linea", OBJPROP_CORNER, LabelCorner);
      ObjectSet("linea", OBJPROP_YDISTANCE, OffsetVertical + 76);
      ObjectSet("linea", OBJPROP_XDISTANCE, OffsetHorizontal);
      ObjectSetText("linea", "Account Balance: -", 8, "Tahoma", LabelColor);

      ObjectCreate("lineto", OBJ_LABEL, 0, 0, 0);
      ObjectSet("lineto", OBJPROP_CORNER, LabelCorner);
      ObjectSet("lineto", OBJPROP_YDISTANCE, OffsetVertical + 92);
      ObjectSet("lineto", OBJPROP_XDISTANCE, OffsetHorizontal);
      ObjectSetText("lineto", "Total profits/losses so far: -/-", 8, "Tahoma", LabelColor);

      ObjectCreate("linetp", OBJ_LABEL, 0, 0, 0);
      ObjectSet("linetp", OBJPROP_CORNER, LabelCorner);
      ObjectSet("linetp", OBJPROP_YDISTANCE, OffsetVertical + 108);
      ObjectSet("linetp", OBJPROP_XDISTANCE, OffsetHorizontal);
      ObjectSetText("linetp", "Total P/L so far: -", 8, "Tahoma", LabelColor);
   }

   return(0);
}

//+------------------------------------------------------------------+

int deinit() {
   ObjectDelete("line1");
   ObjectDelete("linec");
   ObjectDelete("line2");
   ObjectDelete("lines");
   ObjectDelete("lineopl");
   ObjectDelete("linea");
   ObjectDelete("lineto");
   ObjectDelete("linetp");
   return(0);
}

//+------------------------------------------------------------------+

double getSpecialSL(double value) {
   return(value);
}

double getSpecialPT(double value) {
   return(value);
}

double getNormalSL(double value) {
   return(value);
}

double getNormalPT(double value) {
   return(value);
}

double getBid() {
   return(Bid);
}

double getAsk() {
   return(Ask);
}

//+------------------------------------------------------------------+

void manageTradeSLPT() {
}

//+------------------------------------------------------------------+

double getTradeOpenPrice(int tradeDirection) {
   RefreshRates();

   if(tradeDirection == 1) {
      // long
         return(getHighest(2, 1) + (0) * (sqToPips(2.0)));
   } else {
      // short
      return(getLowest(2, 1) + (0) * (sqToPips(2.0)));
   }
}

//+------------------------------------------------------------------+

double getStopLoss(int tradeDirection) {
   if(tradeDirection == 1) {
      // long
      return(checkCorrectMinMaxSLPT((0.43 * iATR(NULL, 0, 36, 1))));
   } else {
      // short
      return(checkCorrectMinMaxSLPT((0.43 * iATR(NULL, 0, 36, 1))));
   }
   
   return(0);
}

//+------------------------------------------------------------------+

double getProfitTarget(int tradeDirection) {
   if(tradeDirection == 1) {
      // long
      return(checkCorrectMinMaxSLPT((3.51 * iATR(NULL, 0, 68, 1))));
   } else {
      // short
      return(checkCorrectMinMaxSLPT((3.51 * iATR(NULL, 0, 68, 1))));
   }
   
   return(0);
}
//+------------------------------------------------------------------+

double getProfitTrailingByTick() {
   if (OrderType() == OP_BUY) {
      // long
      return((0.34 * iATR(NULL, 0, 92, 1)));
   } else if (OrderType() == OP_SELL) {
      // short
      return((0.34 * iATR(NULL, 0, 92, 1)));
   }

   return(0);
}

//+------------------------------------------------------------------+

double getStopTrailingByClose() {
   double value = 0;
   if (OrderType() == OP_BUY) {

   } else if (OrderType() == OP_SELL) {
   }

   return(value);
}

//+------------------------------------------------------------------+

double getMoveSLValueByTick() {
   if (OrderType() == OP_BUY) {
      // long
   } else if (OrderType() == OP_SELL) {
      // short
   }

   return(0);
}


//+------------------------------------------------------------------+

void drawStats() {
    // changed recognition of bar open to support also range/renko charts
   static datetime tmp;
   static double open;

   if (tmp!= Time[0]) { // } || open != Open[0]) { - this doesn't work with renko charts
      tmp =  Time[0];
      open = Open[0];
      sqIsBarOpen = true;
   } else {
      sqIsBarOpen = false;
   }
/*
    // old way of checking for new bar open, doesn't work with range/renko bars
   string currentPeriod = sqGetTimeAsStr();
   if(currentPeriod == sqLastPeriod) {
      sqIsBarOpen = false;
   } else {
      sqLastPeriod = currentPeriod;
      sqIsBarOpen = true;
   }
*/   
   sqTextFillOpens();
   if(sqIsBarOpen) {
      sqTextFillTotals();
   }
}

//+------------------------------------------------------------------+

bool manageTrades() {
   if(Bars<30) {
      Print("NOT ENOUGH DATA: Less Bars than 30");
      return(0);
   }

   closeTradesAtEndOfRange();

   if(!sqIsBarOpen) return(false);

   if(getMarketPosition() != 0) {
      manageTradeSLPT();
   }

   manageOrdersExpiration();

   if(LimitSignalsToRange && checkInsideTradingRange() == false) {
      return(false);
   }

   if(!isCorrectDayOfWeek(Time[0])) {
      return(false);
   }

   if(MaxTradesPerDay > 0) {
     if(getNumberOfTradesToday() >= MaxTradesPerDay) {
        return(false);
     }
   }

   return(true);
}

//+------------------------------------------------------------------+

void closeTradesAtEndOfRange() {
   if(isSomeOrderActive() != 0) {
      if(ExitAtEndOfDay) {
         if(ExitTimeEOD == "00:00" || ExitTimeEOD == "0:00") {
            closeTradeFromPreviousDay();
         } else if(TimeCurrent() >= TimeStringToDateTime(ExitTimeEOD)) {
            closeActiveOrders();
            closePendingOrders();
         }
      }

      if(ExitOnFriday) {
         int dow = TimeDayOfWeek(Time[0]);

         if(ExitTimeOnFriday == "00:00" || ExitTimeOnFriday == "0:00") {
            if(dow == 6 || dow == 0 || dow == 1) {
               closeTradeFromPreviousDay();
            }
         } else if(dow == 5 && TimeCurrent() >= TimeStringToDateTime(ExitTimeOnFriday)) {
            closeActiveOrders();
            closePendingOrders();
         }
      }
   }

   if(LimitSignalsToRange) {
      if(checkInsideTradingRange() == false) {
         // we are out of allowed trading hours
         if(ExitAtEndOfRange) {
            if(tradingRangeReverted == false && TimeCurrent() > TimeStringToDateTime(TimeRangeTo)) {
               closeActiveOrders();
               closePendingOrders();
            } else if(tradingRangeReverted == true && TimeCurrent() > TimeStringToDateTime(TimeRangeTo) && TimeCurrent() < TimeStringToDateTime(TimeRangeFrom)) {
               closeActiveOrders();
               closePendingOrders();
            }
         }
      }
   }
}

//+------------------------------------------------------------------+

double gbTrueRange(int period, int index) {
   int period1 = period + index-1;
   int period2 = period + index;
   return (MathMax(High[period1], Close[period2]) - MathMin(Low[period1], Close[period2]));
}

//+------------------------------------------------------------------+

double gbBarRange(int period, int index) {
   int period2 = period + index-1;
   return (MathAbs(High[period2] - Low[period2]));
}

//+------------------------------------------------------------------+

void openPosition(int tradeDirection) {
   if(tradeDirection == 0) return;


   if(checkTradeClosedThisBar()) {
      return;
   }

   if(checkTradeClosedThisMinute()) {
      return;
   }

   //---------------------------------------
   // get order price
   double openPrice = NormalizeDouble(getTradeOpenPrice(tradeDirection), Digits);

   //---------------------------------------
   // get order type
   int orderType;
   if(tradeDirection == 1) {
      if(getMarketPosition() != 0) return;

      orderType = OP_BUYSTOP;
   } else {
      if(getMarketPosition() != 0) return;

      orderType = OP_SELLSTOP;
   }

   if(orderType != OP_BUY && orderType != OP_SELL) {
      // it is stop or limit order
      double AskOrBid;
      if(tradeDirection == 1) { AskOrBid = Ask; } else { AskOrBid = Bid; }

      // check if stop/limit price isn't too close
      if(NormalizeDouble(MathAbs(openPrice - AskOrBid), Digits) <= NormalizeDouble(eaStopDifferenceNumber, Digits)) {
         //Log("stop/limit order is too close to actual price");
         return;
      }

      // check price according to order type
      if(orderType == OP_BUYSTOP) {
         if(AskOrBid >= openPrice) return;
      } else if(orderType == OP_SELLSTOP) {
         if(AskOrBid <= openPrice) return;

      } else if(orderType == OP_BUYLIMIT) {
         if(AskOrBid <= openPrice) return;
      } else if(orderType == OP_SELLLIMIT) {
         if(AskOrBid >= openPrice) return;
      }

      // there can be only one active order of the same type
      if(checkPendingOrderAlreadyExists(orderType)) {
         if(!ReplacePendingOrders) {
            return;
         } else {
            if(!closePendingOrder()) {
                Log("Cannot close existing previous pending order with ticket: ", OrderTicket(),", reason: ", GetLastError());
                return;  
            }
         }
      }
   }

   //---------------------------------------
   // add SL/PT
   double stopLoss = 0;
   double profitTarget = 0;

   double SL = NormalizeDouble(getStopLoss(tradeDirection), Digits);
   double PT = NormalizeDouble(getProfitTarget(tradeDirection), Digits);

   if(SL != 0) {
      stopLoss = openPrice - tradeDirection * SL;
   }
   if(PT != 0) {
      profitTarget = openPrice + tradeDirection * PT;
   }

   string comment = CustomComment;

   double orderLots = getLots(SL*gPointPow);
   if(orderLots > MaximumLots) {
      orderLots = MaximumLots;
   }

   // open order with error handling and retries
   int ticket = 0;
   int retries = 3;
   while(true) {
      retries--;
      if(retries < 0) return;
      if(getMarketPosition() != 0) return;

      if(sqIsTradeAllowed() == 1) {
         ticket = openOrderWithErrorHandling(orderType, orderLots, openPrice, stopLoss, profitTarget, comment, MagicNumber);
         if(ticket > 0) {
            if(tradeDirection > 0) {
                ObjectSetText("lines", "Last Signal: Long, ticket: "+ticket, 8, "Tahoma", LabelColor);
            } else {
                ObjectSetText("lines", "Last Signal: Short, ticket: "+ticket, 8, "Tahoma", LabelColor);
            }
            return;
         }
      }

      if(ticket == -130 || ticket == -131) {
         // invalid stops or volume, we cannot open the trade
         return;
      }

      Sleep(1000);
   }
   return;
}

//+------------------------------------------------------------------+

int openOrderWithErrorHandling(int orderType, double orderLots, double openPrice, double stopLoss, double profitTarget, string comment, int magicNumber) {

   //---------------------------------------
   // send order
   int error, ticket;
   Log("Opening order, direction: ", orderType,", price: ", openPrice, ", Ask: ", Ask, ", Bid: ", Bid);
   ticket = OrderSend(Symbol(), orderType, orderLots, openPrice, MaxSlippage, 0, 0, comment, magicNumber, 0, Green);
   if(ticket < 0) {
      // order failed, write error to log
      error = GetLastError();
      Log("Error opening order: ",error, " : ", ErrorDescription(error));
      return(-error);
   }

   rettmp = OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES);
   Log("Order opened: ", OrderTicket(), " at price:", OrderOpenPrice());

   stopLoss = getSpecialSL(stopLoss);
   profitTarget = getSpecialPT(profitTarget);

   if(EmailNotificationOnTrade) {
      SendMail("GB Strategy - Order opened", getNotificationText());
   }

   // set up stop loss and profit target");
   // It has to be done separately to support ECN brokers
   if(stopLoss != 0 || profitTarget != 0) {
      Log("Setting SL/PT, SL: ", stopLoss, ", PT: ", profitTarget);
      if(OrderModify(ticket, OrderOpenPrice(), stopLoss, profitTarget, 0, 0)) {
         Log("Order modified, StopLoss: ", OrderStopLoss(),", Profit Target: ", OrderTakeProfit());
      } else {
         Log("Error modifying order: ",error, " : ", ErrorDescription(error));
      }
   }

   return(ticket);
}

//+------------------------------------------------------------------+
/**
 * manage trade - move SL to break even or trailing stop
 */
void manageStop() {

    if(!sqIsBarOpen) return;
      
   double trailingStopValue, moveSLValue;
   double orderSL, normalOrderSL, orderOpen;
   double close = Close[1];
   double tsLevel, newSL;

   for(int i=0; i<OrdersTotal(); i++) {
      if (OrderSelect(i,SELECT_BY_POS)==true && OrderMagicNumber() == MagicNumber && OrderSymbol() == Symbol()) {

         if(OrderType() != OP_BUY && OrderType() != OP_SELL) continue;
         if(OrderOpenTime() >= Time[0]) continue; // exit if the order was just opened

         //------------------------------
         // profit trailing on close
         trailingStopValue = getProfitTrailingByTick();
         if(trailingStopValue > 0) {
            if(OrderType() == OP_BUY) {
               tsLevel = close - trailingStopValue;
            } else {
  	            tsLevel = close + trailingStopValue;
            }
            orderSL = OrderStopLoss();
            normalOrderSL = getNormalSL(orderSL);
            newSL = getSpecialSL(tsLevel);

            if(OrderType() == OP_BUY) {
               if(isSLCorrect(tsLevel) && (orderSL == 0 || normalOrderSL < tsLevel) && !doublesAreEqual(orderSL, newSL)) {
     	            rettmp = OrderModify(OrderTicket(), OrderOpenPrice(), newSL, OrderTakeProfit(), 0);
     	         }
            } else {
     	         if (isSLCorrect(tsLevel) && (orderSL == 0 || normalOrderSL > tsLevel)  && !doublesAreEqual(orderSL, newSL)) {
     	            rettmp = OrderModify(OrderTicket(), OrderOpenPrice(), newSL, OrderTakeProfit(), 0);
     	         }
            }
         }

			//--------------------------------------------------------
   		// manage stop trailing on close
         trailingStopValue = getStopTrailingByClose();
         if(trailingStopValue > 0) {
            orderOpen = OrderOpenPrice();
            orderSL = OrderStopLoss();
            normalOrderSL = getNormalSL(orderSL);
            newSL = getSpecialSL(trailingStopValue);

            if(OrderType() == OP_BUY) {
              	if(isSLCorrect(trailingStopValue) && (orderSL == 0 || normalOrderSL < trailingStopValue) && !doublesAreEqual(orderSL, newSL)) {
	           		rettmp = OrderModify(OrderTicket(), OrderOpenPrice(), newSL, OrderTakeProfit(), 0);
	           	}
	         } else {
	          	if (isSLCorrect(trailingStopValue) && (orderSL == 0 || normalOrderSL > trailingStopValue) && !doublesAreEqual(orderSL, newSL)) {
	           		rettmp = OrderModify(OrderTicket(), OrderOpenPrice(), newSL, OrderTakeProfit(), 0);
	           	}
	         }
         }

         //--------------------------------------------------------
         // manage SL 2 BE (by tick)
         moveSLValue = getMoveSLValueByTick();
         if(moveSLValue > 0) {
            orderSL = OrderStopLoss();
            normalOrderSL = getNormalSL(orderSL);
            orderOpen = OrderOpenPrice();
            newSL = getSpecialSL(orderOpen);

            if(OrderType() == OP_BUY) {
               if(isSLCorrect(orderOpen) && (close - orderOpen >= moveSLValue) && (orderSL == 0 || normalOrderSL < orderOpen) && !doublesAreEqual(orderSL, newSL)) {
                  rettmp = OrderModify(OrderTicket(), orderOpen, newSL, OrderTakeProfit(), 0);
               }
            } else {
               if (isSLCorrect(orderOpen) && (orderOpen - close >= moveSLValue) && (orderSL == 0 || normalOrderSL > orderOpen) && !doublesAreEqual(orderSL, newSL)) {
                  rettmp = OrderModify(OrderTicket(), orderOpen, newSL, OrderTakeProfit(), 0);
               }
            }
         }
      }
   }
}
//+------------------------------------------------------------------+

bool isSLCorrect(double slPrice) {
   if(OrderType() == OP_BUY) {
      if(slPrice < (Bid-eaStopDifferenceNumber)) {
         return(true);
      }
   } else {
      if(slPrice > (Ask+eaStopDifferenceNumber)) {
         return(true);
      }
   }

   return(false);
}

//+------------------------------------------------------------------+

bool checkPendingOrderAlreadyExists(int orderType) {
   for(int i=0; i<OrdersTotal(); i++) {
      if (OrderSelect(i,SELECT_BY_POS)==true && OrderMagicNumber() == MagicNumber && OrderSymbol() == Symbol() && OrderType() == orderType) {
         return(true);
      }
   }
   return(false);
}

//+------------------------------------------------------------------+

bool closePendingOrder() {
   int ticket = OrderTicket();

   if(OrderDelete(ticket)) {
      lastDeletedOrderTicket = ticket;
      return(true);
   }
   
   return(false);
}

//+------------------------------------------------------------------+

int getMarketPosition() {
   for(int i=0; i<OrdersTotal(); i++) {
      if (OrderSelect(i,SELECT_BY_POS)==true && OrderMagicNumber() == MagicNumber && OrderSymbol() == Symbol()) {
         if(OrderType() == OP_BUY) {
            return(1);
         }
         if(OrderType() == OP_SELL) {
            return(-1);
       }
     }
   }

   return(0);
}

//+------------------------------------------------------------------+

bool isMarketLongPosition() {
   for(int i=0; i<OrdersTotal(); i++) {
      if (OrderSelect(i,SELECT_BY_POS)==true && OrderMagicNumber() == MagicNumber && OrderSymbol() == Symbol()) {
         if(OrderType() == OP_BUY) {
            return(true);
         }
      }
   }

   return(false);
}

//+------------------------------------------------------------------+

bool isMarketShortPosition() {
   for(int i=0; i<OrdersTotal(); i++) {
      if (OrderSelect(i,SELECT_BY_POS)==true && OrderMagicNumber() == MagicNumber && OrderSymbol() == Symbol()) {
         if(OrderType() == OP_SELL) {
            return(true);
         }
      }
   }

   return(false);
}

//+------------------------------------------------------------------+

bool isSomeOrderActive() {
   for(int i=0; i<OrdersTotal(); i++) {
      if (OrderSelect(i,SELECT_BY_POS)==true && OrderMagicNumber() == MagicNumber && OrderSymbol() == Symbol()) {
         return(true);
     }
   }

   return(false);
}

//+------------------------------------------------------------------+

bool checkItIsPendingOrder() {
   if(OrderType() != OP_BUY && OrderType() != OP_SELL) {
     return(true);
   }
   return(false);
}

//+------------------------------------------------------------------+

bool selectOrderByMagicNumber() {
   for(int i=0; i<OrdersTotal(); i++) {
     if (OrderSelect(i,SELECT_BY_POS)==true && OrderMagicNumber() == MagicNumber && OrderSymbol() == Symbol()) {

       return(true);
     }
   }

   return(false);
}

//+------------------------------------------------------------------+

bool selectOpenOrderByMagicNumber() {
   for(int i=0; i<OrdersTotal(); i++) {
      if (OrderSelect(i,SELECT_BY_POS)==true && OrderMagicNumber() == MagicNumber && OrderSymbol() == Symbol()) {

         if(checkItIsPendingOrder()) {
            continue;
         }

         return(true);
      }
   }

   return(false);
}

//+------------------------------------------------------------------+

void closePositionAtMarket() {
   RefreshRates();
   double priceCP;

   if(OrderType() == OP_BUY) {
      priceCP = Bid;
   } else {
      priceCP = Ask;
   }

   rettmp = OrderClose(OrderTicket(), OrderLots(), priceCP, MaxSlippage); 
// etc etc etc etc

Doerk Hilger
1181
Doerk Hilger 2016.04.27 12:32  

You´re absolutely right and I agree totally. This result below is from yesterday, where I tested another broker on a live account in view of slippage and requotes. The broker is the one in germany, who calls theirself the best. The test position was 8 points in profit, the stop loss was 2 points above the break even, means it trailed in profit including commission, but the order was closed with a LOSS!. An SL is nothing else than a stop order and the result is simply "magic". There were no news, no increasing spread, nothing. Just a broker who is cheating heavily. 

"Best" german broker with heavy requotes 

Your solution is called synthetic MIT orders (market if touched). For this you need to create your own class which watches levels of such synthetic MIT orders and which executes them as market. A function like this is not provided by MetaTrader itself. I realized this by myself and I guess you have no other choice too.

If you want, drop me an PM.

Giorgio Gaudio
251
Giorgio Gaudio 2016.04.27 13:17  
Doerk Hilger:

You´re absolutely right and I agree totally. This result below is from yesterday, where I tested another broker on a live account in view of slippage and requotes. The broker is the one in germany, who calls theirself the best. The test position was 8 points in profit, the stop loss was 2 points above the break even, means it trailed in profit including commission, but the order was closed with a LOSS!. An SL is nothing else than a stop order and the result is simply "magic". There were no news, no increasing spread, nothing. Just a broker who is cheating heavily. 

 

Your solution is called synthetic MIT orders (market if touched). For this you need to create your own class which watches levels of such synthetic MIT orders and which executes them as market. A function like this is not provided by MetaTrader itself. I realized this by myself and I guess you have no other choice too.

If you want, drop me an PM.

... who is expert in the forex market, he is aware that slippage is part, in an unambiguous, in our trading operations ... but ...
there is a difference transform what should be an extraordinary circumstance in an ordinary circumstance (for each order and for no apparent reason)
most expert eye to recognize the executed "magic" of brokers is very simple ..
This little game is undoubtedly used by most brokers also world famous and who makes the cost is obviously the retail trader, maybe even possessor of good high frequency automatic strategies ... but poorly informed on the trading background

have the possibility of eliminating or even shave this aspect could turn the entire trading activity

To date, the only solution I found was to adapt the most aggressive EAs to these unpleasant circumstances but obviously with less profitable performances ..

 I hope someone can have a suitable instrument in these circumstances

be a good trader does not mean being a good programmer and be a good programmer does not mean being a good trader


able to combine the two, however, it would be ideal

To add comments, please log in or register