margot.signals

The margot.signals module contains classes to help you construct trading algorithms using the MargotDataFrame

class margot.signals.BaseAlgo(env: dict = {}, market='XNYS')

A base class to inherit when implementing your trading algorithm.

You should at least implement signal() which is the output of a trading algorithm.

Parameters
  • env (dict) – a dictionary of environment variables, e.g. API keys. Overrides anything provided in sys env.

  • market (str) – The ISO code for the market we will use.

Raises
  • ValueError – the attribute, ‘data’ must be a reference to a MargotDataFrame.

  • NotImplementedError – If your subclass does not implement signal(), you will receive a NotImplementedError.

weekday(dt)

Return a human readable three letter day of week.

Convert the Python integer representation of day of week into a string.

e.g:

0: 'MON' (also known as self.MONDAY)

Note

You should always use the built in constants when passing days of the week. e.g. self.MONDAY, self.TUESDAY, … these map to the three charater strings.

Parameters

dt (datetime or pd.Timestamp) – The datetime to check

Returns

One of; ‘MON’, ‘TUE’, ‘WED’, ‘THU’, ‘FRI’, ‘SAT’, ‘SUN’

Return type

str

property next_close

Return a UTC pd.Timestamp of the next close of trading session.

Returns

Timestamp of the next close of cash session in UTC.

Return type

pd.Timestamp

signal() → list

Return a list of Position objects for a given datetime.

simulate_signal(when: datetime.datetime)

Simulate a signal from a point in time.

Stores the original MargotDataFrame referenced by self.data on a temporary reference so that the data attribute can be used by signal() to calculate positions at a point in history.

After running signal(), the full dataframe is re-referenced at self.data.

Parameters

when (datetime) – when in history to go back to

Returns

a list of Position objects.

Return type

list

class margot.signals.BackTest(algo, start_balance=100000)

Backtest an trading algo that’s a descendent of BaseAlgo.

Warning

BackTest is still a work in progress - it probably doesn’t even work yet!

algo

A boolean indicating if we like SPAM or not.

starting_balance

An integer count of the eggs we have laid.

calculate_returns()

Calculate returns.

Assumes the trade is made the next period after a signal is generated.

You should construct your MargotDataFrame to be indexed by the trading periods (e.g. days).

create_trade_signals_timeseries()

Create time-series of when position changes occur.

Return the subset of the positions time-series to indicate positions

when signals indicate trade should be placed.

Returns

A dataframe of signals when changes to positions are

suggested.

Return type

pd.DataFrame

create_position_timeseries(periods)

Create Position time-series from signals.

Runs through all of the backtest data, generating position indicating signals.

Parameters

periods (int) – the number of periods to backtest over, counted back from the end of the dataset. If no value is supplied then the whole dataset is used.

Returns

time-series of Positions

Return type

pd.DataFrame

run(periods=None)

Run the backtest.

Returns

[description]

Return type

[type]

volatility(days=30, periods=252)

Return a single float value for realised historical volatility.

TODO: Change the periods parameter to instead examine the data. :param days: Days to lookback. Defaults to 30. :type days: int, optional

class margot.signals.Position(symbol: str, weight: float, order_type: str)

Represents a Position with a symbol and a weight.

Parameters
  • symbol (str) – The identifier of the symbol. e.g. ‘SPY’.

  • weight (float) – A value between -1.0 and +1.0 representing the weight of this symbol in the position list.

as_map()

Return the Position as a dictionary.