correct way to let run something EXACTLY one time a day (on specific time)

Created at 23 Nov 2020, 10:48
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!
xabbu's avatar

xabbu

Joined 20.07.2020

correct way to let run something EXACTLY one time a day (on specific time)
23 Nov 2020, 10:48


Dear Panagiotis,

I have to run a set of functions exactly ONE time a day on a specific time, so onBar is not possible, so I use onTick with the following code:

if (Time.Hour == _tradingStartHour && Time.Minute == 37 && Time.Second > 0 && Time.Second < 5)

which results in multiple running some times.

So I tried

if (Time.Hour == _tradingStartHour && Time.Minute == 37 && Time.Second == 0)

whick results that the functions are not running every day depending from the tick data.

Can you point me in the right direction to solve this problem?

Kindest regards,

 


@xabbu
Replies

PanagiotisCharalampous
23 Nov 2020, 11:00

Hi xabbu,

Here is an approach

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 Onceaday : Robot
    {

        bool _allowTrading;
        protected override void OnStart()
        {
            var _dailyBars = MarketData.GetBars(TimeFrame.Daily);
            _dailyBars.BarOpened += _dailyBars_BarOpened;
            _allowTrading = true;
        }

        private void _dailyBars_BarOpened(BarOpenedEventArgs obj)
        {
            _allowTrading = true;
        }

        protected override void OnTick()
        {
            if (_allowTrading && Time.Hour >= 12 && Time.Minute >= 37)
            {
                _allowTrading = false;
                // Execute trading
            }
        }

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

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous

xabbu
23 Nov 2020, 11:22

Dear Panagiotis,

I'm so greatfull for your knowledge and the ability to share it here - without your kind support my journey with cTrader, which I started 4 months ago, have not been possible. Today, my cBot has more than 1000 lines of code (I had to learn a little coding first) - I will implemet your suggestions imidiatly...

Kindest regards,


@xabbu

xabbu
10 Dec 2020, 13:51 ( Updated at: 10 Dec 2020, 13:53 )

Dear Panatiogis,

I implemented your suggested solution a few weeks ago. It's works great. I would like to learn and gain my understanding in cTrader programming, so I would like to ask you a followup question to your solution:

 

Instead of constructing an own method

private void _dailyBars_BarOpened(BarOpenedEventArgs obj)
        {
            _allowTrading = true;
        }

would placing the "_allowTrading = true;" in the onBar method of the cBot works the same?

Why did you choose your solution instead of using the already existing onBar method? Is there any kind of difference or unwanted side effects when using onBar?

on a daily timeframe, I have to add

Kindest regards,


@xabbu

PanagiotisCharalampous
10 Dec 2020, 14:44

Hi xabbu,

if you plan to run the cBot only on D1 timeframe, then placing the code in OnBar() should work as well.

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous