Download MetaTrader 5

help for moneymanagement code

To add comments, please log in or register
Daminda
110
Daminda  

I use following moneymanagement code for my Ea. it working in micro accounts. but in ECN accounts not working. please some one help me for edit it

if(MoneyManagement ==true)
   {
      double Vol = (AccountEquity()/1000);
   }
      else Vol = FixedLot;

thanks

Dr. Trader
4052
Dr. Trader  
I have tested this code in MT4 build 788 in both ECN and normal accounts, its ok in both cases. What problem exactly do you get?
Daminda
110
Daminda  
Dr.Trader:
I have tested this code in MT4 build 788 in both ECN and normal accounts, its ok in both cases. What problem exactly do you get?

It showing as "ordersend error 131 - wrong volume"

Files:
Alain Verleyen
Moderator
29865
Alain Verleyen  
Daminda:

It showing as "ordersend error 131 - wrong volume"

You have to normalize your volume, nothing to do with ECN or not.
Daminda
110
Daminda  
Alain Verleyen:
You have to normalize your volume, nothing to do with ECN or not.
i have small mql knowledge. can you help me for edit it?
Dr. Trader
4052
Dr. Trader  

You have to call function NormalizeDouble with first parameter - your volume, and second parameter - digits in lot, usually 2 or 1. This will make sure that your volume has only two decimal digits. Its similar to round, and must be mandatory used for all volumes and prices before OrderSend.

Vol = NormalizeDouble(Vol, 2);

Also, make sure to do the same with your order price, stoploss, takeprofit:

double orderPrice = Ask;
double orderStoploss = Ask-0.001;

orderPrice = NormalizeDouble(orderPrice, _Digits);
orderStoploss = NormalizeDouble(orderStoploss, _Digits);

 

Usually I use following code to make sure that volume is setup according to broker settings:

//+------------------------------------------------------------------+
//|                                                  volume_norm.mq4 |
//|                                        Copyright 2015, Dr.Trader |
//|                          https://www.mql5.com/en/users/dr.trader |
//+------------------------------------------------------------------+
#property copyright "Copyright 2015, Dr.Trader"
#property link      "https://www.mql5.com/en/users/dr.trader"
#property version   "1.00"
#property strict

int _LotDigits;
double _MinLot;
double _MaxLot;
   
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   _LotDigits = 0;
   double lotStep = MarketInfo(_Symbol, MODE_LOTSTEP);
   while(MathRound(lotStep) != lotStep){
      _LotDigits++;
      lotStep *= 10.0;
   }
   _MinLot = NormalizeDouble(MarketInfo(_Symbol, MODE_MINLOT), _LotDigits);
   _MaxLot = NormalizeDouble(MarketInfo(_Symbol, MODE_MAXLOT), _LotDigits);

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   double orderVolume = 0;
   
   //insert your money management code here
   //...
   //...
   
   if (orderVolume < _MinLot){
      orderVolume = _MinLot;
   }
   if (orderVolume > _MaxLot){
      orderVolume = _MaxLot;
   }
   orderVolume = NormalizeDouble(orderVolume, _LotDigits);
  }
//+------------------------------------------------------------------+
Daminda
110
Daminda  
Dr.Trader:

You have to call function NormalizeDouble with first parameter - your volume, and second parameter - digits in lot, usually 2 or 1. This will make sure that your volume has only two decimal digits. Its similar to round, and must be mandatory used for all volumes and prices before OrderSend.

Also, make sure to do the same with your order price, stoploss, takeprofit:

 

Usually I use following code to make sure that volume is setup according to broker settings:

Thanks Dr.Trader i will try it...
Daminda
110
Daminda  
Thanks Dr.Trader i Created Ea with moneyManagement for ECN broker. thanks for your advice & thanks for Alain Verleyen.
To add comments, please log in or register