How to get the time of a price from DataSeries?

Created at 21 Jun 2016, 08:18
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!
xjacks's avatar

xjacks

Joined 24.02.2016

How to get the time of a price from DataSeries?
21 Jun 2016, 08:18


I get the Max Price and Min Price in specified period like following:

var period = 50;
var maxPrice = MarketSeries.High.Maximum(period);
var minPrice = MarketSeries.Low.Minimum(period);

Then I want to get the time of maxPrice and minPrice for the following logic:

if (maxPriceTime <= Server.Time.AddHours(-2))
{
  // Do something...
}
if (minPriceTime <= Server.Time.AddHours(-2))
{
  // Do something...
}

Could anyone kindly tell me how to get the price time? The price index is also OK because I can judge time by Index * TimeFrame.

Thank you very much!


@xjacks
Replies

Jiri
21 Jun 2016, 14:44

Hi, this method returns you how many bars back was the highest or lowest bar within a period.

private int HighestBar(DataSeries series, int period)
{
    for (int i = 0; i <= period; i++)
    {
        if (series[series.Count - 1 - i] == series.Maximum(period))
        {
            return i;
        }
    }
    return -1;
}

private int LowestBar(DataSeries series, int period)
{
    for (int i = 0; i <= period; i++)
    {
        if (series[series.Count - 1 - i] == series.Minimum(period))
        {
            return i;
        }
    }
    return -1;
}

 


@Jiri

xjacks
22 Jun 2016, 03:10

RE:

Hi tmc, thank you very much, it's a perfect solution !

 

tmc. said:

Hi, this method returns you how many bars back was the highest or lowest bar within a period.

private int HighestBar(DataSeries series, int period)
{
    for (int i = 0; i <= period; i++)
    {
        if (series[series.Count - 1 - i] == series.Maximum(period))
        {
            return i;
        }
    }
    return -1;
}

private int LowestBar(DataSeries series, int period)
{
    for (int i = 0; i <= period; i++)
    {
        if (series[series.Count - 1 - i] == series.Minimum(period))
        {
            return i;
        }
    }
    return -1;
}

 

 


@xjacks

Drummond360
25 Jan 2018, 17:01

Hi,

Could you please explain how to invoke these methods?

How to do we say 'if HighestBar came before LowestBar'

Sorry for being such a coding novice!

Thanks in advance,

Drummond...

 


@Drummond360

Drummond360
29 Jan 2018, 22:32

int x = HighestBar(series, period);
int y = HighestBar(series, period);

if (x > y)
{ 
do something 

This is the best I have in me! It compiles fine but returns this error...

Crashed in OnTick with NullReferenceException: Object reference not set to an instance of an object.

I can't even get a print statement in the log... Normally my go to for fault finding...

If anyone can humour me it would be greatley appreciated!

Many thanks,

Drummond 


@Drummond360

PanagiotisCharalampous
30 Jan 2018, 10:48

Hi Drummond,

Please post the complete cBot code and we will let you know why you receive the error.

Best Regards,

Panagiotis


@PanagiotisCharalampous

Drummond360
30 Jan 2018, 11:02

Thank you Panagiotis,

Here's a simplified version of the code, still putting out the same error message.

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 FIBBOT : Robot
    {
        [Parameter("TP Pips", DefaultValue = 10, MinValue = 1)]
        public int takeProfitPips { get; set; }

        [Parameter("StopLossPips", DefaultValue = 1, MinValue = 1)]
        public int stopLossPips { get; set; }

        [Parameter("Periods", DefaultValue = 20)]
        public int period { get; set; }

        public DataSeries series { get; set; }

        string label = "Fib Bot";

        protected override void OnStart()
        {

        }
        private int HighestBar(DataSeries series, int period)
        {
            for (int i = 0; i <= period; i++)
            {
                if (series[series.Count - 1 - i] == series.Maximum(period))
                {
                    return i;
                }
            }
            return -1;
        }

        private int LowestBar(DataSeries series, int period)
        {
            for (int i = 0; i <= period; i++)
            {
                if (series[series.Count - 1 - i] == series.Minimum(period))
                {
                    return i;
                }
            }
            return -1;
        }


        protected override void OnTick()
        {



            int x = HighestBar(series, period);
            Print(" HighestBar = " + x);
            int y = LowestBar(series, period);
            Print(" LowestBar = " + y);




            if (x > y)
            {
                ExecuteMarketOrder(TradeType.Buy, Symbol, 10000, label, stopLossPips, takeProfitPips);



            }
        }
    }
}





 


@Drummond360

PanagiotisCharalampous
30 Jan 2018, 11:13

Hi Drummond,

The problem is that you are not initializing the series anywhere. Which series would you like to use?

Best Regards,

Panagiotis


@PanagiotisCharalampous

Drummond360
30 Jan 2018, 11:38

Hi Panagiotis,

I'm using 1 minute bars...

Many thanks,

Drummond


@Drummond360

PanagiotisCharalampous
30 Jan 2018, 12:47

Hi Drummond,

Try the 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 FIBBOT : Robot
    {
        [Parameter("TP Pips", DefaultValue = 10, MinValue = 1)]
        public int takeProfitPips { get; set; }

        [Parameter("StopLossPips", DefaultValue = 1, MinValue = 1)]
        public int stopLossPips { get; set; }

        [Parameter("Periods", DefaultValue = 20)]
        public int period { get; set; }

        public DataSeries series { get; set; }

        string label = "Fib Bot";

        protected override void OnStart()
        {

        }
        private int HighestBar(DataSeries series, int period)
        {
            for (int i = 0; i <= period; i++)
            {
                if (series[series.Count - 1 - i] == series.Maximum(period))
                {
                    return i;
                }
            }
            return -1;
        }

        private int LowestBar(DataSeries series, int period)
        {
            for (int i = 0; i <= period; i++)
            {
                if (series[series.Count - 1 - i] == series.Minimum(period))
                {
                    return i;
                }
            }
            return -1;
        }


        protected override void OnTick()
        {

            int x = HighestBar(MarketSeries.High, period);
            Print(" HighestBar = " + x);
            int y = LowestBar(MarketSeries.Low, period);
            Print(" LowestBar = " + y);

            if (x > y)
            {
                ExecuteMarketOrder(TradeType.Buy, Symbol, 10000, label, stopLossPips, takeProfitPips);

            }
        }
    }
}

Best Regards,

Panagiotis


@PanagiotisCharalampous

Drummond360
30 Jan 2018, 15:37

Thank you, great support, much apreaciated...

Drummond


@Drummond360