arbitragelab.trading.z_score
Module for signal generation for Bollinger Bands Trading strategy.
Module Contents
Classes
This class implements Bollinger Bands strategy from the book |
- class BollingerBandsTradingRule(sma_window: int, std_window: int, entry_z_score: float = 3, exit_z_score_delta: float = 6)
This class implements Bollinger Bands strategy from the book by E.P Chan: “Algorithmic Trading: Winning Strategies and Their Rationale”, page 70. The strategy generates a signal by when
|z_score| >= entry_z_score
and exit from a position when|z_score| <= |entry_z_score + z_score_delta|
.- static get_z_score(spread_slice: collections.deque, sma_window: int, std_window: int) float
Calculate z-score of recent spread values.
- Parameters:
spread_slice – (deque) Spread deque to take values from.
sma_window – (int) Window for SMA (Simple Moving Average).
std_window – (int) Window for SMD (Simple Moving st. Deviation).
- Returns:
(float) Z score.
- update_spread_value(latest_spread_value: float)
Adds latest spread value to self.spread_series. Once it is done - one can check entry/exit signals.
- Parameters:
latest_spread_value – (float) Latest spread value.
- check_entry_signal() tuple
Function which checks entry condition for a spread series based on self.z_score_entry, self.spread_series.
- Returns:
(tuple) Tuple of boolean entry flag and side (if entry flag is True).
- add_trade(start_timestamp: pandas.Timestamp, side_prediction: int, uuid: uuid.UUID = None)
Adds a new trade to track. Calculates trigger prices and trigger timestamp.
- Parameters:
start_timestamp – (pd.Timestamp) Timestamp of the future label.
side_prediction – (int) External prediction for the future label.
uuid – (str) Unique identifier used to link label to tradelog action.
- update_trades(update_timestamp: pandas.Timestamp) list
Checks whether any of the thresholds are triggered and currently open trades should be closed.
- Parameters:
update_timestamp – (pd.Timestamp) New timestamp to check vertical threshold.
- Returns:
(list) List of closed trades.