pyqstrat package

Submodules

pyqstrat.pq_utils module

pyqstrat.pq_utils.async_sleep(secs)[source]
Return type

None

pyqstrat.pq_utils.async_waitfor(predicate_func, timeout_secs=5)[source]

Keep yielding until either predicate is true or timeout elapses Returns when predicate is True. If timeout elapses we raise an exception

Return type

None

pyqstrat.pq_utils.async_yield()[source]

yield so any other async tasks that are ready can run

Return type

None

pyqstrat.pq_utils.date_2_num(d)[source]

Adopted from matplotlib.mdates.date2num so we don’t have to add a dependency on matplotlib here

Return type

Union[int, ndarray]

pyqstrat.pq_utils.day_of_week_num(a)[source]

From https://stackoverflow.com/questions/52398383/finding-day-of-the-week-for-a-datetime64 Get day of week for a numpy array of datetimes Monday is 0, Sunday is 6

Parameters

a (Union[datetime64, ndarray]) – numpy datetime64 or array of datetime64

Returns

Monday is 0, Sunday is 6

Return type

int or numpy ndarray of int

>>> day_of_week_num(np.datetime64('2015-01-04'))
6
pyqstrat.pq_utils.day_symbol(day_int)[source]
Return type

Union[str, ndarray]

pyqstrat.pq_utils.get_child_logger(child_name)[source]
Return type

Logger

pyqstrat.pq_utils.get_empty_np_value(np_dtype)[source]

Get empty value for a given numpy datatype >>> a = np.array([‘2018-01-01’, ‘2018-01-03’], dtype = ‘M8[D]’) >>> get_empty_np_value(a.dtype) numpy.datetime64(‘NaT’)

Return type

Any

pyqstrat.pq_utils.get_main_logger()[source]
Return type

Logger

pyqstrat.pq_utils.get_temp_dir()[source]
Return type

str

pyqstrat.pq_utils.has_display()[source]

If we are running in unit test mode or on a server, then don’t try to draw graphs, etc.

Return type

bool

pyqstrat.pq_utils.in_ipython()[source]

Whether we are running in an ipython (or Jupyter) environment

Return type

bool

pyqstrat.pq_utils.infer_compression(input_filename)[source]

Infers compression for a file from its suffix. For example, given “/tmp/hello.gz”, this will return “gzip” >>> infer_compression(“/tmp/hello.gz”) ‘gzip’ >>> infer_compression(“/tmp/abc.txt”) is None True

Return type

Optional[str]

pyqstrat.pq_utils.infer_frequency(timestamps)[source]

Returns most common frequency of date differences as a fraction of days :type timestamps: ndarray :param timestamps: A numpy array of monotonically increasing datetime64

>>> timestamps = np.array(['2018-01-01 11:00:00', '2018-01-01 11:15:00', '2018-01-01 11:30:00', '2018-01-01 11:35:00'], dtype = 'M8[ns]')
>>> print(round(infer_frequency(timestamps), 8))
0.01041667
Return type

float

pyqstrat.pq_utils.is_newer(filename, ref_filename)[source]

whether filename ctime (modfication time) is newer than ref_filename or either file does not exist >>> import time >>> import tempfile >>> temp_dir = tempfile.gettempdir() >>> touch(f’{temp_dir}/x.txt’) >>> time.sleep(0.1) >>> touch(f’{temp_dir}/y.txt’) >>> is_newer(f’{temp_dir}/y.txt’, f’{temp_dir}/x.txt’) True >>> touch(f’{temp_dir}/y.txt’) >>> time.sleep(0.1) >>> touch(f’{temp_dir}/x.txt’) >>> is_newer(f’{temp_dir}/y.txt’, f’{temp_dir}/x.txt’) False

Return type

bool

pyqstrat.pq_utils.linear_interpolate(a1, a2, x1, x2, x)[source]
>>> print(f'{linear_interpolate(3, 4, 8, 10, 8.9):.3f}')
3.450
Return type

float

pyqstrat.pq_utils.millis_since_epoch(dt)[source]

Given a python datetime, return number of milliseconds between the unix epoch and the datetime. Returns a float since it can contain fractions of milliseconds as well >>> millis_since_epoch(datetime.datetime(2018, 1, 1)) 1514764800000.0

Return type

float

pyqstrat.pq_utils.monotonically_increasing(array)[source]

Returns True if the array is monotonically_increasing, False otherwise

>>> monotonically_increasing(np.array(['2018-01-02', '2018-01-03'], dtype = 'M8[D]'))
True
>>> monotonically_increasing(np.array(['2018-01-02', '2018-01-02'], dtype = 'M8[D]'))
False
Return type

bool

pyqstrat.pq_utils.nan_to_zero(array)[source]

Converts any nans in a numpy float array to 0

Return type

ndarray

pyqstrat.pq_utils.np_find_closest(a, v)[source]

From https://stackoverflow.com/questions/8914491/finding-the-nearest-value-and-return-the-index-of-array-in-python Find index of closest value to array v in array a. Returns an array of the same size as v a must be sorted >>> assert(all(np_find_closest(np.array([3, 4, 6]), np.array([4, 2])) == np.array([1, 0])))

Return type

int

pyqstrat.pq_utils.np_get_index(array, value)[source]

Get index of a value in a numpy array. Returns -1 if the value does not exist.

Return type

int

pyqstrat.pq_utils.np_rolling_window(a, window)[source]

For applying rolling window functions to a numpy array See: https://stackoverflow.com/questions/6811183/rolling-window-for-1d-arrays-in-numpy >>> print(np.std(np_rolling_window(np.array([1, 2, 3, 4]), 2), 1)) [0.5 0.5 0.5]

Return type

ndarray

pyqstrat.pq_utils.np_round(a, clip)[source]

Round all elements in an array to the nearest clip

Parameters
  • a (ndarray) – array with elements to round

  • clip (float) – rounding value

>>> np_round(15.8, 0.25)
15.75
pyqstrat.pq_utils.percentile_of_score(a)[source]

For each element in a, find the percentile of a its in. From stackoverflow.com/a/29989971/5351549 Like scipy.stats.percentileofscore but runs in O(n log(n)) time. >>> a = np.array([4, 3, 1, 2, 4.1]) >>> percentiles = percentile_of_score(a) >>> assert(all(np.isclose(np.array([ 75., 50., 0., 25., 100.]), percentiles)))

Return type

ndarray

pyqstrat.pq_utils.remove_dups(l, key_func=None)[source]

Remove duplicates from a list :type l: Sequence[Any] :param l: list to remove duplicates from :type key_func: Optional[Callable[[Any], Any]] :param key_func: A function that takes a list element and converts it to a key for detecting dups

Returns (List): A list with duplicates removed. This is stable in the sense that original list elements will retain their order

>>> print(remove_dups(['a', 'd', 'a', 'c']))
['a', 'd', 'c']
>>> print(remove_dups(['a', 'd', 'A']))
['a', 'd', 'A']
>>> print(remove_dups(['a', 'd', 'A'], key_func = lambda e: e.upper()))
['a', 'd']
Return type

MutableSequence[Any]

pyqstrat.pq_utils.resample_trade_bars(df, sampling_frequency, resample_funcs=None)[source]

Downsample trade bars using sampling frequency

Parameters
  • df (pd.DataFrame) – Must contain an index of numpy datetime64 type which is monotonically increasing

  • sampling_frequency (str) – See pandas frequency strings

  • (dict of str (resample_funcs) – int): a dictionary of column name -> resampling function for any columns that are custom defined. Default None. If there is no entry for a custom column, defaults to ‘last’ for that column

Returns

Resampled dataframe

Return type

pd.DataFrame

>>> import math
>>> df = pd.DataFrame({'date': np.array(['2018-01-08 15:00:00', '2018-01-09 13:30:00', '2018-01-09 15:00:00', '2018-01-11 15:00:00'], dtype = 'M8[ns]'),
...          'o': np.array([8.9, 9.1, 9.3, 8.6]),
...          'h': np.array([9.0, 9.3, 9.4, 8.7]),
...          'l': np.array([8.8, 9.0, 9.2, 8.4]),
...          'c': np.array([8.95, 9.2, 9.35, 8.5]),
...          'v': np.array([200, 100, 150, 300]),
...          'x': np.array([300, 200, 100, 400])
...         })
>>> df['vwap'] =  0.5 * (df.l + df.h)
>>> df.set_index('date', inplace = True)
>>> df = resample_trade_bars(df, sampling_frequency = 'D', resample_funcs={'x': lambda df,
...   sampling_frequency: df.x.resample(sampling_frequency).agg(np.mean)})
>>> assert(len(df) == 4)
>>> assert(math.isclose(df.vwap.iloc[1], 9.24))
>>> assert(np.isnan(df.vwap.iloc[2]))
>>> assert(math.isclose(df.l[3], 8.4))
pyqstrat.pq_utils.resample_ts(dates, values, sampling_frequency)[source]

Downsample a tuple of datetimes and value arrays using sampling frequency, using the last value if it does not exist at the bin edge. See pandas.Series.resample

Parameters
  • dates (ndarray) – a numpy datetime64 array

  • values (ndarray) – a numpy array

  • sampling_frequency (str) – See pandas frequency strings

Return type

Tuple[ndarray, ndarray]

Returns

Resampled tuple of datetime and value arrays

pyqstrat.pq_utils.resample_vwap(df, sampling_frequency)[source]

Compute weighted average of vwap given higher frequency vwap and volume

Return type

ndarray

pyqstrat.pq_utils.series_to_array(series)[source]

Convert a pandas series to a numpy array. If the object is not a pandas Series return it back unchanged

Return type

ndarray

pyqstrat.pq_utils.set_defaults(df_float_sf=8, df_display_max_rows=200, df_display_max_columns=99, np_seterr='raise', plot_style='ggplot', mpl_figsize=(8, 6))[source]

Set some display defaults to make it easier to view dataframes and graphs.

Parameters
  • df_float_sf (int) – Number of significant figures to show in dataframes (default 4). Set to None to use pandas defaults

  • df_display_max_rows (int) – Number of rows to display for pandas dataframes when you print them (default 200). Set to None to use pandas defaults

  • df_display_max_columns (int) – Number of columns to display for pandas dataframes when you print them (default 99). Set to None to use pandas defaults

  • np_seterr (str) – Error mode for numpy warnings. See numpy seterr function for details. Set to None to use numpy defaults

  • plot_style (str) – Style for matplotlib plots. Set to None to use default plot style.

  • mpl_figsize (Tuple[int, int]) – Default figure size to use when displaying matplotlib plots (default 8,6). Set to None to use defaults

Return type

None

pyqstrat.pq_utils.shift_np(array, n, fill_value=None)[source]

Similar to pandas.Series.shift but works on numpy arrays.

Parameters
  • array (ndarray) – The numpy array to shift

  • n (int) – Number of places to shift, can be positive or negative

  • fill_value (Optional[Any]) – After shifting, there will be empty slots left in the array. If set, fill these with fill_value. If fill_value is set to None (default), we will fill these with False for boolean arrays, np.nan for floats

Return type

ndarray

pyqstrat.pq_utils.str2date(s)[source]

Converts a string like “2008-01-15 15:00:00” to a numpy datetime64. If s is not a string, return s back

Return type

datetime64

pyqstrat.pq_utils.strtup2date(tup)[source]

Converts a string tuple like (“2008-01-15”, “2009-01-16”) to a numpy datetime64 tuple. If the tuple does not contain strings, return it back unchanged

Return type

Tuple[datetime64, datetime64]

pyqstrat.pq_utils.to_csv(df, file_name, index=False, compress=False, *args, **kwargs)[source]

Creates a temporary file then renames to the permanent file so we don’t have half written files. Also optionally compresses using the xz algorithm

Return type

None

pyqstrat.pq_utils.touch(fname, mode=438, dir_fd=None, **kwargs)[source]

replicate unix touch command, i.e create file if it doesn’t exist, otherwise update timestamp

Return type

None

pyqstrat.pq_utils.zero_to_nan(array)[source]

Converts any zeros in a numpy array to nans

Return type

ndarray

pyqstrat.pq_types module

class pyqstrat.pq_types.Contract[source]

Bases: object

static clear()[source]

When running Python interactively you may create a Contract with a given symbol multiple times because you don’t restart Python therefore global variables are not cleared. This function clears global Contracts

Return type

None

contracts_by_symbol = None

A contract such as a stock, option or a future that can be traded

static create(symbol, contract_group, expiry=None, multiplier=1.0, properties=None)[source]
Parameters
  • symbol (str) – A unique string reprenting this contract. e.g IBM or ESH9

  • contract_group (ContractGroup) – We sometimes need to group contracts for calculating PNL, for example, you may have a strategy which has 3 legs, a long option, a short option and a future or equity used to hedge delta. In this case, you will be trading different symbols over time as options and futures expire, but you may want to track PNL for each leg using a contract group for each leg. So you could create contract groups ‘Long Option’, ‘Short Option’ and ‘Hedge’ and assign contracts to these.

  • expiry (Union[datetime64, datetime, None]) – In the case of a future or option, the date and time when the contract expires. For equities and other non expiring contracts, set this to None. Default None.

  • multiplier (float) – If the market price convention is per unit, and the unit is not the same as contract size, set the multiplier here. For example, for E-mini contracts, each contract is 50 units and the price is per unit, so multiplier would be 50. Default 1

  • properties (Optional[SimpleNamespace]) – Any data you want to store with this contract. For example, you may want to store option strike. Default None

Return type

Contract

class pyqstrat.pq_types.ContractGroup[source]

Bases: object

A way to group contracts for figuring out which indicators, rules and signals to apply to a contract and for PNL reporting

add_contract(contract)[source]
static clear()[source]

When running Python interactively you may create a ContractGroup with a given name multiple times because you don’t restart Python therefore global variables are not cleared. This function clears global ContractGroups

Return type

None

static create(name)[source]
Parameters

name (str) – Name of the group

Return type

ContractGroup

get_contract(symbol)[source]
class pyqstrat.pq_types.LimitOrder(contract, timestamp, qty, limit_price, reason_code='none', properties=None, status='open')[source]

Bases: pyqstrat.pq_types.Order

__init__(contract, timestamp, qty, limit_price, reason_code='none', properties=None, status='open')[source]
Parameters
  • contract (Contract) – The contract this order is for

  • timestamp (datetime64) – Time the order was placed

  • qty (float) – Number of contracts or shares. Use a negative quantity for sell orders

  • limit_price (float) – Limit price (float)

  • reason_code (str) – The reason this order was created. Prefer a predefined constant from the ReasonCode class if it matches your reason for creating this order. Default None

  • properties (Optional[SimpleNamespace]) – Any order specific data we want to store. Default None

  • status (str) – Status of the order, “open”, “filled”, etc. (default “open”)

Return type

None

class pyqstrat.pq_types.MarketOrder(contract, timestamp, qty, reason_code='none', properties=None, status='open')[source]

Bases: pyqstrat.pq_types.Order

__init__(contract, timestamp, qty, reason_code='none', properties=None, status='open')[source]
Parameters
  • contract (Contract) – The contract this order is for

  • timestamp (datetime64) – Time the order was placed

  • qty (float) – Number of contracts or shares. Use a negative quantity for sell orders

  • reason_code (str) – The reason this order was created. Prefer a predefined constant from the ReasonCode class if it matches your reason for creating this order. Default None

  • properties (Optional[SimpleNamespace]) – Any order specific data we want to store. Default None

  • status (str) – Status of the order, “open”, “filled”, etc. Default “open”

Return type

None

class pyqstrat.pq_types.Order[source]

Bases: object

class pyqstrat.pq_types.OrderStatus[source]

Bases: object

Enum for order status

FILLED = 'filled'
OPEN = 'open'
class pyqstrat.pq_types.ReasonCode[source]

Bases: object

A class containing constants for predefined order reason codes. Prefer these predefined reason codes if they suit the reason you are creating your order. Otherwise, use your own string.

BACKTEST_END = 'backtest end'
ENTER_LONG = 'enter long'
ENTER_SHORT = 'enter short'
EXIT_LONG = 'exit long'
EXIT_SHORT = 'exit short'
MARKER_PROPERTIES = {'backtest end': {'color': 'green', 'size': 50, 'symbol': '*'}, 'enter long': {'color': 'blue', 'size': 50, 'symbol': 'P'}, 'enter short': {'color': 'red', 'size': 50, 'symbol': 'P'}, 'exit long': {'color': 'blue', 'size': 50, 'symbol': 'X'}, 'exit short': {'color': 'red', 'size': 50, 'symbol': 'X'}, 'none': {'color': 'green', 'size': 50, 'symbol': 'o'}, 'roll future': {'color': 'green', 'size': 50, 'symbol': '>'}}
NONE = 'none'
ROLL_FUTURE = 'roll future'
class pyqstrat.pq_types.RollOrder(contract, timestamp, close_qty, reopen_qty, reason_code='roll future', properties=None, status='open')[source]

Bases: pyqstrat.pq_types.Order

A roll order is used to roll a future from one series to the next. It represents a sell of one future and the buying of another future.

__init__(contract, timestamp, close_qty, reopen_qty, reason_code='roll future', properties=None, status='open')[source]
Parameters
  • contract (Contract) – The contract this order is for

  • timestamp (datetime64) – Time the order was placed

  • close_qty (float) – Quantity of the future you are rolling

  • reopen_qty (float) – Quantity of the future you are rolling to

  • reason_code (str) – The reason this order was created. Prefer a predefined constant from the ReasonCode class if it matches your reason for creating this order. Default None

  • properties (Optional[SimpleNamespace]) – Any order specific data we want to store. Default None

  • status (str) – Status of the order, “open”, “filled”, etc. (default “open”)

Return type

None

class pyqstrat.pq_types.StopLimitOrder(contract, timestamp, qty, trigger_price, limit_price=nan, reason_code='none', properties=None, status='open')[source]

Bases: pyqstrat.pq_types.Order

Used for stop loss or stop limit orders. The order is triggered when price goes above or below trigger price, depending on whether this is a short or long order. Becomes either a market or limit order at that point, depending on whether you set the limit price or not.

__init__(contract, timestamp, qty, trigger_price, limit_price=nan, reason_code='none', properties=None, status='open')[source]
Parameters
  • contract (Contract) – The contract this order is for

  • timestamp (datetime64) – Time the order was placed

  • qty (float) – Number of contracts or shares. Use a negative quantity for sell orders

  • trigger_price (float) – Order becomes a market or limit order if price crosses trigger_price.

  • limit_price (float) – If not set (default), order becomes a market order when price crosses trigger price. Otherwise it becomes a limit order. Default np.nan

  • reason_code (str) – The reason this order was created. Prefer a predefined constant from the ReasonCode class if it matches your reason for creating this order. Default None

  • properties (Optional[SimpleNamespace]) – Any order specific data we want to store. Default None

  • status (str) – Status of the order, “open”, “filled”, etc. (default “open”)

Return type

None

class pyqstrat.pq_types.Trade(contract, order, timestamp, qty, price, fee=0.0, commission=0.0, properties=None)[source]

Bases: object

__init__(contract, order, timestamp, qty, price, fee=0.0, commission=0.0, properties=None)[source]
Parameters
  • contract (Contract) – The contract we traded

  • order (Order) – A reference to the order that created this trade. Default None

  • timestamp (datetime64) – Trade execution datetime

  • qty (float) – Number of contracts or shares filled

  • price (float) – Trade price

  • fee (float) – Fees paid to brokers or others. Default 0

  • commision – Commission paid to brokers or others. Default 0

  • properties (Optional[SimpleNamespace]) – Any data you want to store with this contract. For example, you may want to store bid / ask prices at time of trade. Default None

Return type

None

pyqstrat.holiday_calendars module

class pyqstrat.holiday_calendars.Calendar(holidays)[source]

Bases: object

EUREX = 'eurex'
NYSE = 'nyse'
__init__(holidays)[source]

Do not use this function directly. Use Calendar.get_calendar instead :type holidays: ndarray :param holidays: holidays for this calendar, excluding weekends :type holidays: np.array of datetime64[D]

Return type

None

static add_calendar(exchange_name, holidays)[source]

Add a trading calendar to the class level calendars dict

Parameters
  • exchange_name (str) – Name of the exchange.

  • holidays (ndarray) – holidays for this exchange, excluding weekends

Return type

None

add_trading_days(start, num_days, roll='raise')[source]

Adds trading days to a start date

Parameters
  • start (Union[datetime64, Series, ndarray, str, datetime, date]) – start datetimes(s)

  • num_days (int) – number of trading days to add

  • roll (str) – one of ‘raise’, ‘nat’, ‘forward’, ‘following’, ‘backward’, ‘preceding’, ‘modifiedfollowing’, ‘modifiedpreceding’ or ‘allow’} ‘allow’ is a special case in which case, adding 1 day to a holiday will act as if it was not a holiday, and give you the next business day’ The rest of the values are the same as in the numpy busday_offset function From numpy documentation: How to treat dates that do not fall on a valid day. The default is ‘raise’. ‘raise’ means to raise an exception for an invalid day. ‘nat’ means to return a NaT (not-a-time) for an invalid day. ‘forward’ and ‘following’ mean to take the first valid day later in time. ‘backward’ and ‘preceding’ mean to take the first valid day earlier in time. ‘modifiedfollowing’ means to take the first valid day later in time unless it is across a Month boundary, in which case to take the first valid day earlier in time. ‘modifiedpreceding’ means to take the first valid day earlier in time unless it is across a Month boundary, in which case to take the first valid day later in time.

Return type

Union[datetime64, ndarray]

Returns

The datetime num_days trading days after start

>>> calendar = Calendar.get_calendar(Calendar.NYSE)
>>> calendar.add_trading_days(datetime.date(2015, 12, 24), 1)
numpy.datetime64('2015-12-28')
>>> calendar.add_trading_days(np.datetime64('2017-04-15'), 0, roll = 'preceding') # 4/14/2017 is a Friday and a holiday
numpy.datetime64('2017-04-13')
>>> calendar.add_trading_days(np.datetime64('2017-04-08'), 0, roll = 'preceding') # 4/7/2017 is a Friday and not a holiday
numpy.datetime64('2017-04-07')
>>> calendar.add_trading_days(np.datetime64('2019-02-17 15:25'), 1, roll = 'allow')
numpy.datetime64('2019-02-19T15:25')
>>> calendar.add_trading_days(np.datetime64('2019-02-17 15:25'), -1, roll = 'allow')
numpy.datetime64('2019-02-15T15:25')
static get_calendar(exchange_name)[source]

Get a calendar object for the given exchange:

Parameters
  • exchange_name (str) – The exchange for which you want a calendar. Calendar.NYSE, Calendar.EUREX are predefined.

  • you want to add a new calendar, use the add_calendar class level function (If) –

Return type

Calendar

Returns

The calendar object

get_trading_days(start, end, include_first=False, include_last=True)[source]

Get back a list of numpy dates that are trading days between the start and end

>>> nyse = Calendar.get_calendar(Calendar.NYSE)
>>> nyse.get_trading_days('2005-01-01', '2005-01-08')
array(['2005-01-03', '2005-01-04', '2005-01-05', '2005-01-06', '2005-01-07'], dtype='datetime64[D]')
>>> nyse.get_trading_days(datetime.date(2005, 1, 1), datetime.date(2005, 2, 1))
array(['2005-01-03', '2005-01-04', '2005-01-05', '2005-01-06',
       '2005-01-07', '2005-01-10', '2005-01-11', '2005-01-12',
       '2005-01-13', '2005-01-14', '2005-01-18', '2005-01-19',
       '2005-01-20', '2005-01-21', '2005-01-24', '2005-01-25',
       '2005-01-26', '2005-01-27', '2005-01-28', '2005-01-31', '2005-02-01'], dtype='datetime64[D]')
>>> nyse.get_trading_days(datetime.date(2016, 1, 5), datetime.date(2016, 1, 29), include_last = False)
array(['2016-01-06', '2016-01-07', '2016-01-08', '2016-01-11',
       '2016-01-12', '2016-01-13', '2016-01-14', '2016-01-15',
       '2016-01-19', '2016-01-20', '2016-01-21', '2016-01-22',
       '2016-01-25', '2016-01-26', '2016-01-27', '2016-01-28'], dtype='datetime64[D]')
>>> nyse.get_trading_days('2017-07-04', '2017-07-08', include_first = False)
array(['2017-07-05', '2017-07-06', '2017-07-07'], dtype='datetime64[D]')
>>> nyse.get_trading_days(np.datetime64('2017-07-04'), np.datetime64('2017-07-08'), include_first = False)
array(['2017-07-05', '2017-07-06', '2017-07-07'], dtype='datetime64[D]')
Return type

Union[int, ndarray]

is_trading_day(dates)[source]

Returns whether the date is not a holiday or a weekend

Parameters

dates (Union[datetime64, Series, ndarray, str, datetime, date]) – date times to check

Return type

Union[bool, ndarray]

Returns

Whether this date is a trading day

>>> import datetime
>>> eurex = Calendar.get_calendar(Calendar.EUREX)
>>> eurex.is_trading_day('2016-12-25')
False
>>> eurex.is_trading_day(datetime.date(2016, 12, 22))
True
>>> nyse = Calendar.get_calendar(Calendar.NYSE)
>>> nyse.is_trading_day('2017-04-01') # Weekend
False
>>> nyse.is_trading_day(np.arange('2017-04-01', '2017-04-09', dtype = np.datetime64)) 
array([False, False,  True,  True,  True,  True,  True, False]...)
num_trading_days(start, end, include_first=False, include_last=True)[source]

Count the number of trading days between two date series including those two dates

>>> eurex = Calendar.get_calendar(Calendar.EUREX)
>>> eurex.num_trading_days('2009-01-01', '2011-12-31')
772
>>> dates = pd.date_range('20130101',periods=8)
>>> increments = np.array([5, 0, 3, 9, 4, 10, 15, 29])
>>> import warnings
>>> import pandas as pd
>>> warnings.filterwarnings(action = 'ignore', category = pd.errors.PerformanceWarning)
>>> dates2 = dates + increments * dates.freq
>>> df = pd.DataFrame({'x': dates, 'y' : dates2})
>>> df.iloc[4]['x'] = np.nan
>>> df.iloc[6]['y'] = np.nan
>>> nyse = Calendar.get_calendar(Calendar.NYSE)
>>> np.set_printoptions(formatter = {'float' : lambda x : f'{x:.1f}'})  # After numpy 1.13 positive floats don't have a leading space for sign
>>> print(nyse.num_trading_days(df.x, df.y))
[3.0 0.0 1.0 5.0 nan 8.0 nan 20.0]
Return type

Union[int, ndarray]

third_friday_of_month(month, year, roll='backward')[source]
>>> nyse = Calendar.get_calendar(Calendar.NYSE)
>>> nyse.third_friday_of_month(3, 2017)
numpy.datetime64('2017-03-17')
Return type

datetime64

pyqstrat.holiday_calendars.get_date_from_weekday(weekday, year, month, week)[source]

Return the date that falls on a given weekday (Monday = 0) on a week, year and month >>> get_date_from_weekday(1, 2019, 10, 4) numpy.datetime64(‘2019-10-22’)

Return type

datetime64

pyqstrat.holiday_calendars.read_holidays(calendar_name, dirname=None)[source]

Reads a csv with a holidays column containing holidays (not including weekends)

Return type

ndarray

pyqstrat.account module

class pyqstrat.account.Account(contract_groups, timestamps, price_function, strategy_context, starting_equity=1000000.0, pnl_calc_time=900)[source]

Bases: object

An Account calculates pnl for a set of contracts

__init__(contract_groups, timestamps, price_function, strategy_context, starting_equity=1000000.0, pnl_calc_time=900)[source]
Parameters
  • contract_groups (Sequence[ContractGroup]) – Contract groups that we want to compute PNL for

  • timestamps (ndarray) – Timestamps that we might compute PNL at

  • price_function (Callable[[Contract, ndarray, int, SimpleNamespace], float]) – Function that returns contract prices used to compute pnl

  • strategy_context (SimpleNamespace) – This is passed into the price function so we can use current state of strategy to compute prices

  • starting_equity (float) – Starting equity in account currency. Default 1.e6

  • pnl_calc_time (int) – Number of minutes past midnight that we should calculate PNL at. Default 15 * 60, i.e. 3 pm

Return type

None

add_trades(trades)[source]
Return type

None

calc(timestamp)[source]

Computes P&L and stores it internally for all contracts.

Parameters

timestamp (datetime64) – timestamp to compute P&L at. Account remembers the last timestamp it computed P&L up to and will compute P&L between these and including timestamp. If there is more than one day between the last index and current index, we will include pnl for at the defined pnl_calc_time for those dates as well.

Return type

None

df_account_pnl(contract_group=None)[source]

Returns PNL at the account level.

Parameters

contract_group (Optional[ContractGroup]) – If set, we only return pnl for this contract_group. Otherwise we return pnl for all contract groups

Return type

DataFrame

df_pnl(contract_groups=None)[source]

Returns a dataframe with P&L columns broken down by contract group and symbol

Parameters

contract_group – Return PNL for this contract group. If None (default), include all contract groups

Return type

DataFrame

df_trades(contract_group=None, start_date=None, end_date=None)[source]

Returns a dataframe of trades

Parameters
  • contract_group (Optional[ContractGroup]) – Return trades for this contract group. If None (default), include all contract groups

  • start_date (Optional[datetime64]) – Include trades with date greater than or equal to this timestamp.

  • end_date (Optional[datetime64]) – Include trades with date less than or equal to this timestamp.

Return type

DataFrame

equity(timestamp)[source]

Returns equity in this account in Account currency. Will cause calculation if Account has not previously calculated up to this date

Return type

float

position(contract_group, timestamp)[source]

Returns netted position for a contract_group at a given date in number of contracts or shares.

Return type

float

positions(contract_group, timestamp)[source]

Returns all non-zero positions in a contract group

Return type

MutableSequence[Tuple[Contract, float]]

symbols()[source]
Return type

MutableSequence[str]

trades(contract_group=None, start_date=None, end_date=None)[source]

Returns a list of trades with the given symbol and with trade date between (and including) start date and end date if they are specified. If symbol is None trades for all symbols are returned

Return type

MutableSequence[Trade]

class pyqstrat.account.ContractPNL(contract, account_timestamps, price_function, strategy_context)[source]

Bases: object

Computes pnl for a single contract over time given trades and market data

calc_net_pnl(timestamp)[source]
Return type

None

df()[source]

Returns a pandas dataframe with pnl data

Return type

DataFrame

net_pnl(timestamp)[source]
Return type

float

pnl(timestamp)[source]
Return type

Tuple[float, float, float, float, float, float, float]

position(timestamp)[source]
Return type

float

pyqstrat.account.calc_trade_pnl(open_qtys, open_prices, new_qtys, new_prices, multiplier)[source]
>>> print(calc_trade_pnl(
...          open_qtys = np.array([], dtype = np.float), open_prices = np.array([], dtype = np.float),
...            new_qtys = np.array([-8, 9, -4]), new_prices = np.array([10, 11, 6]), multiplier = 100))
(array([-3.]), array([6.]), -3.0, 6.0, -1300.0)
>>> print(calc_trade_pnl(open_qtys = np.array([], dtype = np.float), open_prices = np.array([], dtype = np.float), new_qtys = np.array([3, 10, -5]),
...          new_prices = np.array([51, 50, 45]), multiplier = 100))
(array([8.]), array([50.]), 8.0, 50.0, -2800.0)
>>> print(calc_trade_pnl(open_qtys = np.array([]), open_prices = np.array([]),
...                new_qtys = np.array([-58, -5, -5, 6, -8, 5, 5, -5, 19, 7, 5, -5, 39]),
...                new_prices = np.array([2080, 2075.25, 2070.75, 2076, 2066.75, 2069.25, 2074.75, 2069.75, 2087.25, 2097.25, 2106, 2088.25, 2085.25]),
...                multiplier = 50))
(array([], dtype=float64), array([], dtype=float64), 0.0, 0, -33762.5)
Return type

Tuple[ndarray, ndarray, float, float, float]

pyqstrat.account.find_index_before(sorted_dict, key)[source]

Find index of the first key in a sorted dict that is less than or equal to the key passed in. If the key is less than the first key in the dict, return -1

Return type

int

pyqstrat.account.find_last_non_nan_index(array)[source]
Return type

int

pyqstrat.account.leading_nan_to_zero(df, columns)[source]
Return type

DataFrame

pyqstrat.account.test_account()[source]

pyqstrat.strategy module

class pyqstrat.strategy.Strategy(timestamps, contract_groups, price_function, starting_equity=1000000.0, pnl_calc_time=901, trade_lag=0, run_final_calc=True, strategy_context=None)[source]

Bases: object

