How can I get Trade signal from this indicator ? (iCustom function)

To add comments, please log in or register
pazival
7
pazival  

I'm try to get Trade signal from TMA+CG indicator (mql4) as Attach file  . i'm try to create EA from This indicator with iCustom function but it doesn't work!

This is  indicator code:

//+------------------------------------------------------------------+
//|                                                       TMA+CG.mq4 |
//|                                                                  |
//|                                                                  |
//+------------------------------------------------------------------+
 
#property indicator_chart_window
#property indicator_buffers 5
#property indicator_color1  Gold
#property indicator_color2  DarkGray
#property indicator_color3  DarkGray
#property indicator_color4  Snow
#property indicator_color5  RoyalBlue
#property indicator_style1  STYLE_DOT

//
//
 
extern string TimeFrame       = "current time frame";
extern int    HalfLength      = 55;
extern int    Price           = PRICE_WEIGHTED;
extern double BandsDeviations = 2.8;
extern bool   Interpolate     = true;
extern bool   alertsOn        = true;
extern bool   alertsOnCurrent = false;
extern bool   alertsOnHighLow = true;
extern bool   alertsMessage   = true;
extern bool   alertsSound     = false;
extern bool   alertsEmail     = false;
bool   DrawCrosss  = true;
color  UpCrossColor = clrSnow;
int    UpCrossSize = 1;
color  DnCrossColor = clrRoyalBlue;
int    DnCrossSize = 1;
double CrossDisplacement = 0;

//
//
 
double tmBuffer[];
double upBuffer[];
double dnBuffer[];
double wuBuffer[];
double wdBuffer[];
double upArrow[];
double dnArrow[];

//
//
 
string IndicatorFileName;
bool   calculatingTma = false;
bool   returningBars  = false;
int    timeFrame;
string lbl = "TmaCG.";
int q=1;
int w=2;
int e=3; 
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
 
int init()
{
   timeFrame  = Period();
   //Print(" timeframe = ", timeFrame);
   HalfLength = MathMax(HalfLength,1);
   IndicatorBuffers(7);
         SetIndexBuffer(0,tmBuffer);  SetIndexDrawBegin(0,HalfLength);
         SetIndexBuffer(1,upBuffer);  SetIndexDrawBegin(1,HalfLength);
         SetIndexBuffer(2,dnBuffer);  SetIndexDrawBegin(2,HalfLength);
         SetIndexBuffer(3,dnArrow);   SetIndexStyle(3,DRAW_ARROW); SetIndexArrow(5,242);
         SetIndexBuffer(4,upArrow);   SetIndexStyle(4,DRAW_ARROW); SetIndexArrow(6,241);
         SetIndexBuffer(5,wuBuffer);
         SetIndexBuffer(6,wdBuffer);
 
        if (TimeFrame=="calculateTma")  { calculatingTma=true; return(0); }
        if (TimeFrame=="returnBars")    { returningBars=true;  return(0); }
 
   IndicatorFileName = WindowExpertName();
   return(0);
}
//+------------------------------------------------------------------+
int deinit() { ObjectsDeleteAll(0, lbl); return(0); } 
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

int start()
{

   int counted_bars=IndicatorCounted();
    
   int i,limit;
 
   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
           limit=MathMin(Bars-1,Bars-counted_bars+HalfLength);
           if (returningBars)  { tmBuffer[0] = limit; return(0); }
           if (calculatingTma) { calculateTma(limit); return(0); }
           if (timeFrame > Period()) limit = MathMax(limit,MathMin(Bars-1,iCustom(Symbol(),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);
 
      tmBuffer[i] = iCustom(NULL,timeFrame,IndicatorFileName,"calculateTma",HalfLength,Price,BandsDeviations,0,shift1);
      upBuffer[i] = iCustom(NULL,timeFrame,IndicatorFileName,"calculateTma",HalfLength,Price,BandsDeviations,1,shift1);
      dnBuffer[i] = iCustom(NULL,timeFrame,IndicatorFileName,"calculateTma",HalfLength,Price,BandsDeviations,2,shift1);

      upArrow[i] = EMPTY_VALUE;
      dnArrow[i] = EMPTY_VALUE;            
 
      if (DrawCrosss)
      {
        double shift = iATR(NULL, 0, 10, i)*CrossDisplacement;
        if (High[i+1]>upBuffer[i+1] && Close[i+1]>Open[i+1] && Close[i]<Open[i]) DrawArrow(lbl+IntegerToString(Time[i])+".DN", 251, Time[i], High[i]+shift, DnCrossColor, DnCrossSize, ANCHOR_BOTTOM);
        else if (ObjectFind(lbl+IntegerToString(Time[i])+".DN")!=-1) ObjectDelete(lbl+IntegerToString(Time[i])+".DN");
        if ( Low[i+1]<dnBuffer[i+1] && Close[i+1]<Open[i+1] && Close[i]>Open[i]) DrawArrow(lbl+IntegerToString(Time[i])+".UP", 251, Time[i], Low[i]-shift, UpCrossColor, UpCrossSize, ANCHOR_TOP);
        else if (ObjectFind(lbl+IntegerToString(Time[i])+".UP")!=-1) ObjectDelete(lbl+IntegerToString(Time[i])+".UP");
      }

      if (timeFrame <= Period() || shift1==iBarShift(NULL,timeFrame,Time[i-1])) continue;
      if (!Interpolate) continue;
 
   }
 
   if (alertsOn)
   {
      if (alertsOnCurrent)
            int forBar = 0;
      else      forBar = 1;
      if (alertsOnHighLow)       
      {
         if (High[forBar] > upBuffer[forBar] && High[forBar+1] < upBuffer[forBar+1]) doAlert("high penetrated upper bar");
         if (Low[forBar]  < dnBuffer[forBar] && Low[forBar+1]  > dnBuffer[forBar+1]) doAlert("low penetrated lower bar");
      }
      else
      {
         if (Close[forBar] > upBuffer[forBar] && Close[forBar+1] < upBuffer[forBar+1]) doAlert("close penetrated upper bar");
         if (Close[forBar] < dnBuffer[forBar] && Close[forBar+1] > dnBuffer[forBar+1]) doAlert("close penetrated lower bar");
      }
   } 
 
   return(q);
}
 
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
 
void calculateTma(int limit)
{
   int i,j,k;
   double FullLength = 2.0*HalfLength+1.0;
    
   //
    
   for (i=limit; i>=0; i--)
   {
      double sum  = (HalfLength+1)*iMA(NULL,0,1,0,MODE_SMA,Price,i);
      double sumw = (HalfLength+1);
      for(j=1, k=HalfLength; j<=HalfLength; j++, k--)
      {
         sum  += k*iMA(NULL,0,1,0,MODE_SMA,Price,i+j);
         sumw += k;
 
         if (j<=i)
         {
            sum  += k*iMA(NULL,0,1,0,MODE_SMA,Price,i-j);
            sumw += k;
         }
      }
      tmBuffer[i] = sum/sumw;
 
      //
      //
      //
      //
      //
             
         double diff = iMA(NULL,0,1,0,MODE_SMA,Price,i)-tmBuffer[i];
         if (i> (Bars-HalfLength-1)) continue;
         if (i==(Bars-HalfLength-1))
         {
            upBuffer[i] = tmBuffer[i];
            dnBuffer[i] = tmBuffer[i];
            if (diff>=0)
               {
                  wuBuffer[i] = MathPow(diff,2);
                  wdBuffer[i] = 0;
               }
            else
               {               
                  wdBuffer[i] = MathPow(diff,2);
                  wuBuffer[i] = 0;
               }                  
            continue;
         }
       
         //
         //
         //
         //
         //
          
         if(diff>=0)
            {
               wuBuffer[i] = (wuBuffer[i+1]*(FullLength-1)+MathPow(diff,2))/FullLength;
               wdBuffer[i] =  wdBuffer[i+1]*(FullLength-1)/FullLength;
            }
         else
            {
               wdBuffer[i] = (wdBuffer[i+1]*(FullLength-1)+MathPow(diff,2))/FullLength;
               wuBuffer[i] =  wuBuffer[i+1]*(FullLength-1)/FullLength;
            }
         upBuffer[i] = tmBuffer[i] + BandsDeviations*MathSqrt(wuBuffer[i]);
         dnBuffer[i] = tmBuffer[i] - BandsDeviations*MathSqrt(wdBuffer[i]);
   }
} 
 
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
 
int 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()," at ",TimeToStr(TimeLocal(),TIME_SECONDS)," THA : ",doWhat);
         if (alertsMessage){ 
         Comment (message);
         Alert(message);
         SendNotification(message);
         }
         if (alertsEmail)   SendMail(StringConcatenate(Symbol(),"TMA "),message);
         if (alertsSound)   PlaySound("alert2.wav");
    }
    return(w);
}
 
//+------------------------------------------------------------------+

int stringToTimeFrame(string tfs)
{
   for(int l = StringLen(tfs)-1; l >= 0; l--)
   {
      int Char = StringGetChar(tfs,l);
          if((Char > 96 && Char < 123) || (Char > 223 && Char < 256))
               tfs = StringSetChar(tfs, 1, Char - 32);
          else
              if(Char > -33 && Char < 0)
                  tfs = StringSetChar(tfs, 1, Char + 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);
}

//+------------------------------------------------------------------+
void DrawArrow(string name, int arrow, datetime time, double price, color col, int width, ENUM_ARROW_ANCHOR anchor)
{
  if (ObjectFind(0, name) == -1)
  {
    ObjectCreate(0, name, OBJ_ARROW, 0, time, price);
    ObjectSetInteger(0, name, OBJPROP_BACK, false);
    ObjectSetInteger(0, name, OBJPROP_SELECTABLE, false);
    ObjectSetInteger(0, name, OBJPROP_HIDDEN, true);
  }
  if (ObjectGetInteger(0, name, OBJPROP_ANCHOR) != anchor) ObjectSetInteger(0, name, OBJPROP_ANCHOR, anchor);
  if (ObjectGetInteger(0, name, OBJPROP_ARROWCODE) != arrow) ObjectSetInteger(0, name, OBJPROP_ARROWCODE, arrow);
  if (ObjectGetInteger(0, name, OBJPROP_COLOR) != col) ObjectSetInteger(0, name, OBJPROP_COLOR, col);
  if (ObjectGetInteger(0, name, OBJPROP_WIDTH) != width) ObjectSetInteger(0, name, OBJPROP_WIDTH, width);
  if (ObjectGetInteger(0, name, OBJPROP_TIME, 0) != time) ObjectSetInteger(0, name, OBJPROP_TIME, 0, time);
  if (NormalizeDouble(ObjectGetDouble(0, name, OBJPROP_PRICE, 0), _Digits) != NormalizeDouble(price, _Digits)) ObjectSetDouble(0, name, OBJPROP_PRICE, 0, price);
}
//+------------------------------------------------------------------+

and This is my EA code:

//+------------------------------------------------------------------+
//|                                                         TmA3.mq4 |
//|                        Copyright 2019, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict


extern string TimeFrame       = "current time frame";
extern int    HalfLength      = 55;
extern int    Price           = PRICE_WEIGHTED;
extern double BandsDeviations = 2.8;
extern bool   Interpolate     = true;
extern bool   alertsOn        = true;
extern bool   alertsOnHighLow = true;
extern bool   alertsMessage   = true;
extern bool   alertsSound     = false;
extern bool   alertsEmail     = false;

int shift=0;



//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
      double x;
      x=iCustom(NULL,0,"TMA+CG",TimeFrame,HalfLength,Price,BandsDeviations,Interpolate,alertsOn,alertsOnHighLow,alertsMessage,alertsSound,alertsEmail,2,shift);
      Comment(x);
      
  }

HELP ME PLEASE.

Files:
TMAzCG.mq4 11 kb
TmA3.mq4 2 kb
Naguisa Unada
524
Naguisa Unada  

"extern bool alertsOnCurrent = false;" is missing.

If you want to keep the default settings, you can omit everything.

x = iCustom(NULL, 0, "TMA+CG", 2, shift);

It may be extra, but TMA is repaint, so it is not suitable for EA.

To add comments, please log in or register