How do I delay a Notifications.SendEmail to be sent?

Created at 22 Apr 2013, 20:58
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!
kricka's avatar

kricka

Joined 13.03.2013

How do I delay a Notifications.SendEmail to be sent?
22 Apr 2013, 20:58


Hi,

When sending an email after closing  a position the Account.Balance do not have enough time to be updated so the email contains old account balance info. The email is sent off too fast. Have tried with  System Threading sleep but it wont work because it halts the account balance update. Hope anyone can give a code that take care of the delay. 

Notifications.SendEmail("email1@gmail.com", "email2@gmail.com", "Subject.", " Message" + Account.Balance );

Thanks..


@kricka
Replies

cAlgo_Fanatic
23 May 2013, 14:43

If the Notification is in the OnPositionClosed() event the balance is updated correctly. If you add the Notification after the Trade.Close() then the email will probably be sent before the position is closed. 


@cAlgo_Fanatic

kricka
20 Jun 2013, 02:59

Hi, thanks for your reply.

I've tried both versions with the Notification after and before Trade.Close(position);. Still get the wrong balance info. Seems like its the commision maybe whats holding up the balance update.

I'm testing it on a VPS and the difference is around the commision cost and slippage too. Have a question about Trade.Close(position);, should it be placed at the bottom after every notfication? According to what you wrote it is executed before notifications even if its placed at the bottom.

Thanks..

 

 


@kricka

cAlgo_Fanatic
21 Jun 2013, 09:54

Did you try adding the notification in the OnPositionClosed event?

protected override void OnPositionClosed(Position position)
{
    var balance = Account.Balance;
    Print("{0}", balance);
    Notifications.SendEmail("from_email@somedomain.com", "to_email@somedomain.com", "Position Closed", Account.Balance.ToString());

}




@cAlgo_Fanatic

kricka
22 Jun 2013, 08:48

Hi,

the robot do not place any orders or position, just closing orders. So "foreach (var position in AccountPositions)" and  Trade.Close(position) is what I'm working with to try to solve the issue.

Thanks.. 

 


@kricka

kricka
23 Jun 2013, 23:02

Hi,

I have also noticed when the email is delivered to the email addresses there is several copies of the same email. I'm using Account.Equity as a trigger when I Trade.Close(position). Any suggestions how to avoid several emails to be sent off?

Thanks..


@kricka

cAlgo_Fanatic
24 Jun 2013, 14:30

The following code maybe what you need or point you in the right direction. We will investigate any issues regarding duplicate emails sent.

The logic of the following code is that it keeps a list of all the positions in the account which is updated on each tick.
If a position that existed in the previous tick is not found an email is sent and the position is removed from the list.

// -------------------------------------------------------------------------------
//
//      This is a Template used as a guideline to build your own Robot. 
//      Please use the “Feedback” tab to provide us with your suggestions about cAlgo’s API.
//
//      The logic of the following code is that it keeps a list of all the positions in the account 
//      which is updated on each tick.
//      If a position that existed in the previous tick is not found an email is sent with the balance info
//      and the position is removed from the list.
//
// -------------------------------------------------------------------------------

using System;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Requests;
using cAlgo.Indicators;
using System.Collections.Generic;
using System.Linq;

namespace cAlgo.Robots
{
    [Robot]
    public class NewRobot : Robot
    {
        private List<Position> _listPosition = new List<Position>();
        private List<Position> _listPositionToBeRemoved = new List<Position>();
                
        protected override void OnStart()
        {
            foreach (Position pos in Account.Positions)
                _listPosition.Add(pos);     
        }

        protected override void OnTick()
        {
            // Put your core logic here
            
            foreach (var position in _listPosition)
            {
                if (Account.Positions.Contains(position))
                    continue;
                Notifications.SendEmail("...", "...", "Position Closed", Account.Balance.ToString());                
                
                _listPositionToBeRemoved.Add(position);
                
            }     
            
            UpdateList();            
        }
        
        private void UpdateList()
        {
            foreach (var position in _listPositionToBeRemoved)
                _listPosition.Remove(position);

            foreach (var position in Account.Positions)
            {
                if (_listPosition.Contains(position))
                    continue;
                _listPosition.Add(position);
            }
        }

        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }               
    }
}






@cAlgo_Fanatic