Download MetaTrader 5
To add comments, please log in or register
Find out how social trading can improve your profits
muruez
61
muruez 2017.03.17 14:17 

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
14457
whroeder1 2017.03.17 15:14  
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
15518
Mladen Rakic 2017.03.17 15:16  
Friday afternoon and the usual "becoming a coder over the weekend" stuff :)
muruez
61
muruez 2017.03.21 13:21  
#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
61
muruez 2017.03.21 13:22  
thank's :)
/
To add comments, please log in or register