keep getting errors

 
hi Guys, when you have a few min spare, could you take a look at the code and see what im doing wrong please.

#property indicator_chart_window
#property indicator_buffers    3
#property indicator_color1     Yellow
#property indicator_color2     DodgerBlue
#property indicator_color3     Red
#property indicator_style2     STYLE_DOT
#property indicator_style3     STYLE_DOT

//
//
//
//
//

extern string TimeFrame       = "current time frame";
extern int    HalfLength      = 56;
extern int    Price           = PRICE_CLOSE;
extern double ATRMultiplier   = 2.0;
extern int    ATRPeriod       = 100;
extern bool   Interpolate     = true;

extern bool   alertsOn        = false;
extern bool   alertsOnCurrent = false;
extern bool   alertsOnHighLow = true;
extern bool   alertsMessage   = true;
extern bool   alertsSound     = false;
extern bool   alertsEmail     = false;

//
//
//
//
//

double buffer1[];
double buffer2[];
double buffer3[];
double trend[];

//
//
//
//
//

string indicatorFileName;
bool   calculateValue;
bool   returnBars;
int    timeFrame;

int gi_PipsDecimal;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//

int init()
{
   for (int i=0; i<indicator_buffers; i++) SetIndexStyle(i,DRAW_LINE);
   IndicatorBuffers(4);
   HalfLength=MathMax(HalfLength,1);
      SetIndexBuffer(0,buffer1); SetIndexDrawBegin(0,HalfLength);
      SetIndexBuffer(1,buffer2); SetIndexDrawBegin(1,HalfLength);
      SetIndexBuffer(2,buffer3); SetIndexDrawBegin(2,HalfLength);
      SetIndexBuffer(3,trend);

      //
      //
      //
      //
      //
   
      indicatorFileName = WindowExpertName();
      returnBars        = TimeFrame=="returnBars";     if (returnBars)     return(0);
      calculateValue    = TimeFrame=="calculateValue"; if (calculateValue) return(0);
      timeFrame         = stringToTimeFrame(TimeFrame);
      
      //
      //
      //
      //
      //
      
   IndicatorShortName(timeFrameToString(timeFrame)+" TMA bands ("+HalfLength+")");
   
   gi_PipsDecimal = Get_Pips_Decimal();
   
   return(0);
}
int deinit() { return(0); }




