Author: Copyright 2019, MetaQuotes Software Corp.
0 Views
0 Downloads
0 Favorites
GridCalc
ÿþ//+------------------------------------------------------------------+

//|                                                        GUIDE.mq5 |

//|                        Copyright 2019, MetaQuotes Software Corp. |

//|                                             https://www.mql5.com |

//+------------------------------------------------------------------+

#property copyright "Copyright 2019, MetaQuotes Software Corp."

#property link      "https://www.mql5.com"

#property version   "1.00"

#property script_show_inputs

sinput int maxrange = 10000;

sinput double equityrate = 0.1;

sinput double volume = 0.01;

sinput bool calcmargin = true;

sinput ENUM_ORDER_TYPE type = ORDER_TYPE_BUY;

//+------------------------------------------------------------------+

//| Script program start function                                    |

//+------------------------------------------------------------------+

void OnStart()

  {

//---

   ResetLastError() ;

   double Ex = AccountInfoDouble(ACCOUNT_EQUITY) * equityrate;

   double price = SymbolInfoDouble(_Symbol,(type == ORDER_TYPE_BUY) ? SYMBOL_ASK : SYMBOL_BID);

   double maxlossn;

   if(!OrderCalcProfit(type,_Symbol,volume,price,(type == ORDER_TYPE_BUY) ? price + maxrange * _Point : price - maxrange * _Point,maxlossn) || maxlossn <= 0)

     {

      Print(__FUNCTION__,_LastError);

      return;

     }



   double perm = 0.0;

   if(calcmargin)

      if(!OrderCalcMargin(type,_Symbol,volume,price,perm))

        {

         Print(_LastError);

         return;

        }



   int maxn = int((2.0 * Ex - maxlossn) / (2.0 * perm + maxlossn)); //maxn=int(2*Ex/maxlossn)-1;

   if(maxn <= 0)

     {

      Print(maxn);

      return;

     }

//-----------

   int perpip = maxrange / maxn;

   double perpa;

   if(!OrderCalcProfit(type,_Symbol,volume,price,(type == ORDER_TYPE_BUY) ? price + perpip*_Point : price - perpip * _Point,perpa))

     {

      Print(_LastError);

      return;

     }

   double maxvol = volume * maxn; //*(1+maxn)/2.0,^—XÓN

   double maxmargin;

   if(!OrderCalcMargin(type,_Symbol,maxvol,price,maxmargin))

     {

      Print(_LastError);

      return;

     }

   printf("per:%f,pip:%d,n:%d,ex:%f,maxlossn:%f,maxvol:%f,maxmargin:%f",perpa,perpip,maxn,Ex,maxlossn,maxvol,maxmargin);

//--------------

   int n = 1;

   double pms[][3];//iprice,imargin,iloss

   while(!IsStopped())

     {

      ArrayResize(pms,n);

      perpip = maxrange / n;

      double out = (type == ORDER_TYPE_BUY) ? price - maxrange *  _Point : price + maxrange *  _Point;

      double sumargin = 0,sumloss = 0;

      for(int i = 0; i < n; i++)

        {

         pms[i][0] = (type == ORDER_TYPE_BUY) ? price - i * perpip * _Point : price + i * perpip * _Point;

         if(!OrderCalcMargin(type,_Symbol,volume,pms[i][0],pms[i][1]) || pms[i][1] <= 0)

           {

            Print(__FUNCTION__,_LastError);

            return;

           }

         if(!OrderCalcProfit(type,_Symbol,volume,out,pms[i][0],pms[i][2]))

           {

            Print(__FUNCTION__,_LastError);

            return;

           }

         if(calcmargin)

            sumargin += pms[i][1];

         sumloss += pms[i][2];

        }

      if(sumargin + sumloss >= Ex)

        {

         printf("margin:%f,loss:%f,out:%f,pip:%d,n:%d",sumargin,sumloss,out,perpip,n);

         break;

        }

      n++;

     }

   ArrayPrint(pms);

//Calc_Grid(maxrange,Ex,volume,price,calcmargin,NULL,type);

  }

//+------------------------------------------------------------------+



//+------------------------------------------------------------------+

Comments