Is it possible to use a trendline to set a alert ?

 
Hello,

Is it possible to use a trendline to set a alert ?
With something like that :
If close > trendline then Alert("Resistance breaked");

Regards,

Tintin92
 
Tintin,

Everything is possible with MQL4. See the following code:

// 1. Define the coordinates 
datetime time1 = CurTime()-(Period()*60*30);
double price1    = High[10];
 
datetime time2 = CurTime();
double price2    = High[1];
 
// 2. Create Trend Line using the 2 coordinates above
ObjectCreate("BuyStop", OBJ_TREND, 0, time1, price1, time2, price2);
 
// 3. Setting Trend Line style
ObjectSet("BuyStop", 6, LimeGreen);
ObjectSet("BuyStop", 7, STYLE_DOT);
ObjectSet("BuyStop", 10, 0);
ObjectSetText("BuyStop", "BuyStop");
 
// 4. Checking buy signal...
double trendLineCurrent  = ObjectGetValueByShift("BuyStop", 1);
double trendLinePrevious = ObjectGetValueByShift("BuyStop", 2);
 
if (Close[1] > trendLineCurrent && Close[2] <= trendLinePrevious) {
  Alert("Buy Signal");
}

See more details:

https://docs.mql4.com/objects


Regards,
 
Hello rfiche,

Thanks for your answer.
Steps 1 to 3 have to be in a script, and step 4 have to be in a Expert Advisors, right ?

Tintin92.
 
tintin92:
Hello rfiche,

Thanks for your answer.
Steps 1 to 3 have to be in a script, and step 4 have to be in a Expert Advisors, right ?

Tintin92.



Tintin,

you can put the steps 1 to 3 on init() and the step 4 on start() function.

Read about init(), deinit() and start() MQL4 functions. I think it is very important to understand the execution flow of experts, indicators and scripts.
 

Hello rfiche:
Can i ask you a question?
It's very easy to get a maximum value of the price within a period, example:
High[i] = High[Highest(NULL,0,MODE_HIGH, 10, i)];
But how to get a maximum value of the "macd" within 10 period?
macd[i] = iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,i);
then how to get the maximum value of the "macd" within 10 period?
why can't write as "High[i] = High[Highest('macd', 0,MODE_HIGH, 10, i)];

thanks, weikang

 
jianweikang:

Hello rfiche:
Can i ask you a question?
It's very easy to get a maximum value of the price within a period, example:
High[i] = High[Highest(NULL,0,MODE_HIGH, 10, i)];
But how to get a maximum value of the "macd" within 10 period?
macd[i] = iMACD(NULL,0,12,26,9,PRICE_CLOSE, MODE_MAIN, i);
then how to get the maximum value of the "macd" within 10 period?
why can't write as "High[i] = High[Highest('macd', 0,MODE_HIGH, 10, i)];

thanks, weikang


Sure. I am here to help you all.

  • The first parameter of 'Highest' and 'Lowest' functions should only be set with the symbol (i.e: EURUSD, GBPUSD etc) or NULL (chart symbol). So you can´t use 'macd' word instead, ok?
  • In order to get the maximum MACD value within 10 period, you can do that:

double macdValues[10]; // create a temporary Array
 
// fill array with MACD values
for (int i = 0; i < 10; i++) {
  macdValues[i] = iMACD("GBPUSD", ... , i);
}
 
// get max. and min. values
double maxMACD = ArrayMaximum(macdValues);
double minMACD = ArrayMinimum(macdValues);

Regards,

Renato.
 
Hello rfiche:
Thanks for your help. I have understood that must array function, but still a little question:
Could it used to the following code:

if(High[0] > High[Highest(NULL,0,MODE_HIGH,10,0) && MACD[0] < maxMACD) // Deviation
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid......
or indicates on chart_window
I am not successful to have a try. Can you help me?

     Thanks, weikang     
 
I think somewhre above was how to get the high of a macd line during 10 periods on a chart:
this is how I would do it, using your imacd... ie the highest close:

double mac,highest;
for(int a=0; a<10; a++)
mac=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,a);
if(highest<mac) {highest=mac;}
}
Print(highest);
(this would include the current period. Change the 0 to a 1 if current period is not wanted)
 
oldman wrote:
I think somewhre above was how to get the high of a macd line during 10 periods on a chart:
this is how I would do it, using your imacd... ie the highest close:

double mac,highest;
for(int a=0; a<10; a++)
mac=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,a);
if(highest<mac) {highest=mac;}
}
Print(highest);
(this would include the current period. Change the 0 to a 1 if current period is not wanted)
All have tried, but the result was wrong so have got the correct value.
My idea is :
if(High[i]>=High[Highest(NULL,0,MODE_HIGH, 10, i+1)] && Macd[i]<maxMACD && Macd[i]>0) OR
if(Low[i]<=Low[Lowest(NULL,0,MODE_LOW,10, i+1)] && Macd[i]>minMACDL && Macd[i]<0)
" maxMACD OR minMACD " are within 10 period back from i .
can you write all codes and successful? and on chart_window display indicating

   Thanks, weikang     
Reason: