⚒️Utility

ImportCommand

Dynamically imports a list of modules at runtime and returns a dictionary of the imported modules.

  • Constructor Parameters:

    • *modules (str): A variable number of module names to be imported.

  • Methods:

    • execute(self, data_provider=None): Attempts to import each module in self.modules. If successful, adds the module to self.imported_modules and returns this dictionary. Prints a success message for each imported module, and an error message if a module cannot be imported.

  • Returns: A dictionary with module names as keys and the corresponding imported module objects as values.

Example Usage:

import_cmd = ImportCommand('pandas', 'numpy')
imported_modules = import_cmd.execute()

SMACrossCommand

Checks for a crossover between a short-period SMA and a long-period SMA over a specified time range.

  • Constructor Parameters:

    • start (str): The start date for the calculation.

    • end (str): The end date for the calculation.

    • short_period (int): The number of periods for the short-term SMA. Default is 50.

    • long_period (int): The number of periods for the long-term SMA. Default is 200.

  • Methods:

    • execute(self, data_provider): Calculates the SMA for the short and long periods and identifies crossovers.

  • Returns: A series indicating where crossovers occur.

Example Usage:

sma_cross_command = SMACrossCommand(start="2023-01-01", end="2023-06-30", short_period=50, long_period=200)
crossover_result = sma_cross_command.execute(data_provider)

PriceLevelCheckCommand

Checks whether the closing prices of a security cross a specified price level during a given time range.

  • Constructor Parameters:

    • level (float): The price level to check against.

    • start (str): The start date for the calculation.

    • end (str): The end date for the calculation.

  • Methods:

    • execute(self, data_provider): Retrieves the closing prices and checks where they cross the specified price level.

  • Returns: A series indicating where price level crossings occur.

Example Usage:

price_check_command = PriceLevelCheckCommand(level=150, start="2023-01-01", end="2023-06-30")
level_cross_result = price_check_command.execute(data_provider)

TimeBasedCommand

  • Description: Executes a given command at a specified time.

  • Constructor Parameters:

    • command (Command): The command to be executed.

    • execution_time (datetime.time): The time at which the command should be executed.

  • Methods:

    • execute(self, data_provider): Checks the current time and executes the command if the current time is greater than or equal to the specified execution_time.

  • Returns: The result of the command execution, or None if the time condition is not met.

Example Usage:

from datetime import time
time_based_command = TimeBasedCommand(command=SomeCommand(), execution_time=time(14, 30))
result = time_based_command.execute(data_provider)

Last updated