Money management MQL4

 

Hello every one!


I am coding a robot to which I would like to add the classic money management however I am encountering an OrderSend error 131 problem that I cannot resolve even though I am sure that my error is super stupid!

The code :

double Lots;



{ 
      
        if(OrdersTotal() !=0) closeexisting();
            Lots=(AccountBalance()*(Risk_Percent))/(StopLoss * Pips) /(MarketInfo(Symbol(),MODE_TICKVALUE));
         OrderSend(Symbol (),OP_SELL,Lots,Bid,3,Bid + (StopLoss * Pips), Bid - (TakeProfit * Pips),NULL,MagicNumber,0,Red);
      }

What do you think..?

Thanks you :)

 

Risk depends on your initial stop loss, lot size, and the value of the symbol. It does not depend on margin and leverage. No SL means you have infinite risk. Never risk more than a small percentage of your trading funds, certainly less than 2% per trade, 6% total.

  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. AccountBalance * 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.
              MODE_TICKVALUE is not reliable on non-fx instruments with many brokers - MQL4 programming forum 2017.10.10
              Is there an universal solution for Tick value? - Currency Pairs - General - MQL5 programming forum 2018.02.11
              Lot value calculation off by a factor of 100 - MQL5 programming forum 2019.07.19

  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 or 0.1 Lots maximum.

 
Guillaume Roeder :

Le risque dépend de votre stop loss initial, de la taille du lot et de la valeur du symbole. Cela ne dépend pas de la marge et de l'effet de levier. Pas de SL signifie que vous avez un risque infini . Ne risquez jamais plus qu'un petit pourcentage de vos fonds de trading, certainement moins de 2% par trade, 6% au total.

  1. Vous placez le stop là où il doit être, là où le motif de la transaction n'est plus valable. Par exemple, en échangeant un rebond de support, le stop passe en dessous du support.

  2. AccountBalance * pourcentage/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerLot + CommissionPerLot) (Notez que OOP-OSL inclut le spread , et DeltaPerLot est généralement d'environ 10 $/pip mais il prend en compte le taux de change de la paire vs. la devise de votre compte.)

  3. N'utilisez PAS TickValue seul - DeltaPerLot et vérifier que MODE_TICKVALUE renvoie une valeur dans la devise de votre dépôt, comme la documentation, ou s'il renvoie une valeur dans la devise de base de l'instrument. MODE_TICKVALUE n'est pas fiable sur les instruments non-fx avec de nombreux courtisans - Forum de programmation MQL4 2017.10.10 Existe-t-il une solution universelle pour la valeur Tick ? - Paires de devises - Général - Forum de programmation MQL5 20 18 .02.11 Calcul de la valeur du lot d'un facteur 100 - Forum de programmation MQL5 2019.07.19
              
              
              

  4. Vous devez normaliser les lots correctement et vérifier par rapport au min et au max .

  5. Vous devez également vérifier FreeMargin pour éviter d' arrêter

La plupart des paires valent environ 10 $ par PIP . Un risque de 5 $ avec un (très petit) 5  PIP SL est de 5 $/10 $/5 ou 0,1 lot maximum.

Hi, thank you very much I hadn't thought of all this !!

So I did this:

double GetPipValue()
{
   if(_Digits >=4)
   {
      return 0.0001;
      }
      else
      {
      return 0.01;
      }
}
double OptimalLotSize(double maxRiskPrc, int maxLossInPips)
{

  double accEquity = AccountEquity();
  //Alert("accEquity: " + accEquity);
  
  double lotSize = MarketInfo(NULL,MODE_LOTSIZE);
 // Alert("lotSize: " + lotSize);
  
  double tickValue = MarketInfo(NULL,MODE_TICKVALUE);
  
  if(Digits <= 3)
  {
   tickValue = tickValue /100;
  }
  
 // Alert("tickValue: " + tickValue);
  
  double maxLossDollar = accEquity * maxRiskPrc;
 // Alert("maxLossDollar: " + maxLossDollar);
  
  double maxLossInQuoteCurr = maxLossDollar / tickValue;
 // Alert("maxLossInQuoteCurr: " + maxLossInQuoteCurr);
  
  double optimalLotSize = NormalizeDouble(maxLossInQuoteCurr /(maxLossInPips * GetPipValue())/lotSize,2);
  
  return optimalLotSize;
 
}

What do you think about it?

Me it turns nickel

Reason: