Navigation:Home > Content >

i-Profit_(3).mq4

Time: 2015-03-25 | Download file:i-Profit_(3).mq4

//+----------------------------------------------------------------------------+
//|                                                              i-Profit.mq4  |
//|                                                                            |
//|                                                    Ким Игорь В. aka KimIV  |
//|                                                       http://www.kimiv.ru  |
//|                                                                            |
//|  12.05.2008  Indicator for:                                                    |
//|              - current profit                                             |
//|              - profit for today                                          |
//|              - profit for yesterday                                            |
//|              - profit for several past days                         |
//|              - profit for current week                                   |
//|              - profit for current month                                    |
//|              - profit for current quarter                                  |
//|              - profit for current year                                      |
//+----------------------------------------------------------------------------+
#property copyright "Ким Игорь В. aka KimIV"
#property link      "http://www.kimiv.ru"
#property indicator_chart_window

//------- Внешние параметры индикатора ----------------------------------------+
extern int   eiPercent = 5;            // Calculation of the profit procentage depends on:
                                       //  0 - current balance
                                       //  1 - balance on the beginning of the day
                                       //  2 - balance on the beginning of the week
                                       //  3 - balance on the beginning of the month
                                       //  4 - balance on the beginning of the quarter
                                       //  5 - balance on the beginning of the year
extern int   eiOffsetY = 30;           // Versital shifting the text
extern int   eiStepY   = 12;           // Versital shifting step
extern int   eiX1Row   = 3;            // X coordinate first column
extern int   eiX2Row   = 165;          // X coordinate second column
extern int   eiX3Row   = 240;          // X coordinate 3rd column
extern color ecText    = Gray;         // Color of the text
extern color ecProfit  = Green;        // Color of the profit
extern color ecLoss    = FireBrick;    // Color of the loss

//------- Глобальные переменные индикатора ------------------------------------+

//------- Буферы индикатора ---------------------------------------------------+

//------- Поключение внешних модулей ------------------------------------------+

//+----------------------------------------------------------------------------+
//|  Custom indicator initialization function                                  |
//+----------------------------------------------------------------------------+
void init() {
  DeleteObjects();
  Comment("");
}

//+----------------------------------------------------------------------------+
//|  Custom indicator deinitialization function                                |
//+----------------------------------------------------------------------------+
void deinit() {
  DeleteObjects();
  Comment("");
}

//+----------------------------------------------------------------------------+
//|  Custom indicator iteration function                                       |
//+----------------------------------------------------------------------------+
void start() {
  datetime d0, d1, d2, d3, d4, d5, d6, d7, d8;

  d0=StrToTime(TimeToStr(TimeCurrent(), TIME_DATE));
  while (TimeDayOfWeek(d0)<1 || TimeDayOfWeek(d0)>5) d0-=24*60*60;
  d1=d0-24*60*60;
  while (TimeDayOfWeek(d1)<1 || TimeDayOfWeek(d1)>5) d1-=24*60*60;
  d2=d1-24*60*60;
  while (TimeDayOfWeek(d2)<1 || TimeDayOfWeek(d2)>5) d2-=24*60*60;
  d3=d2-24*60*60;
  while (TimeDayOfWeek(d3)<1 || TimeDayOfWeek(d3)>5) d3-=24*60*60;
  d4=d3-24*60*60;
  while (TimeDayOfWeek(d4)<1 || TimeDayOfWeek(d4)>5) d4-=24*60*60;
  d5=DateOfMonday();
  d6=StrToTime(Year()+"."+Month()+".01");
  d7=DateBeginQuarter();
  d8=StrToTime(Year()+".01.01");

  double tb=AccountBalance(), tp=AccountProfit();
  double p0=GetProfitFromDateInCurrency("", -1, -1, d0);
  double p1=GetProfitFromDateInCurrency("", -1, -1, d1)-p0;
  double p2=GetProfitFromDateInCurrency("", -1, -1, d2)-p1-p0;
  double p3=GetProfitFromDateInCurrency("", -1, -1, d3)-p2-p1-p0;
  double p4=GetProfitFromDateInCurrency("", -1, -1, d4)-p3-p2-p1-p0;
  double p5=GetProfitFromDateInCurrency("", -1, -1, d5);
  double p6=GetProfitFromDateInCurrency("", -1, -1, d6);
  double p7=GetProfitFromDateInCurrency("", -1, -1, d7);
  double p8=GetProfitFromDateInCurrency("", -1, -1, d8);

  string st;
  switch (eiPercent) {
    case 0 :         st="current"          ; break;
    case 1 : tb-=p0; st="on the beginning of the day"     ; break;
    case 2 : tb-=p5; st="on the beginning of the week"  ; break;
    case 3 : tb-=p6; st="нon the beginning of month"  ; break;
    case 4 : tb-=p7; st="on the beginning of quarter"; break;
    default: tb-=p8; st="on the beginning of the year"    ; break;
  }
  double tr=tp*100/tb;
  double r0=p0*100/tb;
  double r1=p1*100/tb;
  double r2=p2*100/tb;
  double r3=p3*100/tb;
  double r4=p4*100/tb;
  double r5=p5*100/tb;
  double r6=p6*100/tb;
  double r7=p7*100/tb;
  double r8=p8*100/tb;

  SetLabel("iProfit11", "Current Profit", ecText, eiX1Row, eiOffsetY);
  SetLabel("iProfit21", "Profit for today", ecText, eiX1Row, eiOffsetY+eiStepY);
  SetLabel("iProfit31", "Profit for "+TimeToStr(d1, TIME_DATE), ecText, eiX1Row, eiOffsetY+2*eiStepY);
  SetLabel("iProfit41", "Profit for "+TimeToStr(d2, TIME_DATE), ecText, eiX1Row, eiOffsetY+3*eiStepY);
  SetLabel("iProfit51", "Profit for "+TimeToStr(d3, TIME_DATE), ecText, eiX1Row, eiOffsetY+4*eiStepY);
  SetLabel("iProfit61", "Profit for "+TimeToStr(d4, TIME_DATE), ecText, eiX1Row, eiOffsetY+5*eiStepY);
  SetLabel("iProfit71", "Profit for a week", ecText, eiX1Row, eiOffsetY+6*eiStepY);
  SetLabel("iProfit81", "Profit for month", ecText, eiX1Row, eiOffsetY+7*eiStepY);
  SetLabel("iProfit91", "Profit for quarter", ecText, eiX1Row, eiOffsetY+8*eiStepY);
  SetLabel("iProfit01", "Profit for the year", ecText, eiX1Row, eiOffsetY+9*eiStepY);

  SetLabel("iProfit12", DoubleToStr(tp, 2), ColorOnSign(tp), eiX2Row, eiOffsetY);
  SetLabel("iProfit22", DoubleToStr(p0, 2), ColorOnSign(p0), eiX2Row, eiOffsetY+eiStepY);
  SetLabel("iProfit32", DoubleToStr(p1, 2), ColorOnSign(p1), eiX2Row, eiOffsetY+2*eiStepY);
  SetLabel("iProfit42", DoubleToStr(p2, 2), ColorOnSign(p2), eiX2Row, eiOffsetY+3*eiStepY);
  SetLabel("iProfit52", DoubleToStr(p3, 2), ColorOnSign(p3), eiX2Row, eiOffsetY+4*eiStepY);
  SetLabel("iProfit62", DoubleToStr(p4, 2), ColorOnSign(p4), eiX2Row, eiOffsetY+5*eiStepY);
  SetLabel("iProfit72", DoubleToStr(p5, 2), ColorOnSign(p5), eiX2Row, eiOffsetY+6*eiStepY);
  SetLabel("iProfit82", DoubleToStr(p6, 2), ColorOnSign(p6), eiX2Row, eiOffsetY+7*eiStepY);
  SetLabel("iProfit92", DoubleToStr(p7, 2), ColorOnSign(p7), eiX2Row, eiOffsetY+8*eiStepY);
  SetLabel("iProfit02", DoubleToStr(p8, 2), ColorOnSign(p8), eiX2Row, eiOffsetY+9*eiStepY);

  SetLabel("iProfit13", DoubleToStr(tr, 2)+" %", ColorOnSign(tr), eiX3Row, eiOffsetY);
  SetLabel("iProfit23", DoubleToStr(r0, 2)+" %", ColorOnSign(r0), eiX3Row, eiOffsetY+eiStepY);
  SetLabel("iProfit33", DoubleToStr(r1, 2)+" %", ColorOnSign(r1), eiX3Row, eiOffsetY+2*eiStepY);
  SetLabel("iProfit43", DoubleToStr(r2, 2)+" %", ColorOnSign(r2), eiX3Row, eiOffsetY+3*eiStepY);
  SetLabel("iProfit53", DoubleToStr(r3, 2)+" %", ColorOnSign(r3), eiX3Row, eiOffsetY+4*eiStepY);
  SetLabel("iProfit63", DoubleToStr(r4, 2)+" %", ColorOnSign(r4), eiX3Row, eiOffsetY+5*eiStepY);
  SetLabel("iProfit73", DoubleToStr(r5, 2)+" %", ColorOnSign(r5), eiX3Row, eiOffsetY+6*eiStepY);
  SetLabel("iProfit83", DoubleToStr(r6, 2)+" %", ColorOnSign(r6), eiX3Row, eiOffsetY+7*eiStepY);
  SetLabel("iProfit93", DoubleToStr(r7, 2)+" %", ColorOnSign(r7), eiX3Row, eiOffsetY+8*eiStepY);
  SetLabel("iProfit03", DoubleToStr(r8, 2)+" %", ColorOnSign(r8), eiX3Row, eiOffsetY+9*eiStepY);

  Comment("Profit procentage depends on balance " +st);
}

//+----------------------------------------------------------------------------+
//|  Возвращает цвет по знаку числа                                            |
//+----------------------------------------------------------------------------+
color ColorOnSign(double nu) {
  color lcColor=ecText;

  if (nu>0) lcColor=ecProfit;
  if (nu<0) lcColor=ecLoss;

  return(lcColor);
}

//+----------------------------------------------------------------------------+
//|  Автор    : Ким Игорь В. aka KimIV,  http://www.kimiv.ru                   |
//+----------------------------------------------------------------------------+
//|  Версия   : 12.05.2008                                                     |
//|  Описание : Возвращает дату начала квартала                                |
//+----------------------------------------------------------------------------+
//|  Parameters:                                                                |
//|                                    (-2 - before last)                       |
//|                                    (-1 - previous)                          |
//|    no - number of quarter          ( 0 - current)                           |
//|                                    ( 1 - next)                              |
//|                                    ( 2 - following)                         |
//+----------------------------------------------------------------------------+
datetime DateBeginQuarter(int nk=0) {
  int ye=Year()-MathFloor(nk/4);
  nk=MathMod(nk, 4);
  int mo=Month()-MathMod(Month()+2, 3)+3*nk;
  if (mo<1) {
    mo+=12;
    ye--;
  }
  if (mo>12) {
    mo-=12;
    ye++;
  }

  return(StrToTime(ye+"."+mo+".01"));
}

//+----------------------------------------------------------------------------+
//|  Автор    : Ким Игорь В. aka KimIV,  http://www.kimiv.ru                   |
//+----------------------------------------------------------------------------+
//|  Версия   : 12.05.2008                                                     |
//|  Описание : Возвращает дату понедельника                                   |
//+----------------------------------------------------------------------------+
//|  Parameters:                                                                |
//|                                    (-2 - before last week)                 |
//|                                    (-1 - past weel)                        |
//|    no - numer of Monday            ( 0 - current week)                     |
//|                                    ( 1 - next)                             |
//|                                    ( 2 - following)                        |
//+----------------------------------------------------------------------------+
datetime DateOfMonday(int no=0) {
  datetime dt=StrToTime(TimeToStr(TimeCurrent(), TIME_DATE));

  while (TimeDayOfWeek(dt)!=1) dt-=24*60*60;
  dt+=no*7*24*60*60;

  return(dt);
}

//+----------------------------------------------------------------------------+
//|  Удаление объектов.                                                        |
//+----------------------------------------------------------------------------+
void DeleteObjects() {
  string st="iProfit";
  int    i, j;

  for (i=0; i<10; i++) {
    for (j=1; j<4; j++) ObjectDelete(st+i+j);
  }
}

//+----------------------------------------------------------------------------+
//|  Автор    : Ким Игорь В. aka KimIV,  http://www.kimiv.ru                   |
//+----------------------------------------------------------------------------+
//|  Версия   : 19.02.2008                                                     |
//|  Описание : Возвращает суммарный профит в валюте депозита                  |
//|             закрытых с определённой даты позиций                           |
//+----------------------------------------------------------------------------+
//|  Parameters:                                                               |
//|    sy - numer of instrument          (""   - any symbol,                   |
//|                                      NULL - current symbol)                |
//|    op - operation                    (-1   - any position)                 |
//|    mn - MagicNumber                  (-1   -  any magic)                   |
//|    dt - Data time in the seconds since 1970 ( 0 - beginning of history)    |
//+----------------------------------------------------------------------------+
double GetProfitFromDateInCurrency(string sy="", int op=-1, int mn=-1, datetime dt=0)
{
  double p=0;
  int    i, k=OrdersHistoryTotal();

  if (sy=="0") sy=Symbol();
  for (i=0; i        

Recommend