Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing feature: Bracket Orders #63

Closed
Graeme22 opened this issue Jul 5, 2023 · 7 comments · Fixed by #107
Closed

Missing feature: Bracket Orders #63

Graeme22 opened this issue Jul 5, 2023 · 7 comments · Fixed by #107
Labels
good first issue Good for newcomers

Comments

@Graeme22
Copy link
Contributor

Graeme22 commented Jul 5, 2023

Currently the SDK doesn't support bracket orders as documented here: https://developer.tastytrade.com/order-management/

This is a feature I'd like to support at some point.

@Graeme22 Graeme22 added the good first issue Good for newcomers label Jul 5, 2023
@kaidaniel82
Copy link
Contributor

kaidaniel82 commented Nov 13, 2023

Hi, is it still possible to use the OTOCO order to implement a Bracket order? The example on the Tasty website shows this, right? It would be great if you could post an example, as I'm having trouble understanding the process from the documentation.

https://developer.tastytrade.com/order-management/#submit-complex-order

{
    "type": "OTOCO",
    "trigger-order": {
        "order-type": "Limit",
        "price": 157.97,
        "price-effect": "Debit",
        "time-in-force": "Day",
        "legs": [{
            "instrument-type": "Equity",
            "symbol": "AAPL",
            "action": "Buy to Open",
            "quantity": 100
        }]
    },
    "orders": [
        {
            "order-type": "Limit",
            "price": 198.68,
            "price-effect": "Credit",
            "time-in-force": "GTC",
            "legs": [{
                "symbol": "AAPL",
                "instrument-type": "Equity",
                "action": "Sell to Close",
                "quantity": 100
            }]
        },
        {
            "order-type": "Stop",
            "time-in-force": "GTC",
            "stop-trigger": 143.06,
            "legs": [{
                "symbol": "AAPL",
                "instrument-type": "Equity",
                "action": "Sell to Close",
                "quantity": 100
            }]
        }
    ]
}

@Graeme22
Copy link
Contributor Author

Hi, this is still not implemented. If you'd like to create a PR to add it, feel welcome! It looks like it's a grouping of three orders: opening order, stop loss, and profit taking, all wrapped into one. The ComplexOrder class is a wrapper for one of these that's already placed, but in order to actually create one using the API you'd need to make a new class similar to NewOrder but with the OTOCO type and the relevant parts.

@Quenos Quenos mentioned this issue Nov 27, 2023
2 tasks
@Quenos
Copy link
Contributor

Quenos commented Nov 27, 2023

Solved in PR #107

@kaidaniel82
Copy link
Contributor

When will it be merged to main?

@Graeme22
Copy link
Contributor Author

As soon as the tests are passing!

@Graeme22
Copy link
Contributor Author

This can now be done pretty easily:

symbol = Equity.get_equity(session, 'AAPL')
opening = symbol.build_leg(Decimal(1), OrderAction.BUY_TO_OPEN)
closing = symbol.build_leg(Decimal(1), OrderAction.SELL_TO_CLOSE)
otoco = NewComplexOrder(
    trigger_order=NewOrder(
        time_in_force=OrderTimeInForce.DAY,
        order_type=OrderType.LIMIT,
        legs=[opening],
        price=Decimal('180'),
        price_effect=PriceEffect.DEBIT
    ),
    orders=[
        NewOrder(
            time_in_force=OrderTimeInForce.GTC,
            order_type=OrderType.LIMIT,
            legs=[closing],
            price=Decimal('200'),  # take profits
            price_effect=PriceEffect.CREDIT
        ),
        NewOrder(
            time_in_force=OrderTimeInForce.GTC,
            order_type=OrderType.STOP,
            legs=[closing],
            stop_trigger=Decimal('160'),  # stop loss
            price_effect=PriceEffect.CREDIT
        )
    ]
)
resp = account.place_complex_order(session, otoco, dry_run=False)

For an OCO order, it's exactly the same, but without the trigger_order, and it only works for positions you already have open.

@kaidaniel82
Copy link
Contributor

Thats great. Thanks for developing this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants