new trading with trailing stop loss

Created at 30 Oct 2017, 17:13
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!
JO

john123321

Joined 30.10.2017

new trading with trailing stop loss
30 Oct 2017, 17:13


/algos/cbots/show/257

hi in the above link the c algo published news trading with trailing stop loss is not working can you look into it.

its a lot of inconvenience for c tradr users

we want it to be fixed soon.please.

give us proper support

 


@john123321
Replies

tradermatrix
30 Oct 2017, 21:36

Hello
now it should work

using System;
using cAlgo.API;

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.None)]
    public class TradingNewsRobot : Robot
    {
        private PendingOrder _buyOrder;
        private bool _ordersCreated;
        private PendingOrder _sellOrder;
        private Position position;

        [Parameter("News Day (1-5)", DefaultValue = 1, MinValue = 1, MaxValue = 5)]
        public int NewsDay { get; set; }

        [Parameter("News Hour", DefaultValue = 14, MinValue = 0, MaxValue = 23)]
        public int NewsHour { get; set; }

        [Parameter("News Minute", DefaultValue = 30, MinValue = 0, MaxValue = 59)]
        public int NewsMinute { get; set; }

        [Parameter("Seconds Before", DefaultValue = 5, MinValue = 1)]
        public int SecondsBefore { get; set; }

        [Parameter("Seconds Timeout", DefaultValue = 10, MinValue = 1)]
        public int SecondsTimeout { get; set; }

        [Parameter("One Cancels Other", DefaultValue = 1, MinValue = 0, MaxValue = 1)]
        public int Oco { get; set; }

        [Parameter("Pips away", DefaultValue = 10)]
        public int PipsAway { get; set; }

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

        [Parameter("Take Profit", DefaultValue = 80)]
        public int TakeProfit { get; set; }

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

        [Parameter("Trigger (pips)    if > 0", DefaultValue = 0)]
        public int Trigger { get; set; }

        [Parameter("Trailing Stop (pips)   if > 0 ", DefaultValue = 0)]
        public int TrailingStop { get; set; }


        protected override void OnStart()
        {
            MarketData.GetMarketDepth(Symbol).Updated += MarketDepth_Updated;
        }

        protected override void OnTick()
        {
            if (position == null)
                return;

            TRAILING();
        }
        private void TRAILING()
        {


            foreach (var position in Positions)
            {
                if (TrailingStop > 0 && Trigger > 0)
                {


                    if (position.TradeType == TradeType.Sell)
                    {

                        double distance = position.EntryPrice - Symbol.Ask;


                        if (distance >= Trigger * Symbol.PipSize)
                        {

                            double newStopLossPrice = Symbol.Ask + TrailingStop * Symbol.PipSize;


                            if (position.StopLoss == null || newStopLossPrice < position.StopLoss)
                            {

                                ModifyPosition(position, newStopLossPrice, position.TakeProfit);

                            }
                        }
                    }


                    else
                    {

                        double distance = Symbol.Bid - position.EntryPrice;


                        if (distance >= Trigger * Symbol.PipSize)
                        {

                            double newStopLossPrice = Symbol.Bid - TrailingStop * Symbol.PipSize;


                            if (position.StopLoss == null || newStopLossPrice > position.StopLoss)
                            {

                                ModifyPosition(position, newStopLossPrice, position.TakeProfit);

                            }
                        }
                    }
                }
            }
        }

        private void MarketDepth_Updated()
        {
            if ((int)Server.Time.DayOfWeek == NewsDay && !_ordersCreated)
            {
                var triggerTime = new DateTime(Server.Time.Year, Server.Time.Month, Server.Time.Day, NewsHour, NewsMinute, 0);

                if (Server.Time <= triggerTime && (triggerTime - Server.Time).TotalSeconds <= SecondsBefore)
                {
                    _ordersCreated = true;
                    DateTime expirationTime = triggerTime.AddSeconds(SecondsTimeout);

                    double sellOrderTargetPrice = Symbol.Bid - PipsAway * Symbol.PipSize;
                    Trade.CreateSellStopOrder(Symbol, Volume, sellOrderTargetPrice, sellOrderTargetPrice + StopLoss * Symbol.PipSize, sellOrderTargetPrice - TakeProfit * Symbol.PipSize, expirationTime);

                    double buyOrderTargetPrice = Symbol.Ask + PipsAway * Symbol.PipSize;
                    Trade.CreateBuyStopOrder(Symbol, Volume, buyOrderTargetPrice, buyOrderTargetPrice - StopLoss * Symbol.PipSize, buyOrderTargetPrice + TakeProfit * Symbol.PipSize, expirationTime);
                }
            }
        }

        protected override void OnPendingOrderCreated(PendingOrder newOrder)
        {
            if (newOrder.TradeType == TradeType.Buy)
                _buyOrder = newOrder;
            else
                _sellOrder = newOrder;
        }

        protected override void OnPositionOpened(Position openedPosition)
        {
            position = openedPosition;
            if (Oco == 1)
            {
                Trade.DeletePendingOrder(_buyOrder);
                Trade.DeletePendingOrder(_sellOrder);
                _ordersCreated = false;
            }
        }

        protected override void OnPositionClosed(Position closedPosition)
        {
            position = null;
        }
    }
}

 


