//+------------------------------------------------------------------+
//|                                                Donchian.mq4 |
//|                                                   Andrei Andreev |
//|                                             http://www.andand.ru |
//+------------------------------------------------------------------+

// BASED ON:
//+------------------------------------------------------------------+
//|                                                   ^X_Sensors.mq4 |
//|                                                    Version 2.0.1 |
//|------------------------------------------------------------------|
//|                     Copyright © 2007, Mr.WT, Senior Linux Hacker |
//|                                     http://w-tiger.narod.ru/wk2/ |
//+------------------------------------------------------------------+
#property copyright "Andrei Andreev"
#property link      "http://www.andand.ru"

#property indicator_chart_window

extern int ForDays=10;


int _N_Time,ObjectId;
string OBJECT_PREFIX="LEVELS";
int _My_Period=PERIOD_D1;
//-------------------------------------------------------------------------------------------
int init() 
  {
   _N_Time=0;
   return(0);
  }
//-------------------------------------------------------------------------------------------
int deinit() 
  {
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start() 
  {

//if ( _N_Time == Time[0] ) return(0);
   int counted_bars=IndicatorCounted();
   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
   int limit=Bars-counted_bars;
   if(counted_bars==0) limit-=1+ForDays+1;
   
   double _rates[][6];

   double open,close,high,low;
   double AverageRange;

   ArrayCopyRates(_rates,NULL,_My_Period);
   int err= GetLastError();
   
   if(err == 4066)
     {
      Sleep(1000);
      if(iClose(NULL,_My_Period,0)!=Close[0])
        {
         Sleep(1000);
         return(0);
        }
     }
   close= _rates[1][4];
   high = _rates[0][3];
   low=_rates[0][2];
   open=_rates[0][1];

   AverageRange=0;
   for(int i=1;i<ForDays+1;i++)
     {
      AverageRange=AverageRange+(_rates[i][3]-_rates[i][2])/Point;
     }
   AverageRange=NormalizeDouble(AverageRange/ForDays,0);

   double Risk=0.05;
   double N=NormalizeDouble(iATR(Symbol(),0,96,1)/Point,0);
   double pips=MarketInfo(Symbol(),MODE_TICKVALUE);
   double NUS=NormalizeDouble(N*pips,0);

   Comment("\nПрибыльных сделок  : ",TotalProfitableDeals()," на ",ProfitableDealsAmount()," тугриков",
           "\nУбыточных сделок    : ",TotalLoosingDeals()," на ",-LoosingDealsAmount()," тугриков",
           "\nПрибыль по стопам  : ",NormalizeDouble(TotalProfitByStops(),0)," тугриков",
           "\nОтложенных сделок : ",CalculatePendingTotal(),
           "\n",
           "\nДневной диапазон:",
           "\nСегодня   :  ",(high-low)/Point," пипсов",
           "\nВчера       : ",(_rates[1][3]-_rates[1][2])/Point," пипсов",
           "\nСредний   :  ",AverageRange," пипсов"," за ",ForDays," дней",
           "\nОдин пипс : ",pips," тугриков"
           );



   _N_Time=Time[0];
//---- End Of Program
   return(0);
  }
//+------------------------------------------------------------------+
double TotalProfitByStops()
  {
   int cnt,ticket,total;
   double Profit=0;

   total=OrdersTotal();
   for(cnt=0;cnt<total;cnt++)
     {
      OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
      if(OrderStopLoss()!=0)
        {
         if(OrderType()==OP_BUY)  Profit=Profit+(OrderStopLoss()-OrderOpenPrice())/MarketInfo(OrderSymbol(),MODE_POINT)*OrderLots()*MarketInfo(OrderSymbol(),MODE_TICKVALUE)+OrderSwap();
         if(OrderType()==OP_SELL) Profit=Profit+(OrderOpenPrice()-OrderStopLoss())/MarketInfo(OrderSymbol(),MODE_POINT)*OrderLots()*MarketInfo(OrderSymbol(),MODE_TICKVALUE)+OrderSwap();
        }
     }
   return(Profit);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double TotalProfitableDeals()
  {
   int cnt,ticket,total;
   double ProfitableDeals=0;

   total=OrdersTotal();
   for(cnt=0;cnt<total;cnt++)
     {
      OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
      if(OrderProfit()>0)
        {
         ProfitableDeals=ProfitableDeals+1;
        }
     }
   return(ProfitableDeals);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double ProfitableDealsAmount()
  {
   int cnt,ticket,total;
   double Amount=0;

   total=OrdersTotal();
   for(cnt=0;cnt<total;cnt++)
     {
      OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
      if(OrderProfit()>0)
        {
         Amount=Amount+(OrderProfit()+OrderSwap());
        }
     }
   return(Amount);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double LoosingDealsAmount()
  {
   int cnt,ticket,total;
   double Amount=0;

   total=OrdersTotal();
   for(cnt=0;cnt<total;cnt++)
     {
      OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
      if(OrderProfit()<0)
        {
         Amount=Amount+(OrderProfit()+OrderSwap());
        }
     }
   return(Amount);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double TotalLoosingDeals()
  {
   int cnt,ticket,total;
   double LoosingDeals=0;

   total=OrdersTotal();
   for(cnt=0;cnt<total;cnt++)
     {
      OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
      if(OrderProfit()<0)
        {
         LoosingDeals=LoosingDeals+1;
        }
     }
   return(LoosingDeals);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int CalculatePendingTotal()
  {
   int cmd;
   int orders=0;
//----
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
      cmd=OrderType();
      if(cmd!=OP_BUY && cmd!=OP_SELL) orders++;

     }
//---- return orders volume
   return(orders);

  }

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