Created with Highcharts 12.1.2EquityFeb 3Feb 5Feb 7Feb 9Feb 11Feb 13Feb 15Feb 17Feb 19Feb 21Feb 23Feb 2595k97.5k100k102.5k-4-2000.020.0400.00020.00040100k200k05k10k00.005
Overall Statistics
Total Orders
4149
Average Win
0%
Average Loss
0.00%
Compounding Annual Return
-39.381%
Drawdown
3.100%
Expectancy
-1
Start Equity
100000
End Equity
96939.29
Net Profit
-3.061%
Sharpe Ratio
-10.718
Sortino Ratio
-9.933
Probabilistic Sharpe Ratio
0%
Loss Rate
100%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
-0.314
Beta
-0.081
Annual Standard Deviation
0.033
Annual Variance
0.001
Information Ratio
-6.95
Tracking Error
0.121
Treynor Ratio
4.386
Total Fees
$2761.00
Estimated Strategy Capacity
$2400000.00
Lowest Capacity Asset
PEV XZ448Z77KM79
Portfolio Turnover
0.37%
# region imports
from AlgorithmImports import *
# endregion

class BiotechStrategy(QCAlgorithm):
    def Initialize(self):
        self.SetStartDate(2024, 2, 1)
        self.SetEndDate(2024, 2, 25)
        self.SetCash(100000)

        # Add symbols
        self.symbols = ['ACON', 'SGLY', 'SLRN', 'PEV', 'SOPA']
        self.order_tickets = {}  # Store order tickets for TP and SL

        for symbol in self.symbols:
            equity = self.AddEquity(symbol, Resolution.Minute)
            equity.SetDataNormalizationMode(DataNormalizationMode.Raw)

    def OnData(self, data):
        for symbol in self.symbols:
            if not self.Portfolio[symbol].Invested:
                # Example entry logic
                quantity = 1
                entry_ticket = self.MarketOrder(symbol, quantity)
                
                # Define TP and SL prices
                fill_price = entry_ticket.AverageFillPrice
                tp_price = round(fill_price * 1.10, 2)  # Take profit at +10%
                sl_price = round(fill_price * 0.95, 2)  # Stop loss at -5%
                
                # Place TP and SL orders
                tp_ticket = self.LimitOrder(symbol, -quantity, tp_price)
                sl_ticket = self.StopMarketOrder(symbol, -quantity, sl_price)
                
                # Store tickets for cancellation logic
                self.order_tickets[symbol] = {"tp": tp_ticket, "sl": sl_ticket}

    def OnOrderEvent(self, order_event):
        if order_event.Status == OrderStatus.Filled:
            symbol = str(order_event.Symbol)
            
            # Check if there are active tickets for this symbol
            if symbol not in self.order_tickets or self.order_tickets[symbol] is None:
                return  # No active tickets to process
            
            tickets = self.order_tickets[symbol]
            
            # Check if Take Profit was filled
            if tickets["tp"] is not None and order_event.OrderId == tickets["tp"].OrderId:
                # TP filled; cancel SL
                if tickets["sl"] is not None:
                    tickets["sl"].Cancel()
                    self.Debug(f"Take Profit filled for {symbol}. Stop Loss canceled.")
                self.order_tickets[symbol] = None  # Clear tickets after TP fills
            
            # Check if Stop Loss was filled
            elif tickets["sl"] is not None and order_event.OrderId == tickets["sl"].OrderId:
                # SL filled; cancel TP
                if tickets["tp"] is not None:
                    tickets["tp"].Cancel()
                    self.Debug(f"Stop Loss filled for {symbol}. Take Profit canceled.")
                self.order_tickets[symbol] = None  # Clear tickets after SL fills