@tradermatrix

john123321
30 Oct 2017, 22:37

thanks for your time and effort but still not working

tradermatrix said:

Hello
now it should work

using System;
using cAlgo.API;

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.None)]
    public class TradingNewsRobot : Robot
    {
        private PendingOrder _buyOrder;
        private bool _ordersCreated;
        private PendingOrder _sellOrder;
        private Position position;

        [Parameter("News Day (1-5)", DefaultValue = 1, MinValue = 1, MaxValue = 5)]
        public int NewsDay { get; set; }

        [Parameter("News Hour", DefaultValue = 14, MinValue = 0, MaxValue = 23)]
        public int NewsHour { get; set; }

        [Parameter("News Minute", DefaultValue = 30, MinValue = 0, MaxValue = 59)]
        public int NewsMinute { get; set; }

        [Parameter("Seconds Before", DefaultValue = 5, MinValue = 1)]
        public int SecondsBefore { get; set; }

        [Parameter("Seconds Timeout", DefaultValue = 10, MinValue = 1)]
        public int SecondsTimeout { get; set; }

        [Parameter("One Cancels Other", DefaultValue = 1, MinValue = 0, MaxValue = 1)]
        public int Oco { get; set; }

        [Parameter("Pips away", DefaultValue = 10)]
        public int PipsAway { get; set; }

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

        [Parameter("Take Profit", DefaultValue = 80)]
        public int TakeProfit { get; set; }

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

        [Parameter("Trigger (pips)    if > 0", DefaultValue = 0)]
        public int Trigger { get; set; }

        [Parameter("Trailing Stop (pips)   if > 0 ", DefaultValue = 0)]
        public int TrailingStop { get; set; }


        protected override void OnStart()
        {
            MarketData.GetMarketDepth(Symbol).Updated += MarketDepth_Updated;
        }

        protected override void OnTick()
        {
            if (position == null)
                return;

            TRAILING();
        }
        private void TRAILING()
        {


            foreach (var position in Positions)
            {
                if (TrailingStop > 0 && Trigger > 0)
                {


                    if (position.TradeType == TradeType.Sell)
                    {

                        double distance = position.EntryPrice - Symbol.Ask;


                        if (distance >= Trigger * Symbol.PipSize)
                        {

                            double newStopLossPrice = Symbol.Ask + TrailingStop * Symbol.PipSize;


                            if (position.StopLoss == null || newStopLossPrice < position.StopLoss)
                            {

                                ModifyPosition(position, newStopLossPrice, position.TakeProfit);

                            }
                        }
                    }


                    else
                    {

                        double distance = Symbol.Bid - position.EntryPrice;


                        if (distance >= Trigger * Symbol.PipSize)
                        {

                            double newStopLossPrice = Symbol.Bid - TrailingStop * Symbol.PipSize;


                            if (position.StopLoss == null || newStopLossPrice > position.StopLoss)
                            {

                                ModifyPosition(position, newStopLossPrice, position.TakeProfit);

                            }
                        }
                    }
                }
            }
        }

        private void MarketDepth_Updated()
        {
            if ((int)Server.Time.DayOfWeek == NewsDay && !_ordersCreated)
            {
                var triggerTime = new DateTime(Server.Time.Year, Server.Time.Month, Server.Time.Day, NewsHour, NewsMinute, 0);

                if (Server.Time <= triggerTime && (triggerTime - Server.Time).TotalSeconds <= SecondsBefore)
                {
                    _ordersCreated = true;
                    DateTime expirationTime = triggerTime.AddSeconds(SecondsTimeout);

                    double sellOrderTargetPrice = Symbol.Bid - PipsAway * Symbol.PipSize;
                    Trade.CreateSellStopOrder(Symbol, Volume, sellOrderTargetPrice, sellOrderTargetPrice + StopLoss * Symbol.PipSize, sellOrderTargetPrice - TakeProfit * Symbol.PipSize, expirationTime);

                    double buyOrderTargetPrice = Symbol.Ask + PipsAway * Symbol.PipSize;
                    Trade.CreateBuyStopOrder(Symbol, Volume, buyOrderTargetPrice, buyOrderTargetPrice - StopLoss * Symbol.PipSize, buyOrderTargetPrice + TakeProfit * Symbol.PipSize, expirationTime);
                }
            }
        }

        protected override void OnPendingOrderCreated(PendingOrder newOrder)
        {
            if (newOrder.TradeType == TradeType.Buy)
                _buyOrder = newOrder;
            else
                _sellOrder = newOrder;
        }

        protected override void OnPositionOpened(Position openedPosition)
        {
            position = openedPosition;
            if (Oco == 1)
            {
                Trade.DeletePendingOrder(_buyOrder);
                Trade.DeletePendingOrder(_sellOrder);
                _ordersCreated = false;
            }
        }

        protected override void OnPositionClosed(Position closedPosition)
        {
            position = null;
        }
    }
}

Hi bro.

i really appreciate your time and effort but still it does not place order and does not work...i request you to please look into it and help me.

The following news robot works /algos/cbots/show/199  but it does not have trailling stop triger and trail pips option.

please help me in order to have trailing stop triger pips and trail stop by option to work.

thanks you so much

 

 


@john123321

tradermatrix
31 Oct 2017, 15:11

 

HI

look at this one it is more efficient and the trailingstop works

using System;
using System.Linq;
using System.Text;
using cAlgo.API;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewsScalp : Robot
    {
        [Parameter("News Day (1-5)", DefaultValue = 1, MinValue = 1, MaxValue = 5)]
        public int NewsDay { get; set; }

        [Parameter("News Hour", DefaultValue = 14, MinValue = 0, MaxValue = 23)]
        public int NewsHour { get; set; }

        [Parameter("News Minute", DefaultValue = 30, MinValue = 0, MaxValue = 59)]
        public int NewsMinute { get; set; }

        [Parameter("Seconds Before", DefaultValue = 10, MinValue = 1)]
        public int SecondsBefore { get; set; }

        [Parameter("Seconds Timeout", DefaultValue = 10, MinValue = 1)]
        public int SecondsTimeout { get; set; }

        [Parameter("One Cancels Other")]
        public bool Oco { get; set; }

        [Parameter("ShowTimeLeftNews", DefaultValue = true)]
        public bool ShowTimeLeftToNews { get; set; }

        [Parameter("ShowTimeLeftPlaceOrders", DefaultValue = true)]
        public bool ShowTimeLeftToPlaceOrders { get; set; }

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

        [Parameter("Pips away", DefaultValue = 3)]
        public int PipsAway { get; set; }

        [Parameter("Take Profit", DefaultValue = 80)]
        public int TakeProfit { get; set; }

        [Parameter("Stop Loss", DefaultValue = 20)]
        public int StopLoss { get; set; }

        [Parameter("trigger ", DefaultValue = 0)]
        public int Trigger { get; set; }

        [Parameter("Trailing", DefaultValue = 10)]
        public int Trailing { get; set; }



        private bool _ordersCreated;

        private DateTime _triggerTimeInServerTimeZone;

        private const string Label = "News Robot";

        protected override void OnStart()
        {
            Positions.Opened += OnPositionOpened;
            Timer.Start(1);

            var triggerTimeInLocalTimeZone = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, NewsHour, NewsMinute, 0);
            if (triggerTimeInLocalTimeZone < DateTime.Now)
                triggerTimeInLocalTimeZone = triggerTimeInLocalTimeZone.AddDays(1);
            _triggerTimeInServerTimeZone = TimeZoneInfo.ConvertTime(triggerTimeInLocalTimeZone, TimeZoneInfo.Local, TimeZone);
        }

        protected override void OnTick()
        {
            TRAILING();
        }

        private void TRAILING()
        {


            Position[] positions = Positions.FindAll(Label, Symbol);

            foreach (Position position in positions)
            {

                if (position.TradeType == TradeType.Sell)
                {

                    double distance = position.EntryPrice - Symbol.Ask;

                    if (distance >= Trigger * Symbol.PipSize)
                    {

                        double newStopLossPrice = Symbol.Ask + Trailing * Symbol.PipSize;

                        if (position.StopLoss == null || newStopLossPrice < position.StopLoss)
                        {

                            ModifyPosition(position, newStopLossPrice, position.TakeProfit);

                        }
                    }
                }

                else
                {

                    double distance = Symbol.Bid - position.EntryPrice;

                    if (distance >= Trigger * Symbol.PipSize)
                    {

                        double newStopLossPrice = Symbol.Bid - Trailing * Symbol.PipSize;

                        if (position.StopLoss == null || newStopLossPrice > position.StopLoss)
                        {

                            ModifyPosition(position, newStopLossPrice, position.TakeProfit);

                        }
                    }
                }
            }
        }

        protected override void OnTimer()
        {
            if ((int)Server.Time.DayOfWeek == NewsDay && !_ordersCreated)
            {
                var remainingTime = _triggerTimeInServerTimeZone - Server.Time;
                DrawRemainingTime(remainingTime);

                if (!_ordersCreated)
                {
                    var sellOrderTargetPrice = Symbol.Bid - PipsAway * Symbol.PipSize;
                    ChartObjects.DrawHorizontalLine("sell target", sellOrderTargetPrice, Colors.Red, 1, LineStyle.DotsVeryRare);
                    var buyOrderTargetPrice = Symbol.Ask + PipsAway * Symbol.PipSize;
                    ChartObjects.DrawHorizontalLine("buy target", buyOrderTargetPrice, Colors.Blue, 1, LineStyle.DotsVeryRare);

                    if (Server.Time <= _triggerTimeInServerTimeZone && (_triggerTimeInServerTimeZone - Server.Time).TotalSeconds <= SecondsBefore)
                    {
                        _ordersCreated = true;
                        var expirationTime = _triggerTimeInServerTimeZone.AddSeconds(SecondsTimeout);

                        PlaceStopOrder(TradeType.Sell, Symbol, Volume, sellOrderTargetPrice, Label, StopLoss, TakeProfit, expirationTime);
                        PlaceStopOrder(TradeType.Buy, Symbol, Volume, buyOrderTargetPrice, Label, StopLoss, TakeProfit, expirationTime);

                        ChartObjects.RemoveObject("sell target");
                        ChartObjects.RemoveObject("buy target");
                    }
                }

                if (_ordersCreated && !PendingOrders.Any(o => o.Label == Label))
                {
                    Print("Orders expired");
                    return;
                }
            }
        }

        private void DrawRemainingTime(TimeSpan remainingTimeToNews)
        {
            if (ShowTimeLeftToNews)
            {
                if (remainingTimeToNews > TimeSpan.Zero)
                {
                    ChartObjects.DrawText("countdown1", "Time left to news: " + FormatTime(remainingTimeToNews), StaticPosition.TopLeft);
                }
                else
                {
                    ChartObjects.RemoveObject("countdown1");
                }
            }
            if (ShowTimeLeftToPlaceOrders)
            {
                var remainingTimeToOrders = remainingTimeToNews - TimeSpan.FromSeconds(SecondsBefore);
                if (remainingTimeToOrders > TimeSpan.Zero)
                {
                    ChartObjects.DrawText("countdown2", "Time left to place orders: " + FormatTime(remainingTimeToOrders), StaticPosition.TopRight);
                }
                else
                {
                    ChartObjects.RemoveObject("countdown2");
                }
            }
        }

        private static StringBuilder FormatTime(TimeSpan remainingTime)
        {
            var remainingTimeStr = new StringBuilder();
            if (remainingTime.TotalHours >= 1)
                remainingTimeStr.Append((int)remainingTime.TotalHours + "h ");
            if (remainingTime.TotalMinutes >= 1)
                remainingTimeStr.Append(remainingTime.Minutes + "m ");
            if (remainingTime.TotalSeconds > 0)
                remainingTimeStr.Append(remainingTime.Seconds + "s");
            return remainingTimeStr;
        }

        private void OnPositionOpened(PositionOpenedEventArgs args)
        {
            var position = args.Position;
            if (position.Label == Label && position.SymbolCode == Symbol.Code)
            {
                if (Oco)
                {
                    foreach (var order in PendingOrders)
                    {
                        if (order.Label == Label && order.SymbolCode == Symbol.Code)
                        {
                            CancelPendingOrderAsync(order);
                        }
                    }
                }
                return;
            }
        }
    }
}


 


