How to Reference chart count down Timer/Clock

Created at 30 Dec 2021, 16:58
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!
SU

SummerSalt

Joined 30.12.2021

How to Reference chart count down Timer/Clock
30 Dec 2021, 16:58


Hello everyone. Does anyone know how to reference the countdown clock on the chart? I want to use it to exit positions at a specified time like 10 seconds before bar closes. I will greatly appreciate any help. Thanks for your time. 


@SummerSalt
Replies

amusleh
31 Dec 2021, 08:48

Hi,

You can't use the bar counter on chart from Automate API, and there is no need for it.

You can easily code one, each bar has an open time and if you know the time frame you can subtract the bar open time from current time, ex:

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 NewcBot : Robot
    {
        private TimeSpan _barTimeLeft;

        protected override void OnStart()
        {
            Timer.Start(1);
        }
        
        protected override void OnTimer()
        {
            var previousBarTimePeriod = Bars.LastBar.OpenTime - Bars.Last(1).OpenTime;

            _barTimeLeft = previousBarTimePeriod - (Server.Time - Bars.LastBar.OpenTime);
            
            // Check logs, it should match with chart counter
            Print("{0:hh':'mm':'ss}", _barTimeLeft);

            // Close all positions if the time left is less than or equal 10 seconds
            if (_barTimeLeft.TotalSeconds <= 10)
            {
                foreach (var position in Positions)
                {
                    ClosePosition(position);
                }
            }
        }
    }
}

 


@amusleh

SummerSalt
31 Dec 2021, 11:08

Order execution error

Thank you very much amusleh, the code works great. I have another question if you don't mind. How do I prevent order execution errors when running the same bot on multiple FX pairs/assets? My strategy requires me to execute a lot of orders at the open of each bar and exit on the close of each bar. The coding works well when running on one FX pair/asset but behaves erratic on multiple charts. For example: executing orders and closing it immediately, delayed execution or not executing orders at all. I have tried to counter this by closing position well before the next bar opens (which was why I initially sought your help), so that the closing orders does not conflict with opening orders but the problem still persist. I have also tried creating multiple cbots with the same code but for the different pairs that I want to trade to avoid conflict but this also did not work. Is there any kind of solution for this problem or does it mean I can only run one bot at a time? Thanks for your time.  


@SummerSalt

amusleh
31 Dec 2021, 12:00

RE: Order execution error

SummerSalt said:

Thank you very much amusleh, the code works great. I have another question if you don't mind. How do I prevent order execution errors when running the same bot on multiple FX pairs/assets? My strategy requires me to execute a lot of orders at the open of each bar and exit on the close of each bar. The coding works well when running on one FX pair/asset but behaves erratic on multiple charts. For example: executing orders and closing it immediately, delayed execution or not executing orders at all. I have tried to counter this by closing position well before the next bar opens (which was why I initially sought your help), so that the closing orders does not conflict with opening orders but the problem still persist. I have also tried creating multiple cbots with the same code but for the different pairs that I want to trade to avoid conflict but this also did not work. Is there any kind of solution for this problem or does it mean I can only run one bot at a time? Thanks for your time.  

Hi,

There is no connection between two running instances of a cBot, so there should be no conflict at all.

Most probably something is wrong with your code, post it then I will be able to help you.

And also post the error messages.


@amusleh

SummerSalt
31 Dec 2021, 13:47 ( Updated at: 31 Dec 2021, 13:50 )

RE: RE: Order execution error

amusleh said:

SummerSalt said:

Thank you very much amusleh, the code works great. I have another question if you don't mind. How do I prevent order execution errors when running the same bot on multiple FX pairs/assets? My strategy requires me to execute a lot of orders at the open of each bar and exit on the close of each bar. The coding works well when running on one FX pair/asset but behaves erratic on multiple charts. For example: executing orders and closing it immediately, delayed execution or not executing orders at all. I have tried to counter this by closing position well before the next bar opens (which was why I initially sought your help), so that the closing orders does not conflict with opening orders but the problem still persist. I have also tried creating multiple cbots with the same code but for the different pairs that I want to trade to avoid conflict but this also did not work. Is there any kind of solution for this problem or does it mean I can only run one bot at a time? Thanks for your time.  

Hi,

There is no connection between two running instances of a cBot, so there should be no conflict at all.

Most probably something is wrong with your code, post it then I will be able to help you.

And also post the error messages.

 

Hello, you were right, there is no connection between instances, the fault was mine. I ran all the bots with the same instance name which resulted in the execution errors but that has been fixed now. Thank you very much for your help.

Is there by by chance other resources/manual for cbot coding other than the API and Youtube videos? Something more detailed and explanatory?


@SummerSalt

amusleh
03 Jan 2022, 09:24

Hi,

What kind of resources do you need?

cTrader automate API works on .NET environment, If you have a moderate level of C# .NET experience then the API references with its examples codes will be enough.

There are a ton of resources available for C# and .NET on internet.


@amusleh

zoo.digitaladv
03 May 2024, 14:15 ( Updated at: 03 May 2024, 14:50 )

RE: How to Reference chart count down Timer/Clock

amusleh said: 

Hi,

You can't use the bar counter on chart from Automate API, and there is no need for it.

You can easily code one, each bar has an open time and if you know the time frame you can subtract the bar open time from current time, ex:

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 NewcBot : Robot    {        private TimeSpan _barTimeLeft;        protected override void OnStart()        {            Timer.Start(1);        }                protected override void OnTimer()        {            var previousBarTimePeriod = Bars.LastBar.OpenTime - Bars.Last(1).OpenTime;            _barTimeLeft = previousBarTimePeriod - (Server.Time - Bars.LastBar.OpenTime);                        // Check logs, it should match with chart counter            Print("{0:hh':'mm':'ss}", _barTimeLeft);            // Close all positions if the time left is less than or equal 10 seconds            if (_barTimeLeft.TotalSeconds <= 10)            {                foreach (var position in Positions)                {                    ClosePosition(position);                }            }        }    }}

By any chance, there is one for mobile Android CTrader app? 

 


@zoo.digitaladv

zoo.digitaladv
03 May 2024, 14:15 ( Updated at: 03 May 2024, 14:50 )

RE: How to Reference chart count down Timer/Clock

amusleh said: 

Hi,

You can't use the bar counter on chart from Automate API, and there is no need for it.

You can easily code one, each bar has an open time and if you know the time frame you can subtract the bar open time from current time, ex:

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 NewcBot : Robot    {        private TimeSpan _barTimeLeft;        protected override void OnStart()        {            Timer.Start(1);        }                protected override void OnTimer()        {            var previousBarTimePeriod = Bars.LastBar.OpenTime - Bars.Last(1).OpenTime;            _barTimeLeft = previousBarTimePeriod - (Server.Time - Bars.LastBar.OpenTime);                        // Check logs, it should match with chart counter            Print("{0:hh':'mm':'ss}", _barTimeLeft);            // Close all positions if the time left is less than or equal 10 seconds            if (_barTimeLeft.TotalSeconds <= 10)            {                foreach (var position in Positions)                {                    ClosePosition(position);                }            }        }    }}

By any chance, there is one for mobile Android CTrader app? 

 


@zoo.digitaladv

PanagiotisCharalampous
03 May 2024, 15:09

RE: RE: How to Reference chart count down Timer/Clock

zoo.digitaladv said: 

amusleh said: 

Hi,

You can't use the bar counter on chart from Automate API, and there is no need for it.

You can easily code one, each bar has an open time and if you know the time frame you can subtract the bar open time from current time, ex:

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 NewcBot : Robot    {        private TimeSpan _barTimeLeft;        protected override void OnStart()        {            Timer.Start(1);        }                protected override void OnTimer()        {            var previousBarTimePeriod = Bars.LastBar.OpenTime - Bars.Last(1).OpenTime;            _barTimeLeft = previousBarTimePeriod - (Server.Time - Bars.LastBar.OpenTime);                        // Check logs, it should match with chart counter            Print("{0:hh':'mm':'ss}", _barTimeLeft);            // Close all positions if the time left is less than or equal 10 seconds            if (_barTimeLeft.TotalSeconds <= 10)            {                foreach (var position in Positions)                {                    ClosePosition(position);                }            }        }    }}

By any chance, there is one for mobile Android CTrader app? 

 

No there isn't one


@PanagiotisCharalampous