Need support on Email alerts

Created at 24 Jul 2018, 08:18
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
HI

hiba7rain

Joined 20.07.2014

Need support on Email alerts
24 Jul 2018, 08:18


Hi, 

How to code the email alerts to have only one email if conditions are true and avoid issue of sending several emails?

 

Thanks for support 


@hiba7rain
Replies

PanagiotisCharalampous
24 Jul 2018, 09:24

Hi hiba7rain,

You could raise a flag as soon as you send a notification and check the flag in order not to send it again. See below an example

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        private bool _notificationSent;
        protected override void OnStart()
        {

        }

        protected override void OnTick()
        {
            var sendNotification = false;
            //  Make all your checks here and update sendNotification variable accordingly
            //   .
            //   .
            //   .
            if (sendNotification && !_notificationSent)
            {
                Notifications.SendEmail("email", "email", "Subject", "Text");
                _notificationSent = true;
            }
        }
        protected override void OnBar()
        {
            _notificationSent = false;
        }
        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }
}

Best Regards,

Panagiotis


@PanagiotisCharalampous

hiba7rain
24 Jul 2018, 10:10

RE:

Panagiotis Charalampous said:

Hi hiba7rain,

You could raise a flag as soon as you send a notification and check the flag in order not to send it again. See below an example

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        private bool _notificationSent;
        protected override void OnStart()
        {

        }

        protected override void OnTick()
        {
            var sendNotification = false;
            //  Make all your checks here and update sendNotification variable accordingly
            //   .
            //   .
            //   .
            if (sendNotification && !_notificationSent)
            {
                Notifications.SendEmail("email", "email", "Subject", "Text");
                _notificationSent = true;
            }
        }
        protected override void OnBar()
        {
            _notificationSent = false;
        }
        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }
}

Best Regards,

Panagiotis

Thanks Panagiotis,

I will try to fix it as you mentioned but need your advise as I have tried to do the same logic as below but did not work with me so what was missing or mistake I made on below code 

using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class SignalINdi : Indicator
    {


        private bool _emailSent;

protected override void Initialize()
        {
           

            //

         
        }

public override void Calculate(int index)
        {
            
        


            
            if ((close2 < JV4) && (close1 > JV3))
            {
                
                SSig[index - 1] = MarketSeries.Median[index] - Symbol.TickSize * 20;
                
                
                if ( !_emailSent)
                    Notifications.SendEmail("XXX@XX.com", "XXX@XX.com", Symbol.Code + "Signal", "XXXXX");
                
                _emailSent = true;
                

            }

 


@hiba7rain

PanagiotisCharalampous
24 Jul 2018, 10:13

Hi hiba7rain,

What do you mean what you say it is not working? Is the email not sent? Is it sent multiple times? Alos note that I use a cBot and not an indicator,

Best Regards,

Panagiotis


@PanagiotisCharalampous

hiba7rain
24 Jul 2018, 10:18

RE:

Panagiotis Charalampous said:

Hi hiba7rain,

What do you mean what you say it is not working? Is the email not sent? Is it sent multiple times? Alos note that I use a cBot and not an indicator,

Best Regards,

Panagiotis

Hi 

yes i noticed that you using Cbot coding 

my first issue was emails alerts are sent multiple times, so I added the code as i showed to you above (as to flag the emails sent out and stop sending multiple times) but it did not work at all no emails sent   


@hiba7rain

PanagiotisCharalampous
24 Jul 2018, 10:25

This is because you set _emailSent to true even if no email was sent. Change it to the following

                if ( !_emailSent)
                 {
                     Notifications.SendEmail("XXX@XX.com", "XXX@XX.com", Symbol.Code + "Signal", "XXXXX");
                     _emailSent = true;
                 }

 


@PanagiotisCharalampous

hiba7rain
24 Jul 2018, 10:36

RE:

Panagiotis Charalampous said:

This is because you set _emailSent to true even if no email was sent. Change it to the following

                if ( !_emailSent)
                 {
                     Notifications.SendEmail("XXX@XX.com", "XXX@XX.com", Symbol.Code + "Signal", "XXXXX");
                     _emailSent = true;
                 }

 

sorry i didnt get you, its the same code I did 

do you mean to set it to false?


@hiba7rain

PanagiotisCharalampous
24 Jul 2018, 10:38

Hi hiba7rain,

It is not. You did not put the brackets.


@PanagiotisCharalampous

hiba7rain
24 Jul 2018, 10:41

RE:

Panagiotis Charalampous said:

Hi hiba7rain,

It is not. You did not put the brackets.

oops yes i forgot the brackets :)

 

thanks alot for the correction 

ill give it a try and see how it works 


@hiba7rain

hiba7rain
25 Jul 2018, 07:35

RE: RE:

hiba7rain said:

Panagiotis Charalampous said:

Hi hiba7rain,

It is not. You did not put the brackets.

oops yes i forgot the brackets :)

 

thanks alot for the correction 

ill give it a try and see how it works 

its still with this code having issue of not sending emails and if i remove it the indicator keeps sending multiple times 

ill try to put the same indicator logic in Cbot and see how it works althought that the purpose should be indicator and not cBot 

maybe you cank share different code with us for indicators to send email only once other than the provided above ?

thanks 


@hiba7rain

PanagiotisCharalampous
25 Jul 2018, 09:30

Hi hiba7rain,

First of all you need to sort out the logic of what you are trying to do. Do you want the indicator to send an email once and never again? Then just raise a flag and do not reset it ever. But I guess your purpose is not that. Probably you would like to send one email per bar, when conditions are met. That is why proposed a cBot since it is easier to track bar changes with the OnBar() method. If you still want to keep the code in an indicator, there is nothing wrong with that, you just need to decide when you want your flag to be reset.

Best Regards,

Panagiotis


@PanagiotisCharalampous

Symposium
25 Jul 2018, 10:09

RE: Email Notification

Panagiotis Charalampous said:

Hi hiba7rain,

First of all you need to sort out the logic of what you are trying to do. Do you want the indicator to send an email once and never again? Then just raise a flag and do not reset it ever. But I guess your purpose is not that. Probably you would like to send one email per bar, when conditions are met. That is why proposed a cBot since it is easier to track bar changes with the OnBar() method. If you still want to keep the code in an indicator, there is nothing wrong with that, you just need to decide when you want your flag to be reset.

Best Regards,

Panagiotis

Hi Panagiotis, I assume what is required with the Email Notification, most posters are requiring the email be generated each time the Indicator displays a change of trend signal.

Change of signal code needs to be generated by the Indicator Logic and then trigger the Email Notification......once each change of trend / condition.

Example for an MA Indicator.

 Downtrend[index - 1] = _movingAverage3.Result[index - 1];

// Sell signal
IsSell = true;
IsBuy = false;

if (SoundAlert)

Notifications.PlaySound(_soundFileS);

if (EmailAlert)

Notifications.SendEmail("email@gmail.com", "email@tgmail.com", "Heikin Ashi is indicating a SELL Signal", emailBodyS);

It can be done inside an Indicator, the Indicator Logic needs to signal once per condition change.... not every Tick or Bar. Hope I haven't added any confusion...


@Symposium