Kindly add push notification alert

 

Hello,

Can anyone kindly add push notification alert when price cross below or over on previous day closed on bar closed?

Appreciate your kind gesture.

Thank you.


D

Files:
 
sharpie0319:

Hello,

Can anyone kindly add push notification alert when price cross below or over on previous day closed on bar closed?


Appreciate your kind gesture.

Thank you.


D

To add a push notification alert to the provided MT4 indicator code, you'll need to use the `WebRequest` function in MetaTrader 4. Here's how you can modify the code:


1. At the beginning of the code, add the following lines to include the necessary libraries and define the notification URL:

#import "kernel32.ex_"

int SendNotification(string url);

#import



extern string NotificationURL = ""; // Replace with your notification URL


2. In the `start()` function, add the following code after the `DailyOpen` function call to check for specific conditions and send a notification if the conditions are met:

//+------------------------------------------------------------------+

//| Custom indicator iteration function                               |

//+------------------------------------------------------------------+

int start()

{

    int counted_bars=IndicatorCounted();

    if(counted_bars < 0) return(-1);

    if(counted_bars>0) counted_bars--;

    int lastbar=Bars-counted_bars;

    if(counted_bars==0) lastbar-=1+1;



    DailyOpen(1,lastbar);



    // Check for conditions and send notification

    if(!IsTesting() && !IsVisualMode() && StringLen(NotificationURL) > 0)

    {

        // Your condition to send notification (example)

        if(TodayOpenBuffer[0] > 1.2 * TodayOpenBuffer[1])

        {

            string message = "Significant price move detected.";

            SendNotification(NotificationURL + "?message=" + UrlEncode(message));

        }

    }



    return(0);

}


3. After the `start()` function, add the following functions to handle the notification sending process:

//+------------------------------------------------------------------+

//| Send notification to specified URL                               |

//+------------------------------------------------------------------+

int SendNotification(string url)

{

    char data[], headers[];

    string result[];

    int tmp = StringToCharArray(url, data, 0, StringLen(url)) + 1;

    ResetLastError();



    int res = WebRequest("GET", DoubleToStr(randbyt, 0), data, headers, data, headers, result, headers);



    if(res == -1)

    {

        Print("Send notification failed, error: ", GetLastError());

        return(FALSE);

    }



    return(TRUE);

}



//+------------------------------------------------------------------+

//| Encode string for URL                                            |

//+------------------------------------------------------------------+

string UrlEncode(string str)

{

    static string l_result, l_symbols = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";

    int l_symbols_total = StringLen(l_symbols);

    int l_str_len = StringLen(str);

    int l_result_size = 0;



    for(int i=0; i<l_str_len; i++)

    {

        if(StringFind(l_symbols, StringGetChar(str, i)) >= 0)

        {

            l_result_size++;

        }

        else

        {

            l_result_size += 3;

        }

    }



    l_result = StringRepeat("%", l_result_size);



    for(int i=0, j=0; i<l_str_len; i++)

    {

        int l_sym = StringGetChar(str, i);

        int l_pos = StringFind(l_symbols, CharToString(l_sym));



        if(l_pos >= 0)

        {

            StringSetChar(l_result, j, l_sym);

            j++;

        }

        else

        {

            StringSetChar(l_result, j, '%');

            j++;

            StringSetChar(l_result, j, CharToString(l_sym / 16 + (l_sym / 16 > 9 ? 55 : 48)));

            j++;

            StringSetChar(l_result, j, CharToString(l_sym % 16 + (l_sym % 16 > 9 ? 55 : 48)));

            j++;

        }

    }



    return(l_result);

}



//+------------------------------------------------------------------+

//| Return random byte                                                |

//+------------------------------------------------------------------+

int randbyt()

{

    static int l_seed = 0;

    l_seed = (l_seed * 31415 + 6789) % 65536;

    return(l_seed);

}


4. Replace `"Replace with your notification URL"` with the URL of your push notification service. Make sure to encode any special characters in the URL using the `UrlEncode` function.

With these modifications, the indicator will send a push notification to the specified URL when the condition `TodayOpenBuffer[0] > 1.2 * TodayOpenBuffer[1]` is met (which you can modify according to your requirements). The message sent in the notification will be "Significant price move detected.".

 
Oleksandr Medviediev #:


3. After the `start()` function, add the following functions to handle the notification sending process:


4. Replace `"Replace with your notification URL"` with the URL of your push notification service. Make sure to encode any special characters in the URL using the `UrlEncode` function.

With these modifications, the indicator will send a push notification to the specified URL when the condition `TodayOpenBuffer[0] > 1.2 * TodayOpenBuffer[1]` is met (which you can modify according to your requirements). The message sent in the notification will be "Significant price move detected.".

Thank you Oleksandr,

Appreciate it alot.
Reason: