Author: © Dimeon
0 Views
0 Downloads
0 Favorites
AllProfit
ÿþ//+------------------------------------------------------------------+

//|                                                    AllProfit.mq5 |

//|                                                         © Dimeon |

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

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

#property copyright "© Dimeon"

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

#property version   "1.00"

#property indicator_chart_window



#property indicator_plots   0



input int Hour = 12;





enum per

{

   Yestoday,

   Today,

   Three_days,

   Week,

   Ten_days,

   Month,

   Three_Month,

   Six_Month,

   From_Year,

   All_Time

};





datetime from_date = 0;

datetime fr[10];



string profit[];

int b = 0;

double profall = 0;

double perc  = 0;

double profall0 = 0;

datetime from_date0 = 0;







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

//| Custom indicator initialization function                         |

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

int OnInit()

{

   EventSetTimer(5);

   ArrayResize(profit, 12);



//---

   return(INIT_SUCCEEDED);

}



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

//|                                                                  |

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

void OnDeinit(const int reason)

{

   ObjectsDeleteAll(0, "Prof");

}

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

//| Custom indicator iteration function                              |

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

int OnCalculate(const int rates_total,     // @07<5@ <0AA820 price[]

                const int prev_calculated, // A:>;L:> 10@>2 >1@01>B0=> @0=55

                const int begin,           // >B:C40 =0G8=0NBAO 7=0G8<K5 40==K5

                const double &price[])





{

//---

   if (rates_total != prev_calculated)

   {

      for (int y = 0; y <= 9; y++)

      {

         if(ObjectFind(0, "Profit" + (string) y) < 0) ObjectCreate(0, "Profit" + (string) y, OBJ_LABEL, 0, 0, 0);

         ObjectSetInteger(0, "Profit" + (string)  y, OBJPROP_ANCHOR, ANCHOR_RIGHT_UPPER);

         ObjectSetInteger(0, "Profit" + (string) y, OBJPROP_CORNER, CORNER_RIGHT_LOWER);

         ObjectSetInteger(0, "Profit" + (string) y, OBJPROP_XDISTANCE, 10);

         ObjectSetInteger(0, "Profit" + (string) y, OBJPROP_YDISTANCE, 155 - 15 * y);



         ObjectSetInteger(0, "Profit" + (string) y, OBJPROP_FONTSIZE, 10);

         ObjectSetInteger(0, "Profit" + (string) y, OBJPROP_COLOR, clrGray);

         ObjectSetString(0, "Profit" + (string) y, OBJPROP_FONT, "Arial");

      }







      ObjectCreate(0, "Close", OBJ_BUTTON, 0, 0, 0);

      SetButton1("Close", "X", 175, 28, 18, 18, clrRed, 8, false);

      //Print ("OnInit OK");



      datetime tc = TimeCurrent();

      MqlDateTime t ;

      MqlDateTime tt ;

      TimeToStruct(tc, tt);

      TimeToStruct(tc, t);



      per i = Yestoday;



      for (i = 0; i < 9; i++)

      {

         if(i == 0)

         {

              MqlDateTime dt;

            dt.year = t.year;

            dt.day = t.day;

            dt.mon = t.mon;



            dt.hour = Hour;

            dt.min = 00;

            dt.sec = 0;

            

            from_date = tc - 60 * 60 * 24 - t.min * 60 - t.hour * 60 * 60 - t.sec + Hour * 3600;

            fr[i] = from_date;

         }



         if(i == 1)

         {

            from_date = tc - t.min * 60 - t.hour * 60 * 60 + Hour * 3600 - t.sec;

            fr[i] = from_date;

         }



         if(i == 2)

         {

            from_date = tc - 3 * 60 * 60 * 24 - t.min * 60 - t.hour * 60 * 60 + Hour * 3600 - t.sec;

            fr[i] = from_date;

         }



         if(i == 3)

         {

            from_date = tc - 7 * 60 * 60 * 24 - t.min * 60 - t.hour * 60 * 60 + Hour * 3600 - t.sec;

            fr[i] = from_date;

         }



         if(i == 4)

         {

            from_date = tc - 10 * 60 * 60 * 24 - t.min * 60 - t.hour * 60 * 60 + Hour * 3600 - t.sec;

            fr[i] = from_date;

         }



         if(i == 5)//month

         {

            int mon = t.mon;

            MqlDateTime dt;

            dt.day = t.day;

            //Print (t.mon);

            if (t.mon <= 1)

            {

               mon = t.mon + 12;

               t.year --;

            }

            else

               dt.year  = t.year;



            dt.mon = mon - 1;



            dt.year  = t.year;

            dt.hour = Hour;

            dt.min = 00;

            dt.sec = 0;



            from_date = StructToTime(dt) ;

            fr[i] = from_date;

         }



         if(i == 6) //3 monthes

         {

            int mon = 0;

            MqlDateTime dt;

            dt.day = t.day;

            //Print (t.mon);

            if (t.mon <= 3)

            {

               mon = t.mon + 12;

               dt.year = t.year - 1;

            }

            else

            {

               dt.year  = t.year;

            }



            dt.mon = t.mon - 3;



            dt.hour = Hour;

            dt.min = 00;

            dt.sec = 0;





            from_date = StructToTime(dt) ;

            fr[i] = from_date;

         }



         if(i == 7) //6 monthes

         {

            int mon = 0;

            MqlDateTime dt;

            dt.day = t.day;

            //Print (t.mon=0);

            if (t.mon <= 6)

            {

               mon = t.mon + 12;

               dt.year = t.year - 1;

            }



            else

               dt.year  = t.year;





            dt.mon = t.mon - 6;

            dt.hour = Hour;

            dt.min = 00;

            dt.sec = 0;





            from_date = StructToTime(dt) ;

            fr[i] = from_date;

         }



         if(i == 8) //year

         {

            MqlDateTime dt;

            dt.year = t.year-1;

            dt.day = t.day;

            dt.mon = t.mon;



            dt.hour = Hour;

            dt.min = 00;

            dt.sec = 0;





            from_date = StructToTime(dt) ;

            fr[i] = from_date;

         }







      }



      if(i == 9) //2 years

      {

         from_date = 0 ;

         fr[i] = from_date;

      }







      i = 0;

      for  (int y = 0 ; y <= 9; y++)

      {





         profit[y] = EnumToString(i) + "  " + (string) fr[y] + "  " + DoubleToString( Calc(i, fr[y], perc), 1) + "   " + DoubleToString(perc, 1) + "%";

         ObjectSetString(0, "Profit" + (string) y, OBJPROP_TEXT, profit[y]);

         i++;

      }



      //Print ("***************************************");

   }





//--- return value of prev_calculated for next call

   return(rates_total);



}

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

double sum = 0;





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

//|                                                                  |

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

double Calc(int  y, datetime dt, double & _perc)

{



   bool flag = false;

   bool flag0 = true;

   double prof = 0;//?@>D8B A5@88

   double prof_part  = 0;

   double prof0 = 0; //?@>D8B A45;:8

   double profd = 1.0; //?@>D8B 2 G0ABOE

   _perc = 0;



   double bal = AccountInfoDouble(ACCOUNT_BALANCE);

   datetime dealtime = 0;



   double dep = 0;//?>?>;=58=5 8;8 A=OB85 >1I55

   double dep0 = 0; // ?>?>;=5=85 8;8 A=OB85 >4=>



   HistorySelect(dt, TimeTradeServer());

   ulong    ticket = 0;



   string sym = "";

   for(int i = HistoryDealsTotal() - 1; i >= 0 ; i--)

   {

      ticket = HistoryDealGetTicket(i);

      sym = HistoryDealGetString(ticket, DEAL_SYMBOL);

      





      ENUM_DEAL_TYPE type = (ENUM_DEAL_TYPE) HistoryDealGetInteger(ticket, DEAL_TYPE);



      if(type>1 ) //deposit

      {



         dealtime = (datetime) HistoryDealGetInteger(ticket, DEAL_TIME);

         if (flag)

         {

            flag = false;

//

           // if (prof_part<0) prof_part =0;

            double prof1 = 1 + (prof_part / bal);

            

            profd *= prof1;

           //profd-=1;

           //Print (y, " -->DEP<--   ", dealtime, "   ", s2(prof), "   part  ", s2(prof_part),"  bal  ", s2(bal), " p1   ", s2(prof1), " pd  ", s2(profd));

           prof_part = 0;

           

         }



         dep0 = HistoryDealGetDouble(ticket, DEAL_PROFIT) + HistoryDealGetDouble(ticket, DEAL_SWAP) + HistoryDealGetDouble(ticket, DEAL_COMMISSION);

         bal -= dep0;



         //***************************





      }

      if(type<=1 )  //deal

      {

         flag =  true;

         dealtime = (datetime) HistoryDealGetInteger(ticket, DEAL_TIME);

         prof0 = HistoryDealGetDouble(ticket, DEAL_PROFIT) + HistoryDealGetDouble(ticket, DEAL_SWAP) + HistoryDealGetDouble(ticket, DEAL_COMMISSION);



         prof += prof0;

         prof_part += prof0;

         bal -= prof0;

         //*******************************************



      }

   }









 if (flag) 

 {

   double prof1 = 1 + (prof_part / bal);

   profd *= prof1;

  // Print (y, " -->BAL<--   ", dealtime, "   ", s2(prof), "   ", s2(bal), "   ", s2(profd));



 }

 



   _perc = (profd - 1) * 100;





   return  (prof);



}





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

//|                                                                  |

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

void SetButton1(string name, string text, int y = 40, int x = 140, int vsize = 150, int hsize = 25, color Color = clrRed, int FontSize = 8, bool state = false)

{

   ObjectSetInteger(0, name, OBJPROP_XDISTANCE, x);

   ObjectSetInteger(0, name, OBJPROP_YDISTANCE, y);

   ObjectSetInteger(0, name, OBJPROP_XSIZE, vsize);

   ObjectSetInteger(0, name, OBJPROP_YSIZE, hsize);

   ObjectSetString(0, name, OBJPROP_TEXT, text);

   ObjectSetInteger(0, name, OBJPROP_COLOR, clrWhite);

   if(!state)

      ObjectSetInteger(0, name, OBJPROP_BGCOLOR, clrRed); //F25B 8AE>4=>3> D>=0

   if(state)

      ObjectSetInteger(0, name, OBJPROP_BGCOLOR, clrGreen); //F25B 8AE>4=>3> D>=0   ObjectSetInteger(0,name,OBJPROP_BORDER_COLOR,clrBlack);//@0<:0

   ObjectSetInteger(0, name, OBJPROP_BACK, false); //D>=

   ObjectSetInteger(0, name, OBJPROP_HIDDEN, true); //2 A?8A:5 >1J5:B>2

   ObjectSetInteger(0, name, OBJPROP_STATE, state); // :=>?:0 =060B0/>B60B0

   ObjectSetInteger(0, name, OBJPROP_FONTSIZE, FontSize); //@07<5@ H@8DB0

   ObjectSetInteger(0, name, OBJPROP_CORNER, CORNER_RIGHT_LOWER);

   ObjectSetInteger(0, name, OBJPROP_ZORDER, 9999999999999); // ?@8>@8B5B =060B8O

}



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

//|                                                                  |

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

bool     CreateLabel(const long             chart_ID = 0,             // chart's ID

                     const string           name = "Bl_100",       // label name

                     const int              sub_window = 0,           // subwindow index

                     const int              x = 228,                    // X coordinate

                     const int              y = 20,                    // Y coordinate

                     const int              width = 210,               // width

                     const int              height = 200,              // height

                     const color            back_clr = clrBlack, // background color

                     const ENUM_BORDER_TYPE border = BORDER_RAISED,   // border type

                     const ENUM_BASE_CORNER corner = CORNER_RIGHT_UPPER, // chart corner for anchoring

                     const color            clr = clrBlack,             // flat border color (Flat)

                     const ENUM_LINE_STYLE  style = STYLE_SOLID,      // flat border style

                     const int              line_width = 1,           // flat border width

                     const bool             back = false,             // in the background

                     const bool             selection = false,        // highlight to move

                     const bool             hidden = true,            // hidden in the object list

                     const long             z_order = 0)              // priority for mouse click

{

//--- reset the error value

   ResetLastError();

//--- create a rectangle label

   if(!ObjectCreate(chart_ID, name, OBJ_RECTANGLE_LABEL, sub_window, 0, 0))

   {

      Print(__FUNCTION__,

            ": failed to create a rectangle label! Error code = ", GetLastError());

      return(false);

   }

//--- set label coordinates

   ObjectSetInteger(chart_ID, name, OBJPROP_XDISTANCE, x);

   ObjectSetInteger(chart_ID, name, OBJPROP_YDISTANCE, y);

//--- set label size

   ObjectSetInteger(chart_ID, name, OBJPROP_XSIZE, width);

   ObjectSetInteger(chart_ID, name, OBJPROP_YSIZE, height);

//--- set background color

   ObjectSetInteger(chart_ID, name, OBJPROP_BGCOLOR, clrRed);

//--- set border type

   ObjectSetInteger(chart_ID, name, OBJPROP_BORDER_TYPE, border);

//--- set the chart's corner, relative to which point coordinates are defined

   ObjectSetInteger(chart_ID, name, OBJPROP_CORNER, corner);

//--- set flat border color (in Flat mode)

   ObjectSetInteger(chart_ID, name, OBJPROP_COLOR, clrRed);

//--- set flat border line style

   ObjectSetInteger(chart_ID, name, OBJPROP_STYLE, style);

//--- set flat border width

   ObjectSetInteger(chart_ID, name, OBJPROP_WIDTH, line_width);

//--- display in the foreground (false) or background (true)

   ObjectSetInteger(chart_ID, name, OBJPROP_BACK, back);

//--- enable (true) or disable (false) the mode of moving the label by mouse

   ObjectSetInteger(chart_ID, name, OBJPROP_SELECTABLE, selection);

   ObjectSetInteger(chart_ID, name, OBJPROP_SELECTED, selection);

//--- hide (true) or display (false) graphical object name in the object list

   ObjectSetInteger(chart_ID, name, OBJPROP_HIDDEN, hidden);

//--- set the priority for receiving the event of a mouse click in the chart

   ObjectSetInteger(chart_ID, name, OBJPROP_ZORDER, z_order);

//--- successful execution



   return(true);

}



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

//|                                                                  |

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

void OnChartEvent(const int id, const long & lparam, const double & dparam, const string & sparam)

{



   if(sparam == "Close")

   {

      ObjectsDeleteAll(0, "Pr");

      ObjectDelete(0, "Close");

      ChartIndicatorDelete(0, 0, "AllProfit");





   }

   ChartRedraw();

}

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



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

//|                                                                  |

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

string s2 (double x)

{

   return DoubleToString(x, 2);



}

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





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

//|                                                                  |

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

string s5 (double x)

{

   return DoubleToString(x, 5);



}

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

Comments