TP
TP Instructions: Implementing a Strategy and a Portfolio for a backtesting task
Objective: You need to implement methods and functionalities for a Portfolio
class. The class should handle the operations of initializing the portfolio with financial instrument, rebalancing its positions based on a given strategy, and providing a summary of the portfolio positions.
Tips: - Use the methods and attributes of other provided classes like Instrument
, Quote
, and Position
. - Before starting the exercise be sure to fully understand how the object provided for it worked. - Remember to handle cases where certain attributes might be None
or missing. - If you finish the exercise early, you can implement method to populate the attribute of the Portfolio class that are not handle yet.
PART I: Creating the meta class strategy and one specific strategy:
Create an abstract class Strategy that will serve as the base for all strategies. This class should:
- Include an abstract method generate_signals that will generate trading signals based on input data.
- Document the method generate_signals with a docstring explaining the parameters and return value:
- Parameters for the class : A dictionary with tickers as keys and positions as values. The dict should be named data_for_signal_generation
- Return: A dictionary with tickers as keys and signals as values.
Create a derived class EqualWeightStrategy from Strategy:
- This class should inherit from Strategy and implement the generate_signals method.
- It should calculate and return a dictionary with equal-weight signals for each ticker.
- To Implement the generate_signals method:
- The method should calculate equal-weight signals for each ticker by dividing 1 by the total number of tickers.
- It should return a dictionary where each ticker has an equal weight as its signal.
Part II : Creating the Portfolio class:
Class Attributes: Based on the provided code framework, create a
Portfolio
class which should have the following attributes:name
: Name of the portfolio.currency
: The currency in which the portfolio is denominated.aum
: Assets Under Management.- ‘nav’ : The last net asset value computed for the portfolio
historical_nav
: A list that keeps track of the historical Net Asset Values.positions
: A list ofPosition
objects representing the portfolio’s assets.strategy
: A strategy object, an instance of a class derived from theStrategy
class.
Initialization: Implement the
__init__
method to initialize the portfolio attributes.Initialize Portfolio Positions: Implement a
initialize_position_from_instrument_list
method to initialize the portfolio’s positions with a given list ofFinancialAsset
objects. Each financial asset should be wrapped in aPosition
object.Positions to Dictionary: Implement the
_positions_to_dict
method which returns a dictionary of positions with ticker symbols as keys andPosition
objects as values. This will help in rebalancing operations.Rebalancing: Implement the
rebalance_portfolio
method which performs the following tasks:- Convert the positions list into a dictionary.
- Generate trading signals using the provided strategy.
- Update the weight and quantity of each position in the portfolio based on the generated signals.
Portfolio Summary: Implement the
portfolio_position_summary
method. This method should return a dataframe summarizing the portfolio’s current positions, including the ticker symbols, weights, quantities, and last close for the assets
After implementing the Part I and Part II, you can launch this unit test to validate your code.