How to calculate % lotSeize on Free Equity

 

Hi everyone,

here posting on a simple apparently stupid topic for the mayority of y'all but still struggling in it. I've been coding for some months and gaining more experience every time but there's still this aspect i'm struggling coding.

I'd like to create a function that can work on all the currencies pairs that calculate the lotSeize in percentage (i.e: input int lotSeizePercentage = 2.5;) on my equity / free equity so i can use it instead of a static one. 

I've been struggling with all concerning pips, points, digits etc... as you can guess. is there here anyone that can make it simple to me so i can get it coded?

Thanks

 

This is my solution after quite a lot of trials and errors:

double CalculateLots(double op, double sl, ENUM_ORDER_TYPE order_type)
  {
   double lot=SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MIN);
   while(true)
     {
      double pnl=0;
      if(OrderCalcProfit(order_type, _Symbol, lot, op, sl, pnl)==false)
         return lot;
      if(pnl>=0)
         return lot;
      if(pnl<-1*AccountInfoDouble(ACCOUNT_BALANCE)*risk_per_trade/100)
         return lot;
      lot+=SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_STEP);
     }
   return NormalizeVolume(lot);
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double NormalizeVolume(double lot)
  {
   double vol_step=SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_STEP);
   lot=int(lot / vol_step)*vol_step;
   if(lot>SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MAX))
      return SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MAX);
   if(lot<SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MIN))
      return SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MIN);
   return lot;

  }
 

Thank you so much! Been kind, I'll analyse the code

Yashar Seyyedin #:

This is my solution after quite a lot of trials and errors:

 

Normally you use stop loss as a metric for risk. But that function can work if you want the thing to be independent of stop loss and fully based on percentage of the account.

But you should change 

AccountInfoDouble(ACCOUNT_BALANCE)

to

AccountInfoDouble(ACCOUNT_MARGIN_FREE)

if you want the lot size to be shaped based on free equity that you can trade with