PYTHON
# region imports
from AlgorithmImports import *
# endregion

class AdaptableFluorescentOrangeBear(QCAlgorithm):

    def initialize(self):
        # 7/4/2025  -> 7/7/2025  (hausse)
        # 5/11/2021 -> 12/1/2022 (range)
        # 3/2/2025  -> 25/4/2025 (baisse)
        self.set_start_date(2025, 1, 3)
        self.set_end_date(2025, 5, 25)
        self.set_cash(100000)

        self.spy = self.add_equity("SPY", Resolution.DAILY)
        self.symbol_ = self.spy.symbol
        self.sto_ = self.sto(self.symbol_, 14, 3, 3, Resolution.DAILY)

        self.set_warm_up(30, Resolution.DAILY)

    def on_data(self, data: Slice):
        if not self.sto_.is_ready:
            return

        self.stoch_k_ = self.sto_.stoch_k.current.value
        self.log(f"K: {self.stoch_k_:.2f}")

        holdings = self.portfolio[self.symbol_]

        if self.stoch_k_ < 15 and not holdings.is_long:
            self.set_holdings(self.symbol_, 0.1)

        elif self.stoch_k_ > 85 and not holdings.is_short:
            self.set_holdings(self.symbol_, -0.1)

        if self.stoch_k_ > 50 and holdings.is_long:
            self.liquidate()

        elif self.stoch_k_ < 50 and holdings.is_short:
            self.liquidate()