0

I have imported information about some stocks through a loop through the MetaTrader 5 module.

import MetaTrader5 as mt5  
 
tickers = ['Apple', 'Amazon', 'Facebook', 'Microsoft'] 
results = {}

for ticker in tickers:
    results[ticker] = mt5.copy_rates_range(ticker, mt5.TIMEFRAME_M1, inicio, fin)
    results[ticker] = pd.DataFrame(results[ticker]).set_index('time')

The data has been stored in results [ticker]. For example, when ticker = 'Apple'

results['Apple']

{'Apple':               open    high     low   close  tick_volume  spread  real_volume
 time                                                                        
 1606149300  117.33  117.55  117.31  117.47          126      12            0
 1606149360  117.48  117.54  117.31  117.39          134      12            0
 1606149420  117.38  117.54  117.36  117.41           95      12            0
 1606149480  117.43  117.47  117.32  117.33           90      12            0
 1606149540  117.32  117.33  117.24  117.26          123      12            0
 ...            ...     ...     ...     ...          ...     ...          ...

when ticker = 'Amazon'

results['Amazon']

open    high    low close   tick_volume spread  real_volume
time                            
1606149300  3114.25 3132.43 3114.25 3131.28 44  429 0
1606149360  3131.28 3133.25 3122.69 3131.52 83  450 0
1606149420  3131.52 3132.12 3122.69 3130.11 61  449 0
1606149480  3127.53 3135.92 3122.69 3127.05 80  448 0
1606149540  3129.77 3135.54 3123.50 3131.98 49  441 0
... ... ... ... ... ... ... ...

My question is how can I join all these tables into a single DataFrame? For example, the 'close' column for each of the tickers in a single DataFrame as in the example below

CLOSE       Apple Amazon Microsoft ETC...
time                            
1606149300  3114.25 3132.43 3114.25 
1606149360  3131.28 3133.25 3122.69 
1606149420  3131.52 3132.12 3122.69 
1606149480  3127.53 3135.92 3122.69 
1606149540  3129.77 3135.54 3123.50 
... ... ... ... ... ... ... ...

Thanks in advance for the help

4jano20
  • 25
  • 1
  • 5

1 Answers1

0

You could try with the join function in Pandas.

merged_df = results[tickers[0]]
for t in tickers[1:]:
    merged_df = merged_df.merge(results[t][['close']], left_index=True, right_index=True)

I hope it solves your problem!

MoriSan
  • 1
  • 2