Closing each opened trade after x amount of time

Created at 09 Apr 2020, 22:52
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!
AL

alisalehicontact

Joined 24.08.2019

Closing each opened trade after x amount of time
09 Apr 2020, 22:52


Hi everyone,I have made this  simple bot a while ago with help  of ctrader community and sample bots  because I dont have any knowledge in coding it was really hard for me but good thing is i am learing in this way,now i have one question wich I can't solve by my self so i really need your help,I need the bot to close each trade after for example 15 min from the time it has been opened

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 Cowabungastratey : Robot
    {
        [Parameter("Instance Name", DefaultValue = "001")]
        public string InstanceName { get; set; }

        [Parameter("Lot Size", DefaultValue = 0.1)]
        public double LotSize { get; set; }

        [Parameter("Source FastEma")]
        public DataSeries SourceFastEma { get; set; }

        [Parameter("Source SlowEma")]
        public DataSeries SourceSlowEma { get; set; }

        [Parameter("Period FastEma", DefaultValue = 5, MinValue = 1, MaxValue = 100)]
        public int PeriodFastEma { get; set; }

        [Parameter("Period SloeEma", DefaultValue = 10, MinValue = 5, MaxValue = 100)]
        public int PeriodSlowEma { get; set; }

        [Parameter("ATR Value", DefaultValue = 0.00095)]
        public double AtrValue { get; set; }

        [Parameter("Calculate OnBar", DefaultValue = false)]
        public bool CalculateOnBar { get; set; }


        private ExponentialMovingAverage FastEma;

        private ExponentialMovingAverage SlowEma;

        private MarketSeries h4;

        private ExponentialMovingAverage ema1;

        private ExponentialMovingAverage ema2;

        private AverageTrueRange atr;


        protected override void OnStart()
        {
            FastEma = Indicators.ExponentialMovingAverage(SourceFastEma, PeriodFastEma);

            SlowEma = Indicators.ExponentialMovingAverage(SourceSlowEma, PeriodSlowEma);

            h4 = MarketData.GetSeries(TimeFrame.Hour4);

            ema1 = Indicators.ExponentialMovingAverage(h4.High, 12);

            ema2 = Indicators.ExponentialMovingAverage(h4.High, 9);

            atr = Indicators.AverageTrueRange(MarketSeries, 14, MovingAverageType.Exponential);

            // Put your initialization logic here
        }


        protected override void OnTick()
        {
            if (CalculateOnBar)
            {
                return;
            }
            ManagePostions();
        }

        protected override void OnBar()
        {
            if (!CalculateOnBar)
            {
                return;
            }
            ManagePostions();
        }

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

        private void ManagePostions()
        {


            if (SlowEma.Result.LastValue < FastEma.Result.LastValue)
            {
                if (atr.Result.LastValue > AtrValue)
                    if (ema2.Result.LastValue > ema1.Result.LastValue)
                        if (!IsPositionOpenByType(TradeType.Buy))
                        {
                            OpenPosition(TradeType.Buy);
                        }

                ClosePosition(TradeType.Sell);
            }


            if (SlowEma.Result.LastValue > FastEma.Result.LastValue)
            {
                if (atr.Result.LastValue > AtrValue)
                    if (ema2.Result.LastValue < ema1.Result.LastValue)
                        if (!IsPositionOpenByType(TradeType.Sell))
                        {
                            OpenPosition(TradeType.Sell);
                        }

                ClosePosition(TradeType.Buy);
            }
        }


        private void OpenPosition(TradeType type)
        {
            double Volume = Symbol.QuantityToVolumeInUnits(LotSize);
            ExecuteMarketOrder(type, this.SymbolName, Volume, InstanceName, null, null);
        }

        private void ClosePosition(TradeType type)
        {
            var P = Positions.Find(InstanceName, this.SymbolName, type);
            if (P != null)
            {
                ClosePosition(P);
            }
        }

        private bool IsPositionOpenByType(TradeType type)
        {
            var P = Positions.FindAll(InstanceName, SymbolName, type);
            if (P.Count() >= 1)
            {
                return true;
            }
            return false;
        }

    }
}

 


@alisalehicontact
Replies

firemyst
30 Aug 2020, 12:54

What you need to do is:

1) get the position open time

2) create a timer object

3) when 15 minutes has passed on the timer object, trigger it to call a method. That method could be the close method, which closes your position.

Examples for the C# timer object:

https://docs.microsoft.com/en-us/dotnet/api/system.timers.timer?view=netcore-3.1

and

https://stackoverflow.com/questions/12535722/what-is-the-best-way-to-implement-a-timer


@firemyst