QuickStart - Notifications

Spotware's avatar

Spotware since: 23 Sep 2013;

  15 Jan 2014, 12:10
QuickStart - Notifications
// ---------------------------------------------------------------------------------------
//
//    This cBot is intended to demonstrate the use of Notifications
//
//    The "Sell Order Notification Robot" will execute a sell market order upon start up with SL and TP. 
//    On the next tick after each position is closed it will execute another and send a notification.
//
// ---------------------------------------------------------------------------------------

using cAlgo.API;

namespace cAlgo.Robots
{
    [Robot]
    public class SellOrderNotification : Robot
    {
        [Parameter(DefaultValue = "C:/Windows/Media/notify.wav")]
        public string SoundFile { get; set; }

        [Parameter(DefaultValue = 10000, MinValue = 1000)]
        public int Volume { get; set; }

        [Parameter(DefaultValue = "Sell Order Robot")]
        public string cBotLabel { get; set; }

        [Parameter("Stop Loss", DefaultValue = 10, MinValue = 1)]
        public int StopLossPips { get; set; }

        [Parameter("Take Profit", DefaultValue = 10, MinValue = 1)]
        public int TakeProfitPips { get; set; }

        protected override void OnStart()
        {
            var result = ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, cBotLabel, 
                                                        StopLossPips, TakeProfitPips);
            if(result.IsSuccessful)
            {
                var position = result.Position;
                Print("Position {0} opened at {1}", position.Label, position.EntryPrice);
                SendNotifications(position);
            }
        }

        private void SendNotifications(Position position)
        {
            var emailBody = string.Format("{0} Order Created at {1}", position.TradeType, position.EntryPrice);
            Notifications.SendEmail("from@somewhere.com", "to@somewhere.com", "Order Created", emailBody);
            Notifications.PlaySound(SoundFile);
        }

        protected override void OnTick()
        {            
            var position = Positions.Find(cBotLabel);
            
            if (position != null)
                return;
            
            var result = ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, cBotLabel, 
                                                StopLossPips, TakeProfitPips);

            if (result.IsSuccessful)
            {
                position = result.Position;
                SendNotifications(position);
            }
        }        
    }
}
TRADERS FIRSTâ„¢ Vote for your favorite features: https://ctrader.com/forum/suggestions