High/low/close between specific times
High/low/close between specific times
19 Jul 2023, 12:05
Hi all,
I'm trying to code a pivot indicator which calculates the high/low/close over specific times, rather than the whole of the previous day.
For example I'd like to calculate pivots based on the HLC between 0800-2000 on the previous day.
I've managed to do this in MT4 but have no idea how to begin with cTrader - I've searched other posts on the same topic but couldn't really figure it out from those.
Thanks for any help.
Replies
graeme.j.nash
19 Jul 2023, 14:31
RE:
PanagiotisChar said:
Hi there,
You need to get the bars for a timeframe that covers your timespan e.g. h1 and loop through them to get the info you need.
Need help? Join us on Telegram
Thanks for the reply - Sounds great, problem is I have no idea how to do that? :)
@graeme.j.nash
graeme.j.nash
19 Jul 2023, 19:30
RE:
firemyst said:
Adding to what PanagiotisChar said, example of getting the H1 timeframe:
Bars myBars = MarketData.GetBars(TimeFrame.Hour);
Thanks for the guidance, it's just how to set specific time boundaries I'm not sure about. In MT4 it's done by finding the shift value of the specified date/time, but cTrader code is all new to me, not sure how to do it!
@graeme.j.nash
pick
20 Jul 2023, 12:51
With many things in programming, there are multiple ways this could be achieved. My route of choice would be something like this:
DayData getPrevDayValues(Bars bars, DateTime dt, int startHour = 8, int endHour = 20)
{
DateTime prevTime = getStartPrevDay(dt);
DateTime startTime = prevTime.AddHours(startHour);
DateTime endTime = prevTime.AddHours(endHour);
Bar[] usableBars = bars.Where(b => b.OpenTime >= startTime && b.OpenTime < endTime).ToArray();
if (usableBars.Length <= 0)
return null;
double max = usableBars.Max(b => b.High);
double min = usableBars.Min(b => b.Low);
double close = usableBars[usableBars.Length - 1].Close;
return new DayData(max, min, close);
}
DateTime getStartPrevDay(DateTime dt)
{
DateTime prevStart = dt.Date.AddDays(-1);
for (; prevStart.DayOfWeek == DayOfWeek.Sunday || prevStart.DayOfWeek == DayOfWeek.Saturday; prevStart = prevStart.AddDays(-1)) ;
return prevStart;
}
internal class DayData
{
public DayData(double h, double l, double c)
{
// Calculate in here
}
}
I would also store the date as a key in a dictionary with the value being the DayData, so you'd only have to calculate it once per day.
@pick
graeme.j.nash
20 Jul 2023, 15:03
RE:
pick said:
Wow, that's really helpful, thanks very much! I will get to work! :)
@graeme.j.nash
PanagiotisChar
19 Jul 2023, 13:18
Hi there,
You need to get the bars for a timeframe that covers your timespan e.g. h1 and loop through them to get the info you need.
Aieden Technologies
Need help? Join us on Telegram
@PanagiotisChar