Skip to content
TheChatArchiveThe Chat Archive
Metallama-4-maverick-03-26-experimental· MetaHugging Face LMArena Human Preference 140K

What are the rules of TP/SL in this code? ========= ---------- TP/SL правила…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Geminigemini-2.5-pro’s answer.
  1. User

    What are the rules of TP/SL in this code?

    =========

    ---------- TP/SL правила -------------

            if idx == 3 and tp_price and sl_price:
                # ATR‑основанный TP/SL
                order_params.update({
                    'tpTriggerPx': str(round_price(tp_price, tick)),
                    'tpOrdPx':     '-1',
                    'tpTriggerPxType': 'last',
                    'slTriggerPx': str(round_price(sl_price, tick)),
                    'slOrdPx':     '-1',
                    'slTriggerPxType': 'last',
                })
            elif idx < 1:          
                tp_chunk = round_price(limit_px * 0.67, tick)
                sl_chunk = round_price(limit_px * 1.9,  tick)
                order_params.update({
                    'tpTriggerPx': str(tp_chunk),
                    'tpOrdPx':     '-1',
                    'tpTriggerPxType': 'last',
                    'slTriggerPx': str(sl_chunk),
                    'slOrdPx':     '-1',
                    'slTriggerPxType': 'last',
                })
            else:                  
                tp_chunk = round_price(limit_px * 0.50, tick)
                order_params.update({
                    'tpTriggerPx': str(tp_chunk),
                    'tpOrdPx':     '-1',
                    'tpTriggerPxType': 'last',
                    # SL отсутствует
                })
            # --------------------------------------
    
  2. Meta Assistant

    A nice piece of trading bot code!

    Let's break down the rules for Take Profit (TP) and Stop Loss (SL) in this code snippet. There are three scenarios, depending on the value of idx. Here are the rules:

    Scenario 1: idx == 3 ( ATR-based TP/SL)

    • Both TP and SL are set.
    • tp_price and sl_price are pre-calculated values (not shown in this snippet), presumably based on Average True Range (ATR) calculations.
    • TP Trigger Price: tp_price (rounded to tick precision)
    • TP Order Price: Market order ('-1')
    • TP Trigger Type: last (i.e., trigger when the last traded price reaches tp_price)
    • SL Trigger Price: sl_price (rounded to tick precision)
    • SL Order Price: Market order ('-1')
    • SL Trigger Type: last (i.e., trigger when the last traded price reaches sl_price)

    Scenario 2: idx < 1 ( Wide TP/SL)

    • Both TP and SL are set.
    • Calculations are based on limit_px ( likely the order's limit price).
    • TP Trigger Price: limit_px * 0.67 (rounded to tick precision) ≈ 33% below limit price (for long orders) or 67% above limit price (for short orders, assuming limit_px is negative)
    • TP Order Price: Market order ('-1')
    • TP Trigger Type: last
    • SL Trigger Price: limit_px * 1.9 (rounded to tick precision) ≈ 90% above limit price (for long orders) or -190% below limit price (for short orders, which doesn't make sense, see note)
    • SL Order Price: Market order ('-1')
    • SL Trigger Type: last

    Note: The SL calculation for idx < 1 seems suspicious, as limit_px * 1.9 would set a very wide (almost meaningless) stop loss for long positions and an invalid (negative price) stop loss for short positions. This might be a bug or a specific strategy assumption not shown here.

    Scenario 3: idx >= 1 ( TP only)

    • Only TP is set; no SL.
    • Calculation is based on limit_px.
    • For idx == 1 (not explicitly stated, but implied by elif chain):
      • TP Trigger Price: limit_px * 0.50 (rounded to tick precision) ≈ 50% below limit price (for long orders) or 50% above limit price (for short orders)
    • For idx > 1 (including idx == 2):
      • Same TP calculation as above (limit_px * 0.50)
    • TP Order Price: Market order ('-1')
    • TP Trigger Type: last
    • No SL is set in this scenario.

    In summary:

    idx valueTPSL
    == 3ATR-basedATR-based
    < 1limit_px * 0.67limit_px * 1.9 ( potentially buggy )
    >= 1limit_px * 0.50No SL

    Keep in mind that the actual order side (long/short) is not explicitly stated in this snippet, so the interpretations above assume a long position. For short positions, the calculations would be mirrored (e.g., limit_px * 0.67 would be above the limit price).

    Also, round_price(x, tick) simply rounds price x to the nearest multiple of tick (the exchange's minimum price increment).

    Now, go forth and trade (or debug) wisely!

  3. User

    How to relocate ATR-Based TP/SL from 4 order (idx==3) to 2nd order?

  4. Meta Assistant

    A nice refactoring task!

    You want to move the ATR-based TP/SL logic from the 4th order (idx == 3) to the 2nd order (idx == 1). Here's the modified code snippet:

    # ---------- TP/SL правила -------------
    if idx == 1 and tp_price and sl_price:
        # ATR‑основанный TP/SL ( MOVED FROM idx == 3 )
        order_params.update({
            'tpTriggerPx': str(round_price(tp_price, tick)),
            'tpOrdPx':     '-1',
            'tpTriggerPxType': 'last',
            'slTriggerPx': str(round_price(sl_price, tick)),
            'slOrdPx':     '-1',
            'slTriggerPxType': 'last',
        })
    elif idx < 1:          
        tp_chunk = round_price(limit_px * 0.67, tick)
        sl_chunk = round_price(limit_px * 1.9,  tick)
        order_params.update({
            'tpTriggerPx': str(tp_chunk),
            'tpOrdPx':     '-1',
            'tpTriggerPxType': 'last',
            'slTriggerPx': str(sl_chunk),
            'slOrdPx':     '-1',
            'slTriggerPxType': 'last',
        })
    else:                  
        tp_chunk = round_price(limit_px * 0.50, tick)
        order_params.update({
            'tpTriggerPx': str(tp_chunk),
            'tpOrdPx':     '-1',
            'tpTriggerPxType': 'last',
            # SL отсутствует
        })
    # --------------------------------------
    

    What changed?

    1. The condition idx == 3 was replaced with idx == 1.
    2. The rest of the ATR-based TP/SL logic remains the same.

    Important assumptions:

    • tp_price and sl_price are still calculated elsewhere in your code (not shown in this snippet) and are available when idx == 1.
    • The tp_price and sl_price calculations are correct and make sense for the 2nd order (idx == 1).

    Order of operations remains:

    1. idx == 1: ATR-based TP/SL (new location)
    2. idx < 1: Wide TP/SL (unchanged)
    3. idx > 1 (including idx == 2 and idx == 3): TP only, no SL (unchanged)

    Note on idx == 2 and idx == 3 now:

    Since we moved the ATR-based logic to idx == 1, the idx == 2 and idx == 3 cases will now fall under the else clause, which means:

    • idx == 2: TP set to limit_px * 0.50, no SL.
    • idx == 3: TP set to limit_px * 0.50, no SL.

    If you intended to have ATR-based TP/SL for idx == 3 previously, you'll need to either:

    a. Recalculate tp_price and sl_price specifically for idx == 3 (if needed). b. Reconsider the logic for idx >= 2 if ATR-based TP/SL is required for those orders too.

    Double-check tp_price and sl_price availability:

    Ensure that tp_price and sl_price are correctly calculated and available when idx == 1. You might need to adjust the calculation timing or add checks to avoid None or incorrect values.

    That's it! Your ATR-based TP/SL should now be applied to the 2nd order (idx == 1). Test thoroughly to ensure the changes didn't introduce any unintended behavior. Happy trading!

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.