Referencing Time

Created at 22 Sep 2012, 11:04
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!
ER

ErykSosenka

Joined 22.09.2012

Referencing Time
22 Sep 2012, 11:04


Hello,

I'm trying to write a piece of code that does stuff depending on the current time e.g. if current time > 22:00 don't open new positions

any suggestions on how to do that?


Cheers


@ErykSosenka
Replies

rkokerti
23 Sep 2012, 23:05

Hello,

Please check out the code of this robot. /algos/show/115

I hope it will help you.


@rkokerti

admin
24 Sep 2012, 10:50

Hello TETL,

 

DateTime.Now is the current date and time on your machine

Server.Time is the current date and time on the server.

You can access various members of the Datetime like Date, Time, Hour, etc. as well as functions like AddHours(), AddMinutes(), etc.

 

 

In a Robot you need to initialize your time variable in the OnStart event and check the condition in either the OnTick or the OnBar event, depending on your algorithm.

Look at the code snippet below as an example of how you can use DateTime and Server.Time.

 

 

 

private DateTime stopTime;

protected override void OnStart()
{
    stopTime = DateTime.Now.Date.AddHours(22);            

}
protected override void OnTick()
{
    // 

    if (Server.Time < stopTime)
    {
        // Open new positions                
    }



 

 


@admin

romiko
26 Sep 2012, 14:28

Referencing Time

Hi,

 

This is flawed, since backtesting needs the time off the reference data, so how can we use time that is back testable?


@romiko

admin
26 Sep 2012, 15:10

Hello,

 

For backtesting purposes you can use MarketSeries.OpenTime:

 

    int index = MarketSeries.OpenTime.Count - 1;
    var stopTime = MarketSeries.OpenTime[index].Date.AddHours(22);


    if (MarketSeries.OpenTime.LastValue < stopTime)
    {
        Print("Server Time = {0}", Server.Time);
        // Open new positions                
    }



 


@admin

romiko
30 Sep 2012, 10:36

Referencing Time

Is this ok to use, I use this condition to trade and specfic times. I tried to stay away from the index accessor.

 

        protected override void OnBar()

        {

            if (MarketSeries.OpenTime.LastValue.Hour >= 21 || (MarketSeries.OpenTime.LastValue.Hour >= 0 && MarketSeries.OpenTime.LastValue.Hour <= 13))

                Initialize();

        }


@romiko

admin
02 Oct 2012, 12:21

Provided that the method Initialize() is a method defined in the rest of your code to perform trade operations, then your above assumption "to trade at specific times" is correct.

Otherwise, please clarify your question.

 


@admin