1

My data consist of a particular OHLCV object that is a bit weird in that it can only be accessed by the name, like this:

# rA = [<MtApi.MqlRates object at 0x000000A37A32B308>,...]
type(rA)
# <class 'list'>

ccnt = len(rA)              # 100
for i in range(ccnt):
    print('{} {} {} {} {} {} {}'.format(i, rA[i].MtTime, rA[i].Open, rA[i].High, rA[i].Low, rA[i].Close, rA[i].TickVolume))

#0 1607507400 0.90654 0.90656 0.90654 0.90656 7
#1 1607507340 0.90654 0.9066  0.90653 0.90653 20
#2 1607507280 0.90665 0.90665 0.90643 0.90653 37
#3 1607507220 0.90679 0.90679 0.90666 0.90666 22
#4 1607507160 0.90699 0.90699 0.90678 0.90678 29

with some additional formatting I have:

Time         Open     High     Low      Close     Volume
-----------------------------------------------------------------
1607507400   0.90654  0.90656  0.90654  0.90656   7
1607507340   0.90654  0.90660  0.90653  0.90653   20
1607507280   0.90665  0.90665  0.90643  0.90653   37
1607507220   0.90679  0.90679  0.90666  0.90666   22

I have tried things like this:

df = pd.DataFrame(data = rA, index = range(100), columns = ['MtTime', 'Open', 'High','Low', 'Close', 'TickVolume'])

# Resulting in:
# TypeError: iteration over non-sequence

How can I convert this thing into a Panda DataFrame, so that I can plot this using the original names?


Plotting using matplotlib should then be possible with something like this:


import matplotlib.pyplot as plt
import pandas as pd
from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()
...

df = pd.DataFrame(rA)   # not working

df['time'] = pd.to_datetime(df['MtTime'], unit='s')
plt.plot(df['MtTime'], df['Open'], 'r-', label='Open')
plt.plot(df['MtTime'], df['Close'], 'b-', label='Close')
plt.legend(loc='upper left')
plt.title('EURAUD candles')
plt.show()

Possibly related questions (but were not helpful to me):

not2qubit
  • 14,531
  • 8
  • 95
  • 135

1 Answers1

1

One idea is use list comprehension for extract values to list of tuples:

L = [(rA[i].MtTime, rA[i].Open, rA[i].High, rA[i].Low, rA[i].Close, rA[i].TickVolume) 
      for i in range(len(rA))]

df = pd.DataFrame(L, columns = ['MtTime', 'Open', 'High','Low', 'Close', 'TickVolume']))

Or if possible:

df = pd.DataFrame({'MtTime':list(rA.MtTime), 'Open':list(rA.Open), 
                   'High':list(rA.High),'Low':list(rA.Low), 
                   'Close':list(rA.Close), 'TickVolume':list(rA.TickVolume)})
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252