margot.data¶
In our journey with Margot, we begin with data.
To the quantitative trader, data is the link to the observable universe upon which we overlaycour ideas, to seek to uncover statistical or seasonality effects, mispricings or premium for carrying risk.
Margot data aims to provide a framework within which we can encapsulate this complexity, making the act of defining trading algorithms as declarative and as simple as possible.
-
class
margot.data.
MargotDataFrame
(env: dict = {})¶ A MargotDataFrame brings together symbols, columns, features and ratios.
Example:
class Equity(Symbol): adj_close = av.Column(function='historical_daily_adjusted', time_series='adjusted_close') log_returns = finance.LogReturns(column='adj_close') realised_vol = finance.RealisedVolatility(column='log_returns', window=30) class ExampleDF(MargotDataFrame): spy = Equity(symbol='SPY', trading_calendar='NYSE') vtwo = Equity(symbol='VTWO', trading_calendar='NYSE') spy_russ_ratio = Ratio(numerator=spy.adj_close, denominator=vtwo.adj_close, label='spy_russ') mydf = ExampleDF()
- Parameters
env (dict) – optional env dictionary as an alternative to sysenv variables.
-
to_pandas
(when: datetime.datetime = None, dropna=True) → pandas.core.frame.DataFrame¶ Return a pandas Dataframe representing this MargotDataFrame.
- Parameters
when (datetime, optional) – slice to only show data that was available at when. That is, the EOD from the previous day. Defaults to None.
- Returns
- a Pandas dataframe representing all data from
the MargotDataFrame
- Return type
pd.DataFrame
-
refresh
()¶ Refresh all Symbols in this DataFrame.
-
property
start_date
¶ First Timestamp of the time-series index.
- Returns
a pandas timestamp.
- Return type
Timestamp
-
property
end_date
¶ Last Timestamp value of the time-series index.
- Returns
a pandas timestamp.
- Return type
Timestamp
-
property
index
¶ Return the time-series index.
- Returns
a pandas timeseries index.
- Return type
pd.Index
-
simulate
(when)¶ Create a dataframe simulating a datetime in history.
Used for backtesting to simplify the writing of trading algorithms. After simulating a historical datetime, it is not possible to go back to the future.
- Parameters
when (tz_aware datetime or pd.Timestamp) – when to go back to.
-
class
margot.data.
Symbol
(symbol: str, trading_calendar: str, env: dict = {})¶ A Symbol, represents a securitised tradable asset.
A symbol can contain columns and features as members.
Usage example:
class Equity(Symbol): adj_close = av.Column(function='historical_daily_adjusted', time_series='adjusted_close') log_returns = finance.LogReturns(column='adj_close') realised_vol = finance.RealisedVolatility(column='log_returns', window=30) spy = Equity(symbol='SPY', trading_calendar='NYSE')
- Parameters
symbol (str) – the code for this symbol
trading_calendar (str) – ISO Code market identifier uses https://github.com/quantopian/trading_calendars/blob/master/README.md
env (dict) – Optional - pass in env values rather than use sysenv.
-
refresh
()¶ Refresh all columns in this Symbol.
-
simulate
(when=None)¶ Make the Symbol simulate a datetime in history.
Used for backtesting to simplify the writing of trading algorithms.
- Parameters
when (tz_aware datetime or pd.Timestamp) – when to go back to.
-
class
margot.data.
Ratio
(numerator, denominator, label, **kwargs)¶ Ratio of two series.
Ratio = numerator / denominator of two time-series.
Example:
my_ratio = Ratio(numerator=symbol_1.adj_close, denominator=symbol_2.adj_close, label='s1_s2_ratio')
- Parameters
numerator (str) – the series to numerate the ratio
denominator (str) – the series to denominate the ratio
label (str) – give it a name for your dataframe. e.g. current_ratio
-
property
latest
¶ Return the latest value in this series