Different candle close

14 Sep 2017, 11:13Different candle close#1
hungtonydangposts: 59since: 19 Jul 2017

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.

16 Sep 2017, 14:52#2
hungtonydangposts: 59since: 19 Jul 2017

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();
        }
    }
}

 

20 Sep 2017, 00:13#3
hungtonydangposts: 59since: 19 Jul 2017

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.

20 Sep 2017, 23:49#4
hungtonydangposts: 59since: 19 Jul 2017

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.

21 Sep 2017, 09:31#5
Panagiotis Charalampousposts: 160since: 13 Jan 2017

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


Head of Community Management at cTrader
22 Sep 2017, 15:42#6
hungtonydangposts: 59since: 19 Jul 2017

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.

 

22 Sep 2017, 17:50#7
Panagiotis Charalampousposts: 160since: 13 Jan 2017

Hi hungtonydang,

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

Best Regards,

Panagiotis


Head of Community Management at cTrader
26 Sep 2017, 15:09#8
hungtonydangposts: 59since: 19 Jul 2017

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 

26 Sep 2017, 15:16#9
Panagiotis Charalampousposts: 160since: 13 Jan 2017

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


Head of Community Management at cTrader
04 Oct 2017, 23:18#10
hungtonydangposts: 59since: 19 Jul 2017

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?