Different candle close

05 Oct 2017, 09:41Different candle close#11
Panagiotis Charalampousposts: 160since: 13 Jan 2017

Hi hungtonydang,

If you know the exact time of the hour you are after then you can specify it as shown below

MarketSeries.OpenTime.GetIndexByExactTime(new DateTime(2017,10,5,10,0,0))

The sample above returns the value for X hours (if you are on the h1 timeframe) before the current time.

Let me know if this helps.

Best Regards,

Panagiotis


Head of Community Management at cTrader
09 Oct 2017, 23:49#12
hungtonydangposts: 59since: 19 Jul 2017
Hi Panagiotis, The function I am after is to get the close of a certain candle within the day irrespective of the time my cbot is started and the current time. For example getting the close of the hourly candle of 2 hours before the New York open if my cbot is set to Tasmaniastandardtime. All the previous examples given calculate the value from the current candle. If this is not possible is there another way to achieve the result I am after? Thanks,
10 Oct 2017, 11:27#13
Panagiotis Charalampousposts: 160since: 13 Jan 2017

Hi hungtonydang,

If we suppose that you need the close price of a candle at 11:30 UTC (two hours before NY open time), the example below shows how to get the Close price of yesterday's candle at that time.

 MarketSeries.Close[MarketSeries.OpenTime.GetIndexByExactTime(new DateTime(2017, 10, 9, 11, 30, 0).AddHours(TimeZone.BaseUtcOffset.Hours))]

Let me know if this is what you are looking for.

Best Regards,

Panagiotis


Head of Community Management at cTrader
12 Oct 2017, 23:14#14
hungtonydangposts: 59since: 19 Jul 2017

Hi Panagiotis,

After days of testing this is the right piece of code that I am after. However I am looking to modify it now. I need it to be able to change with every 24 hours (1 day) to be a rolling input in my further cbot calculations. I have tried the following but to no avail.

var close = MarketSeries.Close[MarketSeries.OpenTime.GetIndexByExactTime(new DateTime(2017, 10, 9, 11, 30, 0).AddDays(1))];

Is there something that I am missing to make this rollover with each new day?

13 Oct 2017, 15:07#15
Panagiotis Charalampousposts: 160since: 13 Jan 2017

Hi hungtonydang,

How about the following?

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 NewcBot : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }
        private DateTime _date;
        protected override void OnStart()
        {
            _date = new DateTime(2017, 10, 9, 11, 30, 0);
        }

        protected override void OnBar()
        {
            if (_date.Day != DateTime.Now.Day)
                _date = _date.AddDays(1);
            var close = MarketSeries.Close[MarketSeries.OpenTime.GetIndexByExactTime(_date)];
        }

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

Best Regards,

Panagiots


Head of Community Management at cTrader
17 Oct 2017, 22:57#16
hungtonydangposts: 59since: 19 Jul 2017

Hi Panagiots,

I am nearly there with this code.I made the adjustments as recommended however noticed that the code would need to be altered for each successive day. I think I may have found another approach which suits better. Please see code below

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 NewcBot : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }
        private DateTime _date;
        protected override void OnStart()
        {
            int month = System.DateTime.Now.Month;
            int day = System.DateTime.Now.Day;

            _date = new DateTime(2017, month, day, 11, 30, 0);
        
            var close = MarketSeries.Close[MarketSeries.OpenTime.GetIndexByExactTime(_date)];
        }
 
        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }
}

I just need a little help with 1 line of the code, since we are requesting the current day as an int, however I would like to get the previous value for calculation purposes. I have tried the .Last function and -1 but both show up errors on the build. Any help and thoughts are greatly appreciated.

Thanks,

Tony