Different candle close

Created at 14 Sep 2017, 11:13
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!
HT

HTtrader

Joined 19.07.2017

Different candle close
14 Sep 2017, 11:13


Hi all,

Have another query anyone can help me with. How exactly would I code a close of a particular candle. I understand that this generally works out the daily candle close to be the new York close

var Daily = MarketData.GetSeries(TimeFrame.Daily);
    var High = Daily.High.Last(1);
    var Low = Daily.Low.Last(1);
    var Close = Daily.Close.Last(1);

But what I am after is the close of a candle just before say the New york open. Can I change my timeframe to hourly and pinpoint a candle? What would the correct syntax for that as .last would calculate it on a rolling calculation if I'm not mistaken.

Thanks for any help on out there on this.


@HTtrader
Replies

HTtrader
16 Sep 2017, 14:52

So I have managed to work up this code and was wondering before I test it on actual market data will it work as it should as I am making a call of the daily market data and hourly data. I have made the variable to be any start time and so it should calculate the bar before the start time as the close I am looking for. Can someone please confirm if this is the right method to be using or is there another way to achieve the result I am after.

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Requests;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.TasmaniaStandardTime, AccessRights = AccessRights.None)]
    public class Pivotpoints : Robot
    {
        [Parameter("Source")]
        public DataSeries Source { get; set; }

        [Parameter("Start Hour", DefaultValue = 10.0)]
        public double StartTime { get; set; }

        private DateTime _startTime;

        protected override void OnStart()
        {
            // Start Time is the same day at 22:00:00 Server Time
            _startTime = Server.Time.Date.AddHours(StartTime);

            var Daily = MarketData.GetSeries(TimeFrame.Daily);
            var high = Daily.High.Last(1);
            var low = Daily.Low.Last(1);

            var close = MarketSeries.Close.Last(1);

            var PP = (high + low + close) / 3;
            var R1 = (2 * PP) - low;
            var S1 = (2 * PP) - high;

            Print("Example 1...R1: {0}", R1);
        }
        protected override void OnStop()
        {
            Stop();
        }
    }
}

 


@HTtrader

HTtrader
20 Sep 2017, 00:13

Hi I have been reading up on using index but there doesn't seem to much around.

Can we set index to be open of a particular market?

int index;
var close = MarketSeries.Close.Last(Index -1);

as I understand it that code above should get the previous candle of the index start which is the timezone?

Please help as I am a little bit confused and my cbot crashed with nan target price is not acceptable.


@HTtrader

HTtrader
20 Sep 2017, 23:49

I have found this piece of code now that might be able to help me do what I need but there is not much information out there on how to use it. Can someone possibly give me a sample of how they have used it.

var indexSeries2 = indexSeries2.OpenTime.GetIndexByExactTime(MarketSeries.OpenTime.LastValue);

As I understand it you can get a particular hr value in the day say the close of 0800 or 1600 candle depending on your timezone class for your bot. I am just having a bit of trouble with the syntax at the moment. Any help is much appreciated.


@HTtrader

PanagiotisCharalampous
21 Sep 2017, 09:31

Hi hungtonydang,

MarketSeries.Close.Last() will return a value using the index passed as parameter and counting backwards. For example

MarketSeries.Close.Last(0)

will return the last value of the series and the following

MarketSeries.Close.Last(1)

will return the penultimate value.

On the other hand GetIndexByExactTime() uses forward counting, meaning that 

indexSeries2.OpenTime.GetIndexByExactTime(MarketSeries.OpenTime.LastValue);

will return an index based on the number of values loaded on the chart e.g. 6213. You can verify this if you print the value in the Log using the Print() function.

If you want to combine the two functions, you can try something like the following

 MarketSeries.Close.Last(MarketSeries.OpenTime.Count - (MarketSeries.OpenTime.GetIndexByExactTime(MarketSeries.OpenTime.LastValue) + 1));

I hope the above clarifies the situation for you.

Best Regards,

Panagiotis


@PanagiotisCharalampous

HTtrader
22 Sep 2017, 15:42

Thanks for the clarification Panagiotis, I am in the process of implementing the above parameters into my code but for some reason I am not getting the end results as expected. All variable calculate properly albeit a spread issue I think. Please see the code below, results and expected results

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Requests;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
using System.Threading;
using System.Threading.Tasks;

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.TasmaniaStandardTime, AccessRights = AccessRights.None)]
    public class Pivot : Robot
    {
        [Parameter("Source")]
        public DataSeries Source { get; set; }

        [Parameter("Start Hour", DefaultValue = 10)]
        public int StartTime { get; set; }

        protected override void OnStart()
        {
            var Daily = MarketData.GetSeries(TimeFrame.Daily);
            var Hour = MarketData.GetSeries(TimeFrame.Hour);
            var high = Daily.High.Last(1);
            var low = Daily.Low.Last(1);
            var close = Hour.Close.Last(StartTime);

            var PP = (high + low + close) / 3;
            var R1 = (2 * PP) - low;
            var S1 = (2 * PP) - high;

            Print("high", high);
            Print("low", low);
            Print("close", close);
            Print("R1", R1);
            Print("S1", S1);
            Print("pp", PP);
        }

        protected override void OnStop()
        {
            Stop();
        }
    }
}

Results:

22/09/2017 12:32:00.192 | high1.19537
22/09/2017 12:32:00.192 | low1.18657
22/09/2017 12:32:00.192 | close1.19952
22/09/2017 12:32:00.192 | R11.20107
22/09/2017 12:32:00.192 | S11.19227
22/09/2017 12:32:00.192 | pp1.19382

Expected Results:

R1 = 1.20036

S1= 1.19058

PP = 1.19398

I have made the proper calculations above so any help on this issue is much appreciated. I think the Close is out too but still the S1 value shouldn't be that far off.

 


@HTtrader

PanagiotisCharalampous
22 Sep 2017, 17:50

Hi hungtonydang,

I made the calculations as well and the results seem correct. Can you please check again your calculations?

Best Regards,

Panagiotis


@PanagiotisCharalampous

HTtrader
26 Sep 2017, 15:09

Hi Panagiotis,

Sorry you are right about the calculation and they processed correctly. However on testing tonight I have noticed that the code starts from when the cbot is first started it takes that as the calculation index series. I was hoping that it would take the values from a certain fixed time. Is there anyway to hard code this and could you possibly provide an example.

Thanks,

Tony 


@HTtrader

PanagiotisCharalampous
26 Sep 2017, 15:16

Hi hungtonydang,

The last example of this post demonstrated how to get the values for a specific time. If this is not what you need please elaborate a little bit more on what you are trying to do.

Best Regards,

Panagiotis


@PanagiotisCharalampous

HTtrader
04 Oct 2017, 23:18

Hi Panagiotis,

I am trying to find the variable in which to invoke to get a particular time I'm after with the following

MarketSeries.Close.Last(MarketSeries.OpenTime.Count - (MarketSeries.OpenTime.GetIndexByExactTime(MarketSeries.OpenTime.LastValue) + 1));

I tried

MarketSeries.Close.Last(MarketSeries.OpenTime.Count - (MarketSeries.OpenTime.GetIndexByExactTime(MarketSeries.OpenTime.Last(X)) + 1));

where X is the hour I'm after but that just calculates it from when the cbot is started.

I'm guessing that it is specifying the opentime somewhere as a precursor to being used in the formula is this correct?


@HTtrader

PanagiotisCharalampous
05 Oct 2017, 09:41

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


@PanagiotisCharalampous

HTtrader
09 Oct 2017, 23:49

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,
@HTtrader

PanagiotisCharalampous
10 Oct 2017, 11:27

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


@PanagiotisCharalampous

HTtrader
12 Oct 2017, 23:14

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?


@HTtrader

PanagiotisCharalampous
13 Oct 2017, 15:07

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


@PanagiotisCharalampous

HTtrader
17 Oct 2017, 22:57

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


@HTtrader