fxcm / ForexConnectAPI

Designed to trade, retrieve live/history price. Intended to be used to build auto-trading robots, custom trading on FXCM accounts.
132 stars 47 forks source link

Tick data timestamps in ms #4

Closed Sdoof closed 3 years ago

Sdoof commented 4 years ago

It seems there is timestamp bug when it comes to Tick, because the timestamp for each update is not set to milliseconds, only showing the seconds.

For example I currently get the following timestamps. 2019-06-24 19:13:43 2019-06-24 19:13:43 2019-06-24 19:13:43 2019-06-24 19:13:43 2019-06-24 19:13:43

Should be something like this. 2019-06-24 19:13:43.022 2019-06-24 19:13:43.053 2019-06-24 19:13:43.068 2019-06-24 19:13:43.070

This is a fairly critical bug because there is no way to eliminate 'out of order' ticks/updates, what is the best way to fix this?

import pandas as pd
import datetime
import csv
import numpy as np
data_file='C:\\Users\\FOREXCONNECT_DB.csv'

from forexconnect import fxcorepy, ForexConnect

def session_status_changed(session: fxcorepy.O2GSession,
                           status: fxcorepy.AO2GSessionStatus.O2GSessionStatus):
    print("Trading session status: " + str(status))

def main():
    with ForexConnect() as fx:
        try:
            fx.login(REAL_ACCOUNT_ID, PASSWORD, "fxcorporate.com/Hosts.jsp",
                     "Real", session_status_callback=session_status_changed)
            history = fx.get_history("EUR/USD", "t1",
                                     datetime.datetime.strptime("08.06.2020 17:51:21.000", '%m.%d.%Y %H:%M:%S.%f').replace(
                                         tzinfo=datetime.timezone.utc),
                                     datetime.datetime.strptime("08.06.2020 18:00:21.000", '%m.%d.%Y %H:%M:%S.%f').replace(
                                         tzinfo=datetime.timezone.utc))
            np.savetxt(data_file, history, delimiter=",")

            print("Date, Bid, Ask")
            for row in history:
                print("{0:s}, {1:,.5f}, {2:,.5f}".format(
                    pd.to_datetime(str(row['Date'])).strftime('%m.%d.%Y %H:%M:%S.%f'), row['Bid'], row['Ask']))
                    # row['BidLow'], row['BidClose'], row['Volume']))

        except Exception as e:
            print("Exception: " + str(e))

        try:
            fx.logout()
        except Exception as e:
            print("Exception: " + str(e))

if __name__ == "__main__":
    main()
fxcm-dhalpert commented 4 years ago

The milliseconds is not included in the price report, if you just need it for DB index, then you can put some sequence number in the millisecond field.

Sdoof commented 4 years ago

Thanks for the reply, one last thing, how to identify the first date & time (%m.%d.%Y %H:%M:%S) where tick data is HISTORICALLY available for the first time. In other words as of today how far back can I retrieve TICK data. At the moment I am using trial and error by changing the date_from argument, It would help if you could share the storage rules on your server and the date where those ticks are first available as of today?

fxcm-dhalpert commented 4 years ago

you can check if tada is tick with:

history = fx.get_history(str_instrument, str_timeframe, date_from, date_to, quotes_count) currentunit, = ForexConnect.parse_timeframe(str_timeframe) if current_unit == fxcorepy.O2GTimeFrameUnit.TICK: