Strategies¶
-
class
traderion.strategies.base.
Base
(api, options)¶ Bases:
object
The Base class serves as the foundation for all strategy implementations. It defines a set of methods that can be overridden by derived classes to react to different events in the trading environment.
- Methods:
- on_position_change(new_position): Called when the bot position changes. on_price_curve_change(new_price_curve): Called when the price curve changes. on_eb_depth_change(new_depth): Called when the electronic broker depth changes. on_new_client_call(client_call): Called when a new client call appears. on_client_deal(client_call): Called when a client deal occurs. on_client_reject(client_call): Called when a client call is rejected. run(): Contains the main logic to be executed by the strategy. log_info(): Should be implemented by subclasses to log strategy-specific information.
-
log_info
()¶ Should be implemented by subclasses to log strategy-specific information.
- Raises:
- NotImplementedError: If the subclass does not implement this method.
-
on_client_deal
(client_call)¶ Called when a client deal occurs. Override in subclass.
-
on_client_reject
(client_call)¶ Called when a client call is rejected. Override in subclass.
-
on_eb_depth_change
(new_depth)¶ Called when the electronic broker depth changes. Override in subclass.
-
on_new_client_call
(client_call)¶ Called when a new client call appears. Override in subclass.
-
on_position_change
(new_position)¶ Called when the bot position changes. Override in subclass.
-
on_price_curve_change
(new_price_curve)¶ Called when the price curve changes. Override in subclass.
-
run
()¶ Contains the main logic to be executed by the strategy. Override in subclass.
-
class
traderion.strategies.technical_analysis.
TechnicalAnalysis
(api, options)¶ Bases:
traderion.strategies.base.Base
The TechnicalAnalysis class is a base class for implementing technical analysis-based trading strategies. It extends the Base class and introduces common functionalities relevant to technical analysis strategies, including handling target amount setup and responding to changes in electronic broker depth and price curves.
- Attributes:
- target_amount (int): The target amount for the strategy to act upon. depth (list): The current depth of the electronic broker. price_curve (list): The current price curve.
- Inherits from:
- Base (class): The base class for all strategies that provides foundational attributes and methods.
-
log_info
()¶ Should be implemented by subclasses to log strategy-specific information.
- Raises:
- NotImplementedError: Indicates that subclasses must implement this method.
-
on_eb_depth_change
(depth)¶ Updates the strategy's view of the electronic broker depth when it changes.
- Parameters:
- new_depth (list): The new electronic broker depth.
-
on_price_curve_change
(price_curve)¶ Updates the strategy's view of the price curve when it changes.
- Parameters:
- new_price_curve (list): The new price curve.
-
run
()¶ Contains the main logic to be executed by the strategy. This method must be implemented by subclasses.
- Raises:
- NotImplementedError: Indicates that subclasses must implement this method.
-
class
traderion.strategies.ema.
CrossingEMAs
(api, options)¶ Bases:
traderion.strategies.technical_analysis.TechnicalAnalysis
The bot implements a simple crossing moving average strategy using the 5 period EMA as the signal and the 20 period EMA.
When a crossing happens the bot fills its position to the position limit in the respective direction and waits for a signal in the other direction.
When this happens it unloads its positions and loads in the new direction using the same position limit as target.
In case we get the same values for the two EMAs from the initial price curve, the bot will sell/buy when the signal drops below/rises above the base EMA.
- Inherits from:
- TechnicalAnalysis (class): The base class for technical analysis-based trading strategies.
- Attributes:
- small_ema_period (int): The period for the short/signal EMA. big_ema_period (int): The period for the long/base EMA. current_amount (int): The current position amount. small_ema (float): The latest value of the short/signal EMA. big_ema (float): The latest value of the long/base EMA. last_cross (int): The direction of the last EMA cross, either BEARISH_CROSS or BULLISH_CROSS.
BEARISH_CROSS, BULLISH_CROSS = (0, 1)
-
compute_emas
()¶ Computes the current values of the short/signal and long/base EMAs based on the latest price curve.
-
get_new_cross_direction
()¶ Determines the direction of any new EMA cross based on the current EMA values.
- Returns:
- int: The direction of the cross (BEARISH_CROSS or BULLISH_CROSS), or None if no new cross has occurred.
-
load
(direction)¶ Adjusts the trading position based on the direction of the EMA cross, aiming to reach the target position amount.
- Parameters:
- direction (int): The direction to load the position, either BEARISH_CROSS or BULLISH_CROSS.
-
log_info
()¶ Generates a log message describing the current state of the strategy, including the current amount, EMA values, and the direction of the last cross.
- Returns:
- str: A log message summarizing the strategy's current state.
-
on_price_curve_change
(price_curve)¶ Updates the strategy's internal state in response to a change in the price curve. Recomputes the EMAs based on the new price curve.
- Parameters:
- new_price_curve (list): The updated price curve.
-
run
()¶ Executes the strategy's trading logic based on the current state of the EMAs. Identifies new EMA crosses and adjusts the trading position accordingly.
-
class
traderion.strategies.clients.
Clients
(api, options)¶ Bases:
traderion.strategies.base.Base
This strategy involves responding to client calls by either either quoting a new price or deciding to accept or decline the client price, and hitting the EB in search of arbitrage opportunities.
This strategy uses callable parameters for dynamic spread calculation, allowing for flexibility in how spreads are determined in response to market conditions.
Parameters: - api (
traderion.client.TraderionClient
) -- The API interface for interacting with the trading system. - options (dict) -- Strategy-specific options, must include callable functions for client_quote_spread and own_quote_spread.
Raises: Exception -- If required keys are missing in options or if the provided spread parameters are not callable.
-
check_for_arbitrage
(client_call)¶ Determines if accepting a client's proposed price would result in an arbitrage opportunity.
Parameters: client_call (dict) -- The client call containing the client's proposed price and direction. Returns: True if accepting the call results in arbitrage, False otherwise. Return type: bool
-
compute_price_from_spread
(anchor_price, spread, direction)¶ Computes a quote price from a given spread and direction relative to an anchor price.
Parameters: - anchor_price (float) -- The market price serving as an anchor for the quote calculation.
- spread (float) -- The spread to apply to the anchor price.
- direction (int) -- The direction of the quote (BID or ASK).
Returns: The computed quote price.
Return type: float
-
log_info
()¶ Generates and returns a log message summarizing the strategy's activity, including the number of accepted market maker and market taker calls.
Returns: A log message summarizing the strategy's activity. Return type: str
-
on_client_deal
(client_call)¶ Callback method for when a client deal is accepted.
Parameters: client_call (dict) -- The client call that was accepted.
-
on_client_reject
(client_call)¶ Callback method for when a client call is rejected.
Parameters: client_call (dict) -- The client call that was rejected.
-
on_new_client_call
(client_call)¶ Responds to new client calls by either quoting a price to the client or deciding to accept or decline the client's proposed price.
Parameters: client_call (dict) -- The client call to respond to.
-
quote_to_client_call
(client_call)¶ Quotes a new price to the client based on the strategy's spread calculation.
Parameters: client_call (dict) -- The client call requiring a quote.
- api (
-
class
traderion.strategies.cover.
Cover
(api, options)¶ Bases:
traderion.strategies.base.Base
Implements a trading strategy that automatically adjusts its positions to ensure they do not exceed a specified position limit. This strategy utilizes the TraderionClient API for interacting with the trading platform, allowing it to make informed trading decisions based on the current position and market conditions.
This class inherits from the
Base
class, extending it with specific logic to manage trading positions within defined limits.Parameters: - api (
traderion.client.TraderionClient
) -- The API interface for interacting with the trading system. - options (dict) -- Strategy-specific options, must include
position_limit
.
Raises: Exception -- If
position_limit
is not included inoptions
or does not meet the required criteria.-
get_cover_amount
()¶ Calculates the amount needed to adjust the current position to meet the position limit.
Returns: The amount that needs to be covered to adhere to the position limit. Return type: int
-
log_info
()¶ Generates and returns a log message describing the current state of the strategy, including the current position amount and the amount to be covered.
Returns: A log message summarizing the current state of the strategy. Return type: str
-
on_position_change
(new_position)¶ Callback method to handle position changes. Updates the current amount based on the new position.
Parameters: new_position (dict) -- The new position data received from the trading system.
-
run
()¶ Contains the main logic of the strategy. This method checks if the current position exceeds the position limit and attempts to adjust the position to align with the defined limit.
- api (
-
class
traderion.strategies.macd.
Macd
(api, options)¶ Bases:
traderion.strategies.technical_analysis.TechnicalAnalysis
Implements a trading strategy based on the Moving Average Convergence Divergence (MACD) indicator. The MACD is a trend-following momentum indicator that shows the relationship between two moving averages of a security's price. The MACD is calculated by subtracting the 26-period Exponential Moving Average (EMA) from the 12-period EMA.
This strategy acts on the signal generated when the MACD line crosses above or below the signal line. A bullish crossover (MACD crosses above the signal line) suggests buying, while a bearish crossover (MACD crosses below the signal line) suggests selling.
Parameters: - api (
traderion.client.TraderionClient
) -- The API interface for interacting with the trading system. - options (dict) -- Strategy-specific options including periods for the EMAs used in MACD calculation.
Raises: Exception -- If required keys (small_ema_period, big_ema_period, signal_ema_period) are missing in options.
BEARISH_CROSS, BULLISH_CROSS = (0, 1)
-
compute_macd
()¶ Computes the MACD and the signal line based on the latest price curve. This method updates the macd and signal_line attributes with the latest values.
-
get_new_cross_direction
()¶ Determines the direction of any new MACD cross based on the current values of the MACD line and the signal line.
Returns: The direction of the cross (BEARISH_CROSS or BULLISH_CROSS), or None if no new cross has occurred. Return type: int or None
-
load
(direction)¶ Adjusts the trading position based on the direction of the MACD cross, aiming to reach the target position amount.
Parameters: direction (int) -- The direction to load the position, either BEARISH_CROSS or BULLISH_CROSS.
-
log_info
()¶ Generates and returns a log message describing the current state of the strategy, including the current position amount, the MACD value, the signal line value, and the last cross direction.
Returns: A log message summarizing the strategy's current state. Return type: str
-
on_price_curve_change
(price_curve)¶ Responds to changes in the price curve by recalculating the MACD and the signal line.
Parameters: price_curve (list) -- The updated price curve data.
-
run
()¶ Executes the strategy's logic. Determines if there's a new MACD cross and updates positions accordingly.
- api (
-
class
traderion.strategies.rsi.
Rsi
(api, options)¶ Bases:
traderion.strategies.technical_analysis.TechnicalAnalysis
Implements a trading strategy based on the Relative Strength Index (RSI). The RSI is a momentum indicator that measures the magnitude of recent price changes to evaluate overbought or oversold conditions in the price of a stock or other asset.
This strategy acts when the RSI crosses above the overbought threshold (indicating a sell signal) or below the oversold threshold (indicating a buy signal).
Parameters: - api (
traderion.client.TraderionClient
) -- The API interface for interacting with the trading system. - options (dict) -- Strategy-specific options including the RSI calculation period and threshold values.
Raises: Exception -- If required keys (period, overbought_threshold, oversold_threshold) are missing in options.
-
compute_rsi
()¶ Computes the Relative Strength Index (RSI) based on the latest price curve. This method updates the rsi attribute with the latest RSI value.
-
get_new_cross_direction
()¶ Determines the direction of any new signal based on the current RSI value and predefined thresholds.
Returns: The signal direction (BEARISH_CROSS for overbought, BULLISH_CROSS for oversold), or None if no new signal has occurred. Return type: int or None
-
load
(direction)¶ Adjusts the trading position based on the signal direction, aiming to reach the target position amount.
Parameters: direction (int) -- The signal direction, either BEARISH_CROSS or BULLISH_CROSS.
-
log_info
()¶ Generates and returns a log message describing the current state of the strategy, including the current position amount, the RSI value, and the last signal direction.
Returns: A log message summarizing the strategy's current state. Return type: str
-
on_price_curve_change
(price_curve)¶ Responds to changes in the price curve by recalculating the RSI.
Parameters: price_curve (list) -- The updated price curve data.
-
run
()¶ Executes the strategy's logic. Determines if there's a new RSI signal and updates positions accordingly.
- api (