Help in getting exact code

Hi Team, i want to sell CE and PE with 25% SL on both side, Max entry on CE and PE will be 2 times, if for example CE sl is hit 2 times, then shift PE SL to cost to cost. Re-run the strategy once more if all 3 SL gets hit. Please help me with the code and review below one.

        total_sl_hits = 0

        # Track CE SL hits
        if ce_sl_1:
            orderbook[name]['ce_sl_hits'] = orderbook[name].get('ce_sl_hits', 0) + 1
            total_sl_hits += 1
            print(f"CE SL hit count: {orderbook[name]['ce_sl_hits']}")

        # Track PE SL hits  
        if pe_sl_1:
            orderbook[name]['pe_sl_hits'] = orderbook[name].get('pe_sl_hits', 0) + 1
            total_sl_hits += 1
            print(f"PE SL hit count: {orderbook[name]['pe_sl_hits']}")

        # Set maximum allowed values
        max_ce_sl = 2  # Maximum CE SL hits allowed
        max_pe_sl = 2  # Maximum PE SL hits allowed 
        max_strategy_runs = 2  # Maximum strategy re-entries allowed

Hi @Sobhit ,

Here’s the pseudocode demonstrating the same,

total_sl_hits = 0
strategy_runs = 0
max_ce_sl = 2
max_pe_sl = 2
max_strategy_runs = 2

orderbook = {}
name = "strategy_1"
orderbook[name] = {
    'ce_sl_hits': 0,
    'pe_sl_hits': 0,
    'ce_entries': 0,
    'pe_entries': 0,
    'ce_sl_to_cost': False,
    'pe_sl_to_cost': False
}

while strategy_runs < max_strategy_runs:
    
    # Check CE SL hit
    if ce_sl_1:
        orderbook[name]['ce_sl_hits'] += 1
        total_sl_hits += 1
        print(f"CE SL hit count: {orderbook[name]['ce_sl_hits']}")

    # Check PE SL hit
    if pe_sl_1:
        orderbook[name]['pe_sl_hits'] += 1
        total_sl_hits += 1
        print(f"PE SL hit count: {orderbook[name]['pe_sl_hits']}")

    # Re-enter CE if allowed
    if orderbook[name]['ce_sl_hits'] < max_ce_sl:
        if ce_sl_1:
            # place new CE sell
            orderbook[name]['ce_entries'] += 1
            print("New CE Entry Placed")

    # Re-enter PE if allowed
    if orderbook[name]['pe_sl_hits'] < max_pe_sl:
        if pe_sl_1:
            # place new PE sell
            orderbook[name]['pe_entries'] += 1
            print("New PE Entry Placed")

    # If CE SL hit max → shift PE SL to cost
    if orderbook[name]['ce_sl_hits'] >= max_ce_sl:
        orderbook[name]['pe_sl_to_cost'] = True
        print("PE SL moved to Cost")

    # If PE SL hit max → shift CE SL to cost
    if orderbook[name]['pe_sl_hits'] >= max_pe_sl:
        orderbook[name]['ce_sl_to_cost'] = True
        print("CE SL moved to Cost")

    # If total 3 SLs hit → re-run strategy once
    if total_sl_hits >= 3:
        strategy_runs += 1
        total_sl_hits = 0
        orderbook[name]['ce_sl_hits'] = 0
        orderbook[name]['pe_sl_hits'] = 0
        orderbook[name]['ce_entries'] = 0
        orderbook[name]['pe_entries'] = 0
        orderbook[name]['ce_sl_to_cost'] = False
        orderbook[name]['pe_sl_to_cost'] = False