__init__(timestamps, contract_groups, price_function, starting_equity=1000000.0, pnl_calc_time=901, trade_lag=0, run_final_calc=True, strategy_context=None)[source]
Parameters
  • timestamps (np.array of np.datetime64) – The “heartbeat” of the strategy. We will evaluate trading rules and simulate the market at these times.

  • contract_groups (Sequence[ContractGroup]) – The contract groups we will potentially trade.

  • price_function (Callable[[Contract, ndarray, int, SimpleNamespace], float]) – A function that returns the price of a contract at a given timestamp

  • starting_equity (float) – Starting equity in Strategy currency. Default 1.e6

  • pnl_calc_time (int) – Time of day used to calculate PNL. Default 15 * 60 (3 pm)

  • trade_lag (int) – Number of bars you want between the order and the trade. For example, if you think it will take 5 seconds to place your order in the market, and your bar size is 1 second, set this to 5. Set this to 0 if you want to execute your trade at the same time as you place the order, for example, if you have daily bars. Default 0.

  • run_final_calc (bool) – If set, calculates unrealized pnl and net pnl as well as realized pnl when strategy is done. If you don’t need unrealized pnl, turn this off for faster run time. Default True

  • strategy_context (Optional[SimpleNamespace]) – A storage class where you can store key / value pairs relevant to this strategy. For example, you may have a pre-computed table of correlations that you use in the indicator or trade rule functions. If not set, the __init__ function will create an empty member strategy_context object that you can access.

Return type

None

add_indicator(name, indicator, contract_groups=None, depends_on=None)[source]
Parameters
  • name (str) – Name of the indicator

  • indicator (Union[ndarray, Series, Callable[[ContractGroup, ndarray, SimpleNamespace, SimpleNamespace], ndarray]]) – A function that takes strategy timestamps and other indicators and returns a numpy array containing indicator values. The return array must have the same length as the timestamps object. Can also be a numpy array or a pandas Series in which case we just store the values.

  • contract_groups (Optional[Sequence[ContractGroup]]) – Contract groups that this indicator applies to. If not set, it applies to all contract groups. Default None.

  • depends_on (Optional[Sequence[str]]) – Names of other indicators that we need to compute this indicator. Default None.

Return type

None

add_market_sim(market_sim_function)[source]

Add a market simulator. A market simulator is a function that takes orders as input and returns trades.

Return type

None

add_rule(name, rule_function, signal_name, sig_true_values=None, position_filter=None)[source]
Add a trading rule. Trading rules are guaranteed to run in the order in which you add them. For example, if you set trade_lag to 0,

and want to exit positions and re-enter new ones in the same bar, make sure you add the exit rule before you add the entry rule to the strategy.

Parameters
  • name (str) – Name of the trading rule

  • rule_function (Callable[[ContractGroup, int, ndarray, SimpleNamespace, ndarray, Account, SimpleNamespace], Sequence[Order]]) – A trading rule function that returns a list of Orders

  • signal_name (str) – The strategy will call the trading rule function when the signal with this name matches sig_true_values

  • sig_true_values (Optional[Sequence[+T_co]]) – If the signal value at a bar is equal to one of these values, the Strategy will call the trading rule function. Default [TRUE]

  • position_filter (Optional[str]) – Can be “zero”, “nonzero” or None. Zero rules are only triggered when the corresponding contract positions are 0 Nonzero rules are only triggered when the corresponding contract positions are non-zero. If not set, we don’t look at position before triggering the rule. Default None

Return type

None

add_signal(name, signal_function, contract_groups=None, depends_on_indicators=None, depends_on_signals=None)[source]
Parameters
  • name (str) – Name of the signal

  • signal_function (function) – A function that takes timestamps and a dictionary of indicator value arrays and returns a numpy array containing signal values. The return array must have the same length as the input timestamps

  • contract_groups (list of ContractGroup, optional) – Contract groups that this signal applies to. If not set, it applies to all contract groups. Default None.

  • depends_on_indicators (list of str, optional) – Names of indicators that we need to compute this signal. Default None.

  • depends_on_signals (list of str, optional) – Names of other signals that we need to compute this signal. Default None.

Return type

None

df_data(contract_groups=None, add_pnl=True, start_date=None, end_date=None)[source]

Add indicators and signals to end of market data and return as a pandas dataframe.

Parameters
  • contract_groups (list of:obj:ContractGroup, optional) – list of contract groups to include. All if set to None (default)

  • add_pnl (bool) – If True (default), include P&L columns in dataframe

  • start_date (Union[datetime64, str, None]) – string or numpy datetime64. Default None

  • end_date (Union[datetime64, str, None]) – string or numpy datetime64: Default None

Return type

DataFrame

df_orders(contract_group=None, start_date=None, end_date=None)[source]

Returns a dataframe with data from orders with the given contract group and with order date between (and including) start date and end date if they are specified. If contract_group is None orders for all contract_groups are returned

Return type

DataFrame

df_pnl(contract_group=None)[source]

Returns a dataframe with P&L columns. If contract group is set to None (default), sums up P&L across all contract groups

Return type

DataFrame

df_returns(contract_group=None, sampling_frequency='D')[source]

Return a dataframe of returns and equity indexed by date.

Parameters
  • contract_group (Optional[ContractGroup]) – The contract group to get returns for. If set to None (default), we return the sum of PNL for all contract groups

  • sampling_frequency (str) – Downsampling frequency. Default is None. See pandas frequency strings for possible values

Return type

DataFrame

df_trades(contract_group=None, start_date=None, end_date=None)[source]

Returns a dataframe with data from trades with the given contract group and with trade date between (and including) start date and end date if they are specified. If contract_group is None trades for all contract_groups are returned

Return type

DataFrame

evaluate_returns(contract_group=None, plot=True, display_summary=True, float_precision=4, return_metrics=False)[source]

Returns a dictionary of common return metrics.

Parameters
  • contract_group (ContractGroup, optional) – Contract group to evaluate or None (default) for all contract groups

  • plot (bool) – If set to True, display plots of equity, drawdowns and returns. Default False

  • float_precision (float, optional) – Number of significant figures to show in returns. Default 4

  • return_metrics (bool, optional) – If set, we return the computed metrics as a dictionary

Return type

Optional[Mapping[~KT, +VT_co]]

orders(contract_group=None, start_date=None, end_date=None)[source]

Returns a list of orders with the given contract group and with order date between (and including) start date and end date if they are specified. If contract_group is None orders for all contract_groups are returned

Return type

Sequence[Order]

plot(contract_groups=None, primary_indicators=None, primary_indicators_dual_axis=None, secondary_indicators=None, secondary_indicators_dual_axis=None, indicator_properties=None, signals=None, signal_properties=None, pnl_columns=None, title=None, figsize=(20, 15), date_range=None, date_format=None, sampling_frequency=None, trade_marker_properties=None, hspace=0.15)[source]

Plot indicators, signals, trades, position, pnl

Parameters
  • contract_groups (Optional[Sequence[ContractGroup]]) – Contract groups to plot or None (default) for all contract groups.

  • indicators (primary) – List of indicators to plot in the main indicator section. Default None (plot everything)

  • indicators – List of indicators to plot in the secondary indicator section. Default None (don’t plot anything)

  • indicator_properties (Optional[Mapping[str, Mapping[str, Any]]]) – If set, we use the line color, line type indicated for the given indicators

  • signals (Optional[Sequence[str]]) – Signals to plot. Default None (plot everything).

  • plot_equity – If set, we plot the equity curve. Default is True

  • title (Optional[str]) – Title of plot. Default None

  • figsize (Tuple[int, int]) – Figure size. Default (20, 15)

  • date_range (Union[Tuple[str, str], Tuple[datetime64, datetime64], None]) – Used to restrict the date range of the graph. Default None

  • date_format (Optional[str]) – Date format for tick labels on x axis. If set to None (default), will be selected based on date range. See matplotlib date format strings

  • sampling_frequency (Optional[str]) – Downsampling frequency. The graph may get too busy if you have too many bars of data, so you may want to downsample before plotting. See pandas frequency strings for possible values. Default None.

  • trade_marker_properties (Optional[Mapping[str, Mapping[str, Any]]]) – A dictionary of order reason code -> marker shape, marker size, marker color for plotting trades with different reason codes. By default we use the dictionary from the ReasonCode class

  • hspace (float) – Height (vertical) space between subplots. Default is 0.15

Return type

None

plot_returns(contract_group=None)[source]

Display plots of equity, drawdowns and returns for the given contract group or for all contract groups if contract_group is None (default)

Return type

Optional[Tuple[Figure, Axes]]

run()[source]
Return type

None

run_indicators(indicator_names=None, contract_groups=None, clear_all=False)[source]

Calculate values of the indicators specified and store them.

Parameters
  • indicator_names (Optional[Sequence[str]]) – List of indicator names. If None (default) run all indicators

  • contract_groups (Optional[Sequence[ContractGroup]]) – Contract group to run this indicator for. If None (default), we run it for all contract groups.

  • clear_all (bool) – If set, clears all indicator values before running. Default False.

Return type

None

run_rules(rule_names=None, contract_groups=None, start_date=None, end_date=None)[source]

Run trading rules.

Parameters
  • rule_names (Optional[Sequence[str]]) – List of rule names. If None (default) run all rules

  • contract_groups (Optional[Sequence[ContractGroup]]) – Contract groups to run this rule for. If None (default), we run it for all contract groups.

  • start_date (Optional[datetime64]) – Run rules starting from this date. Default None

  • end_date (Optional[datetime64]) – Don’t run rules after this date. Default None

Return type

None

run_signals(signal_names=None, contract_groups=None, clear_all=False)[source]

Calculate values of the signals specified and store them.

Parameters
  • signal_names (Optional[Sequence[str]]) – List of signal names. If None (default) run all signals

  • contract_groups (Optional[Sequence[ContractGroup]]) – Contract groups to run this signal for. If None (default), we run it for all contract groups.

  • clear_all (bool) – If set, clears all signal values before running. Default False.

Return type

None

trades(contract_group=None, start_date=None, end_date=None)[source]

Returns a list of trades with the given contract group and with trade date between (and including) start date and end date if they are specified. If contract_group is None trades for all contract_groups are returned

Return type

Sequence[Trade]

pyqstrat.strategy.test_strategy()[source]
Return type

Strategy

pyqstrat.portfolio module

class pyqstrat.portfolio.Portfolio(name='main')[source]

Bases: object

A portfolio contains one or more strategies that run concurrently so you can test running strategies that are uncorrelated together.

__init__(name='main')[source]

Args: name: String used for displaying this portfolio

Return type

None

add_strategy(name, strategy)[source]
Parameters
  • name (str) – Name of the strategy

  • strategy (Strategy) – Strategy instance

Return type

None

df_returns(sampling_frequency='D', strategy_names=None)[source]

Return dataframe containing equity and returns with a date index. Equity and returns are combined from all strategies passed in.

Parameters
  • sampling_frequency (str) – Date frequency for rows. Default ‘D’ for daily so we will have one row per day

  • strategy_names (Optional[Sequence[str]]) – By default this is set to None and we use all strategies.

Return type

DataFrame

evaluate_returns(sampling_frequency='D', strategy_names=None, plot=True, float_precision=4)[source]

Returns a dictionary of common return metrics.

Parameters
  • sampling_frequency (str) – Date frequency. Default ‘D’ for daily so we downsample to daily returns before computing metrics

  • strategy_names (Optional[Sequence[str]]) – By default this is set to None and we use all strategies.

  • plot (bool) – If set to True, display plots of equity, drawdowns and returns. Default False

  • float_precision (int) – Number of significant figures to show in returns. Default 4

Return type

Mapping[~KT, +VT_co]

plot(sampling_frequency='D', strategy_names=None)[source]

Display plots of equity, drawdowns and returns

Parameters
  • sampling_frequency (str) – Date frequency. Default ‘D’ for daily so we downsample to daily returns before computing metrics

  • strategy_names (Optional[Sequence[str]]) – A list of strategy names. By default this is set to None and we use all strategies.

Return type

None

run(strategy_names=None, start_date=None, end_date=None)[source]

Run indicators, signals and rules.

Parameters
  • strategy_names (Optional[Sequence[str]]) – A list of strategy names. By default this is set to None and we use all strategies.

  • start_date (Optional[datetime64]) – Run rules starting from this date. Sometimes we have a few strategies in a portfolio that need different lead times before they are ready to trade so you can set this so they are all ready by this date. Default None

  • end_date (Optional[datetime64]) – Don’t run rules after this date. Default None

Return type

None

run_indicators(strategy_names=None)[source]

Compute indicators for the strategies specified

Parameters

strategy_names (Optional[Sequence[str]]) – By default this is set to None and we use all strategies.

Return type

None

run_rules(strategy_names=None, start_date=None, end_date=None)[source]

Run rules for the strategies specified. Must be called after run_indicators and run_signals. See run function for argument descriptions

Return type

None

run_signals(strategy_names=None)[source]

Compute signals for the strategies specified. Must be called after run_indicators

Parameters

strategy_names (Optional[Sequence[str]]) – By default this is set to None and we use all strategies.

Return type

None

pyqstrat.optimize module

class pyqstrat.optimize.Experiment(suggestion, cost, other_costs)[source]

Bases: object

An Experiment stores a suggestion and its result

__init__(suggestion, cost, other_costs)[source]
Parameters
  • suggestion (Mapping[str, Any]) – A dictionary of variable name -> value

  • cost (float) – A float representing output of the function we are testing with this suggestion as input.

  • other_costs (Mapping[str, float]) – A dictionary of other results we want to store and look at later.

Return type

None

valid()[source]

Returns True if all suggestions and costs are finite, i.e not NaN or +/- Infinity

Return type

bool

class pyqstrat.optimize.Optimizer(name, generator, cost_func, max_processes=None)[source]

Bases: object

Optimizer is used to optimize parameters for a strategy.

__init__(name, generator, cost_func, max_processes=None)[source]
Parameters
  • name (str) – Display title for plotting, etc.

  • generator (Generator[Mapping[str, Any], Tuple[float, Mapping[str, float]], None]) – A generator (see Python Generators) that takes no inputs and yields a dictionary with parameter name -> parameter value.

  • cost_func (Callable[[Mapping[str, Any]], Tuple[float, Mapping[str, float]]]) – A function that takes a dictionary of parameter name -> parameter value as input and outputs cost for that set of parameters.

  • max_processes (Optional[int]) – If not set, the Optimizer will look at the number of CPU cores on your machine to figure out how many processes to run.

Return type

None

df_experiments(sort_column='cost', ascending=True)[source]

Returns a dataframe containing experiment data, sorted by sort_column (default “cost”)

Return type

DataFrame

experiment_list(sort_order='lowest_cost')[source]

Returns the list of experiments we have run

Parameters

sort_order (str) – Can be set to lowest_cost, highest_cost or sequence. If set to sequence, experiments are returned in the sequence in which they were run

Return type

Sequence[Experiment]

plot_2d(x, y='all', plot_type='line', figsize=(15, 8), marker='X', marker_size=50, marker_color='r', xlim=None, hspace=None)[source]

Creates a 2D plot of the optimization output for plotting 1 parameter and costs.

Parameters
  • x (str) – Name of the parameter to plot on the x axis, corresponding to the same name in the generator.

  • y (str) – Can be one of: “cost” The name of another cost variable corresponding to the output from the cost function “all”, which creates a subplot for cost plus all other costs

  • plot_type (str) – line or scatter (default line)

  • figsize (Tuple[float, float]) – Figure size

  • marker (str) – Adds a marker to each point in x, y to show the actual data used for interpolation. You can set this to None to turn markers off.

  • hspace (Optional[float]) – Vertical space between subplots

Return type

None

plot_3d(x, y, z='all', plot_type='surface', figsize=(15, 15), interpolation='linear', cmap='viridis', marker='X', marker_size=50, marker_color='r', xlim=None, ylim=None, hspace=None)[source]

Creates a 3D plot of the optimization output for plotting 2 parameters and costs.

Parameters
  • x (str) – Name of the parameter to plot on the x axis, corresponding to the same name in the generator.

  • y (str) – Name of the parameter to plot on the y axis, corresponding to the same name in the generator.

  • z (str) – Can be one of: “cost” The name of another cost variable corresponding to the output from the cost function “all”, which creates a subplot for cost plus all other costs

  • plot_type (str) – surface or contour (default surface)

  • figsize (Tuple[float, float]) – Figure size

  • interpolation (str) – Can be ‘linear’, ‘nearest’ or ‘cubic’ for plotting z points between the ones passed in. See scipy.interpolate.griddata for details

  • cmap (str) – Colormap to use (default viridis). See matplotlib colormap for details

  • marker (str) – Adds a marker to each point in x, y, z to show the actual data used for interpolation. You can set this to None to turn markers off.

  • hspace (Optional[float]) – Vertical space between subplots