@tradermatrix

john123321
02 Nov 2017, 14:59

tradermatrix thanks

Hi,

 

I tested on a demo account in normal market itseems to work fine. thank you so much...but during news event the platform hangs while using this algo...will try again for few more news events..i will keep you posted....i appreciate your help a lot....may god bless you...tcare..i wish you all the best for your life....


@john123321

swervehomez@gmail.com
05 Nov 2017, 23:14

RE: tradermatrix thanks

john123321 said:

Hi,

 

I tested on a demo account in normal market itseems to work fine. thank you so much...but during news event the platform hangs while using this algo...will try again for few more news events..i will keep you posted....i appreciate your help a lot....may god bless you...tcare..i wish you all the best for your life....

Hallo, does it really work? It's still not showing time remaining and doesn't seem to work. Is it still working for you?

 


@swervehomez@gmail.com

swervehomez@gmail.com
05 Nov 2017, 23:33

RE:

tradermatrix said:

 

HI

look at this one it is more efficient and the trailingstop works

using System;
using System.Linq;
using System.Text;
using cAlgo.API;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewsScalp : Robot
    {
        [Parameter("News Day (1-5)", DefaultValue = 1, MinValue = 1, MaxValue = 5)]
        public int NewsDay { get; set; }

        [Parameter("News Hour", DefaultValue = 14, MinValue = 0, MaxValue = 23)]
        public int NewsHour { get; set; }

        [Parameter("News Minute", DefaultValue = 30, MinValue = 0, MaxValue = 59)]
        public int NewsMinute { get; set; }

        [Parameter("Seconds Before", DefaultValue = 10, MinValue = 1)]
        public int SecondsBefore { get; set; }

        [Parameter("Seconds Timeout", DefaultValue = 10, MinValue = 1)]
        public int SecondsTimeout { get; set; }

        [Parameter("One Cancels Other")]
        public bool Oco { get; set; }

        [Parameter("ShowTimeLeftNews", DefaultValue = true)]
        public bool ShowTimeLeftToNews { get; set; }

        [Parameter("ShowTimeLeftPlaceOrders", DefaultValue = true)]
        public bool ShowTimeLeftToPlaceOrders { get; set; }

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

        [Parameter("Pips away", DefaultValue = 3)]
        public int PipsAway { get; set; }

        [Parameter("Take Profit", DefaultValue = 80)]
        public int TakeProfit { get; set; }

        [Parameter("Stop Loss", DefaultValue = 20)]
        public int StopLoss { get; set; }

        [Parameter("trigger ", DefaultValue = 0)]
        public int Trigger { get; set; }

        [Parameter("Trailing", DefaultValue = 10)]
        public int Trailing { get; set; }



        private bool _ordersCreated;

        private DateTime _triggerTimeInServerTimeZone;

        private const string Label = "News Robot";

        protected override void OnStart()
        {
            Positions.Opened += OnPositionOpened;
            Timer.Start(1);

            var triggerTimeInLocalTimeZone = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, NewsHour, NewsMinute, 0);
            if (triggerTimeInLocalTimeZone < DateTime.Now)
                triggerTimeInLocalTimeZone = triggerTimeInLocalTimeZone.AddDays(1);
            _triggerTimeInServerTimeZone = TimeZoneInfo.ConvertTime(triggerTimeInLocalTimeZone, TimeZoneInfo.Local, TimeZone);
        }

        protected override void OnTick()
        {
            TRAILING();
        }

        private void TRAILING()
        {


            Position[] positions = Positions.FindAll(Label, Symbol);

            foreach (Position position in positions)
            {

                if (position.TradeType == TradeType.Sell)
                {

                    double distance = position.EntryPrice - Symbol.Ask;

                    if (distance >= Trigger * Symbol.PipSize)
                    {

                        double newStopLossPrice = Symbol.Ask + Trailing * Symbol.PipSize;

                        if (position.StopLoss == null || newStopLossPrice < position.StopLoss)
                        {

                            ModifyPosition(position, newStopLossPrice, position.TakeProfit);

                        }
                    }
                }

                else
                {

                    double distance = Symbol.Bid - position.EntryPrice;

                    if (distance >= Trigger * Symbol.PipSize)
                    {

                        double newStopLossPrice = Symbol.Bid - Trailing * Symbol.PipSize;

                        if (position.StopLoss == null || newStopLossPrice > position.StopLoss)
                        {

                            ModifyPosition(position, newStopLossPrice, position.TakeProfit);

                        }
                    }
                }
            }
        }

        protected override void OnTimer()
        {
            if ((int)Server.Time.DayOfWeek == NewsDay && !_ordersCreated)
            {
                var remainingTime = _triggerTimeInServerTimeZone - Server.Time;
                DrawRemainingTime(remainingTime);

                if (!_ordersCreated)
                {
                    var sellOrderTargetPrice = Symbol.Bid - PipsAway * Symbol.PipSize;
                    ChartObjects.DrawHorizontalLine("sell target", sellOrderTargetPrice, Colors.Red, 1, LineStyle.DotsVeryRare);
                    var buyOrderTargetPrice = Symbol.Ask + PipsAway * Symbol.PipSize;
                    ChartObjects.DrawHorizontalLine("buy target", buyOrderTargetPrice, Colors.Blue, 1, LineStyle.DotsVeryRare);

                    if (Server.Time <= _triggerTimeInServerTimeZone && (_triggerTimeInServerTimeZone - Server.Time).TotalSeconds <= SecondsBefore)
                    {
                        _ordersCreated = true;
                        var expirationTime = _triggerTimeInServerTimeZone.AddSeconds(SecondsTimeout);

                        PlaceStopOrder(TradeType.Sell, Symbol, Volume, sellOrderTargetPrice, Label, StopLoss, TakeProfit, expirationTime);
                        PlaceStopOrder(TradeType.Buy, Symbol, Volume, buyOrderTargetPrice, Label, StopLoss, TakeProfit, expirationTime);

                        ChartObjects.RemoveObject("sell target");
                        ChartObjects.RemoveObject("buy target");
                    }
                }

                if (_ordersCreated && !PendingOrders.Any(o => o.Label == Label))
                {
                    Print("Orders expired");
                    return;
                }
            }
        }

        private void DrawRemainingTime(TimeSpan remainingTimeToNews)
        {
            if (ShowTimeLeftToNews)
            {
                if (remainingTimeToNews > TimeSpan.Zero)
                {
                    ChartObjects.DrawText("countdown1", "Time left to news: " + FormatTime(remainingTimeToNews), StaticPosition.TopLeft);
                }
                else
                {
                    ChartObjects.RemoveObject("countdown1");
                }
            }
            if (ShowTimeLeftToPlaceOrders)
            {
                var remainingTimeToOrders = remainingTimeToNews - TimeSpan.FromSeconds(SecondsBefore);
                if (remainingTimeToOrders > TimeSpan.Zero)
                {
                    ChartObjects.DrawText("countdown2", "Time left to place orders: " + FormatTime(remainingTimeToOrders), StaticPosition.TopRight);
                }
                else
                {
                    ChartObjects.RemoveObject("countdown2");
                }
            }
        }

        private static StringBuilder FormatTime(TimeSpan remainingTime)
        {
            var remainingTimeStr = new StringBuilder();
            if (remainingTime.TotalHours >= 1)
                remainingTimeStr.Append((int)remainingTime.TotalHours + "h ");
            if (remainingTime.TotalMinutes >= 1)
                remainingTimeStr.Append(remainingTime.Minutes + "m ");
            if (remainingTime.TotalSeconds > 0)
                remainingTimeStr.Append(remainingTime.Seconds + "s");
            return remainingTimeStr;
        }

        private void OnPositionOpened(PositionOpenedEventArgs args)
        {
            var position = args.Position;
            if (position.Label == Label && position.SymbolCode == Symbol.Code)
            {
                if (Oco)
                {
                    foreach (var order in PendingOrders)
                    {
                        if (order.Label == Label && order.SymbolCode == Symbol.Code)
                        {
                            CancelPendingOrderAsync(order);
                        }
                    }
                }
                return;
            }
        }
    }
}



