Download MetaTrader 5

indicator to EA

To add comments, please log in or register
muruez
65
muruez  

how to change this indicator to EA any help? this is trend line break indicator. when candle cross trend line or horizontal line it will send alert. thanks in advance.    

#property copyright ""
#property link      "http://"
#property version   "1.00"
#property description "Audible alert when candlestick closes above / below the trendline or horizontal line. For instant alert or instant exe"

#include <stdlib.mqh>
#include <stderror.mqh>

//--- indicator settings
#property indicator_chart_window
#property indicator_buffers 2

#property indicator_type1 DRAW_ARROW
#property indicator_width1 1
#property indicator_color1 0xFFAA00
#property indicator_label1 "Buy"

#property indicator_type2 DRAW_ARROW
#property indicator_width2 1
#property indicator_color2 0x0000FF
#property indicator_label2 "Sell"

//--- indicator buffers
double Buffer1[];
double Buffer2[];

datetime time_alert; //used when sending alert
extern bool Audible_Alerts = true;
double myPoint; //initialized in OnInit

void myAlert(string type, string message)
  {
   if(type == "print")
      Print(message);
   else if(type == "error")
     {
      Print(type+" | Trendline Break Alert @ "+Symbol()+","+Period()+" | "+message);
     }
   else if(type == "order")
     {
     }
   else if(type == "modify")
     {
     }
   else if(type == "indicator")
     {
      if(Audible_Alerts) Alert(type+" | Trendline Break Alert @ "+Symbol()+","+Period()+" | "+message);
     }
  }

double TrendlinePriceUpper(int shift) //returns current price on the highest horizontal line or trendline found in the chart
  {
   int obj_total = ObjectsTotal();
   double maxprice = -1;
   for(int i = obj_total - 1; i >= 0; i--)
     {
      string name = ObjectName(i);
      double price;
      if(ObjectType(name) == OBJ_HLINE
      && (price = ObjectGet(name, OBJPROP_PRICE1)) > maxprice)
         maxprice = price;
      else if(ObjectType(name) == OBJ_TREND
      && (price = ObjectGetValueByShift(name, shift)) > maxprice)
         maxprice = price;
     }
   return(maxprice); //not found => -1
  }

double TrendlinePriceLower(int shift) //returns current price on the lowest horizontal line or trendline found in the chart
  {
   int obj_total = ObjectsTotal();
   double minprice = MathPow(10, 308);
   for(int i = obj_total - 1; i >= 0; i--)
     {
      string name = ObjectName(i);
      double price;
      if(ObjectType(name) == OBJ_HLINE
      && (price = ObjectGet(name, OBJPROP_PRICE1)) < minprice)
         minprice = price;
      else if(ObjectType(name) == OBJ_TREND
      && (price = ObjectGetValueByShift(name, shift)) < minprice)
         minprice = price;
     }
   if (minprice > MathPow(10, 307))
      minprice = -1; //not found => -1
   return(minprice);
  }

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {  
   IndicatorBuffers(2);
   SetIndexBuffer(0, Buffer1);
   SetIndexEmptyValue(0, 0);
   SetIndexArrow(0, 241);
   SetIndexBuffer(1, Buffer2);
   SetIndexEmptyValue(1, 0);
   SetIndexArrow(1, 242);
   //initialize myPoint
   myPoint = Point();
   if(Digits() == 5 || Digits() == 3)
     {
      myPoint *= 10;
     }
   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime& time[],
                const double& open[],
                const double& high[],
                const double& low[],
                const double& close[],
                const long& tick_volume[],
                const long& volume[],
                const int& spread[])
  {
   int limit = rates_total - prev_calculated;
   //--- counting from 0 to rates_total
   ArraySetAsSeries(Buffer1, true);
   ArraySetAsSeries(Buffer2, true);
   //--- initial zero
   if(prev_calculated < 1)
     {
      ArrayInitialize(Buffer1, 0);
      ArrayInitialize(Buffer2, 0);
     }
   else
      limit++;
  
   if(TrendlinePriceUpper(0) < 0 && TrendlinePriceLower(0) < 0) return(0);
   //--- main loop
   for(int i = limit-1; i >= 0; i--)
     {
      if (i >= MathMin(5000-1, rates_total-1-50)) continue; //omit some old rates to prevent "Array out of range" or slow calculation  
      //Indicator Buffer 1
      if(Close[i] > TrendlinePriceUpper(i) //Candlestick Close > Upper Trendline
      )
        {
         Buffer1[i] = Low[i] - iATR(NULL, PERIOD_CURRENT, 14, i); //Set indicator value at Candlestick Low - Average True Range
         if(i == 1 && Time[1] != time_alert) myAlert("indicator", "Buy"); //Alert on next bar open
         time_alert = Time[1];
        }
      else
        {
         Buffer1[i] = 0;
        }
      //Indicator Buffer 2
      if(Close[i] < TrendlinePriceLower(i)
      && Close[i+1] > TrendlinePriceLower(i+1) //Candlestick Close crosses below Lower Trendline
      )
        {
         Buffer2[i] = High[i] + iATR(NULL, PERIOD_CURRENT, 14, i); //Set indicator value at Candlestick High + Average True Range
         if(i == 1 && Time[1] != time_alert) myAlert("indicator", "Sell"); //Alert on next bar open
         time_alert = Time[1];
        }
      else
        {
         Buffer2[i] = 0;
        }
     }
   return(rates_total);
  }
//+-------------------

whroeder1
14772
whroeder1  
muruez: how to change this indicator to EA
  1. Don't paste code
    Play video
    Please edit your post.
    For large amounts of code, attach it

  2. Don't do that. Just get the value of the indicator. You should write a self documenting function instead of calling iCustom directly, see Detailed explanation of iCustom - MQL4 forum
Mladen Rakic
56456
Mladen Rakic  
Friday afternoon and the usual "becoming a coder over the weekend" stuff :)
muruez
65
muruez  
#property description "Audible alert when candlestick closes above / below the trendline or horizontal line. For instant alert or instant exe"
#include <stdlib.mqh>
#include <stderror.mqh>
//--- indicator settings
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_type1 DRAW_ARROW
#property indicator_width1 1
#property indicator_color1 0xFFAA00
#property indicator_label1 "Buy"
#property indicator_type2 DRAW_ARROW
#property indicator_width2 1
#property indicator_color2 0x0000FF
#property indicator_label2 "Sell"
//--- indicator buffers
double Buffer1[];
double Buffer2[];
datetime time_alert; //used when sending alert
extern bool Audible_Alerts = true;
double myPoint; //initialized in OnInit
void myAlert(string type, string message)
  {
   if(type == "print")
      Print(message);
   else if(type == "error")
     {
      Print(type+" | Trendline Break Alert @ "+Symbol()+","+Period()+" | "+message);
     }
   else if(type == "order")
     {
     }
   else if(type == "modify")
     {
     }
   else if(type == "indicator")
     {
      if(Audible_Alerts) Alert(type+" | Trendline Break Alert @ "+Symbol()+","+Period()+" | "+message);
     }
  }
double TrendlinePriceUpper(int shift) //returns current price on the highest horizontal line or trendline found in the chart
  {
   int obj_total = ObjectsTotal();
   double maxprice = -1;
   for(int i = obj_total - 1; i >= 0; i--)
     {
      string name = ObjectName(i);
      double price;
      if(ObjectType(name) == OBJ_HLINE
      && (price = ObjectGet(name, OBJPROP_PRICE1)) > maxprice)
         maxprice = price;
      else if(ObjectType(name) == OBJ_TREND
      && (price = ObjectGetValueByShift(name, shift)) > maxprice)
         maxprice = price;
     }
   return(maxprice); //not found => -1
  }
