Programmation Orientée Objet en Python
  • Back to Main Website
  • Home
  • Introduction: Histoire et Concepts
    • Introduction: Histoire et Concepts
    • Histoire de la programmation
    • Première Structuration des données
    • Naissance de la POO
    • Python: tout n’est qu’objet
    • Python : Simplicité des objets et performance sous-jacente
    • Classes en Python : Concepts fondamentaux

    • Travaux Pratiques
    • Construire sa propre Liste
    • Construire son propre DataFrame
  • Encapsulation, Héritage, Composition et Dunder
    • Encapsulation, Heritage, Composition et Dunder
    • Encapsulation en Python
    • Héritage en Python : Concept et intérêt
    • Héritage vs Composition
    • Méthodes Dunder en Python
    • Python call Method: A Fun Exploration

    • Travaux Pratiques
    • TP: Heritage avec le pricing d’option
    • TP : Ajouter des méthodes dunder à DataFrameSimple
    • TP : Étendre la classe Liste avec des méthodes dunder
    • TP: Dunder Method with Tensor for Automatic Differentiation
  • Polymorphisme et Surcharge
    • Polymorphisme et Surcharge
    • Polymorphism in Object-Oriented Programming
    • Polymorphism in Python: Function Overloading and Type Checking
    • Class Creation: Standard vs type()
    • Type Hinting, Typing Module, and Linters in Python
    • Abstract Classes
    • Protocol Classes

    • Travaux Pratiques
    • TP
  • Decorators
    • Design Patterns
    • The decorator pattern
    • Decorator Practically
    • Built-in Decorators and Standard Library Decorators in Python
    • Practical Decorators in Python Libraries

    • Travaux Pratiques
    • TP: Monte Carlo Option Pricing with Decorators
    • TP: Optimizing Heston Model Monte Carlo Simulation
  • Project Management and Packaging
    • Project and Package
    • Organizing Python Projects
    • Understanding imports
    • Python Package Management and Virtual Environments
    • Unit Testing in Python

    • Travaux Pratiques
    • TP: Creating a Linear Regression Package
  • Design Patterns
    • OOP Design Patterns
    • Python-Specific Design Patterns
    • Creation Design Patterns
    • Structural Design Patterns
    • Behavioral Design Pattern

    • Travaux Pratiques
    • TP
  • Sujets de Projets possibles
    • Projets
    • Projets POO - 2024-2025
  • Code source
  1. Travaux Pratiques
  2. TP
  • Polymorphisme et Surcharge
  • Polymorphism in Object-Oriented Programming
  • Polymorphism in Python: Function Overloading and Type Checking
  • Class Creation: Standard vs type()
  • Type Hinting, Typing Module, and Linters in Python
  • Abstract Classes
  • Protocol Classes
  • Travaux Pratiques
    • TP

On this page

  • TP Instructions: Implementing a Strategy and a Portfolio for a backtesting task
    • PART I: Creating the meta class strategy and one specific strategy:
  • Part II : Creating the Portfolio class:
  1. Travaux Pratiques
  2. TP

TP

Cours
TP
Author

Remi Genet

Published

2024-10-21

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:

  1. 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 of Position objects representing the portfolio’s assets.
    • strategy: A strategy object, an instance of a class derived from the Strategy class.
  2. Initialization: Implement the __init__ method to initialize the portfolio attributes.

  3. Initialize Portfolio Positions: Implement a initialize_position_from_instrument_list method to initialize the portfolio’s positions with a given list of FinancialAsset objects. Each financial asset should be wrapped in a Position object.

  4. Positions to Dictionary: Implement the _positions_to_dict method which returns a dictionary of positions with ticker symbols as keys and Position objects as values. This will help in rebalancing operations.

  5. 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.
  6. 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.


Back to top
Protocol Classes

Programmation Orienté Object en Python, Rémi Genet.
Licence
Code source disponible sur Github

 

Site construit avec et Quarto
Inspiration pour la mise en forme du site ici
Code source disponible sur GitHub