Invalid Volume - Error during EA Validation - page 2

 
I know, because I wrote it also. Just want to understand what is the variable that causes, sometimes the expert can buy the biggest lot, and sometime not. 
 
Zsolt Szabo #: I know, because I wrote it also. Just want to understand what is the variable that causes, sometimes the expert can buy the biggest lot, and sometime not. 

Already answered—margin requirements.

Forum on trading, automated trading systems and testing trading strategies

lot size = money size

Fernando Carreiro, 2022.11.29 01:30

For this example I will use EUR/USD, where 1 pip-lot is worth 10 USD and a pip is defined as 0.0001 (not 0.00001 which is a point).

However when placing the order, one also has to account for the margin requirements and in this case 1 lot is 100000 EUR (1:1 leverage).

For example, for a leverage of 1:100, one would require 1000 USD for a full lot, or 500 USD for 0.5 lots or 50 USD for 0.05 lots.

In this case, your balance of 400 USD would allow placing the 0.05 order but would give a "not enough money" error for the 0.5 lots, even if your stop-loss risk is only 20 pips (100 USD for 20 pips at 0.5 lots).

 
Could someone please help me with the script below? It's giving me a volume error and I've tried to fix it but the error persists
   
#property copyright "Copyright 2024, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"

#include <Trade\Trade.mqh>
CTrade trade;



int h_sar;
int h_mm;

double sar[];
double ema[];
MqlRates rate[];


input double _volume = 0.01; // Informe a quantidade de lotes.

int OnInit(){

   ArraySetAsSeries(rate, true);
   ArraySetAsSeries(sar,true);
   ArraySetAsSeries(ema, true);
   
   
   h_sar = iSAR(Symbol(), Period(), 0.02, 0.2);
   h_mm  = iMA(Symbol(), Period(), 9, 0,MODE_EMA, PRICE_CLOSE);

   
   return(INIT_SUCCEEDED);
}



void OnTick(){  

   CopyBuffer(h_sar, 0, 0, 3, sar);
   CopyBuffer(h_mm, 0, 0, 3, ema);
   
   CopyRates(Symbol(), Period(), 0, 3, rate);
   
   
   if(PositionsTotal() == 0 && OrdersTotal() == 0){
   
      if(rate[1].open < ema[1] && rate[1].close > ema[1] && ema[2] < ema[1]){
         
         double priceOpen = rate[1].high + (30 *_Point);
         double _sl = rate[1].low - (30 *_Point);
         trade.BuyStop(_volume, priceOpen, Symbol(), _sl, 0.00, 0, 0, "buy");

        }
   }

}

 
Discover new MetaTrader 5 opportunities with MQL5 community and services
Discover new MetaTrader 5 opportunities with MQL5 community and services
  • 2024.04.23
  • www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions
 
vieirawesley #Could someone please help me with the script below? It's giving me a volume error and I've tried to fix it but the error persists  

Add the following function to your code and change this line:

trade.BuyStop(NormalizeVolume(_volume), priceOpen, Symbol(), _sl, 0.00, 0, 0, "buy");

Forum on trading, automated trading systems and testing trading strategies

Desperate "No trading operations" error

Vinicius Pereira De Oliveira, 2024.04.21 14:42

It must be corrected, Juan. For example: if the EA uses a fixed volume parameter and by default this volume is set at 0.01, but during validation testing the tested symbols do not allow volume 0.01, then the EA does not open a position and will not be able to be published. So, to avoid this problem, you can use a function like the following, in addition to the volume checks recommended in the article:

//+--------------------------------------------------------------------------------------------------------------------+
//| This function normalizes the volume according to the minimum volume change step                                    |
//+--------------------------------------------------------------------------------------------------------------------+
double NormalizeVolume(double Lot)
  {
   ResetLastError();
//--- Minimal and maximal allowed volume for trade operations
   double LotMin  = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MIN);
   double LotMax  = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MAX);
//--- Get minimal step of volume changing
   double LotStep = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_STEP);

//--- Check if an execution error occurred
   if(GetLastError() != ERR_SUCCESS)
     {
      return(WRONG_VALUE);
     }

//--- Normalizes the volume
   Lot = LotMin + MathFloor((Lot - LotMin) / LotStep) * LotStep;
   Lot = NormalizeDouble(MathMin(LotMax, MathMax(LotMin, Lot)), 2);

//--- Normalized volume
   return(Lot);
  }

 
Vinicius Pereira De Oliveira #:

Add the following function to your code and change this line:


Thank you my brother! It helped me a lot!
Reason: