Method to execute code at 59 seconds

Created at 21 May 2015, 18:40
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!
GM

Gman89

Joined 13.08.2014

Method to execute code at 59 seconds
21 May 2015, 18:40


Hi everyone,

I'm trying to run code that enters an OnBar() like method with the exception that it happens whenever the server time reaches 59 seconds. So far I have the following:

protected override void OnTick()
        {

            // Wait until close
            if (atClose())
            {
                  // Code here
            }

        }
        private bool atClose()
        {
            if (Time.Second == 59)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

 

The problem with placing my bool in the OnTick() method is that sometimes there won't be a tick at all between 59-60 seconds, meaning that time will tick over to a new minute and code won't get executed. Does anyone have any ideas on what the best way to do this is? I will need to be able to use MarketSeries with LastValue in the code.

 


@Gman89
Replies

LSR2412
22 May 2015, 13:04

hi

I am using something similar..to calculate one indicator just prior close

try this..

TimeSpan duration = MarketSeries.OpenTime.Last(0) - MarketSeries.OpenTime.Last(1);

if (Server.Time > MarketSeries.OpenTime.LastValue.AddHours(duration.Hours).AddMinutes(duration.Minutes).AddSeconds(duration.Seconds - 2))

 


@LSR2412

Gman89
22 May 2015, 13:47

Hey,

Thanks for the reply.

Is that placed in the OnTick() method? Because the issue I have is that I cannot access the latest value of MarketSeries.Close.LastValue unless the bot is prompted to do so on a tick. If there is no tick then I can't read the latest value.

Lets say for example that the last tick movement was at 50 seconds, and because of low volume there isn't another tick movement until 10 seconds in the next minute; I still want to be able to get the candlestick at 59 seconds. Currently, I cannot do this because OnBar() and OnTick() only get called when there is an actual tick. If I used my code in my original post it would just skip the bar completely, and if I used OnBar() to utilized Close.Last(1) there would be a 10 second delay even though no real information has changed in that time.  

 


@Gman89

Gman89
22 May 2015, 15:57

I solved the issue.

I realized in newer versions of cAlgo there is an OnTimer method. I just set this to 1 second and utilized the RefreshData() function to ensure the MarketSeries values are up to date.

 


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

}


protected override void OnTimer()
        {
 
            // Wait until close
            if (atClose())
            {
                  RefreshData()
                  // further code

            }
 
        }
        private bool atClose()
        {
            if (Time.Second == 59)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

 


@Gman89