Author: Copyright 2015, mrak297.
0 Views
0 Downloads
0 Favorites
mmi
//+------------------------------------------------------------------+
//|                                                          MMI.mq4 |
//|                                         Copyright 2015, mrak297. |
//|                            https://www.mql5.com/ru/users/mrak297 |
//+------------------------------------------------------------------+
#property copyright "Copyright 2015, mrak297."
#property link      "https://www.mql5.com/ru/users/mrak297"
#property version   "15.7"
#property strict
#property indicator_chart_window
//--- input parameters
input double Risk=15;
input color  LabelColor=clrSilver; //Color
input int    FontSize=10;
input ENUM_BASE_CORNER CORNER=CORNER_LEFT_LOWER;
//---
double Lots,Spread;
ENUM_ANCHOR_POINT ANCHOR=ANCHOR_LEFT_LOWER;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   EventSetMillisecondTimer(250);
//---
   if(CORNER==CORNER_LEFT_LOWER) ANCHOR=ANCHOR_LEFT_LOWER;
   else if(CORNER == CORNER_LEFT_UPPER) ANCHOR = ANCHOR_LEFT_UPPER;
   else if(CORNER == CORNER_RIGHT_LOWER) ANCHOR = ANCHOR_RIGHT_LOWER;
   else if(CORNER == CORNER_RIGHT_UPPER) ANCHOR = ANCHOR_RIGHT_UPPER;
//---
   Lots=CalculateLot(Risk);
   Spread=MarketInfo(_Symbol,MODE_SPREAD);
//---
   LabelCreate("Lot",10,20);
   LabelCreate("Risk",10,40);
   LabelCreate("Spread",10,60);
//---
   SetLabelText();
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
   return(rates_total);
  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
   Lots=CalculateLot(Risk);
   Spread=MarketInfo(_Symbol,MODE_SPREAD);
//---
   SetLabelText();
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   ObjectDelete(0,"Lot");
   ObjectDelete(0,"Risk");
   ObjectDelete(0,"Spread");
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double CalculateLot(double risk)
  {
   double Free       = NormalizeDouble(AccountFreeMargin() * risk / 100, 2);
   double MargRec    = MarketInfo(Symbol(), MODE_MARGINREQUIRED);
   double MinLot     = MarketInfo(Symbol(), MODE_MINLOT);
   double MaxLot     = MarketInfo(Symbol(), MODE_MAXLOT);
   double LotResult  = 0.0;
//---
   if(Free!=0 && MargRec!=0)
      LotResult=NormalizeDouble(Free/MargRec,2);
   else
      LotResult=-1;
//---
   if(LotResult!=-1)
     {
      if(MinLot!=0)
        {
         if(LotResult<MinLot) LotResult=MinLot;
        }
      if(MaxLot!=0)
        {
         if(LotResult>MaxLot) LotResult=MaxLot;
        }
     }
//---
   return(LotResult);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void LabelCreate(string name,int x,int y)
  {
   ObjectDelete(name);
//---
   ResetLastError();
//---
   if(ObjectCreate(0,name,OBJ_LABEL,0,0,0))
     {
      ObjectSetInteger(0,name,OBJPROP_XDISTANCE,x);
      ObjectSetInteger(0,name,OBJPROP_YDISTANCE,y);
      ObjectSetInteger(0,name,OBJPROP_CORNER,CORNER);
      ObjectSetString(0,name,OBJPROP_FONT,"Arial");
      ObjectSetInteger(0,name,OBJPROP_FONTSIZE,FontSize);
      ObjectSetInteger(0,name,OBJPROP_ANCHOR,ANCHOR);
      ObjectSetInteger(0,name,OBJPROP_COLOR,LabelColor);
      ObjectSetInteger(0,name,OBJPROP_HIDDEN,true);
      ObjectSetInteger(0,name,OBJPROP_SELECTABLE,false);
     }
   else Print("Error: ",GetLastError());
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void SetLabelText()
  {
   if(Lots!=-1)
      ObjectSetString(0,"Lot",OBJPROP_TEXT,"Lot = "+DoubleToString(Lots,2));
   else
      ObjectSetString(0,"Lot",OBJPROP_TEXT,"Lot = NaN");
//---
   ObjectSetString(0,"Risk",OBJPROP_TEXT,"Risk = "+DoubleToString(Risk,2));
//---
   if(Spread!=0)
      ObjectSetString(0,"Spread",OBJPROP_TEXT,"Spread = "+DoubleToString(Spread,2));
   else
      ObjectSetString(0,"Spread",OBJPROP_TEXT,"Spread = NaN");
  }
//+--------------------------------------------------------------------+

Comments