Return type

None

run(raise_on_error=False)[source]

Run the optimizer.

Parameters

raise_on_error (bool) – If set to True, even if we are running a multiprocess optimization, any Exceptions will bubble up and stop the Optimizer. This can be useful for debugging to see stack traces for Exceptions.

Return type

None

pyqstrat.optimize.test_optimize()[source]

pyqstrat.plot module

class pyqstrat.plot.BarPlotAttributes(color='red')[source]

Bases: pyqstrat.plot.DisplayAttributes

color = 'red'
class pyqstrat.plot.BoxPlotAttributes(proportional_widths=True, show_means=True, show_all=True, show_outliers=False, notched=False)[source]

Bases: pyqstrat.plot.DisplayAttributes

proportional_widths

if set to True, the width each box in the boxplot will be proportional to the number of items in its corresponding array

show_means

Whether to display a marker where the mean is for each array

show_outliers

Whether to show markers for outliers that are outside the whiskers. Box is at Q1 = 25%, Q3 = 75% quantiles, whiskers are at Q1 - 1.5 * (Q3 - Q1), Q3 + 1.5 * (Q3 - Q1)

notched

Whether to show notches indicating the confidence interval around the median

notched = False
proportional_widths = True
show_all = True
show_means = True
show_outliers = False
class pyqstrat.plot.BucketedValues(name, bucket_names, bucket_values, display_attributes=None)[source]

Bases: pyqstrat.plot.PlotData

Data in a subplot where we summarize properties of a numpy array.

For example, drawing a boxplot with percentiles. x axis is a categorical

__init__(name, bucket_names, bucket_values, display_attributes=None)[source]
Parameters
  • name (str) – name used for this data in a plot legend

  • bucket_names (Sequence[str]) – list of strings used on x axis labels

  • bucket_values (Sequence[ndarray]) – list of numpy arrays that are summarized in this plot

Return type

None

class pyqstrat.plot.CandleStickPlotAttributes(colorup='darkgreen', colordown='#F2583E')[source]

Bases: pyqstrat.plot.DisplayAttributes

colordown = '#F2583E'
colorup = 'darkgreen'
class pyqstrat.plot.ContourPlotAttributes(marker='X', marker_size=50, marker_color='red', interpolation='linear', cmap='viridis')[source]

Bases: pyqstrat.plot.DisplayAttributes

cmap = 'viridis'
interpolation = 'linear'
marker = 'X'
marker_color = 'red'
marker_size = 50
class pyqstrat.plot.DateFormatter(timestamps, fmt)[source]

Bases: matplotlib.ticker.Formatter

Formats timestamps on plot axes. See matplotlib Formatter

class pyqstrat.plot.DateLine(date, name=None, line_type='dashed', color=None)[source]

Bases: object

Draw a vertical line on a plot with a datetime x-axis

class pyqstrat.plot.DisplayAttributes[source]

Bases: object

class pyqstrat.plot.FilledLinePlotAttributes(positive_color='blue', negative_color='red')[source]

Bases: pyqstrat.plot.DisplayAttributes

colorup: Color for bars where close >= open. Default “darkgreen” colordown: Color for bars where open < close. Default “#F2583E”

negative_color = 'red'
positive_color = 'blue'
class pyqstrat.plot.HorizontalLine(y, name=None, line_type='dashed', color=None)[source]

Bases: object

Draws a horizontal line on a subplot

class pyqstrat.plot.LinePlotAttributes(line_type='solid', line_width=None, color=None, marker=None, marker_size=None, marker_color=None)[source]

Bases: pyqstrat.plot.DisplayAttributes

color = None
line_type = 'solid'
line_width = None
marker = None
marker_color = None
marker_size = None
class pyqstrat.plot.Plot(subplot_list, title=None, figsize=(15, 8), date_range=None, date_format=None, sampling_frequency=None, show_grid=True, show_date_gaps=True, hspace=0.15)[source]

Bases: object

Top level plot containing a list of subplots to draw

__init__(subplot_list, title=None, figsize=(15, 8), date_range=None, date_format=None, sampling_frequency=None, show_grid=True, show_date_gaps=True, hspace=0.15)[source]
Parameters
  • subplot_list (Sequence[Subplot]) – List of Subplot objects to draw

  • title (Optional[str]) – Title for this plot. Default None

  • figsize (Tuple[float, float]) – Figure size. Default (15, 8)

  • date_range (Union[Tuple[str, str], Tuple[datetime64, datetime64], None]) – Tuple of strings or numpy datetime64 limiting timestamps to draw. e.g. (“2018-01-01 14:00”, “2018-01-05”). Default None

  • date_format (Optional[str]) – Date format to use for x-axis

  • sampling_frequency (Optional[str]) – Set this to downsample subplots that have a datetime x axis. For example, if you have minute bar data, you might want to subsample to hours if the plot is too crowded. See pandas time frequency strings for possible values. Default None

  • show_grid (bool) – If set to True, show a grid on the subplots. Default True

  • show_date_gaps (bool) – If set to True, then when there is a gap between timestamps will draw a dashed vertical line. For example, you may have minute bars and a gap between end of trading day and beginning of next day. Even if set to True, this will turn itself off if there are too many gaps to avoid clutter. Default True

  • hspace (Optional[float]) – Height (vertical) space between subplots. Default 0.15

Return type

None

draw(check_data_size=True)[source]

Draw the subplots.

Parameters

check_data_size (bool) – If set to True, will not plot if there are > 100K points to avoid locking up your computer for a long time. Default True

Return type

Optional[Tuple[Figure, Axes]]

class pyqstrat.plot.PlotData[source]

Bases: object

class pyqstrat.plot.ScatterPlotAttributes(marker='X', marker_size=50, marker_color='red')[source]

Bases: pyqstrat.plot.DisplayAttributes

marker = 'X'
marker_color = 'red'
marker_size = 50
class pyqstrat.plot.Subplot(data_list, secondary_y=None, title=None, xlabel=None, ylabel=None, zlabel=None, date_lines=None, horizontal_lines=None, vertical_lines=None, xlim=None, ylim=None, height_ratio=1.0, display_legend=True, legend_loc='best', log_y=False, y_tick_format=None)[source]

Bases: object

A top level plot contains a list of subplots, each of which contain a list of data objects to draw

__init__(data_list, secondary_y=None, title=None, xlabel=None, ylabel=None, zlabel=None, date_lines=None, horizontal_lines=None, vertical_lines=None, xlim=None, ylim=None, height_ratio=1.0, display_legend=True, legend_loc='best', log_y=False, y_tick_format=None)[source]
Parameters
  • data_list (Union[PlotData, Sequence[PlotData]]) – A list of objects to draw. Each element can contain XYData, XYZData, TimeSeries, TradeBarSeries, BucketedValues or TradeSet

  • secondary_y (Optional[Sequence[str]]) – A list of objects to draw on the secondary y axis

  • title (Optional[str]) – Title to show for this subplot. Default None

  • zlabel (Optional[str]) – Only applicable to 3d subplots. Default None

  • date_lines (Optional[Sequence[DateLine]]) – A list of DateLine objects to draw as vertical lines. Only applicable when x axis is datetime. Default None

  • horizontal_lines (Optional[Sequence[HorizontalLine]]) – A list of HorizontalLine objects to draw on the plot. Default None

  • vertical_lines (Optional[Sequence[VerticalLine]]) – A list of VerticalLine objects to draw on the plot

  • xlim (Union[Tuple[float, float], Tuple[datetime64, datetime64], None]) – x limits for the plot as a tuple of numpy datetime objects when x-axis is datetime, or tuple of floats. Default None

  • ylim (Union[Tuple[float, float], Tuple[datetime64, datetime64], None]) – y limits for the plot. Tuple of floats. Default None

  • height_ratio (float) – If you have more than one subplot on a plot, use height ratio to determine how high each subplot should be. For example, if you set height_ratio = 0.75 for the first subplot and 0.25 for the second, the first will be 3 times taller than the second one. Default 1.0

  • display_legend (bool) – Whether to show a legend on the plot. Default True

  • legend_loc (str) – Location for the legend. Default ‘best’

  • log_y (bool) – Whether the y axis should be logarithmic. Default False

  • y_tick_format (Optional[str]) – Format string to use for y axis labels. For example, you can decide to use fixed notation instead of scientific notation or change number of decimal places shown. Default None

Return type

None

get_all_timestamps(date_range)[source]
Return type

ndarray

class pyqstrat.plot.SurfacePlotAttributes(marker='X', marker_size=50, marker_color='red', interpolation='linear', cmap='viridis')[source]

Bases: pyqstrat.plot.DisplayAttributes

marker

Adds a marker to each point in x, y, z to show the actual data used for interpolation. You can set this to None to turn markers off.

interpolation

Can be ‘linear’, ‘nearest’ or ‘cubic’ for plotting z points between the ones passed in. See scipy.interpolate.griddata for details

cmap

Colormap to use (default viridis). See matplotlib colormap for details

cmap = 'viridis'
interpolation = 'linear'
marker = 'X'
marker_color = 'red'
marker_size = 50
class pyqstrat.plot.TimePlotData[source]

Bases: pyqstrat.plot.PlotData

abstract reindex(timestamps, fill)[source]
Return type

None

class pyqstrat.plot.TimeSeries(name, timestamps, values, display_attributes=None)[source]

Bases: pyqstrat.plot.TimePlotData

Data in a subplot where x is an array of numpy datetimes and y is a numpy array of floats

__init__(name, timestamps, values, display_attributes=None)[source]
Parameters

name (str) – Name to show in plot legend

Return type

None

reindex(timestamps, fill)[source]

Reindex this series given a new array of timestamps, forward filling holes if fill is set to True

Return type

None

class pyqstrat.plot.TradeBarSeries(name, timestamps, o, h, l, c, v=None, vwap=None, display_attributes=None)[source]

Bases: pyqstrat.plot.TimePlotData

Data in a subplot that contains open, high, low, close, volume bars. volume is optional.

__init__(name, timestamps, o, h, l, c, v=None, vwap=None, display_attributes=None)[source]
Parameters

name (str) – Name to show in a legend

Return type

None

df()[source]
Return type

DataFrame

reindex(all_timestamps, fill)[source]
Return type

None

class pyqstrat.plot.TradeSet(name, trades, display_attributes=None)[source]

Bases: pyqstrat.plot.TimePlotData

Data for subplot that contains a set of trades along with marker properties for these trades

__init__(name, trades, display_attributes=None)[source]
Parameters
  • name (str) – String to display in a subplot legend

  • trades (Sequence[Trade]) – List of Trade objects to plot

Return type

None

reindex(all_timestamps, fill)[source]
Return type

None

class pyqstrat.plot.VerticalLine(x, name=None, line_type='dashed', color=None)[source]

Bases: object

Draws a vertical line on a subplot where x axis is not a date-time axis

class pyqstrat.plot.XYData(name, x, y, display_attributes=None)[source]

Bases: pyqstrat.plot.PlotData

Data in a subplot that has x and y values that are both arrays of floats

class pyqstrat.plot.XYZData(name, x, y, z, display_attributes=None)[source]

Bases: pyqstrat.plot.PlotData

Data in a subplot that has x, y and z values that are all floats

__init__(name, x, y, z, display_attributes=None)[source]
Parameters

name (str) – Name to show in plot legend

Return type

None

pyqstrat.plot.draw_3d_plot(ax, x, y, z, plot_type, marker='X', marker_size=50, marker_color='red', interpolation='linear', cmap='viridis')[source]

Draw a 3d plot. See XYZData class for explanation of arguments

>>> points = np.random.rand(1000, 2)
>>> x = np.random.rand(10)
>>> y = np.random.rand(10)
>>> z = x ** 2 + y ** 2
>>> if has_display():
...    fig, ax = plt.subplots()
...    draw_3d_plot(ax, x = x, y = y, z = z, plot_type = 'contour', interpolation = 'linear')
Return type

None

pyqstrat.plot.draw_boxplot(ax, names, values, proportional_widths=True, notched=False, show_outliers=True, show_means=True, show_all=True)[source]

Draw a boxplot. See BucketedValues class for explanation of arguments

Return type

None

pyqstrat.plot.draw_candlestick(ax, index, o, h, l, c, v, vwap, colorup='darkgreen', colordown='#F2583E')[source]

Draw candlesticks given parrallel numpy arrays of o, h, l, c, v values. v is optional. See TradeBarSeries class __init__ for argument descriptions.

Return type

None

pyqstrat.plot.draw_date_line(ax, plot_timestamps, date, linestyle, color)[source]

Draw vertical line on a subplot with datetime x axis

Return type

Line2D

pyqstrat.plot.draw_horizontal_line(ax, y, linestyle, color)[source]

Draw horizontal line on a subplot

Return type

Line2D

pyqstrat.plot.draw_poly(ax, left, bottom, top, right, facecolor, edgecolor, zorder)[source]

Draw a set of polygrams given parrallel numpy arrays of left, bottom, top, right points

Return type

None

pyqstrat.plot.draw_vertical_line(ax, x, linestyle, color)[source]

Draw vertical line on a subplot

Return type

Line2D

pyqstrat.plot.get_date_formatter(plot_timestamps, date_format)[source]

Create an appropriate DateFormatter for x axis labels. If date_format is set to None, figures out an appropriate date format based on the range of timestamps passed in

Return type

DateFormatter

pyqstrat.plot.test_plot()[source]
Return type

None

pyqstrat.plot.trade_sets_by_reason_code(trades, marker_props={'backtest end': {'color': 'green', 'size': 50, 'symbol': '*'}, 'enter long': {'color': 'blue', 'size': 50, 'symbol': 'P'}, 'enter short': {'color': 'red', 'size': 50, 'symbol': 'P'}, 'exit long': {'color': 'blue', 'size': 50, 'symbol': 'X'}, 'exit short': {'color': 'red', 'size': 50, 'symbol': 'X'}, 'none': {'color': 'green', 'size': 50, 'symbol': 'o'}, 'roll future': {'color': 'green', 'size': 50, 'symbol': '>'}}, remove_missing_properties=True)[source]

Returns a list of TradeSet objects. Each TradeSet contains trades with a different reason code. The markers for each TradeSet are set by looking up marker properties for each reason code using the marker_props argument:

Parameters
  • trades (List[Trade]) – We look up reason codes using the reason code on the corresponding orders

  • marker_props (Mapping[str, Mapping[~KT, +VT_co]]) – Dictionary from reason code string -> dictionary of marker properties. See ReasonCode.MARKER_PROPERTIES for example. Default ReasonCode.MARKER_PROPERTIES

  • remove_missing_properties (bool) – If set, we remove any reason codes that dont’ have marker properties set. Default True

Return type

List[TradeSet]

pyqstrat.evaluator module

class pyqstrat.evaluator.Evaluator(initial_metrics)[source]

Bases: object

You add functions to the evaluator that are dependent on the outputs of other functions. The evaluator will call these functions in the right order so dependencies are computed first before the functions that need their output. You can retrieve the output of a metric using the metric member function

>>> evaluator = Evaluator(initial_metrics={'x': np.array([1, 2, 3]), 'y': np.array([3, 4, 5])})
>>> evaluator.add_metric('z', lambda x, y: sum(x, y), dependencies=['x', 'y'])
>>> evaluator.compute()
>>> evaluator.metric('z')
array([ 9, 10, 11])
__init__(initial_metrics)[source]

Inits Evaluator with a dictionary of initial metrics that are used to compute subsequent metrics

Parameters

initial_metrics (Dict[str, Any]) – a dictionary of string name -> metric. metric can be any object including a scalar, an array or a tuple

Return type

None

add_metric(name, func, dependencies)[source]
Return type

None

compute(metric_names=None)[source]

Compute metrics using the internal dependency graph

Parameters

metric_names (Optional[Sequence[str]]) – an array of metric names. If not passed in, evaluator will compute and store all metrics

Return type

None

compute_metric(metric_name)[source]

Compute and store a single metric:

Parameters

metric_name (str) – string representing the metric to compute

Return type

None

metric(metric_name)[source]

Return the value of a single metric given its name

Return type

Any

metrics()[source]

Return a dictionary of metric name -> metric value

Return type

Mapping[str, Any]

pyqstrat.evaluator.compute_amean(returns, periods_per_year)[source]

Computes arithmetic mean of a return array, ignoring NaNs

Parameters
  • returns (ndarray) – Represents returns at any frequency

  • periods_per_year (int) – Frequency of the returns, e.g. 252 for daily returns

>>> compute_amean(np.array([0.003, 0.004, np.nan]), 252)
0.882
Return type

float

pyqstrat.evaluator.compute_annual_returns(timestamps, returns, periods_per_year)[source]

Takes the output of compute_bucketed_returns and returns geometric mean of returns by year

Return type

Tuple[ndarray, ndarray]

Returns

A tuple with the first element being an array of years (integer) and the second element an array of annualized returns for those years

pyqstrat.evaluator.compute_bucketed_returns(timestamps, returns)[source]

Bucket returns by year

Return type

Tuple[Sequence[int], Sequence[ndarray]]

Returns

A tuple with the first element being a list of years and the second a list of

numpy arrays containing returns for each corresponding year

pyqstrat.evaluator.compute_calmar(returns_3yr, periods_per_year, mdd_pct_3yr)[source]

Compute Calmar ratio, which is the annualized return divided by max drawdown over the last 3 years

Return type

float

pyqstrat.evaluator.compute_dates_3yr(timestamps)[source]

Given an array of numpy datetimes, return those that are within 3 years of the last date in the array

Return type

ndarray

pyqstrat.evaluator.compute_equity(timestamps, starting_equity, returns)[source]

Given starting equity, timestamps and returns, create a numpy array of equity at each date

Return type

ndarray

pyqstrat.evaluator.compute_gmean(timestamps, returns, periods_per_year)[source]

Compute geometric mean of an array of returns

Parameters
  • returns (ndarray) – a numpy array of returns, can contain nans

  • periods_per_year (float) – Used for annualizing returns

>>> round(compute_gmean(np.array(['2015-01-01', '2015-03-01', '2015-05-01'], dtype='M8[D]'), np.array([0.001, 0.002, 0.003]), 252.), 6)
0.018362
Return type

float

pyqstrat.evaluator.compute_k_ratio(equity, periods_per_year, halflife_years=None)[source]

Compute k-ratio (2013 or original versions by Lars Kestner). See https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2230949 We also implement a modification that allows higher weighting for more recent returns.

Parameters
  • equity (ndarray) – a numpy array of the equity in your account

  • periods_per_year (int) – 252 for daily values

  • halflife_years (Optional[float]) – If set, we use weighted linear regression to give less weight to older returns. In this case, we compute the original k-ratio which does not use periods per year or number of observations If not set, we compute the 2013 version of the k-ratio which weights k-ratio by sqrt(periods_per_year) / nobs

Return type

float

Returns

weighted or unweighted k-ratio

>>> np.random.seed(0)
>>> t = np.arange(1000)
>>> ret = np.random.normal(loc = 0.0025, scale = 0.01, size = len(t))
>>> equity = (1 + ret).cumprod()
>>> assert(math.isclose(compute_k_ratio(equity, 252, None), 3.888, abs_tol=0.001))
>>> assert(math.isclose(compute_k_ratio(equity, 252, 0.5), 602.140, abs_tol=0.001))
pyqstrat.evaluator.compute_mar(returns, periods_per_year, mdd_pct)[source]

Compute MAR ratio, which is annualized return divided by biggest drawdown since inception.

Return type

float

pyqstrat.evaluator.compute_maxdd_date(rolling_dd_dates, rolling_dd)[source]

Compute date of max drawdown given numpy array of timestamps, and corresponding rolling dd percentages

Return type

float

pyqstrat.evaluator.compute_maxdd_date_3yr(rolling_dd_3yr_timestamps, rolling_dd_3yr)[source]

Compute max drawdown date over the last 3 years

Return type

datetime64

pyqstrat.evaluator.compute_maxdd_pct(rolling_dd)[source]

Compute max drawdown percentage given a numpy array of rolling drawdowns, ignoring NaNs

Return type

float

pyqstrat.evaluator.compute_maxdd_pct_3yr(rolling_dd_3yr)[source]

Compute max drawdown percentage over the last 3 years

Return type

float

pyqstrat.evaluator.compute_maxdd_start(rolling_dd_dates, rolling_dd, mdd_date)[source]

Compute date when max drawdown starts, given numpy array of timestamps corresponding rolling dd percentages and date of the max draw down

Return type

datetime64

pyqstrat.evaluator.compute_maxdd_start_3yr(rolling_dd_3yr_timestamps, rolling_dd_3yr, mdd_date_3yr)[source]

Comput max drawdown start date over the last 3 years

Return type

datetime64

pyqstrat.evaluator.compute_num_periods(timestamps, periods_per_year)[source]
Given an array of timestamps, we compute how many periods there are between the first and last element, where the length

of a period is defined by periods_per_year. For example, if there are 6 periods per year, then each period would be approx. 2 months long.

Parameters
  • timestamps (np.ndarray of np.datetime64) – a numpy array of returns, can contain nans

  • periods_per_year (float) – number of periods between first and last return

>>> compute_num_periods(np.array(['2015-01-01', '2015-03-01', '2015-05-01'], dtype='M8[D]'), 6)
2.0
Return type

float

pyqstrat.evaluator.compute_periods_per_year(timestamps)[source]
Computes trading periods per year for an array of numpy datetime64’s.

e.g. if most of the timestamps are separated by 1 day, will return 252.

Parameters

timestamps (ndarray) – a numpy array of datetime64’s

>>> compute_periods_per_year(np.array(['2018-01-01', '2018-01-02', '2018-01-03', '2018-01-09'], dtype='M8[D]'))
252.0
>>> round(compute_periods_per_year(np.array(['2018-01-01 10:00', '2018-01-01 10:05', '2018-01-01 10:10'], dtype='M8[m]')), 2)
72576.05
Return type

float

pyqstrat.evaluator.compute_return_metrics(timestamps, rets, starting_equity, leading_non_finite_to_zeros=False, subsequent_non_finite_to_zeros=True)[source]

Compute a set of common metrics using returns (for example, of an instrument or a portfolio)

Parameters
  • timestamps (np.array of datetime64) – Timestamps for the returns

  • rets (nd.array of float) – The returns, use 0.01 for 1%

  • starting_equity (float) – Starting equity value in your portfolio

  • leading_non_finite_to_zeros (bool, optional) – If set, we replace leading nan, inf, -inf returns with zeros. For example, you may need a warmup period for moving averages. Default False

  • subsequent_non_finite_to_zeros (bool, optional) – If set, we replace any nans that follow the first non nan value with zeros. There may be periods where you have no prices but removing these returns would result in incorrect annualization. Default True

Return type

Evaluator

Returns

An Evaluator object containing computed metrics off the returns passed in. If needed, you can add your own metrics to this object based on the values of existing metrics and recompute the Evaluator. Otherwise, you can just use the output of the evaluator using the metrics function.

>>> timestamps = np.array(['2015-01-01', '2015-03-01', '2015-05-01', '2015-09-01'], dtype='M8[D]')
>>> rets = np.array([0.01, 0.02, np.nan, -0.015])
>>> starting_equity = 1.e6
>>> ev = compute_return_metrics(timestamps, rets, starting_equity)
>>> metrics = ev.metrics()
>>> assert(round(metrics['gmean'], 6) == 0.021061)
>>> assert(round(metrics['sharpe'], 6) == 0.599382)
>>> assert(all(metrics['returns_3yr'] == np.array([0.01, 0.02, 0, -0.015])))
pyqstrat.evaluator.compute_returns_3yr(timestamps, returns)[source]

Given an array of numpy datetimes and an array of returns, return those that are within 3 years of the last date in the datetime array

Return type

ndarray

pyqstrat.evaluator.compute_rolling_dd(timestamps, equity)[source]

Compute numpy array of rolling drawdown percentage

Parameters
  • timestamps (ndarray) – numpy array of datetime64

  • equity (ndarray) – numpy array of equity

Return type

Tuple[ndarray, ndarray]

pyqstrat.evaluator.compute_rolling_dd_3yr(timestamps, equity)[source]

Compute rolling drawdowns over the last 3 years

Return type

Tuple[ndarray, ndarray]

pyqstrat.evaluator.compute_sharpe(returns, amean, periods_per_year)[source]

Note that this does not take into risk free returns so it’s really a sharpe0, i.e. assumes risk free returns are 0

Parameters
  • returns (ndarray) – a numpy array of returns

  • amean (float) – arithmetic mean of returns

  • periods_per_year (float) – number of trading periods per year

>>> round(compute_sharpe(np.array([0.001, -0.001, 0.002]), 0.001, 252), 6)
0.050508
Return type

float

pyqstrat.evaluator.compute_sortino(returns, amean, periods_per_year)[source]

Note that this assumes target return is 0.

Parameters
  • returns (ndarray) – a numpy array of returns

  • amean (float) – arithmetic mean of returns

  • periods_per_year (float) – number of trading periods per year

>>> print(round(compute_sortino(np.array([0.001, -0.001, 0.002]), 0.001, 252), 6))
0.133631
Return type

float

pyqstrat.evaluator.compute_std(returns)[source]

Computes standard deviation of an array of returns, ignoring nans

Return type

float

pyqstrat.evaluator.display_return_metrics(metrics, float_precision=3)[source]

Creates a dataframe making it convenient to view the output of the metrics obtained using the compute_return_metrics function.

Parameters

float_precision (int) – Change if you want to display floats with more or less significant figures than the default, 3 significant figures.

Return type

DataFrame

Returns

A one row dataframe with formatted metrics.

pyqstrat.evaluator.handle_non_finite_returns(timestamps, rets, leading_non_finite_to_zeros, subsequent_non_finite_to_zeros)[source]
>>> np.set_printoptions(formatter={'float': '{: .6g}'.format})
>>> timestamps = np.arange(np.datetime64('2019-01-01'), np.datetime64('2019-01-07'))
>>> rets = np.array([np.nan, np.nan, 3, 4, np.nan, 5])
>>> handle_non_finite_returns(timestamps, rets, leading_non_finite_to_zeros = False, subsequent_non_finite_to_zeros = True)
(array(['2019-01-03', '2019-01-04', '2019-01-05', '2019-01-06'], dtype='datetime64[D]'), array([ 3,  4,  0,  5]))
>>> handle_non_finite_returns(timestamps, rets, leading_non_finite_to_zeros = True, subsequent_non_finite_to_zeros = False)
(array(['2019-01-01', '2019-01-02', '2019-01-03', '2019-01-04', '2019-01-06'], dtype='datetime64[D]'), array([ 0,  0,  3,  4,  5]))
>>> handle_non_finite_returns(timestamps, rets, leading_non_finite_to_zeros = False, subsequent_non_finite_to_zeros = False)
(array(['2019-01-01', '2019-01-02', '2019-01-03', '2019-01-04', '2019-01-06'], dtype='datetime64[D]'), array([ 0,  0,  3,  4,  5]))
>>> rets = np.array([1, 2, 3, 4, 4.5,  5])
>>> handle_non_finite_returns(timestamps, rets, leading_non_finite_to_zeros = False, subsequent_non_finite_to_zeros = True)
(array(['2019-01-01', '2019-01-02', '2019-01-03', '2019-01-04', '2019-01-05', '2019-01-06'],
    dtype='datetime64[D]'), array([ 1,  2,  3,  4,  4.5,  5]))
Return type

Tuple[ndarray, ndarray]

pyqstrat.evaluator.plot_return_metrics(metrics, title=None)[source]

Plot equity, rolling drawdowns and and a boxplot of annual returns given the output of compute_return_metrics.

Return type

Optional[Tuple[Figure, Axes]]

pyqstrat.evaluator.test_evaluator()[source]
Return type

None

pyqstrat.pyqstrat_cpp module

class pyqstrat.pyqstrat_cpp.Aggregator

Bases: pybind11_builtins.pybind11_object

__call__()
__init__()
class pyqstrat.pyqstrat_cpp.AllOpenInterestAggregator

Bases: pyqstrat.pyqstrat_cpp.Aggregator

Writes out all open interest records

__call__()

Add an open interest record to be written to disk at some point

Parameters
  • oi (OpenInterestRecord) –

  • line_number (int) – The line number of the source file that this trade came from. Used for debugging

__init__()
Parameters
  • writer_creator – A function that takes an output_file_prefix, schema and returns an object implementing the Writer interface

  • output_file_prefix (str) – Path of the output file to create. The writer and aggregator may add suffixes to this to indicate the kind of data and format the file creates. E.g. “/tmp/output_file_1”

  • timestamp_unit (Schema.Type, optional) – Whether timestamps are measured as milliseconds or microseconds since the unix epoch. Defaults to Schema.TIMESTAMP_MILLI

class pyqstrat.pyqstrat_cpp.AllOtherAggregator

Bases: pyqstrat.pyqstrat_cpp.Aggregator

Writes out any records that are not trades, quotes or open interest

__call__()

Add a record to be written to disk at some point

Parameters
  • other (OtherRecord) –

  • line_number (int) – The line number of the source file that this trade came from. Used for debugging

__init__()
Parameters
  • writer_creator – A function that takes an output_file_prefix, schema and returns an object implementing the Writer interface

  • output_file_prefix (str) – Path of the output file to create. The writer and aggregator may add suffixes to this to indicate the kind of data and format the file creates. E.g. “/tmp/output_file_1”

  • timestamp_unit (Schema.Type, optional) – Whether timestamps are measured as milliseconds or microseconds since the unix epoch. Defaults to Schema.TIMESTAMP_MILLI

class pyqstrat.pyqstrat_cpp.AllQuoteAggregator

Bases: pyqstrat.pyqstrat_cpp.Aggregator

Writes out every quote we see

__call__()

Add a quote record to be written to disk at some point

Parameters
  • quote (QuoteRecord) –

  • line_number (int) – The line number of the source file that this trade came from. Used for debugging

__init__()
Parameters

writer_creator

A function that takes an output_file_prefix, schema and returns an object implementing the Writer interface

output_file_prefix (str): Path of the output file to create. The writer and aggregator may add suffixes to this to indicate the kind of data and format the file creates. E.g. “/tmp/output_file_1”

consecutive_ids (bool): Whether we expect to see interleaved quote ids or not. If set, we expect to see quotes for the same instrument

together, and write out a batch after each instrument is done.

timestamp_unit (Schema.Type, optional): Whether timestamps are measured as milliseconds or microseconds since the unix epoch.

Defaults to Schema.TIMESTAMP_MILLI

class pyqstrat.pyqstrat_cpp.AllQuotePairAggregator

Bases: pyqstrat.pyqstrat_cpp.Aggregator

Writes out every quote pair we find

__call__()

Add a quote pair record to be written to disk at some point

Parameters

quote_pair (QuoteRecord) – line_number (int): The line number of the source file that this trade came from. Used for debugging

__init__()
Parameters
  • writer_creator – A function that takes an output_file_prefix, schema and returns an object implementing the Writer interface

  • output_file_prefix (str) – Path of the output file to create. The writer and aggregator may add suffixes to this to indicate the kind of data and format the file creates. E.g. “/tmp/output_file_1”

  • timestamp_unit (Schema.Type, optional) – Whether timestamps are measured as milliseconds or microseconds since the unix epoch. Defaults to Schema.TIMESTAMP_MILLI

class pyqstrat.pyqstrat_cpp.AllTradeAggregator

Bases: pyqstrat.pyqstrat_cpp.Aggregator

Writes out every trade we see

__call__()

Add a trade record to be written to disk at some point

Parameters
  • trade (TradeRecord) –

  • line_number (int) – The line number of the source file that this trade came from. Used for debugging

__init__()
Parameters
  • writer_creator – A function that takes an output_file_prefix, schema and returns an object implementing the Writer interface

  • output_file_prefix (str) – Path of the output file to create. The writer and aggregator may add suffixes to this to indicate the kind of data and format the file creates. E.g. “/tmp/output_file_1”

  • timestamp_unit (Schema.Type, optional) – Whether timestamps are measured as milliseconds or microseconds since the unix epoch. Defaults to Schema.TIMESTAMP_MILLI

class pyqstrat.pyqstrat_cpp.ArrowWriter

Bases: pyqstrat.pyqstrat_cpp.Writer

A subclass of Writer that batches of records to a disk file in the Apache arrow format. See Apache arrow for details

__init__()
Parameters
  • output_file_prefix (str) – Path of the output file to create. The writer and aggregator may add suffixes to this to indicate the kind of data and format the file creates. E.g. “/tmp/output_file_1”

  • schema (Schema) – A schema object containing the names and datatypes of each field we want to save in a record

add_record()

Add a record that will be written to disk at some point.

Parameters
  • line_number (int) – The line number of the source file that this trade came from. Used for debugging

  • tuple (tuple) – Must correspond to the schema defined in the constructor. For example, if the schema has a bool and a float, the tuple could be (False, 0.5)

close()

Close the writer and flush any remaining data to disk

Parameters

success (bool, optional) – If set to False, we had some kind of exception and are cleaning up. Tells the function to not indicate the file was written successfully, for example by renaming a temp file to the actual filename. Defaults to True

write_batch()

Write a batch of records to disk. The batch can have an optional string id so we can later retrieve just this batch of records without reading the whole file.

Parameters

batch_id (str, optional) – An identifier which can later be used to retrieve this batch from disk. Defaults to “”

class pyqstrat.pyqstrat_cpp.ArrowWriterCreator

Bases: pyqstrat.pyqstrat_cpp.WriterCreator

__call__()
__init__()
class pyqstrat.pyqstrat_cpp.BadLineHandler

Bases: pybind11_builtins.pybind11_object

__call__()
__init__()
class pyqstrat.pyqstrat_cpp.CheckFields

Bases: pybind11_builtins.pybind11_object

__call__()
__init__()
class pyqstrat.pyqstrat_cpp.FileProcessor

Bases: pybind11_builtins.pybind11_object

__call__()
__init__()
class pyqstrat.pyqstrat_cpp.FixedWidthTimeParser

Bases: pyqstrat.pyqstrat_cpp.TimestampParser

A helper class that takes a string formatted as HH:MM:SS.xxx and parses it into number of milliseconds or micros since the beginning of the day

__call__()
Parameters

time (str) – A string like “2018-01-01 08:35:22.132”

Returns

Milliseconds or microseconds since Unix epoch

Return type

int

__init__()
Parameters
  • micros (bool, optional) – Whether to return timestamp in millisecs or microsecs since 1970. Default false

  • hours_start (int, optional) – index where the hour starts in the timestamp string. Default -1

  • hours_size (int, optional) – number of characters used for the hour

  • minutes_start (int, optional) –

  • minutes_size (int, optional) –

  • seconds_start (int, optional) –

  • seconds_size (int, optional) –

  • millis_start (int, optional) –

  • millis_size (int, optional) –

  • micros_start (int, optional) –

  • micros_size (int, optional) –

class pyqstrat.pyqstrat_cpp.FormatTimestampParser

Bases: pyqstrat.pyqstrat_cpp.TimestampParser

Helper class that parses timestamps according to the strftime format string passed in. strftime is slow so

use FixedWithTimeParser if your timestamp has a fixed format such as “HH:MM:SS….”

__call__()
Parameters

time (str) – The timestamp to parse

Returns

Number of millis or micros since epoch

Return type

int

__init__()
Parameters
  • base_date (int) – Sometimes the timestamps in a file contain time only and the name of a file contains the date. In these cases, pass in the date as number of millis or micros from the epoch to the start of that date. If the timestamp has date also, pass in 0 here.

  • time_format (str, optional) – strftime format string for parsing the timestamp. Defaults to “%H:%M:%S”

  • micros (bool, optional) – If this is set, we will parse and store microseconds. Otherwise we will parse and store milliseconds. Defaults to True

class pyqstrat.pyqstrat_cpp.HDF5WriterCreator

Bases: pyqstrat.pyqstrat_cpp.WriterCreator

__call__()
__init__()
Parameters

group_name_delimiter – A one character string. We use this to delimit group names and create parent groups.

implementing the Writer interface

close()
class pyqstrat.pyqstrat_cpp.IsFieldInList

Bases: pyqstrat.pyqstrat_cpp.CheckFields

Simple utility class to check whether the value of fields[flag_idx] is in any of flag_values

__call__()
Parameters

flag_values – a vector of strings containing possible values for the field

Returns

a boolean

__init__()
Parameters
  • fields – a vector of strings

  • flag_idx – the index of fields to check

class pyqstrat.pyqstrat_cpp.LineFilter

Bases: pybind11_builtins.pybind11_object

__call__()
__init__()
class pyqstrat.pyqstrat_cpp.MissingDataHandler

Bases: pybind11_builtins.pybind11_object

__call__()
__init__()
class pyqstrat.pyqstrat_cpp.OpenInterestRecord

Bases: pyqstrat.pyqstrat_cpp.Record

Open interest for a future or option. Usually one record per instrument at the beginning of the day

id

Represents a symbol or instrument id, for example, for an option you may concantenate underlying symbol, expiration, strike, put or call to uniquely identify the instrument

Type

str

timestamp

Trade time, in milliseconds or microseconds since 1/1/1970

Type

int

qty

Trade quantity

Type

float

metadata

A string representing any extra information you want to save, such as exchange, or special trade conditions

Type

str

__init__()
property id
property metadata
property qty
property timestamp
class pyqstrat.pyqstrat_cpp.OtherRecord

Bases: pyqstrat.pyqstrat_cpp.Record

Any other data you want to store from market data besides trades, quotes and open interest. You can capture any important fields in the metadata attribute

id

Represents a symbol or instrument id, for example, for an option you may concantenate underlying symbol, expiration, strike, put or call to uniquely identify the instrument

Type

str

timestamp

trade time, in milliseconds or microseconds since 1/1/1970

Type

int

metadata

a string representing any extra information you want to save, such as exchange, or special trade conditions

Type

str

__init__()
property id
property metadata
property timestamp
class pyqstrat.pyqstrat_cpp.PriceQtyMissingDataHandler

Bases: pyqstrat.pyqstrat_cpp.MissingDataHandler

A helper class that takes a Record as an input, checks whether its a trade or a quote or any open interest record, and if any of the prices or quantities are 0, sets them to NAN

__call__()
Parameters

record – Any subclass of Record

__init__()
class pyqstrat.pyqstrat_cpp.PrintBadLineHandler

Bases: pyqstrat.pyqstrat_cpp.BadLineHandler

A helper class that takes in lines we cannot parse and either prints them and continues or raises an Exception

__call__()
Parameters
  • line_number (int) – Line number of the input file that corresponds to this line (for debugging)

  • line (str) – The actual line that failed to parse

  • exception (Exception) – The exception that caused us to fail to parse this line.

__init__()
Parameters

raise (bool, optional) – Whether to raise an exception every time this is called or just print debugging info. Defaults to False

class pyqstrat.pyqstrat_cpp.QuoteRecord

Bases: pyqstrat.pyqstrat_cpp.Record

A parsed quote record that we can save to disk

id

Represents a symbol or instrument id, for example, for an option you may concantenate underlying symbol, expiration, strike, put or call to uniquely identify the instrument

Type

str

timestamp

Trade time, in milliseconds or microseconds since 1/1/1970

Type

int

bid

If True, this is a bid quote, otherwise it is an offer

Type

bool

qty

Trade quantity

Type

float

price

Trade price

Type

float

metadata

A string representing any extra information you want to save, such as exchange, or special trade conditions

Type

str

__init__()
property bid
property id
property metadata
property price
property qty
property timestamp
class pyqstrat.pyqstrat_cpp.QuoteTOBAggregator

Bases: pyqstrat.pyqstrat_cpp.Aggregator

Aggregate top of book quotes to top of book records. If you specify a frequency such as “5m”, we will calculate a record every 5 minutes which has the top of book at the end of that bar. If no frequency is specified, we will create a top of book every time a quote comes in. We assume that the quotes are all top of book quotes and are written in pairs so we have a bid quote followed by a offer quote with the same timestamp or vice versa

__call__()

Add a quote record to be written to disk at some point

Parameters
  • quote (QuoteRecord) –

  • line_number (int) – The line number of the source file that this trade came from. Used for debugging

__init__()
Parameters
  • writer_creator – A function that takes an output_file_prefix, schema and returns an object implementing the Writer interface

  • output_file_prefix (str) – Path of the output file to create. The writer and aggregator may add suffixes to this to indicate the kind of data and format the file creates. E.g. “/tmp/output_file_1”

  • frequency (str, optional) – A string like “5m” indicating the bar size is 5 mins. Units can be s,m,h or d for second, minute, hour or day. Defaults to “5m”. If you set this to “”, each tick will be recorded.

  • timestamp_unit (Schema.Type, optional) – Whether timestamps are measured as milliseconds or microseconds since the unix epoch. Defaults to Schema.TIMESTAMP_MILLI

close()

Flush all unwritten records to the Writer, which writes them to disk when its close function is called

class pyqstrat.pyqstrat_cpp.Record

Bases: pybind11_builtins.pybind11_object

__init__()

Initialize self. See help(type(self)) for accurate signature.

class pyqstrat.pyqstrat_cpp.RecordFieldParser

Bases: pybind11_builtins.pybind11_object

__call__()
__init__()
class pyqstrat.pyqstrat_cpp.RecordFilter

Bases: pybind11_builtins.pybind11_object

__call__()
__init__()
class pyqstrat.pyqstrat_cpp.RecordGenerator

Bases: pybind11_builtins.pybind11_object

__call__()
__init__()
class pyqstrat.pyqstrat_cpp.RecordParser

Bases: pybind11_builtins.pybind11_object

__init__()
add_line()
Parameters

line (str) – The line we need to parse

parse()

Return: a subclass of Record

class pyqstrat.pyqstrat_cpp.RegExLineFilter

Bases: pyqstrat.pyqstrat_cpp.LineFilter

A helper class that filters lines from the input file based on a regular expression. Note that regular expressions are slow, so if you just need to match specific strings, use a string matching filter instead.

__call__()
Parameters

line (str) – The string that the regular expression should match.

Returns

Whether the regex matched

Return type

bool

__init__()
Parameters

pattern (str) – The regex pattern to match. This follows C++ std::regex pattern matching rules as opposed to python

class pyqstrat.pyqstrat_cpp.Schema

Bases: pybind11_builtins.pybind11_object

Describes a list of field names and data types for writing records to disk

types

A list of (str, type) tuples describing a record with the name of each field and its datatype

BOOL = Type.BOOL
FLOAT32 = Type.FLOAT32
FLOAT64 = Type.FLOAT64
INT32 = Type.INT32
INT64 = Type.INT64
STRING = Type.STRING
TIMESTAMP_MICRO = Type.TIMESTAMP_MICRO
TIMESTAMP_MILLI = Type.TIMESTAMP_MILLI
class Type

Bases: pybind11_builtins.pybind11_object

Members:

BOOL

INT32

INT64

FLOAT32

FLOAT64

STRING

TIMESTAMP_MILLI

TIMESTAMP_MICRO

BOOL = Type.BOOL
FLOAT32 = Type.FLOAT32
FLOAT64 = Type.FLOAT64
INT32 = Type.INT32
INT64 = Type.INT64
STRING = Type.STRING
TIMESTAMP_MICRO = Type.TIMESTAMP_MICRO
TIMESTAMP_MILLI = Type.TIMESTAMP_MILLI
__init__()
property name
__init__()
property types
class pyqstrat.pyqstrat_cpp.SubStringLineFilter

Bases: pyqstrat.pyqstrat_cpp.LineFilter

A helper class that will check if a line matches any of a set of strings

__call__()
Parameters

line (str) – We check if any of the patterns are present in this string

Returns

Whether any of the patterns were present

Return type

bool

__init__()
Parameters

patterns (list of str) – The list of strings to match against

class pyqstrat.pyqstrat_cpp.TextFileDecompressor

Bases: pyqstrat.pyqstrat_cpp.RecordGenerator

A helper function that takes a filename and its compression type, and returns a function that we can use to iterate over lines in that file

__call__()
Parameters
  • filename (str) – The file to read

  • compression (str) – One of “” for uncompressed files, “gzip”, “bz2” or “lzip”

Returns

A function that takes an empty string as input, and fills in that string. The function should return False EOF has been reached, True otherwise

__init__()
class pyqstrat.pyqstrat_cpp.TextFileProcessor

Bases: pyqstrat.pyqstrat_cpp.FileProcessor

A helper class that takes text based market data files and creates parsed and aggregated quote, trade, open interest, and other files from them.

__call__()
Parameters
  • input_filename (str) – The file to read

  • compression (str) – One of “” for uncompressed files, “gzip”, “bz2” or “lzip”

Returns

Number of lines processed

Return type

int

__init__()
Parameters
  • record_generator – A function that takes a filename and its compression type, and returns a function that we can use to iterate over lines in that file

  • line_filter – A function that takes a line (str) as input and returns whether we should parse it or discard it

  • record_parser – A function that takes a line (str) as input and returns a Record object

  • bad_line_handler – A function that takes a line that failed to parse and returns a Record object or None

  • record_filter – A function that takes a parsed Record object and returns whether we should keep it or discard it

  • missing_data_handler – A function that takes a parsed Record object and deals with missing data, for example, by converting 0’s to NANs

  • aggregators – A vector of functions that each take a parsed Record object and aggregate it.

  • skip_rows (int, optional) – Number of rows to skip in the file before starting to read it. Defaults to 1 to ignore a header line

class pyqstrat.pyqstrat_cpp.TextOpenInterestParser

Bases: pyqstrat.pyqstrat_cpp.RecordFieldParser

Helper class that parses an open interest record from a list of fields (strings)

__call__()
Parameters

fields (list of str) – A list of fields representing the record

Returns

or None if this record is not an open interest record

Return type

OpenInterestRecord

__init__()
Parameters
  • is_open_interest – A function that takes a list of strings as input and returns a bool if the fields represent an open interest record

  • base_date (int) – If the timestamp in the files does not have a date component, pass in the date as number of millis or micros since the epoch

  • timestamp_indices (list of int) – Index of the timestamp fields within the record. For example, date and time could be in different fields. We add the result of each timestamp field to get the final timestamp

  • qty_idx (int) – Index of the quote size field

  • id_field_indices (list of str) – Indices of the fields identifying an instrument. For example, for a future this could be symbol and expiry. These fields will be concatenated with a separator and placed in the id field in the record

  • meta_field_indices (list of str) – Indices of additional fields you want to store. For example, the exchange.

  • timestamp_parsers – A list of function that takes a timestamp as a string and returns number of millis or micros since the epoch

  • strip_id (bool, optional) – If we want to strip any whitespace from the id fields before concatenating them. Defaults to True

  • strip_meta (bool, optional) – If we want to strip any whitespace from the meta fields before concatenating them. Defaults to True

class pyqstrat.pyqstrat_cpp.TextOtherParser

Bases: pyqstrat.pyqstrat_cpp.RecordFieldParser

Helper class that parses a record that contains information other than a quote, trade or open interest record

__call__()
Parameters

fields (list of str) – a list of fields representing the record

Returns

Return type

OtherRecord

__init__()
Parameters
  • is_other – A function that takes a list of strings as input and returns a bool if we want to parse this record

  • base_date (int) – If the timestamp in the files does not have a date component, pass in the date as number of millis or micros since the epoch

  • timestamp_indices (list of int) – Index of the timestamp fields within the record. For example, date and time could be in different fields. We add the result of each timestamp field to get the final timestamp

  • id_field_indices (list of str) – Indices of the fields identifying an instrument. For example, for a future this could be symbol and expiry. These fields will be concatenated with a separator and placed in the id field in the record

  • meta_field_indices (list of str) – Indices of additional fields you want to store. For example, the exchange.

  • timestamp_parsers – A list of functions that take a timestamp as a string and returns number of millis or micros since the epoch

  • strip_id (bool, optional) – If we want to strip any whitespace from the id fields before concatenating them. Defaults to True

  • strip_meta (bool, optional) – If we want to strip any whitespace from the meta fields before concatenating them. Defaults to True

class pyqstrat.pyqstrat_cpp.TextQuotePairParser

Bases: pyqstrat.pyqstrat_cpp.RecordFieldParser

Helper class that parses a quote containing bid / ask in the same record from a list of fields (strings)

__call__()
Parameters
  • fields (list of str) – A list of fields representing the record

  • Returns

  • QuotePairRecord – Or None if this field is not a quote pair

__init__()
Parameters
  • is_quote_pair – a function that takes a list of strings as input and returns a bool if the fields represent a quote pair

  • base_date (int) – if the timestamp in the files does not have a date component, pass in the date as number of millis or micros since the epoch

  • timestamp_indices (list of int) – Index of the timestamp fields within the record. For example, date and time could be in different fields. We add the result of each timestamp field to get the final timestamp

  • bid_price_idx (int) – index of the field that contains the bid price

  • bid_qty_idx (int) – index of the field that contains the bid quantity

  • ask_price_idx (int) – index of the field that contains the ask price

  • ask_qty_idx (int) – index of the field that contains the ask quantity

  • id_field_indices (list of str) – indices of the fields identifying an instrument. For example, for a future this could be symbol and expiry. These fields will be concatenated with a separator and placed in the id field in the record.

  • meta_field_indices (list of str) – indices of additional fields you want to store. For example, the exchange.

  • timestamp_parsers – a list of functions that takes a timestamp as a string and returns number of millis or micros since the epoch

  • price_multiplier – (float, optional): sometimes the price in a file could be in hundredths of cents, and we divide by this to get dollars. Defaults to 1.0

  • strip_id (bool, optional) – if we want to strip any whitespace from the id fields before concatenating them. Defaults to True

  • strip_meta (bool, optional) – if we want to strip any whitespace from the meta fields before concatenating them. Defaults to True

class pyqstrat.pyqstrat_cpp.TextQuoteParser

Bases: pyqstrat.pyqstrat_cpp.RecordFieldParser

Helper class that parses a quote from a list of fields (strings)

__call__()
Parameters

fields (list of str) – A list of fields representing the record

Returns

Or None if this field is not a quote

Return type

QuoteRecord

__init__()
Parameters
  • is_quote – a function that takes a list of strings as input and returns a bool if the fields represent a quote

  • base_date (int) – if the timestamp in the files does not have a date component, pass in the date as number of millis or micros since the epoch

  • timestamp_indices (list of int) – index of the timestamp field within the record

  • bid_offer_idx (int) – index of the field that contains whether this is a bid or offer quote

  • price_idx (int) – index of the price field

  • qty_idx (int) – index of the quote size field

  • id_field_indices (list of str) – indices of the fields identifying an instrument. For example, for a future this could be symbol and expiry. These fields will be concatenated with a separator and placed in the id field in the record

  • meta_field_indices (list of str) – indices of additional fields you want to store. For example, the exchange.

  • timestamp_parsers – a vector of functions that take a timestamp as a string and returns number of millis or micros since the epoch

  • bid_str (str) – if the field indicated in bid_offer_idx matches this string, we consider this quote to be a bid

  • offer_str (str) – if the field indicated in bid_offer_idx matches this string, we consider this quote to be an offer

  • price_multiplier – (float, optional): sometimes the price in a file could be in hundredths of cents, and we divide by this to get dollars. Defaults to 1.0

  • strip_id (bool, optional) – if we want to strip any whitespace from the id fields before concatenating them. Defaults to True

  • strip_meta (bool, optional) – if we want to strip any whitespace from the meta fields before concatenating them. Defaults to True

class pyqstrat.pyqstrat_cpp.TextRecordParser

Bases: pyqstrat.pyqstrat_cpp.RecordParser

A helper class that takes in a text line, separates it into a list of fields based on a delimiter, and then uess the parsers passed in to try and parse the line as a quote, trade, open interest record or any other type of record

__init__()
Parameters
  • parsers – A vector of functions that each take a list of strings as input and returns a subclass of Record or None

  • exclusive (bool, optional) – Set this when each line can only contain one type of record, after one first parser returns a non None object, we will not call other parsers. Default false

  • separator (str, optional) – A single character string. This is the delimiter we use to separate fields from the text passed in. Default ,

class pyqstrat.pyqstrat_cpp.TextTradeParser

Bases: pyqstrat.pyqstrat_cpp.RecordFieldParser

Helper class that parses a trade from a list of fields (strings)

__call__()
Parameters

fields (list of str) – A list of fields representing the record

Returns

or None if this record is not a trade

Return type

TradeRecord

__init__()
Parameters
  • is_trade – A function that takes a list of strings as input and returns a bool if the fields represent a trade

  • base_date (int) – If the timestamp in the files does not have a date component, pass in the date as number of millis or micros since the epoch

  • timestamp_indices (list of int) – Index of the timestamp fields within the record. For example, date and time could be in different fields. We add the result of each timestamp field to get the final timestamp

  • price_idx (int) – Index of the price field

  • qty_idx (int) – Index of the quote size field

  • id_field_indices (list of str) – Indices of the fields identifying an instrument. For example, for a future this could be symbol and expiry. These fields will be concatenated with a separator and placed in the id field in the record

  • meta_field_indices (list of str) – Indices of additional fields you want to store. For example, the exchange.

  • timestamp_parsers – A list of functions that takes a timestamp as a string and returns number of millis or micros since the epoch

  • price_multiplier – (float, optional): Sometimes the price in a file could be in hundredths of cents, and we divide by this to get dollars. Defaults to 1.0

  • strip_id (bool, optional) – If we want to strip any whitespace from the id fields before concatenating them. Defaults to True

  • strip_meta (bool, optional) – If we want to strip any whitespace from the meta fields before concatenating them. Defaults to True

class pyqstrat.pyqstrat_cpp.TimestampParser

Bases: pybind11_builtins.pybind11_object

__call__()
__init__()
class pyqstrat.pyqstrat_cpp.TradeBarAggregator

Bases: pyqstrat.pyqstrat_cpp.Aggregator

Aggregate trade records to create trade bars, given a frequency. Calculates open, high, low, close, volume, vwap as well as last_update_time

which is timestamp of the last trade that we processed before the bar ended.

__call__()

Add a trade record to be written to disk at some point

Parameters
  • trade (TradeRecord) –

  • line_number (int) – The line number of the source file that this trade came from. Used for debugging

__init__()
Parameters
  • writer_creator – A function that takes an output_file_prefix, schema and returns an object implementing the Writer interface

  • output_file_prefix (str) – Path of the output file to create. The writer and aggregator may add suffixes to this to indicate the kind of data and format the file creates. E.g. “/tmp/output_file_1”

  • frequency – A string like “5m” indicating the bar size is 5 mins. Units can be s,m,h or d for second, minute, hour or day. Defaults to “5m”

close()

Flush all unwritten records to the Writer, which writes them to disk when its close function is called

class pyqstrat.pyqstrat_cpp.TradeRecord

Bases: pyqstrat.pyqstrat_cpp.Record

A parsed trade record that we can save to disk

id

A unique string representing a symbol or instrument id

Type

str

timestamp

Trade time, in milliseconds or microseconds since 1/1/1970

Type

int

qty

Trade quantity

Type

float

price

Trade price

Type

float

metadata

a string representing any extra information you want to save, such as exchange, or special trade conditions

Type

str

__init__()
property id
property metadata
property price
property qty
property timestamp
class pyqstrat.pyqstrat_cpp.Writer

Bases: pybind11_builtins.pybind11_object

An abstract class that you subclass to provide an object that can write to disk

__init__()

Initialize self. See help(type(self)) for accurate signature.

close()

Close the writer and flush any remaining data to disk

Parameters

success (bool, optional) – If set to False, we had some kind of exception and are cleaning up. Tells the function to not indicate the file was written successfully, for example by renaming a temp file to the actual filename. Defaults to True

write_batch()

Write a batch of records to disk. The batch can have an optional string id so we can later retrieve just this batch of records without reading the whole file

Parameters

batch_id (str, optional) – An identifier which can later be used to retrieve this batch from disk. Defaults to “”

class pyqstrat.pyqstrat_cpp.WriterCreator

Bases: pybind11_builtins.pybind11_object

__call__()
__init__()
class pyqstrat.pyqstrat_cpp.ZipFileReader

Bases: pyqstrat.pyqstrat_cpp.RecordGenerator

A helper function that takes the name of a zip file and returns a function that we can use to iterate over lines in that file

__call__()
Parameters
  • filename (str) – The file to read

  • compression (str) – Ignored

Returns

A function that takes an empty string as input, and fills in that string. The function should return False EOF has been reached, True otherwise

__init__()
Parameters

archive_to_filenames (dict of list(str)) – zip archive file name to list of files to read within that archive

pyqstrat.pyqstrat_cpp.black_scholes_price(call: numpy.ndarray[bool], S: numpy.ndarray[float64], K: numpy.ndarray[float64], t: numpy.ndarray[float64], r: numpy.ndarray[float64], sigma: numpy.ndarray[float64], q: numpy.ndarray[float64] = 0.0) → object

Compute Euroepean option price :param call: True for a call option, False for a put :type call: bool :param S: Spot price. For a future discount the future price using exp(-rt) :type S: float :param K: Strike :type K: float :param t: Time to maturity in years :type t: float :param r: Continuously compounded interest rate. Use 0.01 for 1% :type r: float :param sigma: Annualized volatility. Use 0.01 for 1% :type sigma: float :param q: Annualized dividend yield. Use 0.01 for 1%. Default 0 :type q: float, optional

Returns

Option price

Return type

float

pyqstrat.pyqstrat_cpp.cdf(x: numpy.ndarray[float64]) → object

Cumulative density function of normal distribution :param x: random variable :type x: float

Returns

cdf of the random variable

Return type

float

pyqstrat.pyqstrat_cpp.d1(S: numpy.ndarray[float64], K: numpy.ndarray[float64], t: numpy.ndarray[float64], r: numpy.ndarray[float64], sigma: numpy.ndarray[float64], q: numpy.ndarray[float64] = 0.0) → object

d1 from Black Scholes :param S: Spot price. For a future discount the future price using exp(-rt) :type S: float :param K: Strike :type K: float :param t: Time to maturity in years :type t: float :param r: Continuously compounded interest rate. Use 0.01 for 1% :type r: float :param sigma: Annualized volatility. Use 0.01 for 1% :type sigma: float :param q: Annualized dividend yield. Use 0.01 for 1% :type q: float

Returns

Return type

float

pyqstrat.pyqstrat_cpp.d2(S: numpy.ndarray[float64], K: numpy.ndarray[float64], t: numpy.ndarray[float64], r: numpy.ndarray[float64], sigma: numpy.ndarray[float64], q: numpy.ndarray[float64] = 0.0) → object

d2 from Black Scholes :param S: Spot price. For a future discount the future price using exp(-rt) :type S: float :param K: Strike :type K: float :param t: Time to maturity in years :type t: float :param r: Continuously compounded interest rate. Use 0.01 for 1% :type r: float :param sigma: Annualized volatility. Use 0.01 for 1% :type sigma: float :param q: Annualized dividend yield. Use 0.01 for 1% :type q: float

Returns

Return type

float

pyqstrat.pyqstrat_cpp.delta(call: numpy.ndarray[bool], S: numpy.ndarray[float64], K: numpy.ndarray[float64], t: numpy.ndarray[float64], r: numpy.ndarray[float64], sigma: numpy.ndarray[float64], q: numpy.ndarray[float64] = 0.0) → object

Compute European option delta :param call: True for a call option, False for a put :type call: bool :param S: Spot price. For a future discount the future price using exp(-rt) :type S: float :param K: Strike :type K: float :param t: Time to maturity in years :type t: float :param r: Continuously compounded interest rate. Use 0.01 for 1% :type r: float :param sigma: Annualized volatility. Use 0.01 for 1% :type sigma: float :param q: Annualized dividend yield. Use 0.01 for 1%. Default 0 :type q: float, optional

Returns

Option delta

Return type

float

pyqstrat.pyqstrat_cpp.gamma(S: numpy.ndarray[float64], K: numpy.ndarray[float64], t: numpy.ndarray[float64], r: numpy.ndarray[float64], sigma: numpy.ndarray[float64], q: numpy.ndarray[float64] = 0.0) → object

Compute European option gamma. :param S: Spot price. For a future discount the future price using exp(-rt) :type S: float :param K: Strike :type K: float :param t: Time to maturity in years :type t: float :param r: Continuously compounded interest rate. Use 0.01 for 1% :type r: float :param sigma: Annualized volatility. Use 0.01 for 1% :type sigma: float :param q: Annualized dividend yield. Use 0.01 for 1%. Default 0 :type q: float, optional

Returns

Option gamma

Return type

float

pyqstrat.pyqstrat_cpp.implied_vol(call: numpy.ndarray[bool], price: numpy.ndarray[float64], S: numpy.ndarray[float64], K: numpy.ndarray[float64], t: numpy.ndarray[float64], r: numpy.ndarray[float64], q: numpy.ndarray[float64] = 0.0) → object

Compute implied volatility for a European option. :param call: True for a call option, False for a put :type call: bool :param price: The option premium :type price: float :param S: Spot price. For a future discount the future price using exp(-rt) :type S: float :param K: Strike :type K: float :param t: Time to maturity in years :type t: float :param r: Continuously compounded interest rate. Use 0.01 for 1% :type r: float :param q: Annualized dividend yield. Use 0.01 for 1%. Default 0 :type q: float, optional

Returns

Implied volatility. For 1% we return 0.01

Return type

float

class pyqstrat.pyqstrat_cpp.ostream_redirect

Bases: pybind11_builtins.pybind11_object

__init__(self: pyqstrat.pyqstrat_cpp.ostream_redirect, stdout: bool = True, stderr: bool = True) → None
pyqstrat.pyqstrat_cpp.rho(call: numpy.ndarray[bool], S: numpy.ndarray[float64], K: numpy.ndarray[float64], t: numpy.ndarray[float64], r: numpy.ndarray[float64], sigma: numpy.ndarray[float64], q: numpy.ndarray[float64] = 0.0) → object

Compute European option rho. This is Black Scholes formula rho divided by 100 so we get rho per 1% change in interest rate :param call: True for a European call option, False for a put :type call: bool :param S: Spot price. For a future discount the future price using exp(-rt) :type S: float :param K: Strike :type K: float :param t: Time to maturity in years :type t: float :param r: Continuously compounded interest rate. Use 0.01 for 1% :type r: float :param sigma: Annualized volatility. Use 0.01 for 1% :type sigma: float :param q: Annualized dividend yield. Use 0.01 for 1%. Default 0 :type q: float, optional

Returns

Option theta

Return type

float

pyqstrat.pyqstrat_cpp.theta(call: numpy.ndarray[bool], F: numpy.ndarray[float64], K: numpy.ndarray[float64], t: numpy.ndarray[float64], r: numpy.ndarray[float64], sigma: numpy.ndarray[float64], q: numpy.ndarray[float64] = 0.0) → object

Compute European option theta per day. This is Black Scholes formula theta divided by 365 to give us the customary theta per day :param call: True for a call option, False for a put :type call: bool :param S: Spot price. For a future discount the future price using exp(-rt) :type S: float :param K: Strike :type K: float :param t: Time to maturity in years :type t: float :param r: Continuously compounded interest rate. Use 0.01 for 1% :type r: float :param sigma: Annualized volatility. Use 0.01 for 1% :type sigma: float :param q: Annualized dividend yield. Use 0.01 for 1%. Default 0 :type q: float, optional

Returns

Option theta

Return type

float

pyqstrat.pyqstrat_cpp.vega(S: float, K: float, t: float, r: float, sigma: float, q: float = 0.0) → float

Compute European option vega. This is Black Scholes formula vega divided by 100 so we get rho per 1% change in interest rate :param S: Spot price. For a future discount the future price using exp(-rt) :type S: float :param K: Strike :type K: float :param t: Time to maturity in years :type t: float :param r: Continuously compounded interest rate. Use 0.01 for 1% :type r: float :param sigma: Annualized volatility. Use 0.01 for 1% :type sigma: float :param q: Annualized dividend yield. Use 0.01 for 1%. Default 0 :type q: float, optional

Returns

Option vega

Return type

float

pyqstrat.marketdata_processor module

class pyqstrat.marketdata_processor.PathFileNameProvider(path, include_pattern=None, exclude_pattern=None)[source]

Bases: object

A helper class that, given a pattern such as such as “/tmp/abc*.gz” and an optional include and exclude pattern, returns names of all files that match

__call__()[source]
Return type

Sequence[str]

Returns

All matching filenames

__init__(path, include_pattern=None, exclude_pattern=None)[source]
Parameters
  • path (str) – A pattern such as “/tmp/abc*.gz”

  • include_pattern (Optional[str]) – Given a pattern such as “xzy”, will return only filenames that contain xyz

  • exclude_pattern (Optional[str]) – Given a pattern such as “_tmp”, will exclude all filenames containing _tmp

Return type

None

class pyqstrat.marketdata_processor.SingleDirectoryFileNameMapper(output_dir)[source]

Bases: object

A helper class that provides a mapping from input filenames to their corresponding output filenames in an output directory.

__call__(input_filepath)[source]
Parameters

input_filepath (str) – The input file that we are creating an output file for, e.g. “/home/xzy.gz”

Return type

str

Returns

