otp.CSV#
- class CSV(filepath_or_buffer=None, timestamp_name='Time', first_line_is_title=True, names=None, dtype={}, converters={}, order_ticks=False, drop_index=True, change_date_to=None, **kwargs)[source]#
Bases:
onetick.py.core.source.Source
Construct source based on CSV file.
There are several steps determining column types.
Initially, all column treated as
str
.If column name in CSV title have format
type COLUMNNAME
, it will change type fromstr
to specified type.All column type are determined automatically from its data.
You could override determined types in
dtype
argument explicitly.converters
argument is applied afterdtype
and could also change column type.
- Parameters
filepath_or_buffer (str, os.PathLike, FileBuffer) – Path to CSV file or
file buffer
timestamp_name (str, default "Time") – Name of TIMESTAMP column used for ticks. Used only if it is exists in CSV columns, otherwise ignored.
first_line_is_title (bool) –
Use first line of CSV file as a source for column names and types. If CSV file is started with # symbol, this parameter must be
True
.If
True
, column names are inferred from the first line of the file, it is not allowed to have empty name for any column.If
False
, first line is processed as data, column names will be COLUMN_1, …, COLUMN_N. You could specify column names innames
argument.
names (list, optional) – List of column names to use, or None. Length must be equal to columns number in file. Duplicates in this list are not allowed.
dtype (dict, optional) – Data type for columns, as dict of pairs {column_name: type}. Will convert column type from
str
to specified type, before applying converters.converters (dict, optional) –
Dict of functions for converting values in certain columns. Keys are column names. Function must be valid callable with
onetick.py
syntax, example:converters={ "time_number": lambda c: c.apply(otp.nsectime), "stock": lambda c: c.str.lower(), }
Converters applied after
dtype
conversion.order_ticks (bool, optional) – If
True
andtimestamp_name
column are used, then source will order tick by time. Note, that ifFalse
and ticks are not ordered in sequence, then OneTick will raise Exception in runtime.drop_index (bool, optional) – if
True
and ‘Index’ column is in the csv file then this column will be removed.change_date_to (datetime, date, optional) – change date from a timestamp column to a specific date. Default is None, means not changing timestamp column.
Examples
Simple CSV file reading
>>> data = otp.CSV(os.path.join(csv_path, "data.csv")) >>> otp.run(data) Time time_number px side 0 2003-12-01 00:00:00.000 1656690986953602304 30.89 Buy 1 2003-12-01 00:00:00.001 1656667706281508352 682.88 Buy
Read CSV file and get timestamp for ticks from specific field. You need to specify query start/end interval including all ticks.
>>> data = otp.CSV(os.path.join(csv_path, "data.csv"), ... timestamp_name="time_number", ... converters={"time_number": lambda c: c.apply(otp.nsectime)}, ... start=otp.dt(2010, 8, 1), ... end=otp.dt(2022, 9, 2)) >>> otp.run(data) Time px side 0 2022-07-01 11:56:26.953602304 30.89 Buy 1 2022-07-01 05:28:26.281508352 682.88 Buy
See also
CSV_FILE_LISTING OneTick event processor
otp.utils.file#
otp.utils.FileBuffer#
- class FileBuffer(path)[source]#
Bases:
object
Class holds the file content with goal to delivery it to the execution side in case of remote executions.
The basic implementation reads file content to a property that allows to transfer file content as pickled object to the server side since the pickling stores all class property values.
- Parameters
path (Union[str, os.PathLike]) –