Using Stop Limit orders

Created at 09 Mar 2022, 12:35
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!
IS

isabakas11

Joined 03.03.2022

Using Stop Limit orders
09 Mar 2022, 12:35


Hi all,

My bot should do the following. Every New York Open it should place a Stop Limit order both to the the buy and sell side. Also the orders should expire the same day, ensuring the bot places a trade every day. However when I backtest, the bot doesn't place a trade everyday. Any idea why?

source code:

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 NYOpen : Robot
    {
        [Parameter("Entry Time (Hours)", DefaultValue = 14)]
        public double EntryTime { get; set; }

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

        [Parameter("Quantity (Lots)", Group = "Volume", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
        public double Quantity { get; set; }

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

        [Parameter("Take Profit (Pips)", DefaultValue = 10)]
        public int TP { get; set; }

        protected override void OnStart()
        {

        }

        protected override void OnTick()
        {
            var Hour = Server.Time.TimeOfDay.TotalHours;

            //Enter Trade

            if (Hour == EntryTime)
            {
                PlaceStopLimitOrder(TradeType.Buy, SymbolName, VolumeInUnits, Symbol.Ask + (Entry * Symbol.PipSize), 0.0, "NY Open", SL, TP, Server.Time.AddHours(12));
                PlaceStopLimitOrder(TradeType.Sell, SymbolName, VolumeInUnits, Symbol.Bid - (Entry * Symbol.PipSize), 0.0, "NY Open", SL, TP, Server.Time.AddHours(12));
            }

            //Close Trades

        }

        private double VolumeInUnits
        {
            get { return Symbol.QuantityToVolumeInUnits(Quantity); }
        }

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


@isabakas11
Replies

amusleh
10 Mar 2022, 10:21

Hi,

Try this:

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 NYOpen : Robot
    {
        [Parameter("Entry Time (Hours)", DefaultValue = 14)]
        public double EntryTime { get; set; }

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

        [Parameter("Quantity (Lots)", Group = "Volume", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
        public double Quantity { get; set; }

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

        [Parameter("Take Profit (Pips)", DefaultValue = 10)]
        public int TP { get; set; }

        protected override void OnStart()
        {
            Timer.Start(TimeSpan.FromHours(1));
        }

        protected override void OnTimer()
        {
            if (Server.Time.TimeOfDay.Hours != EntryTime) return;

            PlaceStopLimitOrder(TradeType.Buy, SymbolName, VolumeInUnits, Symbol.Ask + (Entry * Symbol.PipSize), 0.0, "NY Open", SL, TP, Server.Time.AddHours(12));
            PlaceStopLimitOrder(TradeType.Sell, SymbolName, VolumeInUnits, Symbol.Bid - (Entry * Symbol.PipSize), 0.0, "NY Open", SL, TP, Server.Time.AddHours(12));
        }

        protected override void OnTick()
        {
            //Close Trades
        }

        private double VolumeInUnits
        {
            get { return Symbol.QuantityToVolumeInUnits(Quantity); }
        }

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

 


@amusleh