//+------------------------------------------------------------------+
//|                                                       TMA+CG.mq4 |
//|                                                           mladen |
//| arrowse coded acording to idea presented by rajiv                |
//+------------------------------------------------------------------+
#property copyright "rajivxxx"
#property link      "rajivxxx@gmail.com"

#property indicator_chart_window
#property indicator_buffers 5
#property indicator_color1  clrGreenYellow
#property indicator_color2  clrWhite
#property indicator_color3  clrWhite
#property indicator_color4  clrWhite
#property indicator_color5  clrYellow
#property indicator_width4 1
#property indicator_width5 1
#property indicator_style1  STYLE_DOT

//
#import "rsfExpander.dll"
   bool LoadMqlProgramA(int hChart, int programType, string programName);
#import

// MQL program types
#define PROGRAMTYPE_INDICATOR   1
#define PROGRAMTYPE_EXPERT      2
#define PROGRAMTYPE_SCRIPT      4
string name = "subfolder\\PeriodConverter";
//extern string path="C:\\Users\\khai_\\ICM\\MQL4\\Scripts\\";

//
//
//
//
//

extern string td_TimeFrame       = "current time frame";
extern int    td_HalfLength      = 7;
extern int    td_Price           = PRICE_WEIGHTED;
extern double td_BandsDeviations = 2.70;
extern bool td_tradeOn=true;
extern double td_Lot = 0.01;
extern int    td_HalfLength1      = 21;
extern double td_BandsDeviations1 = 2.7;

extern bool   td_Interpolate     = true;
extern bool   td_alertsOn        = false;
extern bool   td_alertsOnCurrent = false;
extern bool   td_alertsOnHighLow = false;
extern bool   td_alertsMessage   = false;
extern bool   td_alertsSound     = false;
extern bool   td_alertsEmail     = false;
extern bool   td_Chart_to_front  = false;

//
//
//
//
//

double td_tmBuffer[],td_tmBuffer1[];
double td_upBuffer[],td_upBuffer1[];
double td_dnBuffer[],td_dnBuffer1[];
double td_wuBuffer[];
double td_wdBuffer[];
double td_upArrow[];
double td_dnArrow[];

//
//
//
//
//

string IndicatorFileName;
bool   calculatingTma = false;
bool   returningBars  = false;
int    timeFrame;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//

int init()
{
   timeFrame  = stringToTimeFrame(td_TimeFrame);
   td_HalfLength = MathMax(td_HalfLength,1);
   td_HalfLength1 = MathMax(td_HalfLength1,1);
   IndicatorBuffers(7);
         SetIndexBuffer(0,td_tmBuffer);  SetIndexDrawBegin(0,td_HalfLength);
         SetIndexBuffer(1,td_upBuffer);  SetIndexDrawBegin(1,td_HalfLength);
         SetIndexBuffer(2,td_dnBuffer);  SetIndexDrawBegin(2,td_HalfLength);
         SetIndexBuffer(3,td_dnArrow);   SetIndexStyle(3,DRAW_ARROW); SetIndexArrow(5,200);
         SetIndexBuffer(4,td_upArrow);   SetIndexStyle(4,DRAW_ARROW); SetIndexArrow(6,202);
         SetIndexBuffer(5,td_wuBuffer);
         SetIndexBuffer(6,td_wdBuffer);
         SetIndexBuffer(7,td_upBuffer1);  SetIndexDrawBegin(7,td_HalfLength1);
         SetIndexBuffer(8,td_dnBuffer1);  SetIndexDrawBegin(8,td_HalfLength1);
         

         if (td_TimeFrame=="calculateTma")  { calculatingTma=true; return(0); }
         if (td_TimeFrame=="returnBars")    { returningBars=true;  return(0); }

   
   IndicatorFileName = WindowExpertName();
   return(0);
}
int deinit() { return(0); }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int start()
{
   int counted_bars=IndicatorCounted();
   int i,limit,limit1;

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
           limit=MathMin(Bars-1,Bars-counted_bars+td_HalfLength);
           limit1=MathMin(Bars-1,Bars-counted_bars+td_HalfLength1);
           if (returningBars)  { td_tmBuffer[0] = limit=limit1; return(0); }
           if (calculatingTma) { calculateTma(limit); return(0); }
           if (calculatingTma) { calculateTma(limit1); return(0); }
           if (timeFrame > Period()) limit = MathMax(limit,MathMin(Bars-1,iCustom(NULL,timeFrame,IndicatorFileName,"returnBars",0,0)*timeFrame/Period()));

   //
   //
   //
   //
   //
   
 	for(i = limit; i >= 0; i--)
   {
      int      shift1 = iBarShift(NULL,timeFrame,Time[i]);
      datetime time1  = iTime    (NULL,timeFrame,shift1);

      //
      //
      //
      //
      //
      
         td_tmBuffer[i] = iCustom(NULL,timeFrame,IndicatorFileName,"calculateTma",td_HalfLength,td_Price,td_BandsDeviations,0,shift1);
         td_upBuffer[i] = iCustom(NULL,timeFrame,IndicatorFileName,"calculateTma",td_HalfLength,td_Price,td_BandsDeviations,1,shift1);
         td_dnBuffer[i] = iCustom(NULL,timeFrame,IndicatorFileName,"calculateTma",td_HalfLength,td_Price,td_BandsDeviations,2,shift1);
         
         td_tmBuffer1[i] = iCustom(NULL,timeFrame,IndicatorFileName,"calculateTma",td_HalfLength1,td_Price,td_BandsDeviations1,3,shift1);
         td_upBuffer1[i] = iCustom(NULL,timeFrame,IndicatorFileName,"calculateTma",td_HalfLength1,td_Price,td_BandsDeviations1,4,shift1);
         td_dnBuffer1[i] = iCustom(NULL,timeFrame,IndicatorFileName,"calculateTma",td_HalfLength1,td_Price,td_BandsDeviations1,5,shift1);

         td_upArrow[i] = EMPTY_VALUE;
         td_dnArrow[i] = EMPTY_VALUE;            
            if ( (High[i+1]>td_upBuffer[i+1] && High[i+1]>td_upBuffer1[i+1]) && Close[i+1]>Open[i+1] && Close[i]<Open[i]) td_upArrow[i] = High[i]+20*Point;
            if ( (Low[i+1]<td_dnBuffer[i+1] && Low[i+1]<td_dnBuffer1[i+1] ) && Close[i+1]<Open[i+1] && Close[i]>Open[i]) td_dnArrow[i] = Low[i]-20*Point;

         if (timeFrame <= Period() || shift1==iBarShift(NULL,timeFrame,Time[i-1])) continue;
         if (!td_Interpolate) continue;

      //
      //
      //
      //
      //

         for(int n = 1; i+n < Bars && Time[i+n] >= time1; n++) continue;
         double factor = 1.0 / n;
         for(int k = 1; k < n; k++)
            {
               td_tmBuffer[i+k] = k*factor*td_tmBuffer[i+n] + (1.0-k*factor)*td_tmBuffer[i];
               td_upBuffer[i+k] = k*factor*td_upBuffer[i+n] + (1.0-k*factor)*td_upBuffer[i];
               td_dnBuffer[i+k] = k*factor*td_dnBuffer[i+n] + (1.0-k*factor)*td_dnBuffer[i];
               
               td_tmBuffer1[i+k] = k*factor*td_tmBuffer1[i+n] + (1.0-k*factor)*td_tmBuffer1[i];
               td_upBuffer1[i+k] = k*factor*td_upBuffer1[i+n] + (1.0-k*factor)*td_upBuffer1[i];
               td_dnBuffer1[i+k] = k*factor*td_dnBuffer1[i+n] + (1.0-k*factor)*td_dnBuffer1[i];
               
               
            }               
   }

   //
   //
   //
   //
   //
   
   if (td_alertsOn)
   {
      if (td_alertsOnCurrent)
            int forBar = 0;
      else      forBar = 1;
      if (td_alertsOnHighLow)       
      {
         if (High[forBar] > td_upBuffer[forBar] && High[forBar+1] < td_upBuffer[forBar+1]) doAlert("high penetrated upper bar");
         if (Low[forBar]  < td_dnBuffer[forBar] && Low[forBar+1]  > td_dnBuffer[forBar+1]) doAlert("low penetrated lower bar");
      }
      else
      {
         if (Close[forBar] > td_upBuffer[forBar] && Close[forBar+1] < td_upBuffer[forBar+1]) doAlert("close penetrated upper bar");
         if (Close[forBar] < td_dnBuffer[forBar] && Close[forBar+1] > td_dnBuffer[forBar+1]) doAlert("close penetrated lower bar");
      }
   }
    
   if (td_tradeOn)
   {
         int hChart = WindowHandle(Symbol(), NULL);
      
         if (td_upArrow[i] != EMPTY_VALUE && Close[forBar] < td_upBuffer[forBar] && Close[forBar+1] < td_upBuffer[forBar+1] && Close[forBar] < td_upBuffer1[forBar] && Close[forBar+1] < td_upBuffer1[forBar+1]) 
         {doAlert(Symbol()+" close comes back from above, SELL");
         //OrderSend(Symbol(),OP_SELL, td_Lot, MarketInfo(Symbol(),MODE_BID), 2, NULL, NULL, NULL, 0, 0, CLR_NONE);
   
   bool result = LoadMqlProgramW(hChart, PROGRAMTYPE_SCRIPT,name); // "TR@DE_SELL_Symbol_FROM_script" "PeriodConverter"
   td_upArrow[i] == EMPTY_VALUE;
         }
         if (td_dnArrow[i] != EMPTY_VALUE && Close[forBar] > td_dnBuffer[forBar] && Close[forBar+1] > td_dnBuffer[forBar+1] && Close[forBar] > td_dnBuffer1[forBar] && Close[forBar+1] > td_dnBuffer1[forBar+1]) 
         {
         doAlert(Symbol()+" close comes back from below, BUY");
         //OrderSend(Symbol(),OP_BUY, td_Lot, MarketInfo(Symbol(),MODE_ASK), 2, NULL, NULL, NULL, 0, 0, CLR_NONE);
         
   result = LoadMqlProgramW(hChart, PROGRAMTYPE_SCRIPT,name);  //"TR@DE_BUY_Symbol_FROM_script"
   
         }
          if(result)Print("file retrieved successfully");
             else Print("file retrieval not successful  - " + string(GetLastError()));
   } 
   

   return(0);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

void calculateTma(int limit)
{
   int i,j,k;
   double FullLength = 2.0*td_HalfLength+1.0;
   
   //
   //
   //
   //
   //
   
   for (i=limit; i>=0; i--)
   {
      double sum  = (td_HalfLength+1)*iMA(NULL,0,1,0,MODE_SMA,td_Price,i);
      double sumw = (td_HalfLength+1);
      for(j=1, k=td_HalfLength; j<=td_HalfLength; j++, k--)
      {
         sum  += k*iMA(NULL,0,1,0,MODE_SMA,td_Price,i+j);
         sumw += k;

         if (j<=i)
         {
            sum  += k*iMA(NULL,0,1,0,MODE_SMA,td_Price,i-j);
            sumw += k;
         }
      }
      td_tmBuffer[i] = sum/sumw;

      //
      //
      //
      //
      //
            
         double diff = iMA(NULL,0,1,0,MODE_SMA,td_Price,i)-td_tmBuffer[i];
         if (i> (Bars-td_HalfLength-1)) continue;
         if (i==(Bars-td_HalfLength-1))
         {
            td_upBuffer[i] = td_tmBuffer[i];
            td_dnBuffer[i] = td_tmBuffer[i];
            if (diff>=0)
               {
                  td_wuBuffer[i] = MathPow(diff,2);
                  td_wdBuffer[i] = 0;
               }
            else
               {               
                  td_wdBuffer[i] = MathPow(diff,2);
                  td_wuBuffer[i] = 0;
               }                  
            continue;
         }
      
         //
         //
         //
         //
         //
         
         if(diff>=0)
            {
               td_wuBuffer[i] = (td_wuBuffer[i+1]*(FullLength-1)+MathPow(diff,2))/FullLength;
               td_wdBuffer[i] =  td_wdBuffer[i+1]*(FullLength-1)/FullLength;
            }
         else
            {
               td_wdBuffer[i] = (td_wdBuffer[i+1]*(FullLength-1)+MathPow(diff,2))/FullLength;
               td_wuBuffer[i] =  td_wuBuffer[i+1]*(FullLength-1)/FullLength;
            }
         td_upBuffer[i] = td_tmBuffer[i] + td_BandsDeviations*MathSqrt(td_wuBuffer[i]);
         td_dnBuffer[i] = td_tmBuffer[i] - td_BandsDeviations*MathSqrt(td_wdBuffer[i]);
   }
}
    


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

void doAlert(string doWhat)
{
   static string   previousAlert="";
   static datetime previousTime;
   string message;

   //
   //
   //
   //
   //
   
   if (previousAlert!=doWhat || previousTime!=Time[0]) 
   {
      previousAlert = doWhat;
      previousTime  = Time[0];

      message= StringConcatenate(Symbol()," ",Period(),"M ",TimeToStr(TimeLocal(),TIME_SECONDS),": ",doWhat);
         if (td_alertsMessage) Alert(message);
         if (td_Chart_to_front) ChartSetInteger(0,CHART_BRING_TO_TOP,0,true);
         if (td_alertsEmail)   SendMail(StringConcatenate(Symbol(),"TMA "),message);
         if (td_alertsSound)   PlaySound("alert2.wav");
    }
}

//
//
//
//
//

int stringToTimeFrame(string tfs)
{
   for(int l = StringLen(tfs)-1; l >= 0; l--)
   {
      int tchar = StringGetChar(tfs,l);
          if((tchar > 96 && tchar < 123) || (tchar > 223 && tchar < 256))
               tfs = StringSetChar(tfs, 1, tchar - 32);
          else 
              if(tchar > -33 && tchar < 0)
                  tfs = StringSetChar(tfs, 1, tchar + 224);
   }
   int tf=0;
         if (tfs=="M1" || tfs=="1")     tf=PERIOD_M1;
         if (tfs=="M5" || tfs=="5")     tf=PERIOD_M5;
         if (tfs=="M15"|| tfs=="15")    tf=PERIOD_M15;
         if (tfs=="M30"|| tfs=="30")    tf=PERIOD_M30;
         if (tfs=="H1" || tfs=="60")    tf=PERIOD_H1;
         if (tfs=="H4" || tfs=="240")   tf=PERIOD_H4;
         if (tfs=="D1" || tfs=="1440")  tf=PERIOD_D1;
         if (tfs=="W1" || tfs=="10080") tf=PERIOD_W1;
         if (tfs=="MN" || tfs=="43200") tf=PERIOD_MN1;
         if (tf==0 || tf<Period())      tf=Period();
   return(tf);
} 