0

I'm trying to create my own dataframe. My code

import pandas as pd
from datetime import datetime
import time
import MetaTrader5 as mt5

if not mt5.initialize():
    print("initialize() falhou")
    mt5.shutdown()
ativos = mt5.symbols_get()


mt5.copy_rates_from_pos('@DD', mt5.TIMEFRAME_M1, 0, 1)

def get_ohlc(ativo, timeframe, n=55):
    ativo = mt5.copy_rates_from_pos(ativo, timeframe, 0, n)
    ativo = pd.DataFrame(ativo)
    ativo['time']=pd.to_datetime(ativo['time'], unit='s')
    ativo.set_index('time', inplace=True)
    return ativo

get_ohlc('@DD', mt5.TIMEFRAME_M1)
info = mt5.symbol_info_tick('@DD')



tempo = time.time() + 100000000
while time.time() < tempo:
    tick = mt5.symbol_info_tick('@DD')
    print(f" TIME:{datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]} ,DAX:{tick.last}, BID:{tick.bid}, ASK:{tick.ask}, VOLUME:{tick.volume}, FLAG:{tick.flags}, REAL:{tick.volume_real} ", end='\n')
    time.sleep(0.1)

Program output (not all lines as an example)

TIME:2020-12-09 13:27:18.579 ,TICK LAST:13408.0, BID:13406.5, ASK:13407.5, VOLUME:1, FLAG:2, REAL:1.0 
 TIME:2020-12-09 13:27:18.687 ,TICK LAST:13408.0, BID:13406.5, ASK:13407.5, VOLUME:1, FLAG:2, REAL:1.0 
 TIME:2020-12-09 13:27:18.797 ,TICK LAST:13408.0, BID:13406.5, ASK:13407.5, VOLUME:1, FLAG:2, REAL:1.0 
 TIME:2020-12-09 13:27:18.906 ,TICK LAST:13408.0, BID:13406.5, ASK:13407.5, VOLUME:1, FLAG:2, REAL:1.0 
 TIME:2020-12-09 13:27:19.015 ,TICK LAST:13408.0, BID:13406.5, ASK:13407.5, VOLUME:1, FLAG:2, REAL:1.0 
 TIME:2020-12-09 13:27:19.125 ,TICK LAST:13408.0, BID:13406.5, ASK:13407.5, VOLUME:1, FLAG:2, REAL:1.0 
 TIME:2020-12-09 13:27:19.234 ,TICK LAST:13408.0, BID:13406.5, ASK:13407.5, VOLUME:1, FLAG:2, REAL:1.0 
 TIME:2020-12-09 13:27:19.343 ,TICK LAST:13408.0, BID:13406.5, ASK:13407.5, VOLUME:1, FLAG:2, REAL:1.0 
 TIME:2020-12-09 13:27:19.452 ,TICK LAST:13408.0, BID:13406.5, ASK:13407.5, VOLUME:1, FLAG:2, REAL:1.0 
 TIME:2020-12-09 13:27:19.561 ,TICK LAST:13408.0, BID:13406.5, ASK:13407.5, VOLUME:1, FLAG:2, REAL:1.0  

How to write the result of the program output to a csv file by columns? And the new data was recorded under the results of the previous values. It looks like this ..

TIME                       TCK LAST     BID       ASK      VOLUME  FLAG  REAL
2020-12-09 13:27:19.561    13408.0      13406.5   13407.5   1        2   1.0
xxxHEKETOSxxx
  • 57
  • 1
  • 9
  • https://stackoverflow.com/questions/2363731/append-new-row-to-old-csv-file-python may be of help, it shows how to add data in a format to a csv file, with and without the use of pandas – Paul Brennan Dec 15 '20 at 13:51
  • this is not what I need. or I can't figure out how to apply it . – xxxHEKETOSxxx Dec 16 '20 at 00:35

0 Answers0