Most Suitable points equivalent to lotage (Synthetic Indices)

 

Hello guys i need to determine the number of points suitable for the amount and percentage i want to risk  i realise that as the account balance changes the number of pints used to calculate lot size can produce a lotsize greater or less than the desired amount and some times even greater or less than the max or min lotage

The results shown in the image scanner.png are the is the desired results but here i'd put a fixed lotsize  i intend  for this little experiment of mine to work under all account balances of all sizes so the code  should display the possible range of points starting from the equiverlent to the min lot size up to the max for all account sizes.Your contributions will be greatly apreciated.

#include <Trade/Trade.mqh>
CTrade trade;
double ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
void OnTick()
  {
   //Comment("\n Points_perbar",CHART_POINTS_PER_BAR);

   double points; // variable for points
   double output_lotsize; // variable for output lot size
   double desired_lotsize = 0.2; // variable for desired lot size
   double step_size = 10; // variable for step size
   double start_point; // variable for start point
   double end_point; // variable for end point
   double max_lotsize = SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_MAX); // replace with the maximum lot size for your symbol
   double min_lotsize = SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_MIN); // replace with the minimum lot size for your symbol
   double adjustment_factor = 100; // replace with the actual adjustment factor
Comment("\nMax_Vol ",max_lotsize," Min_Vol ",min_lotsize);
   bool found = false; // variable for found flag


   for(points=100; points<=2000000; points+=step_size)
     {
      double sldistance =ask - points;
      sldistance =sldistance*_Point;
      output_lotsize = Calculate_lotsize(20.0,sldistance);  // call your function with points as input

      // adjust step size if lot size is less or greater than desired volume
      if(output_lotsize < min_lotsize)
        {
         step_size *= (1 + adjustment_factor); // increase step size
         Print("Lotsize less than max_lot",step_size);
        }
      else
         if(output_lotsize > max_lotsize)
           {
            step_size *= (1 - adjustment_factor);
            step_size=NormalizeDouble(step_size,_Digits);
             // decrease step size
            Print("Lotsize greater than max_lot",step_size);
           }
           

   if(output_lotsize==desired_lotsize)
     {
      Print("Found a match: points = ", points, ", lot size = ", output_lotsize);
      // exit the loop if a match is found
      if(!found) // if this is the first match
        {
         start_point = points; // set the start point to the current points
         found = true; // set the found flag to true
        }
      end_point = points; // update the end point to the current points
     }
   else
     {
      Print("No match found: points = ", points, ", lot size = ", output_lotsize);
      }
     
     
     }






  }


//---  Lotsize
double Calculate_lotsize(double Riskpercent,double slDistance)
  {
   double ticksize=SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_SIZE);
   double tickvalue=SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_VALUE);
   double lotstep =SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_STEP);
// Print(ticksize,"ticksize ", tickvalue," tickvalue ",lotstep,"lotstep");
   if(ticksize==0 || tickvalue==0 || lotstep==0)
     {
      Print(__FUNCTION__,"< Lotsize cannot be calculated....");
      return 0;
     }

   double riskMoney =AccountInfoDouble(ACCOUNT_EQUITY)*Riskpercent/100;
   double Moneylotstep=(slDistance/ticksize)*tickvalue*lotstep;
   if(Moneylotstep==0)
     {
      Print(__FUNCTION__,"< Moneylotstep cannot be calculated....");
      return 0;
     }
   double lots =MathFloor(riskMoney/Moneylotstep)*lotstep;
   //Comment("riskmoney ",riskMoney,"Moneytobelost",Moneylotstep);
   return lots;
  }
//+------------------------------------------------------------------+
Files:
scanner.PNG  35 kb