Need help to close all positions after x hours

Created at 29 Mar 2022, 00:36
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!
HU

hugicardoso

Joined 29.03.2022

Need help to close all positions after x hours
29 Mar 2022, 00:36


I will leave the code here.

when the hours >24, the robot dont seems recognize my -24 

 

Can you help?

 

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class z : Robot
    {

        [Parameter("Max time position open(hours)", DefaultValue = 2, MinValue = 1)]
        public int MaxTimeOpen { get; set; }

        protected override void OnStart()
        {
            // Put your initialization logic here
            Print("Start robot: ", Server.Time.Hour);
            if (Server.Time.Hour == 0)
            {
                ExecuteMarketOrder(TradeType.Sell, Symbol, 10000, "", 100, 100);
            }
        }

        protected override void OnTick()
        {
            // Put your core logic here
            Print("OnTick: ", Server.Time.Hour);

            foreach (var position in Positions)
            {
                Print(position.EntryTime.TimeOfDay);
                int posOpenTimeHours = position.EntryTime.Hour + MaxTimeOpen;
                int posOpenTimeMinutes = position.EntryTime.Minute;

                if (posOpenTimeHours > 24)
                {
                    posOpenTimeHours = posOpenTimeHours - 24;
                }

                Print("Pos open time: ", posOpenTimeHours);

                if (Server.Time.Hour > posOpenTimeHours && Server.Time.Minute > posOpenTimeMinutes)
                {
                    ClosePosition(position);
                }
            }
        }

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

@hugicardoso
Replies

amusleh
29 Mar 2022, 10:50

Hi,

Try this:

using cAlgo.API;

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class z : Robot
    {
        [Parameter("Max time position open(hours)", DefaultValue = 2, MinValue = 1)]
        public int MaxTimeOpen { get; set; }

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

            // Put your initialization logic here
            Print("Start robot: ", Server.Time.Hour);
            if (Server.Time.Hour == 0)
            {
                ExecuteMarketOrder(TradeType.Sell, SymbolName, 10000, "", 100, 100);
            }
        }

        protected override void OnTimer()
        {
            foreach (var position in Positions)
            {
                var timeElapsedSincePositionEntryTime = Server.TimeInUtc - position.EntryTime;

                Print("timeElapsedSincePositionEntryTime: {0}", timeElapsedSincePositionEntryTime);

                if (timeElapsedSincePositionEntryTime.TotalHours >= MaxTimeOpen)
                {
                    ClosePosition(position);
                }
            }
        }
    }
}

 


@amusleh

hugicardoso
29 Mar 2022, 19:03

RE:

Thank you very much, but when I use the optimization it is very slow, it goes from 7h to 5 days. which makes it very difficult to use

 

amusleh said:

Hi,

Try this:

using cAlgo.API;

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class z : Robot
    {
        [Parameter("Max time position open(hours)", DefaultValue = 2, MinValue = 1)]
        public int MaxTimeOpen { get; set; }

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

            // Put your initialization logic here
            Print("Start robot: ", Server.Time.Hour);
            if (Server.Time.Hour == 0)
            {
                ExecuteMarketOrder(TradeType.Sell, SymbolName, 10000, "", 100, 100);
            }
        }

        protected override void OnTimer()
        {
            foreach (var position in Positions)
            {
                var timeElapsedSincePositionEntryTime = Server.TimeInUtc - position.EntryTime;

                Print("timeElapsedSincePositionEntryTime: {0}", timeElapsedSincePositionEntryTime);

                if (timeElapsedSincePositionEntryTime.TotalHours >= MaxTimeOpen)
                {
                    ClosePosition(position);
                }
            }
        }
    }
}

 

 


@hugicardoso

amusleh
29 Mar 2022, 19:26 ( Updated at: 29 Mar 2022, 19:27 )

Hi,

Try to increase the timer interval, ex:

using cAlgo.API;

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class z : Robot
    {
        [Parameter("Max time position open(hours)", DefaultValue = 2, MinValue = 1)]
        public int MaxTimeOpen { get; set; }

        protected override void OnStart()
        {
            Timer.Start(3600);

            // Put your initialization logic here
            Print("Start robot: ", Server.Time.Hour);
            if (Server.Time.Hour == 0)
            {
                ExecuteMarketOrder(TradeType.Sell, SymbolName, 10000, "", 100, 100);
            }
        }

        protected override void OnTimer()
        {
            foreach (var position in Positions)
            {
                var timeElapsedSincePositionEntryTime = Server.TimeInUtc - position.EntryTime;

                Print("timeElapsedSincePositionEntryTime: {0}", timeElapsedSincePositionEntryTime);

                if (timeElapsedSincePositionEntryTime.TotalHours >= MaxTimeOpen)
                {
                    ClosePosition(position);
                }
            }
        }
    }
}

 


@amusleh

hugicardoso
29 Mar 2022, 20:41

RE:

Works very good thanks for the help.

amusleh said:

Hi,

Try to increase the timer interval, ex:

using cAlgo.API;

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class z : Robot
    {
        [Parameter("Max time position open(hours)", DefaultValue = 2, MinValue = 1)]
        public int MaxTimeOpen { get; set; }

        protected override void OnStart()
        {
            Timer.Start(3600);

            // Put your initialization logic here
            Print("Start robot: ", Server.Time.Hour);
            if (Server.Time.Hour == 0)
            {
                ExecuteMarketOrder(TradeType.Sell, SymbolName, 10000, "", 100, 100);
            }
        }

        protected override void OnTimer()
        {
            foreach (var position in Positions)
            {
                var timeElapsedSincePositionEntryTime = Server.TimeInUtc - position.EntryTime;

                Print("timeElapsedSincePositionEntryTime: {0}", timeElapsedSincePositionEntryTime);

                if (timeElapsedSincePositionEntryTime.TotalHours >= MaxTimeOpen)
                {
                    ClosePosition(position);
                }
            }
        }
    }
}

 

 


@hugicardoso