Backtesting.strategy

Strategy module for trading algorithm implementations.

Attempt1

Bases: Strategy

Strategy based on patent/filing analysis (placeholder).

Source code in Backtesting/strategy.py
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
class Attempt1(Strategy):
    """Strategy based on patent/filing analysis (placeholder)."""

    @staticmethod
    def find_new_stocks(time_step: int) -> Dict[str, int]:
        """Find stocks based on USPTO/SEC filings.

        Args:
            time_step: Current time step.

        Returns:
            Dictionary mapping stock symbols to scores.
        """
        pass

    @staticmethod
    def strategy(portfolio_obj: portfolio, data_obj: DataAPI, time_step: int) -> List[Tuple[str, int, int, float]]:
        """Generate trades based on patent/filing analysis.

        Args:
            portfolio_obj: Current portfolio state.
            data_obj: DataAPI instance for price data.
            time_step: Current time step.

        Returns:
            List of trade tuples (stock, shares, flag, price).
        """
        pass

find_new_stocks(time_step) staticmethod

Find stocks based on USPTO/SEC filings.

Parameters:
  • time_step (int) –

    Current time step.

Returns:
  • Dict[str, int]

    Dictionary mapping stock symbols to scores.

Source code in Backtesting/strategy.py
75
76
77
78
79
80
81
82
83
84
85
@staticmethod
def find_new_stocks(time_step: int) -> Dict[str, int]:
    """Find stocks based on USPTO/SEC filings.

    Args:
        time_step: Current time step.

    Returns:
        Dictionary mapping stock symbols to scores.
    """
    pass

strategy(portfolio_obj, data_obj, time_step) staticmethod

Generate trades based on patent/filing analysis.

Parameters:
  • portfolio_obj (portfolio) –

    Current portfolio state.

  • data_obj (DataAPI) –

    DataAPI instance for price data.

  • time_step (int) –

    Current time step.

Returns:
  • List[Tuple[str, int, int, float]]

    List of trade tuples (stock, shares, flag, price).

Source code in Backtesting/strategy.py
87
88
89
90
91
92
93
94
95
96
97
98
99
@staticmethod
def strategy(portfolio_obj: portfolio, data_obj: DataAPI, time_step: int) -> List[Tuple[str, int, int, float]]:
    """Generate trades based on patent/filing analysis.

    Args:
        portfolio_obj: Current portfolio state.
        data_obj: DataAPI instance for price data.
        time_step: Current time step.

    Returns:
        List of trade tuples (stock, shares, flag, price).
    """
    pass

Example

Bases: Strategy

Simple moving average crossover strategy example.

Source code in Backtesting/strategy.py
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
class Example(Strategy):
    """Simple moving average crossover strategy example."""

    @staticmethod
    def strategy(portfolio_obj: portfolio, data_obj: DataAPI, time_step: int) -> List[Tuple[str, int, int, float]]:
        """Buy when current price exceeds 5-period moving average.

        Args:
            portfolio_obj: Current portfolio state.
            data_obj: DataAPI instance for price data.
            time_step: Current time step.

        Returns:
            List of trade tuples (stock, shares, flag, price).
        """

        #simple moving average window
        cash = portfolio_obj.cash
        output = [] #[(stock, num shares to buy/sell, buy/sell flag, price per share)]

        for stock in portfolio_obj.positions:
            average_price = 0
            if cash < 0:
                break

            window_size = min(5, time_step)
            if window_size == 0:
                continue

            average_price = 0.0
            start_step = time_step - window_size

            for t in range(start_step, time_step):
                average_price += data_obj.get_price(stock, t)

            average_price /= window_size

            curr_price = data_obj.get_price(stock, time_step)

            if average_price < curr_price and curr_price <= cash: 
                output.append((stock, 1, 1, curr_price))
                cash -= curr_price

        print(output)
        return output

strategy(portfolio_obj, data_obj, time_step) staticmethod

Buy when current price exceeds 5-period moving average.

Parameters:
  • portfolio_obj (portfolio) –

    Current portfolio state.

  • data_obj (DataAPI) –

    DataAPI instance for price data.

  • time_step (int) –

    Current time step.

Returns:
  • List[Tuple[str, int, int, float]]

    List of trade tuples (stock, shares, flag, price).

Source code in Backtesting/strategy.py
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
@staticmethod
def strategy(portfolio_obj: portfolio, data_obj: DataAPI, time_step: int) -> List[Tuple[str, int, int, float]]:
    """Buy when current price exceeds 5-period moving average.

    Args:
        portfolio_obj: Current portfolio state.
        data_obj: DataAPI instance for price data.
        time_step: Current time step.

    Returns:
        List of trade tuples (stock, shares, flag, price).
    """

    #simple moving average window
    cash = portfolio_obj.cash
    output = [] #[(stock, num shares to buy/sell, buy/sell flag, price per share)]

    for stock in portfolio_obj.positions:
        average_price = 0
        if cash < 0:
            break

        window_size = min(5, time_step)
        if window_size == 0:
            continue

        average_price = 0.0
        start_step = time_step - window_size

        for t in range(start_step, time_step):
            average_price += data_obj.get_price(stock, t)

        average_price /= window_size

        curr_price = data_obj.get_price(stock, time_step)

        if average_price < curr_price and curr_price <= cash: 
            output.append((stock, 1, 1, curr_price))
            cash -= curr_price

    print(output)
    return output

Strategy

Bases: ABC

Abstract base class for trading strategies.

Source code in Backtesting/strategy.py
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Strategy(ABC):
    """Abstract base class for trading strategies."""

    @staticmethod
    @abstractmethod
    def strategy(portfolio_obj: portfolio, data_obj: DataAPI, time_step: int) -> List[Tuple[str, int, int, float]]:
        """Generate trading signals.

        Args:
            portfolio_obj: Current portfolio state.
            data_obj: DataAPI instance for price data.
            time_step: Current time step.

        Returns:
            List of trade tuples (stock, shares, flag, price).
        """
        pass

strategy(portfolio_obj, data_obj, time_step) abstractmethod staticmethod

Generate trading signals.

Parameters:
  • portfolio_obj (portfolio) –

    Current portfolio state.

  • data_obj (DataAPI) –

    DataAPI instance for price data.

  • time_step (int) –

    Current time step.

Returns:
  • List[Tuple[str, int, int, float]]

    List of trade tuples (stock, shares, flag, price).

Source code in Backtesting/strategy.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@staticmethod
@abstractmethod
def strategy(portfolio_obj: portfolio, data_obj: DataAPI, time_step: int) -> List[Tuple[str, int, int, float]]:
    """Generate trading signals.

    Args:
        portfolio_obj: Current portfolio state.
        data_obj: DataAPI instance for price data.
        time_step: Current time step.

    Returns:
        List of trade tuples (stock, shares, flag, price).
    """
    pass