error 4107 ordersend

 

Why an I receiving ordersend error 4107 and invalid takeprofit for ordersend function on testing the following code?


extern string SECTION_C = "*** Trade Management ***";
input int StopLoss = 40;  // Stop Loss
input int TakeProfit = 1000;  // Take Profit
input int TrailingStop = 10; // Trailing Stop As Perecentage Of Profit (EG 10.0 = 10%)
input int MinimumProfit = 400; // Minimum Profit To Trailing Stop
input int MinProfitBE = 0;   // Minimum Profit To Break Even    
input int LockProfit = 0;   // Minimum Profit Lock In At Break Even

   // Our order condition  
   if(accountOk && spreaddist && hour == startHour && gBuyTicket == 0 && gSellTicket == 0) 
   {
      double pendingBuyPrice = yhigh; // Here i need the hi and lo instead of
      pendingBuyPrice = NormalizeDouble(pendingBuyPrice,_Digits); 
      double pendingSellPrice = ylow;
      pendingSellPrice = NormalizeDouble(pendingSellPrice,_Digits);
      
      // Create stop and take profit values for our pendingorder parameters (I dont verify stoplevel because it moves SLTP to close to price) 
      double buyStopLoss = 0.0;
      double buyTakeProfit = 0.0;
      double sellStopLoss = 0.0;
      double sellTakeProfit = 0.0;
      if(StopLoss > 0) buyStopLoss = pendingBuyPrice - (StopLoss * _Point);
      if(StopLoss > 0) sellStopLoss = pendingSellPrice + (StopLoss * _Point);        
      if(TakeProfit > 0) buyTakeProfit = pendingBuyPrice + (TakeProfit * _Point);
      if(TakeProfit > 0) sellTakeProfit = pendingSellPrice - (TakeProfit * _Point);                  

      // Verify Buy stop loss & take profit    (This stops order send problems when i exclude SLTP)
      double stopLevel = MarketInfo(_Symbol,MODE_STOPLEVEL) * _Point;
      
      RefreshRates();
      double upperStopLevel = Ask + stopLevel;   // All buystop(pendingorder), selllimit(pendingorder), buytakeprofit and sellstoploss prices must be > upperStopLevel.
      double lowerStopLevel = Bid - stopLevel;   // All sellstop(pendingorder), buylimit(pendingorder), selltakeprofit and buystoploss prices must be < lowerStopLevel.  

      // Buy
      if(pendingBuyPrice <= upperStopLevel) 
      {
         pendingBuyPrice = upperStopLevel + _Point; // buystop (pendingorder) must be > upperStopLevel
         if(StopLoss > 0) buyStopLoss = pendingBuyPrice - (StopLoss * _Point); // Also SL TP must be moved alongwith pendingBuyPrice
         if(TakeProfit > 0) buyTakeProfit = pendingBuyPrice + (TakeProfit * _Point); 
      }
      
      // Sell
      if(pendingSellPrice >= lowerStopLevel) 
      {
         pendingSellPrice = lowerStopLevel - _Point; // sellstop(pendingorder) must be < lowerStopLevel
         if(StopLoss > 0) sellStopLoss = pendingSellPrice + (StopLoss * _Point); // Also SL TP must be moved alongwith pendingSellPrice
         if(TakeProfit > 0) sellTakeProfit = pendingSellPrice - (TakeProfit * _Point);
      }
           
      buyStopLoss = NormalizeDouble(buyStopLoss,_Digits);
      buyTakeProfit = NormalizeDouble(buyTakeProfit,_Digits);
      sellStopLoss = NormalizeDouble(sellStopLoss,_Digits);
      sellTakeProfit = NormalizeDouble(sellTakeProfit,_Digits);

      
      // Pending Orders    if(Bid < pendingBuyPrice)    if(Ask > pendingSellPrice)      
      if(Ask > pendingSellPrice) gSellTicket = OrderSend(_Symbol,OP_SELLSTOP,lotSize,pendingSellPrice,Slippage,sellStopLoss,sellTakeProfit,"SellOrder",MagicNumber,0,clrRed);   
   }

 
  1.       if(StopLoss > 0) buyStopLoss = pendingBuyPrice - (StopLoss * _Point);
          if(StopLoss > 0) sellStopLoss = pendingSellPrice + (StopLoss * _Point);        
          if(TakeProfit > 0) buyTakeProfit = pendingBuyPrice + (TakeProfit * _Point);
          if(TakeProfit > 0) sellTakeProfit = pendingSellPrice - (TakeProfit * _Point);  
    You buy at the Ask and sell at the Bid. There for pendingBuyPrice must be relative to the Ask, but your buyStopLoss and buyTakeProfit must be relative to the Bid. You have them relative to the open price, therefor even if StopLoss equals TakeProfit you will not have a 1:1 RRR.
  2. Why to you calculate those, when you will recalculate them when pendingBuyPrice < upperStopLevel? Move the pending prices, then calculate the stops.
  3. You are not testing if your stops are greater than stopLevel.
  4. You are not adjusting SL, TP, and slippage; for 4/5 digit brokers and for JPY pairs.
  5. Do NOT use NormalizeDouble, EVER. For ANY Reason. It's a kludge, don't use it. It's use is always wrong
  6. if(Ask > pendingSellPrice) gSellTicket = ...
    What is the purpose of the if? You already moved pendingSellPrice below Bid - lowerStopLevel, therefor the if will always be true.
  7. On ECN type brokers you must open first and then set stops.
  8. Check your return codes and find out why. What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
 
whroeder1:
  1. You buy at the Ask and sell at the Bid. There for pendingBuyPrice must be relative to the Ask, but your buyStopLoss and buyTakeProfit must be relative to the Bid. You have them relative to the open price, therefor even if StopLoss equals TakeProfit you will not have a 1:1 RRR.
  2. Why to you calculate those, when you will recalculate them when pendingBuyPrice < upperStopLevel? Move the pending prices, then calculate the stops.
  3. You are not testing if your stops are greater than stopLevel.
  4. You are not adjusting SL, TP, and slippage; for 4/5 digit brokers and for JPY pairs.
  5. Do NOT use NormalizeDouble, EVER. For ANY Reason. It's a kludge, don't use it. It's use is always wrong
  6. What is the purpose of the if? You already moved pendingSellPrice below Bid - lowerStopLevel, therefor the if will always be true.
  7. On ECN type brokers you must open first and then set stops.
  8. Check your return codes and find out why. What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles

Thanks for this advice but I'm a little stuck on how to get the Bid and Ask prices from the previous day? Bid and Ask are predefined variables to only current price?
 
Stephen Reynolds:

Thanks for this advice but I'm a little stuck on how to get the Bid and Ask prices from the previous day? Bid and Ask are predefined variables to only current price?


You can't get the Ask.

You can get the Bid price at the open, high, low and close of any previous bar.

I'd suggest working with the close price.

 
Stephen Reynolds: Thanks for this advice but I'm a little stuck on how to get the Bid and Ask prices from the previous day? Bid and Ask are predefined variables to only current price?
You can't on MT4. You get the Bid price from a previous bar and add the current average spread (Ask-Bid) to estimate it.