//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int start()
{
   int counted_bars=IndicatorCounted();
   int i,j,k,limit;

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
           limit=MathMin(Bars-1,Bars-counted_bars+HalfLength);
            if (returnBars)  { buffer1[0] = limit+1; return(0); }

   //
   //
   //
   //
   //
   
   if (calculateValue || timeFrame==Period())
   {
      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;
            }
         }

         //
         //
         //
         //
         //
      
         double range = iATR(NULL,0,ATRPeriod,i+10)*ATRMultiplier;
            buffer1[i] = sum/sumw;
            buffer2[i] = buffer1[i]+range;
            buffer3[i] = buffer1[i]-range;

         //
         //
         //
         //
         //
          
         trend[i] = 0;                     
            if (alertsOnHighLow)       
            {
               if (High[i] > buffer2[i]) trend[i] =  1;
               if (Low[i]  < buffer3[i]) trend[i] = -1;
            }
            else
            {
               if (Close[i] > buffer2[i]) trend[i] =  1;
               if (Close[i] < buffer3[i]) trend[i] = -1;
            }
      }
      if (!calculateValue) manageAlerts();
      
      // Calculate the distances between bid & bands
      double ld_Dist_Pts, ld_Dist_Pips;
      
      // Distance to mid
      ld_Dist_Pts = MathAbs(Bid - buffer1[0]);
      ld_Dist_Pips = Convert_2_Pips(ld_Dist_Pts);
      ObjectCreate("!Mid",OBJ_TEXT,0,0,0);
      ObjectSet("!Mid",OBJPROP_TIME1,Time[0]+(3*Period()*60));
      ObjectSet("!Mid",OBJPROP_PRICE1,buffer1[0]);
      ObjectSetText("!Mid",DoubleToStr(ld_Dist_Pips,gi_PipsDecimal),10,"Arial",indicator_color1);
      
      // Distance to upper
      ld_Dist_Pts = MathAbs(Bid - buffer2[0]);
      ld_Dist_Pips = Convert_2_Pips(ld_Dist_Pts);
      ObjectCreate("!Upp",OBJ_TEXT,0,0,0);
      ObjectSet("!Upp",OBJPROP_TIME1,Time[0]+(3*Period()*60));
      ObjectSet("!Upp",OBJPROP_PRICE1,buffer2[0]);
      ObjectSetText("!Upp",DoubleToStr(ld_Dist_Pips,gi_PipsDecimal),10,"Arial",indicator_color2);
      
      // Distance to lower
      ld_Dist_Pts = MathAbs(Bid - buffer3[0]);
      ld_Dist_Pips = Convert_2_Pips(ld_Dist_Pts);
      ObjectCreate("!Low",OBJ_TEXT,0,0,0);
      ObjectSet("!Low",OBJPROP_TIME1,Time[0]+(3*Period()*60));
      ObjectSet("!Low",OBJPROP_PRICE1,buffer3[0]);
      ObjectSetText("!Low",DoubleToStr(ld_Dist_Pips,gi_PipsDecimal),10,"Arial",indicator_color3);
      
      
      return(0);            
   }
   
   //
   //
   //
   //
   //

   limit = MathMax(limit,MathMin(Bars-1,iCustom(NULL,timeFrame,indicatorFileName,"returnBars",0,0)*timeFrame/Period()));
   for(i=limit; i>=0; i--)
   {
      int y = iBarShift(NULL,timeFrame,Time[i]);
      buffer1[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateTma",HalfLength,Price,ATRMultiplier,ATRPeriod,0,y);
      buffer2[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateTma",HalfLength,Price,ATRMultiplier,ATRPeriod,1,y);
      buffer3[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateTma",HalfLength,Price,ATRMultiplier,ATRPeriod,2,y);
      trend[i]   = iCustom(NULL,timeFrame,indicatorFileName,"calculateTma",HalfLength,Price,ATRMultiplier,ATRPeriod,3,y);

      //
      //
      //
      //
      //
       
      if (timeFrame <= Period() || y==iBarShift(NULL,timeFrame,Time[i-1])) continue;
      if (!Interpolate) continue;

      //
      //
      //
      //
      //

      datetime time = iTime(NULL,timeFrame,y);
         for(int n = 1; i+n < Bars && Time[i+n] >= time; n++) continue;
         for(k = 1; k < n; k++)
         {
            buffer1[i+k] = buffer1[i]  +(buffer1[i+n]-buffer1[i])*k/n;
            buffer2[i+k] = buffer2[i]  +(buffer2[i+n]-buffer2[i])*k/n;
            buffer3[i+k] = buffer3[i]  +(buffer3[i+n]-buffer3[i])*k/n;
         }               
   }

   //
   //
   //
   //
   //
      
   manageAlerts();
   return(0);
}













//+-------------------------------------------------------------------
//|                                                                  
//+-------------------------------------------------------------------
//
//
//
//
//

void manageAlerts()
{
   if (alertsOn)
   {
      if (alertsOnCurrent)
           int whichBar = 0;
      else     whichBar = 1; whichBar = iBarShift(NULL,0,iTime(NULL,timeFrame,whichBar));
      if (trend[whichBar] != trend[whichBar+1])
      {
         if (trend[whichBar] == 1) doAlert(whichBar,"up");
         if (trend[whichBar] ==-1) doAlert(whichBar,"down");
      }         
   }
}

//
//
//
//
//

void doAlert(int forBar, string doWhat)
{
   static string   previousAlert="nothing";
   static datetime previousTime;
   string message;
   
   if (previousAlert != doWhat || previousTime != Time[forBar]) {
       previousAlert  = doWhat;
       previousTime   = Time[forBar];

       //
       //
       //
       //
       //

       message =  StringConcatenate(Symbol()," at ",TimeToStr(TimeLocal(),TIME_SECONDS)," "+timeFrameToString(timeFrame)+" TMA bands price penetrated ",doWhat," band");
          if (alertsMessage) Alert(message);
          if (alertsEmail)   SendMail(StringConcatenate(Symbol(),"TMA bands "),message);
          if (alertsSound)   PlaySound("alert2_wav");
   }
}

//+-------------------------------------------------------------------
//|                                                                  
//+-------------------------------------------------------------------
//
//
//
//
//

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)
{
   tfs = StringUpperCase(tfs);
   for (int i=ArraySize(iTfTable)-1; i>=0; i--)
         if (tfs==sTfTable[i] || tfs==""+iTfTable[i]) return(MathMax(iTfTable[i],Period()));
                                                      return(Period());
}
string timeFrameToString(int tf)
{
   for (int i=ArraySize(iTfTable)-1; i>=0; i--) 
         if (tf==iTfTable[i]) return(sTfTable[i]);
                              return("");
}

//
//
//
//
//

string StringUpperCase(string str)
{
   string   s = str;

   for (int length=StringLen(str)-1; length>=0; length--)
   {
      int tchar = StringGetChar(s, length);
         if((tchar > 96 && tchar < 123) || (tchar > 223 && tchar < 256))
                     s = StringSetChar(s, length, tchar - 32);
         else if(tchar > -33 && tchar < 0)
                     s = StringSetChar(s, length, tchar + 224);
   }
   return(s);
}
//+------------------------------------------------------------------+
//| create screen objects                                            |
//+------------------------------------------------------------------+
void Object_Create(string ps_name,int pi_x,int pi_y,string ps_text=" ",int pi_size=12,
                  string ps_font="Arial",color pc_colour=CLR_NONE)
  {
//----
   
//   if (colour==CLR_NONE) colour=xcBackground;
      
   ObjectCreate(ps_name,OBJ_LABEL,0,0,0,0,0);
   ObjectSet(ps_name,OBJPROP_CORNER,1);
   ObjectSet(ps_name,OBJPROP_COLOR,pc_colour);
   ObjectSet(ps_name,OBJPROP_XDISTANCE,pi_x);
   ObjectSet(ps_name,OBJPROP_YDISTANCE,pi_y);
   
   ObjectSetText(ps_name,ps_text,pi_size,ps_font,pc_colour);

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| convert to points                                                |
//+------------------------------------------------------------------+
double Convert_2_Pts(double pd_Pips)
  {
//----
   int pd_Points=pd_Pips;  // Default - no conversion
   
  if (Digits == 5 || (Digits == 3 && StringFind(Symbol(), "JPY") != -1)) 
     pd_Points=pd_Pips*10;
     
  if (Digits == 6 || (Digits == 4 && StringFind(Symbol(), "JPY") != -1)) 
     pd_Points=pd_Pips*100;
//----
   return(pd_Points);
  }
//+------------------------------------------------------------------+
//| convert to pips                                                  |
//+------------------------------------------------------------------+
double Convert_2_Pips(double pd_Points)
  {
//----
   double pd_Pips=pd_Points/Point;  // Default - no conversion
   
  if (Digits == 5 || (Digits == 3 && StringFind(Symbol(), "JPY") != -1)) 
     {
     pd_Pips=pd_Points/Point/10;
     }
     
  if (Digits == 6 || (Digits == 4 && StringFind(Symbol(), "JPY") != -1)) 
     {
     pd_Pips=pd_Points/Point/100;
     }
//----
   return(pd_Pips);
  }
//+------------------------------------------------------------------+
//| get the pips decimal places                                      |
//+------------------------------------------------------------------+
int Get_Pips_Decimal()
  {
//----
   int pi_PipsDecimal = 0;  // Default - no decimals
   
  if (Digits == 5 || (Digits == 3 && StringFind(Symbol(), "JPY") != -1)) 
     {
     pi_PipsDecimal = 1;
     }
     
  if (Digits == 6 || (Digits == 4 && StringFind(Symbol(), "JPY") != -1)) 
     {
     pi_PipsDecimal = 2;
     }
//----
   return(pi_PipsDecimal);
  }



void exitbuys()
{if (trend[whichBar] == 1) );
(for (int i=OrdersTotal()-1; i >=0; i--)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if (OrderType() == OP_BUY )
{
while(true)//infinite loop must be escaped by break
{
bool result = OrderClose(OrderTicket(), OrderLots(), Bid, 3, Red);//actual order closing
if (result != true)//if it did not close
{
int err = GetLastError(); Print("LastError = ",err);//get the reason why it didn't close
}
else {err = 0;break;}//if it did close it breaks out of while early DOES NOT RUN SWITCH
switch(err)
{
case 129://INVALID_PRICE //if it was 129 it will run every line until it gets to the break.
case 135://ERR_PRICE_CHANGED//same for 135
case 136://ERR_OFF_QUOTES//and 136
case 137://ERR_BROKER_BUSY//and 137
case 138://ERR_REQUOTE//and 138
case 146:Sleep(1000);RefreshRates();i++;break;//Sleeps,Refreshes and increments.Then breaks out of switch.
default:break;//if the err does not match any of the above. It does not increment. and runs next order in series.
}
break;//after breaking out of switch it breaks out of while loop. which order it runs next depends on i++ or not.
}
}
}
else Print( "When selecting a trade, error ",GetLastError()," occurred");
  { 
  }
   
 
//+------------------------------------------------------------------+
void exitsells()
{if (trend[whichBar] ==-1);
for (int i=OrdersTotal()-1; i >=0; i--)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if (OrderType() == OP_SELL )
{
while(true)//infinite loop must be escaped by break
{
bool result = OrderClose(OrderTicket(), OrderLots(), Ask, 3, Red);//actual order closing
if (result != true)//if it did not close
{
int err = GetLastError(); Print("LastError = ",err);//get the reason why it didn't close
}
else {err = 0;break;}//if it did close it breaks out of while early DOES NOT RUN SWITCH
switch(err)
{
case 129://INVALID_PRICE //if it was 129 it will run every line until it gets to the break.
case 135://ERR_PRICE_CHANGED//same for 135
case 136://ERR_OFF_QUOTES//and 136
case 137://ERR_BROKER_BUSY//and 137
case 138://ERR_REQUOTE//and 138
case 146:Sleep(1000);RefreshRates();i++;break;//Sleeps,Refreshes and increments.Then breaks out of switch.
default:break;//if the err does not match any of the above. It does not increment. and runs next order in series.
break;//after breaking out of switch it breaks out of while loop. which order it runs next depends on i++ or not.
else Print( "When selecting a trade, error ",GetLastError()," occurred")
 

Use the code button to insert your code.



 
Eleni Anna Branou:

Use the code button to insert your code.



sorry Eleni, still finding my feet around the forum. i have uploaded the ea. any help would be appreciated. 

Files:
newa1.2.mq4  16 kb
 
shabaz:

sorry Eleni, still finding my feet around the forum. i have uploaded the ea. any help would be appreciated. 

Hello,

what is the problem with the indicator?

I made a small correction, I don't know if that was the problem you had.

Files:
newa1.3.mq4  19 kb
 
Nikolaos Pantzos:

Hello,

what is the problem with the indicator?

I made a small correction, I don't know if that was the problem you had.

Hi Mate,  


I was trying to make an EA but seems its turned into an indicator. buy order to close  when price crosses the top of the band and close buy trade when prices closes below the bottom band close sell

 
shabaz:

Hi Mate,  


I was trying to make an EA but seems its turned into an indicator. buy order to close  when price crosses the top of the band and close buy trade when prices closes below the bottom band close sell

It's indicator, not expert.

 
ahh ok, is there a way to convert it to an EA. thought if i copy the content from the indicator (for the conditions) and combine it with code i found online to open and close trades. it would work. sorry for the ignorance im new to programming.
 
shabaz:
ahh ok, is there a way to convert it to an EA. thought if i copy the content from the indicator (for the conditions) and combine it with code i found online to open and close trades. it would work. sorry for the ignorance im new to programming.

There is no easy and hard way to do it.

There is only the right way.

And they need knowledge to do it.

 
Nikolaos Pantzos:

There is no easy and hard way to do it.

There is only the right way.

And they need knowledge to do it.

true,

Reason: