//------------------------------------------------------------------
//
//------------------------------------------------------------------
#property copyright "mladen"
#property link      "mladenfx@gmail.com"
#property indicator_chart_window

//
//
//
//
//

extern string AtrTimeFrame   = "Current time frame";
extern int    AtrPeriod      = 20;
extern double AtrMultiplier  = 1.0;
extern double PercentToRisk  = 1.0;
extern color  TextColor      = Black;
extern int    TextSize       = 12;
extern int    TextCorner     = 3;

int timeFrame;
double atr[];
double prices[];
string indicatorFileName;
bool   calculateValue;

//------------------------------------------------------------------
//
//------------------------------------------------------------------
// 
//
//
//
//

int init()
{
   indicatorFileName = WindowExpertName();      
   calculateValue    = (AtrTimeFrame=="calculateValue");
      if (calculateValue)
      {
         IndicatorBuffers(2); 
            SetIndexBuffer(0,atr);
            SetIndexBuffer(1,prices); 
            return(0);
      }
   timeFrame = stringToTimeFrame(AtrTimeFrame);
   return(0);
}
int deinit() { ObjectDelete("comment0"); ObjectDelete("comment1"); return(0); }

//
//
//
//
//

int start()
{
   if (calculateValue)
   {
      int counted_bars=IndicatorCounted();
         if(counted_bars < 0) return(-1);
         if(counted_bars>0) counted_bars--;
            int limit = MathMin(Bars-counted_bars,Bars-1);

      //
      //
      //
      //
      //
    
      for(int i = limit; i >= 0; i--)
      {
         prices[i] = iMA(NULL,0,1,0,MODE_SMA,PRICE_CLOSE,i);
            double minPeriod = AtrPeriod/2.0;
            double maxPeriod = minPeriod*5.0;
            double signal    = MathAbs(prices[i]-prices[i+AtrPeriod]);
            double noise     = 0.00000000001;

               //
               //
               //
               //
               //
               
                     for(int k=0; k<AtrPeriod; k++) noise += MathAbs(prices[i+k]-prices[i+k+1]);
               
               //
               //
               //
               //
               //

            double averagePeriod = ((signal/noise)*(maxPeriod-minPeriod))+minPeriod;
            double alpha         = 2.0 / (1.0+averagePeriod);
            double tatr          = iATR(NULL,0,averagePeriod,i);

            //
            //
            //
            //
            //
         
            atr[i] = atr[i+1]+alpha*(tatr-atr[i+1]);
      }
      return(0);
   }
         
   //
   //
   //
   //
   //
      
   double pipMultiplier = 1; if (Digits==3 || Digits==5) pipMultiplier = 10;
   
   //
   //
   //
   //
   //
      
   int LotDigit = 0;
      double watr      = AtrMultiplier*iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",AtrPeriod,0,0)/(Point*pipMultiplier);
      double TickValue = MarketInfo(Symbol(),MODE_TICKVALUE);
      double Equity    = AccountEquity();
      double MinLots   = NormalizeDouble(MarketInfo(Symbol(),MODE_MINLOT) ,2);
      double MaxLots   = NormalizeDouble(MarketInfo(Symbol(),MODE_MAXLOT) ,2);
      double LotStep   = NormalizeDouble(MarketInfo(Symbol(),MODE_LOTSTEP),2);
            if (LotStep==1)    LotDigit=0;
            if (LotStep==0.1)  LotDigit=1;
            if (LotStep==0.01) LotDigit=2;
      double Lots = NormalizeDouble(((Equity*PercentToRisk)/100.0)/(watr*TickValue*pipMultiplier),LotDigit);
      
      //
      //
      //
      
      SetComment(0,15,"Risk : "+DoubleToStr(PercentToRisk,2)+"%, atr stop loss : "+DoubleToStr(watr,1)+" pips, lot size : "+DoubleToStr(Lots,LotDigit));
      if (Lots<MinLots) SetComment(1,30,"Lot size : "+DoubleToStr(Lots,LotDigit)+" smaller than minimum allowed lot size : "+DoubleToStr(MinLots,LotDigit));
      if (Lots>MaxLots) SetComment(1,30,"Lot size : "+DoubleToStr(Lots,LotDigit)+" greater than maximum allowed lot size : "+DoubleToStr(MaxLots,LotDigit));
   return(0);
}

//------------------------------------------------------------------
//                                                                  
//------------------------------------------------------------------
//
//
//
//
//

void SetComment(string add, int yPos, string value)
{
   string name = "comment"+add;
      ObjectCreate(name,OBJ_LABEL,0,0,0);
         ObjectSet(name,OBJPROP_CORNER,TextCorner);
         ObjectSet(name,OBJPROP_COLOR,TextColor);
         ObjectSet(name,OBJPROP_XDISTANCE,15);
         ObjectSet(name,OBJPROP_YDISTANCE,yPos);
         ObjectSetText(name,value,TextSize,"Arial bold");
         
}

//-------------------------------------------------------------------
//
//-------------------------------------------------------------------
//
//
//
//
//

string sTfTable[] = {"M1","M5","M15","M30","H1","H4","D1","W1","MN"};
int    iTfTable[] = {1,5,15,30,60,240,1440,10080,43200};

//
//
//
//
//

int stringToTimeFrame(string tfs)
{
   StringToUpper(tfs);
   for (int i=ArraySize(iTfTable)-1; i>=0; i--)
         if (tfs==sTfTable[i] || tfs==""+iTfTable[i]) return(MathMax(iTfTable[i],Period()));
                                                      return(Period());
}