double TrendlinePriceLower(int shift) //returns current price on the lowest horizontal line or trendline found in the chart
  {
   int obj_total = ObjectsTotal();
   double minprice = MathPow(10, 308);
   for(int i = obj_total - 1; i >= 0; i--)
     {
      string name = ObjectName(i);
      double price;
      if(ObjectType(name) == OBJ_HLINE
      && (price = ObjectGet(name, OBJPROP_PRICE1)) < minprice)
         minprice = price;
      else if(ObjectType(name) == OBJ_TREND
      && (price = ObjectGetValueByShift(name, shift)) < minprice)
         minprice = price;
     }
   if (minprice > MathPow(10, 307))
      minprice = -1; //not found => -1
   return(minprice);
  }
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {   
   IndicatorBuffers(2);
   SetIndexBuffer(0, Buffer1);
   SetIndexEmptyValue(0, 0);
   SetIndexArrow(0, 241);
   SetIndexBuffer(1, Buffer2);
   SetIndexEmptyValue(1, 0);
   SetIndexArrow(1, 242);
   //initialize myPoint
   myPoint = Point();
   if(Digits() == 5 || Digits() == 3)
     {
      myPoint *= 10;
     }
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime& time[],
                const double& open[],
                const double& high[],
                const double& low[],
                const double& close[],
                const long& tick_volume[],
                const long& volume[],
                const int& spread[])
  {
   int limit = rates_total - prev_calculated;
   //--- counting from 0 to rates_total
   ArraySetAsSeries(Buffer1, true);
   ArraySetAsSeries(Buffer2, true);
   //--- initial zero
   if(prev_calculated < 1)
     {
      ArrayInitialize(Buffer1, 0);
      ArrayInitialize(Buffer2, 0);
     }
   else
      limit++;
   
   if(TrendlinePriceUpper(0) < 0 && TrendlinePriceLower(0) < 0) return(0);
   //--- main loop
   for(int i = limit-1; i >= 0; i--)
     {
      if (i >= MathMin(5000-1, rates_total-1-50)) continue; //omit some old rates to prevent "Array out of range" or slow calculation   
      //Indicator Buffer 1
      if(Close[i] > TrendlinePriceUpper(i) //Candlestick Close > Upper Trendline
      )
        {
         Buffer1[i] = Low[i] - iATR(NULL, PERIOD_CURRENT, 14, i); //Set indicator value at Candlestick Low - Average True Range
         if(i == 1 && Time[1] != time_alert) myAlert("indicator", "Buy"); //Alert on next bar open
         time_alert = Time[1];
        }
      else
        {
         Buffer1[i] = 0;
        }
      //Indicator Buffer 2
      if(Close[i] < TrendlinePriceLower(i)
      && Close[i+1] > TrendlinePriceLower(i+1) //Candlestick Close crosses below Lower Trendline
      )
        {
         Buffer2[i] = High[i] + iATR(NULL, PERIOD_CURRENT, 14, i); //Set indicator value at Candlestick High + Average True Range
         if(i == 1 && Time[1] != time_alert) myAlert("indicator", "Sell"); //Alert on next bar open
         time_alert = Time[1];
        }
      else
        {
         Buffer2[i] = 0;
        }
     }
   return(rates_total);
  }
//+-------------------
muruez
65
muruez  
thank's :)
muruez
65
muruez  
Mladen Rakic:
Friday afternoon and the usual "becoming a coder over the weekend" stuff :)


I KNOW i only have time on weak end. :)

i don't have time to look at chart all the time.so i try to make this EA to work with my strategy.

i make my forecast but most of my trade setting done by pending orders and i can't manage the trade all the time. 

all the magic happen while i'm sleep :(

so i need to make this EA work for me.im new to programming ;) & of course i can ask some to code for me and i pay but i like to try lean by myself.

the most important part is the logic part when upper line crosses & down line crosses. when this happen the EA make trade.

i have found this indicator but i have hard time to make it as EA.:(

i hope someone can  help me or guide me on the way process make this EA.

muruez
65
muruez  
whroeder1:
  1. Please edit your post.
    For large amounts of code, attach it

  2. Don't do that. Just get the value of the indicator. You should write a self documenting function instead of calling iCustom directly, see Detailed explanation of iCustom - MQL4 forum

thank you.
To add comments, please log in or register