Hallo, is it possible to add the trigger pips and trailing stop loss on the following as that algo doesn't work. Even the original one doesn't work. This one works, but doesn't have the trailing stop loss. 

using System;
using System.Linq;
using System.Text;
using cAlgo.API;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewsRobot : Robot
    {
        [Parameter("News Hour", DefaultValue = 14, MinValue = 0, MaxValue = 23)]
        public int NewsHour { get; set; }

        [Parameter("News Minute", DefaultValue = 30, MinValue = 0, MaxValue = 59)]
        public int NewsMinute { get; set; }

        [Parameter("Pips away", DefaultValue = 10)]
        public int PipsAway { get; set; }

        [Parameter("Take Profit", DefaultValue = 50)]
        public int TakeProfit { get; set; }

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

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

        [Parameter("Seconds Before", DefaultValue = 10, MinValue = 1)]
        public int SecondsBefore { get; set; }

        [Parameter("Seconds Timeout", DefaultValue = 10, MinValue = 1)]
        public int SecondsTimeout { get; set; }

        [Parameter("One Cancels Other")]
        public bool Oco { get; set; }

        [Parameter("ShowTimeLeftNews", DefaultValue = false)]
        public bool ShowTimeLeftToNews { get; set; }

        [Parameter("ShowTimeLeftPlaceOrders", DefaultValue = true)]
        public bool ShowTimeLeftToPlaceOrders { get; set; }

        private bool _ordersCreated;

        private DateTime _triggerTimeInServerTimeZone;

        private const string Label = "News Robot";

        protected override void OnStart()
        {
            Positions.Opened += OnPositionOpened;
            Timer.Start(1);

            var triggerTimeInLocalTimeZone = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, NewsHour, NewsMinute, 0);
            if (triggerTimeInLocalTimeZone < DateTime.Now)
                triggerTimeInLocalTimeZone = triggerTimeInLocalTimeZone.AddDays(1);
            _triggerTimeInServerTimeZone = TimeZoneInfo.ConvertTime(triggerTimeInLocalTimeZone, TimeZoneInfo.Local, TimeZone);
        }

        protected override void OnTimer()
        {
            var remainingTime = _triggerTimeInServerTimeZone - Server.Time;
            DrawRemainingTime(remainingTime);

            if (!_ordersCreated)
            {
                var sellOrderTargetPrice = Symbol.Bid - PipsAway * Symbol.PipSize;
                ChartObjects.DrawHorizontalLine("sell target", sellOrderTargetPrice, Colors.Red, 1, LineStyle.DotsVeryRare);
                var buyOrderTargetPrice = Symbol.Ask + PipsAway * Symbol.PipSize;
                ChartObjects.DrawHorizontalLine("buy target", buyOrderTargetPrice, Colors.Blue, 1, LineStyle.DotsVeryRare);

                if (Server.Time <= _triggerTimeInServerTimeZone && (_triggerTimeInServerTimeZone - Server.Time).TotalSeconds <= SecondsBefore)
                {
                    _ordersCreated = true;
                    var expirationTime = _triggerTimeInServerTimeZone.AddSeconds(SecondsTimeout);

                    PlaceStopOrder(TradeType.Sell, Symbol, Volume, sellOrderTargetPrice, Label, StopLoss, TakeProfit, expirationTime);
                    PlaceStopOrder(TradeType.Buy, Symbol, Volume, buyOrderTargetPrice, Label, StopLoss, TakeProfit, expirationTime);

                    ChartObjects.RemoveObject("sell target");
                    ChartObjects.RemoveObject("buy target");
                }
            }

            if (_ordersCreated && !PendingOrders.Any(o => o.Label == Label))
            {
                Print("Orders expired");
                Stop();
            }
        }

        private void DrawRemainingTime(TimeSpan remainingTimeToNews)
        {
            if (ShowTimeLeftToNews)
            {
                if (remainingTimeToNews > TimeSpan.Zero)
                {
                    ChartObjects.DrawText("countdown1", "Time left to news: " + FormatTime(remainingTimeToNews), StaticPosition.TopLeft);
                }
                else
                {
                    ChartObjects.RemoveObject("countdown1");
                }
            }
            if (ShowTimeLeftToPlaceOrders)
            {
                var remainingTimeToOrders = remainingTimeToNews - TimeSpan.FromSeconds(SecondsBefore);
                if (remainingTimeToOrders > TimeSpan.Zero)
                {
                    ChartObjects.DrawText("countdown2", "Time left to place orders: " + FormatTime(remainingTimeToOrders), StaticPosition.TopRight);
                }
                else
                {
                    ChartObjects.RemoveObject("countdown2");
                }
            }
        }

        private static StringBuilder FormatTime(TimeSpan remainingTime)
        {
            var remainingTimeStr = new StringBuilder();
            if (remainingTime.TotalHours >= 1)
                remainingTimeStr.Append((int)remainingTime.TotalHours + "h ");
            if (remainingTime.TotalMinutes >= 1)
                remainingTimeStr.Append(remainingTime.Minutes + "m ");
            if (remainingTime.TotalSeconds > 0)
                remainingTimeStr.Append(remainingTime.Seconds + "s");
            return remainingTimeStr;
        }

        private void OnPositionOpened(PositionOpenedEventArgs args)
        {
            var position = args.Position;
            if (position.Label == Label && position.SymbolCode == Symbol.Code)
            {
                if (Oco)
                {
                    foreach (var order in PendingOrders)
                    {
                        if (order.Label == Label && order.SymbolCode == Symbol.Code)
                        {
                            CancelPendingOrderAsync(order);
                        }
                    }
                }
                Stop();
            }
        }
    }
}

 


@swervehomez@gmail.com