//+------------------------------------------------------------------+
//|                                        Min Equity Max Margin.mq4 |
//|                                                Divyanshu Dwivedi |
//|            http://www.binaryoptionsedge.com/user/8407-blackfoil/ |
//+------------------------------------------------------------------+
#property copyright "Divyanshu Dwivedi"
#property link      "livedivyanshu@gmail.com"
#property version   "1.0"
#property description "Apply on any chart. It calculates data after application."
#property strict
#property indicator_chart_window





#include <stdlib.mqh>
#include <stderror.mqh>

//--- indicator settings
#property indicator_chart_window
#property indicator_buffers 2

#property indicator_type1 DRAW_ARROW
#property indicator_width1 EMPTY_VALUE
#property indicator_color1 0x00FF00
#property indicator_label1 "Buy"

#property indicator_type2 DRAW_ARROW
#property indicator_width2 EMPTY_VALUE
#property indicator_color2 0x0000FF
#property indicator_label2 "Sell"

//--- indicator buffers
double Buffer1[];
double Buffer2[];

bool done_up;
bool done_down;
int counter = 0;
double checkBAL;
double Highest_Margin=AccountMargin();
double Equity, LowestEquity=AccountEquity();
double New_High,New_Low;
extern string Currency_Symbol = "$";
 double Max_Profit = 50.0;
 double Max_Loss = 30.0;
double Target = Max_Profit;
double StopLoss = Max_Loss;
double Profit;
double Loss;
datetime time_alert; //used when sending alert
double Arrow_Gap = 1;
extern int Corner = 1;
 bool Audible_Alerts = false;
 bool Push_Notifications = false;
double myPoint; //initialized in OnInit

void myAlert(string type, string message)
  {
   if(type == "print")
      Print(message);
   else if(type == "error")
     {
      Print(type+" | Smart StopLoss @ "+Symbol()+","+Period()+" | "+message);
     }
   else if(type == "order")
     {
     }
   else if(type == "modify")
     {
     }
   else if(type == "indicator")
     {
      if(Audible_Alerts) Alert(type+" | Smart StopLoss @ "+Symbol()+","+Period()+" | "+message);
      if(Push_Notifications) SendNotification(type+" | Smart StopLoss @ "+Symbol()+","+Period()+" | "+message);
     }
  }

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {   
   IndicatorBuffers(2);
   SetIndexBuffer(0, Buffer1);
   SetIndexEmptyValue(0, 0);
   SetIndexArrow(0, 160);
   SetIndexBuffer(1, Buffer2);
   SetIndexEmptyValue(1, 0);
   SetIndexArrow(1, 160);
   //initialize myPoint
   myPoint = Point();
      ObjectDelete("1");
      ObjectDelete("2");
      ObjectDelete("3");
      ObjectDelete("4");
      ObjectDelete("5");
      ObjectDelete("6");
   if(Digits() == 5 || Digits() == 3)
     {
      myPoint *= 10;
     }
   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[])
  {
   int limit = rates_total - prev_calculated;
   //--- counting from 0 to rates_total
   ArraySetAsSeries(Buffer1, true);
   ArraySetAsSeries(Buffer2, true);
   //--- initial zero
   if(prev_calculated < 1)
     {
      ArrayInitialize(Buffer1, 0);
      ArrayInitialize(Buffer2, 0);
     }
   else
      limit++;
 
   /// Check 1st stamp.
        if(counter == 0)
         {
           // double time1 = TimeCurrent();
    
            checkBAL = AccountMargin();
            Equity = AccountEquity();
    
           // double stamp1 = time1 + checkBAL;
 
            counter = 1;
         }   
         
         ObjectCreate("1",OBJ_LABEL,0,0,0,0,0);
         ObjectSet("1",OBJPROP_CORNER,Corner);
         ObjectSet("1",OBJPROP_XDISTANCE,50);
         ObjectSet("1",OBJPROP_YDISTANCE,80);
         ObjectSetText("1","Starting Margin :  "+ Currency_Symbol + DoubleToStr(checkBAL,2) ,15,"Georgia",White);
         
         ObjectCreate("2",OBJ_LABEL,0,0,0,0,0);
         ObjectSet("2",OBJPROP_CORNER,Corner);
         ObjectSet("2",OBJPROP_XDISTANCE,50);
         ObjectSet("2",OBJPROP_YDISTANCE,100);
         ObjectSetText("2","Starting Equity :  "+ Currency_Symbol + DoubleToStr(Equity,2) ,15,"Georgia",White);
 
     
      Profit = checkBAL + Target;
      Loss = checkBAL - StopLoss; 
     
    
 
   //--- main loop
  for(int i = limit-1; i >= 0; i--)
     {
     
   
     
         Buffer1[i] = 9999999;
         Buffer2[i] = 9999999;
     
      if (i >= MathMin(5000-1, rates_total-1-50)) continue; //omit some old rates to prevent "Array out of range" or slow calculation   
     // if ((Buffer1[i+1] != 0 && Buffer1[i+1] != EMPTY_VALUE) || (Buffer2[i+1] != 0 && Buffer2[i+1] != EMPTY_VALUE))  continue;
     
      if( AccountEquity() < Profit ) { done_up = false; ObjectDelete("3"); ObjectDelete("6"); }
      if( AccountEquity() > Loss ) { done_down = false; ObjectDelete("4"); ObjectDelete("5"); }
      
      if( AccountMargin() > checkBAL
      ) New_High = AccountMargin();
      if( AccountMargin() > checkBAL && AccountMargin() > Highest_Margin
      )
      { Highest_Margin = AccountMargin();
        // Buffer2[i] = 1; 
         //ObjectDelete("1");
         }
         ObjectCreate("5",OBJ_LABEL,0,0,0,0,0);
         ObjectSet("5",OBJPROP_CORNER,Corner);
         ObjectSet("5",OBJPROP_XDISTANCE,50);
         ObjectSet("5",OBJPROP_YDISTANCE,60);
         ObjectSetText("5","Highest Margin : "+ Currency_Symbol + DoubleToStr(Highest_Margin,2),15,"IMPACT",Yellow);
        
      
      if( AccountEquity() < Equity 
      ) New_Low = AccountEquity();
      if( AccountEquity() < Equity && AccountEquity() < LowestEquity
      )
      { LowestEquity = AccountEquity();
        // Buffer2[i] = 1; 
         //ObjectDelete("1");
         }
         ObjectCreate("6",OBJ_LABEL,0,0,0,0,0);
         ObjectSet("6",OBJPROP_CORNER,Corner);
         ObjectSet("6",OBJPROP_XDISTANCE,50);
         ObjectSet("6",OBJPROP_YDISTANCE,40);
         ObjectSetText("6","Lowest Equity : "+ Currency_Symbol + DoubleToStr(LowestEquity,2),15,"IMPACT",Yellow);
        
      
    }  

   return(rates_total);
  }
//+------------------------------------------------------------------+

int deinit()
  {
//----

  
      

      ObjectDelete("1");
      ObjectDelete("5");
      ObjectDelete("2");
      ObjectDelete("6");
     
   
//----
   return(0);
  }