Output file path for that input. We take the filename from the input filepath, strip out any extension

and prepend the output directory name

__init__(output_dir)[source]
Parameters

output_dir (str) – The directory where we want to write output files

Return type

None

class pyqstrat.marketdata_processor.TextHeaderParser(record_generator_creator, skip_rows=0, separator=', ', make_lowercase=True)[source]

Bases: object

Parses column headers from a text file containing market data

__call__(input_filename, compression)[source]

Args:

input_filename The file to read compression: Compression type, e.g. “gzip”, or None if the file is not compressed

Return type

Sequence[str]

Returns

Column headers

__init__(record_generator_creator, skip_rows=0, separator=', ', make_lowercase=True)[source]
Parameters
  • record_generator – A function that takes a filename and its compression type and returns an object that we can use to iterate through lines in that file

  • skip_rows (int) – Number of rows to skip before starting to read the file. Default is 0

  • separator (str) – Separator for headers. Defaults to ,

  • make_lowercase (bool) – Whether to convert headers to lowercase before returning them

Return type

None

pyqstrat.marketdata_processor.base_date_filename_mapper(input_file_path)[source]

A helper function that parses out the date from a filename. For example, given a file such as “/tmp/spx_2018-08-09”, this parses out the date part of the filename and returns milliseconds (no fractions) since the epoch to that date.

Parameters

input_filepath (str) – Full path to the input file

Returns

Milliseconds since unix epoch to the date implied by that file

Return type

int

>>> base_date_filename_mapper("/tmp/spy_1970-1-2_quotes.gz")
86400000
pyqstrat.marketdata_processor.create_text_file_processor(record_generator, line_filter, record_parser, bad_line_handler, record_filter, missing_data_handler, aggregators, skip_rows=1)[source]
Return type

Callable[[str, Optional[str]], int]

pyqstrat.marketdata_processor.get_field_indices(field_names, headers)[source]

Helper function to get indices of field names in a list of headers

Parameters
  • field_names (list of str) – The fields we want indices of

  • headers (list of str) – All headers

Returns

indices of each field name in the headers list

Return type

list of int

pyqstrat.marketdata_processor.process_marketdata(input_filename_provider, file_processor, num_processes=None, raise_on_error=True)[source]

Top level function to process a set of market data files

Parameters
  • input_filename_provider (Callable[[], Sequence[str]]) – A function that returns a list of filenames (incl path) we need to process.

  • file_processor (Callable[[str, Optional[str]], int]) – A function that takes an input filename and processes it, returning number of lines processed.

  • num_processes (int, optional) – The number of processes to run to parse these files. If set to None, we use the number of cores present on your machine. Defaults to None

  • raise_on_error (bool, optional) – If set, we raise an exception when there is a problem with parsing a file, so we can see a stack trace and diagnose the problem. If not set, we print the error and continue. Defaults to True

Return type

None

pyqstrat.marketdata_processor.process_marketdata_file(input_filename, output_file_prefix_mapper, record_parser_creator, aggregator_creator, line_filter=None, compression=None, base_date_mapper=None, file_processor_creator=<function create_text_file_processor>, header_parser_creator=<function <lambda>>, header_record_generator=<function text_file_record_generator_creator>, record_generator=<pyqstrat.pyqstrat_cpp.TextFileDecompressor object>, bad_line_handler=<pyqstrat.pyqstrat_cpp.PrintBadLineHandler object>, record_filter=None, missing_data_handler=<pyqstrat.pyqstrat_cpp.PriceQtyMissingDataHandler object>, writer_creator=<pyqstrat.pyqstrat_cpp.ArrowWriterCreator object>)[source]

Processes a single market data file

Parameters
  • input_filename (str) – file to process

  • output_file_prefix_mapper (Callable[[str], str]) – A function that takes an input filename and returns the corresponding output filename we want

  • record_parser_creator (Callable[[int, Sequence[str]], Callable[[Sequence[str]], Record]]) – A function that takes a date and a list of column names and returns a function that can take a list of fields and return a subclass of Record

  • aggregator_creator (Callable[[Callable[[str, Schema, bool, int], Writer], str], Sequence[Aggregator]]) – A function that takes a writer creator and a output file prefix and returns a list of Aggregators

  • line_filter (Optional[Callable[[str], bool]]) – A function that takes a line and decides whether we want to keep it or discard it. Defaults to None

  • compression (Optional[str]) – Compression type for the input file. Defaults to None

  • base_date_mapper (Optional[Callable[[str], int]]) – A function that takes an input filename and returns the date implied by the filename, represented as millis since epoch. Defaults to helper function base_date_filename_mapper

  • file_processor_creator (Callable[[Union[Iterable[str], Iterable[bytes]], Optional[Callable[[str], bool]], Callable[[Sequence[str]], Record], Callable[[str, Exception], Record], Optional[Callable[[Record], bool]], Callable[[Record], None], Sequence[Aggregator]], Callable[[str, Optional[str]], int]]) – A function that returns an object that we can use to iterate through lines in a file. Defaults to helper function create_text_file_processor

  • header_record_generator (Callable[[str, str], Union[Iterable[str], Iterable[bytes]]]) – A function that takes a filename and compression and returns a generator that we can use to get column headers

  • record_generator (Union[Iterable[str], Iterable[bytes]]) – A function that takes a filename and compression and returns a generator that we can use to iterate through lines in the file

  • bad_line_handler (optional) – A function that takes a line that we could not parse, and either parses it or does something else like recording debugging info, or stopping the processing by raising an exception. Defaults to helper function PrintBadLineHandler

  • record_filter (optional) – A function that takes a parsed TradeRecord, QuoteRecord, OpenInterestRecord or OtherRecord and decides whether we want to keep it or discard it. Defaults to None

  • missing_data_handler (optional) – A function that takes a parsed TradeRecord, QuoteRecord, OpenInterestRecord or OtherRecord, and decides deals with any data that is missing in those records. For example, 0 for bid could be replaced by NAN. Defaults to helper function: price_qty_missing_data_handler

  • writer_creator (optional) – A function that takes an output_file_prefix, schema, whether to create a batch id file, and batch_size and returns a subclass of Writer. Defaults to helper function: arrow_writer_creator

Return type

None

pyqstrat.marketdata_processor.text_file_record_generator_creator(filename, compression=None)[source]

A helper function that returns a generator that we can use to iterate through lines in the input file :type filename: str :param filename: The input filename :type compression: Optional[str] :param compression: The compression type of the input file or None if its not compressed

Return type

Union[Iterable[str], Iterable[bytes]]

pyqstrat.trade_bars module

class pyqstrat.trade_bars.TradeBars(timestamps, c, o=None, h=None, l=None, v=None, vwap=None)[source]

Bases: object

Used to store OHLCV bars. You must at least supply timestamps and close prices. All other fields are optional.

timestamp

A numpy datetime array with the datetime for each bar. Must be monotonically increasing.

c

A numpy float array with close prices for the bar.

o

A numpy float array with open prices . Default None

h

A numpy float array with high prices. Default None

l

A numpy float array with high prices. Default None

v

A numpy integer array with volume for the bar. Default None

vwap

A numpy float array with the volume weighted average price for the bar. Default None

__init__(timestamps, c, o=None, h=None, l=None, v=None, vwap=None)[source]

Zeroes in o, h, l, c are set to nan

Return type

None

add_timestamps(timestamps)[source]

Adds new timestamps to a market data object.

Parameters

timestamps (np.array of np.datetime64) – New timestamps to add. Does not have to be sorted or unique

>>> timestamps = np.array(['2018-01-05', '2018-01-09', '2018-01-10'], dtype = 'M8[ns]')
>>> c = np.array([8.1, 8.2, 8.3])
>>> o = np.array([9, 10, 11])
>>> trade_bar = TradeBars(timestamps, c, o)
>>> new_timestamps = np.array(['2018-01-07', '2018-01-09'], dtype = 'M8[ns]')
>>> trade_bar.add_timestamps(new_timestamps)
>>> print(trade_bar.timestamps)
['2018-01-05T00:00:00.000000000' '2018-01-07T00:00:00.000000000'
 '2018-01-09T00:00:00.000000000' '2018-01-10T00:00:00.000000000']
>>> np.set_printoptions(formatter = {'float': lambda x: f'{x:.4f}'})  # After numpy 1.13 positive floats don't have a leading space for sign
>>> print(trade_bar.o, trade_bar.c)
[9.0000 nan 10.0000 11.0000] [8.1000 nan 8.2000 8.3000]
Return type

None

describe(warn_std=10, time_distribution_frequency='15 min', print_time_distribution=False)[source]

Describe the bars. Shows an overview, errors and warnings for the bar data. This is a good function to use before running any backtests on a set of bar data.

Parameters
  • warn_std (int) – See warning function

  • time_distribution_frequency (str) – See time_distribution function

  • print_time_distribution (bool) – Whether to print the time distribution in addition to plotting it.

Return type

None

df(start_date=None, end_date=None)[source]
Return type

DataFrame

errors(display=True)[source]

Returns a dataframe indicating any highs that are lower than opens, closes, lows or lows that are higher than other columns Also includes any ohlcv values that are negative

Return type

Optional[DataFrame]

freq_str()[source]
Return type

str

has_ohlc()[source]

Returns True if we have all ohlc columns and none are empty

Return type

bool

overview(display=True)[source]

Returns a dataframe showing basic information about the data, including count, number and percent missing, min, max

Parameters

display (bool) – Whether to print out the warning dataframe as well as returning it

Return type

DataFrame

plot(figsize=(15, 8), date_range=None, sampling_frequency=None, title='Price / Volume')[source]

Plot a candlestick or line plot depending on whether we have ohlc data or just close prices

Parameters
  • figsize (Tuple[int, int]) – Size of the figure (default (15,8))

  • date_range (Union[Tuple[str, str], Tuple[datetime64, datetime64], None]) – A tuple of strings or numpy datetimes for plotting a smaller sample of the data, e.g. (“2018-01-01”, “2018-01-06”)

  • sampling_frequency (Optional[str]) – Downsample before plotting. See pandas frequency strings for possible values.

  • title (str) – Title of the graph, default “Price / Volume”

Return type

None

resample(sampling_frequency)[source]

Downsample the trade bars data into a new bar frequency

Parameters

sampling_frequency (str) – See sampling frequency in pandas

Return type

Optional[TradeBars]

time_distribution(frequency='15 minutes', display=True, plot=True, figsize=None)[source]

Return a dataframe with the time distribution of the bars

Parameters
  • frequency (str) – The width of each bin (default “15 minutes”). You can use hours or days as well.

  • display (bool) – Whether to display the data in addition to returning it.

  • plot (bool) – Whether to plot the data in addition to returning it.

  • figsize (Optional[Tuple[int, int]]) – If plot is set, optional figure size for the plot (default (20,8))

Return type

DataFrame

valid_row(i)[source]

Return True if the row with index i has no nans in it.

Return type

bool

warnings(warn_std=10, display=True)[source]

Returns a dataframe indicating any values where the bar over bar change is more than warn_std standard deviations.

Parameters
  • warn_std (int) – Number of standard deviations to use as a threshold (default 10)

  • display (bool) – Whether to print out the warning dataframe as well as returning it

Return type

DataFrame

pyqstrat.trade_bars.roll_futures(fut_prices, date_func, condition_func, expiries=None, return_full_df=False)[source]

Construct a continuous futures dataframe with one row per datetime given rolling logic

Parameters
  • fut_prices (DataFrame) – A dataframe containing the columns ‘date’, ‘series’, and any other market data, for example, ohlcv data. Date can contain time for sub-daily bars. The series column must contain a different string name for each futures series, e.g. SEP2018, DEC2018, etc.

  • date_func (Callable[[DataFrame], ndarray]) – A function that takes the future prices as an input and returns a numpy array of booleans True indicates that the future should be rolled on this date if the condition specified in condition_func is met. This function can assume that we have all the columns in the original market data object plus the same columns suffixed with _next for the potential series to roll over to.

  • condition_func (Callable[[DataFrame], ndarray]) – A function that takes the future prices as input and returns a numpy array of booleans. True indicates that we should try to roll the future at that row.

  • expiries (Optional[DataFrame]) – An optional dataframe with 2 columns, ‘series’ and ‘expiry’. This should have one row per future series indicating that future’s expiry date. If you don’t pass this in, the function will assume that the expiry column is present in the original dataframe.

  • return_full_df (bool) – If set, will return the datframe without removing extra timestamps so you can use your own logic for rolling, including the _next columns and the roll flag

Return type

DataFrame

Returns

A pandas DataFrame with one row per date, which contains the columns in the original md DataFrame and the same columns suffixed with _next

representing the series we want to roll to. There is also a column called roll_flag which is set to True whenever the date and roll condition functions are met.

>>> fut_prices = pd.DataFrame({'timestamp': np.concatenate((np.arange(np.datetime64('2018-03-11'), np.datetime64('2018-03-16')),
...                                           np.arange(np.datetime64('2018-03-11'), np.datetime64('2018-03-16')))),
...                   'c': [10, 10.1, 10.2, 10.3, 10.4] + [10.35, 10.45, 10.55, 10.65, 10.75],
...                   'v': [200, 200, 150, 100, 100] + [100, 50, 200, 250, 300],
...                   'series': ['MAR2018'] * 5 + ['JUN2018'] * 5})[['timestamp','series', 'c', 'v']]
>>> expiries = pd.Series(np.array(['2018-03-15', '2018-06-15'], dtype = 'M8[D]'), index = ['MAR2018', 'JUN2018'], name = "expiry")
>>> date_func = lambda fut_prices: fut_prices.expiry - fut_prices.timestamp <= np.timedelta64(3, 'D')
>>> condition_func = lambda fut_prices: fut_prices.v_next > fut_prices.v
>>> df = roll_futures(fut_prices, date_func, condition_func, expiries)
>>> print(df[df.series == 'MAR2018'].timestamp.max() == np.datetime64('2018-03-14'))
True
>>> print(df[df.series == 'JUN2018'].timestamp.max() == np.datetime64('2018-03-15'))
True
pyqstrat.trade_bars.sort_trade_bars(columns)[source]

Given a list of column names, sort them in olhcv order

Return type

Sequence[str]

pyqstrat.trade_bars.test_trade_bars()[source]
Return type

None

pyqstrat.markets module

class pyqstrat.markets.EminiFuture[source]

Bases: object

calendar = <pyqstrat.holiday_calendars.Calendar object>
static get_current_symbol(curr_date)[source]
>>> assert(EminiFuture.get_current_symbol(datetime.date(2019, 3, 14)) == 'ESH9')
>>> assert(EminiFuture.get_current_symbol(datetime.date(2019, 3, 15)) == 'ESM9')
Return type

str

static get_expiry(fut_symbol)[source]
>>> assert(EminiFuture.get_expiry('ESH8') == np.datetime64('2018-03-16T08:30'))
Return type

datetime64

static get_next_symbol(curr_future_symbol)[source]
>>> assert(EminiFuture.get_next_symbol('ESZ8') == 'ESH9')
Return type

str

static get_previous_symbol(curr_future_symbol)[source]
>>> assert(EminiFuture.get_previous_symbol('ESH9') == 'ESZ8')
Return type

str

class pyqstrat.markets.EminiOption[source]

Bases: object

calendar = <pyqstrat.holiday_calendars.Calendar object>
static decode_symbol(name)[source]
>>> EminiOption.decode_symbol('E1AF8')
(MO, 2018, 1, 1)
Return type

Tuple[weekday, int, int, int]

static get_expiry(symbol)[source]
>>> EminiOption.get_expiry('EW2Z5')
numpy.datetime64('2015-12-11T15:00')
>>> EminiOption.get_expiry('E3AF7')
numpy.datetime64('2017-01-17T15:00')
>>> EminiOption.get_expiry('EWF0')
numpy.datetime64('2020-01-31T15:00')
Return type

datetime64

pyqstrat.markets.future_code_to_month(future_code)[source]

Given a future code such as “X”, return the month abbreviation, such as “nov”

Parameters

future_code (str) – the one letter future code

>>> future_code_to_month('X')
'nov'
Return type

str

pyqstrat.markets.future_code_to_month_number(future_code)[source]

Given a future code such as “X”, return the month number (from 1 - 12)

Parameters

future_code (str) – the one letter future code

>>> future_code_to_month_number('X')
11
Return type

int

pyqstrat.markets.get_future_code(month)[source]

Given a month number such as 3 for March, return the future code for it, e.g. H >>> get_future_code(3) ‘H’

Return type

str

Module contents