0

I have a list with ohlc data:

[(1360710000.0, 69.06, 69.67, 68.53, 69.53, 550000.0, 69.53),
(1360623600.0, 68.98, 69.05, 68.39, 68.94, 604200.0, 68.94)]

and want it to convert into a numpy array which is structured like this one:

array([ (1352934000, 63.130001068115234, 63.59000015258789, 62.900001525878906, 63.16999816894531, 816300.0, 63.16999816894531),
   (1353020400, 62.970001220703125, 63.529998779296875, 62.599998474121094, 62.61000061035156, 942000.0, 62.61000061035156),
   (1353279600, 63.0, 64.97000122070312, 63.0, 64.87999725341797, 812800.0, 64.87999725341797),
   (1353366000, 64.44999694824219, 65.27999877929688, 64.29000091552734, 65.23999786376953, 467700.0, 65.23999786376953),
   (1353452400, 65.16999816894531, 65.4000015258789, 64.58999633789062, 64.8499984741211, 417700.0, 64.8499984741211)], 
  dtype=[('time', '<i4'), ('open', '<f4'), ('high', '<f4'), ('low', '<f4'), ('close', '<f4'), ('volume', '<f4'), ('amount', '<f4')])

Any attempts result in wrong structure as expected:

np.array(list, dtype=Day)

array([[1360710000.0, 69.06, 69.67, 68.53, 69.53, 550000.0, 69.53],
   [1360623600.0, 68.98, 69.05, 68.39, 68.94, 604200.0, 68.94]], dtype=object)
0

1 Answer 1

4
lst = [(1360710000.0, 69.06, 69.67, 68.53, 69.53, 550000.0, 69.53),
       (1360623600.0, 68.98, 69.05, 68.39, 68.94, 604200.0, 68.94)]
np.array(lst, dtype=[('time', '<i4'), ('open', '<f4'), ('high', '<f4'), ('low', '<f4'), ('close', '<f4'), ('volume', '<f4'), ('amount', '<f4')])

returns

array([ (1360710000, 69.05999755859375, 69.66999816894531, 68.52999877929688, 69.52999877929688, 550000.0, 69.52999877929688),
       (1360623600, 68.9800033569336, 69.05000305175781, 68.38999938964844, 68.94000244140625, 604200.0, 68.94000244140625)], 
      dtype=[('time', '<i4'), ('open', '<f4'), ('high', '<f4'), ('low', '<f4'), ('close', '<f4'), ('volume', '<f4'), ('amount', '<f4')])

Isn't it what you wanted?

Sign up to request clarification or add additional context in comments.

1 Comment

works for me now too if I put dtype with the statement above instead of using the imported "Day" which is exactly the same. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.