Calculating a different timeframe from given OHLC Data in Google Sheets ( Query String ?)

112 views Asked by At

I'm constantly fetching OHLC Data and trying to convert it into a different timeframe while staying dynamic. While calculating it manually is not a problem, it won´t stay dynamic this way with updating data. I've tried to query it out, but I'm relative new to query strings and couldn´t come to the wished results. If anyone has experience in this I'd be thankful.

Here is a snippet of given Data (edited):

//  Timestamp   Open1h  High1h  Low1h   Close1h
//  A           B       C       D       E  

1598500800  384.22  385.63  382.88  382.88
1598504400  382.88  383.79  380.40  381.45
1598508000  381.45  386.05  381.45  385.14
1598511600  385.14  386.48  384.31  385.72
1598515200  385.72  385.79  383.49  383.82
1598518800  383.82  387.32  383.00  386.99
1598522400  386.99  387.74  383.43  383.99
1598526000  383.99  385.31  381.80  382.07
1598529600  382.07  386.47  381.04  385.51
1598533200  385.51  396.85  384.99  388.75

Let's say we are trying to convert the data into a 2h timeframe. My 'pseudo query string code' would look like the following:

=QUERY(A1:B,"Every second row of A and B (A2:B2)",0)

=QUERY(A1:C,"Maximum of every two rows (C2:C3)",0)

=QUERY(A1:D,"Minimum of every two rows (D2:D3)",0)

=QUERY(A1:E,"Every second row with an offset of 1(E3)",0)

Result should look like the following:

//Timestamp Open2h High2h Low2h Close2h

1598500800  384.22  385.63  380.40  381.45
1598508000  381.45  386.48  381.45  385.72
1598515200  385.72  387.32  383.00  386.99
1598522400  386.99  387.74  381.80  382.07
1598529600  382.07  396.85  381.04  388.75

Thanks for your time!

1

There are 1 answers

0
Oleg_S On BEST ANSWER

See TestSheet

First part "Every second row of A and B" is returned with following formula.

=ARRAYFORMULA(FILTER(FILTER(A1:B,A1:A<>""),ISODD(SEQUENCE(COUNT(A1:A),1,1,1))))

We create index of sequential numbers and filter odds.

Same goes for last clause "Every second row with an offset of 1(E3)" but with even numbers

=ARRAYFORMULA(FILTER(FILTER(E1:E,E1:E<>""),ISEVEN(SEQUENCE(COUNT(A1:A),1,1,1))))

For MAX value of two rows following formula:

=ARRAYFORMULA(
 FILTER(
 SORT(
  FILTER($C$1:$C,$C$1:$C<>""),
  QUOTIENT(SEQUENCE(COUNT($A$1:$A),1,1,1)+1,2),TRUE,
  FILTER($C$1:$C,$C$1:$C<>""),FALSE),
ISODD(SEQUENCE(COUNT($A$1:$A),1,1,1))))

Where we create index of 1,1,2,2,3,3... and SORT descending by values taking first value for each index which is MAX value of two rows.

For MIN of every two rows almost same formula is used, with difference in SORT option TRUE FILTER($D$1:$D,$D$1:$D<>""),TRUE) for MIN.