Please help I Face error 131 from the trade, what wrong!

 


Hello, here is my code why facing error 131 , Do some one help me... Chaiya


if()..
 {
         buy_TP=0;
         if(StopLoss_In_PIP>0)
            buy_SL=Ask-StopLoss_In_PIP*pip;
         else buy_SL=0;
         buy_TP=NormalizePrice(buy_TP);
         buy_SL=NormalizePrice(buy_SL);
         //
         //set lots
         double LostPerLot=LZ*1000*pip+Trade_Commission_Per_Lot;
         if(StopLoss_In_PIP>0) LostPerLot=LZ*StopLoss_In_PIP*pip+Trade_Commission_Per_Lot;
         double buy_lots=AccountFreeMargin()*Risk_In_Percent/100/LostPerLot;
         //
         buy_lots=NormalizeLots(buy_lots);
         if(buy_lots<minlots) buy_lots=minlots;
         if(buy_lots>maxlots) buy_lots=maxlots;
         //
         // check trade conditions
         if((buy_SL<pip || (Bid-buy_SL)/pip>stop_level) && 
            (buy_TP<pip || (buy_TP-Bid)/pip>stop_level))
            if(external_n_internal_trend()>0 || (!is_internal_trend_active() && !is_external_trend_active()))
              {
              if(is_funds_enough)
               ticket=OrderSend(_Symbol,OP_BUY,buy_lots,Ask,slippage,buy_SL,buy_TP,order_comment,magic,0,clrNONE);
              }
        }
 
  1. Why did you post your MT4 question in the Root / MT5 EA section instead of the MQL4 section, (bottom of the Root page?)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
    Next time post in the correct place. The moderators will likely move this thread there soon.

  2. ticket=OrderSend(_Symbol,OP_BUY,buy_lots,Ask,slippage,buy_SL,buy_TP,order_comment,magic,0,clrNONE);
    Check your return codes for errors, report them and you would know why.
              What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
    Only those functions that return a value (e.g. iClose, MarketInfo, etc.) must you call ResetLastError before in order to check after.

  3. Use the debugger or print out your variables, including _LastError and find out why.

  4. (buy_TP-Bid)/pip>stop_level)
    Is stop_level in pips? Market Info returns points.

  5. double LostPerLot=LZ*1000*pip+Trade_Commission_Per_Lot;
             if(StopLoss_In_PIP>0) LostPerLot=LZ*StopLoss_In_PIP*pip+Trade_Commission_Per_Lot;
             double buy_lots=AccountFreeMargin()*Risk_In_Percent/100/LostPerLot;
    Hard coding constants (1000) means your code breaks on other symbols. Free margin has nothing to do with risk. Risk depends on your initial stop loss, lot size, and the value of the pair.
    1. You place the stop where it needs to be - where the reason for the trade is no longer valid. E.g. trading a support bounce the stop goes below the support.
    2. Account Balance * percent/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerLot + CommissionPerLot) (Note OOP-OSL includes the SPREAD, and DeltaPerLot is usually around $10/pip but it takes account of the exchange rates of the pair vs. your account currency.)
    3. Do NOT use TickValue by itself - DeltaPerLot and verify that MODE_TICKVALUE is returning a value in your deposit currency, as promised by the documentation, or whether it is returning a value in the instrument's base currency (EUR, in this case).
                MODE_TICKVALUE is not reliable on non-fx instruments with many brokers.
    4. You must normalize lots properly and check against min and max.
    5. You must also check FreeMargin to avoid stop out
    Most pairs are worth about $10 per PIP. A $5 risk with a (very small) 5 PIP SL is $5/$10/5=0.1 Lots maximum.
Reason: