Skip to content

Request for Example Script: Grouped Orders with Take Profit and Stop Market in Python SDK #86

Open

Description

@PowerGrb

Hello,

I am currently exploring the Hyperliquid Python SDK but struggled to create a grouped order consisting of:

A buy or sell order,
A take profit order, and
A stop market order.
The available API documentation lacks sufficient details to help implement this functionality effectively. Could you kindly provide an example script in the examples directory to demonstrate how to achieve this?

Specifically, it would be immensely helpful if the example could showcase the necessary steps to group these orders together and manage their relationships as expected within the Hyperliquid system.

Thank you in advance for your support!

Best regards,
PowerGrb

Activity

  1. PowerGrb commented on Dec 25, 2024

    @PowerGrb
    Author

    This is the best I could achieve in terms of scripting without successfully grouping and linking the orders. The buy order and the take profit order are sent correctly, but they are not grouped or linked together.

    `import argparse
    import json
    import example_utils
    from hyperliquid.utils import constants

    def main():
    parser = argparse.ArgumentParser(description="basic_tpsl")
    parser.add_argument("--is_buy", action="store_true")
    args = parser.parse_args()

    # Setup the connection
    address, info, exchange = example_utils.setup(base_url=constants.TESTNET_API_URL, skip_ws=True)
    
    # Prepare the orders
    orders = [
        {
            "coin": "ETH",  # Coin symbol
            "is_buy": True,  # Buy order
            "sz": 0.01,  # Size
            "limit_px": 2500,  # Price
            "order_type": {"limit": {"tif": "Gtc"}},  # Limit order with time in force
            "reduce_only": False,  # Explicitly set reduce_only to False
            "grouping": "normalTpsl"  # Grouping for the orders
        },
        {
            "coin": "ETH",  # Coin symbol
            "is_buy": args.is_buy,  # Take profit order, depends on is_buy flag
            "sz": 0.01,  # Size
            "limit_px": 4500,  # Take profit price
            "order_type": {
                "trigger": {"triggerPx": 4000, "isMarket": True, "tpsl": "tp"}
            },
            "reduce_only": True,  # Reduce-only order
            "grouping": "normalTpsl"  # Grouping for the orders
        }
    ]
    
    # Send both orders in a single request
    order_result = exchange.bulk_orders(orders)
    print(order_result)
    

    if name == "main":
    main()`
    Capture d'écran 2024-12-25 172216

  2. xdmaury commented on Mar 2, 2026

    @xdmaury

    You are set the grouping in the wrong place.

    Remove the grouping in the order and add in the exchanger.bulk_orders:

    exchange.bulk_orders(orders, grouping="normalTpsl")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions