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

Complex orders #107

Merged
merged 8 commits into from
Nov 28, 2023
Merged

Complex orders #107

merged 8 commits into from
Nov 28, 2023

Conversation

Quenos
Copy link
Contributor

@Quenos Quenos commented Nov 27, 2023

Description

Add the possibility to create complex orders:

  • OTOCO
  • OCO

Also cancel channel has been added to the DXLinkStreamer

Related issue(s)

Fixes: #63

Pre-merge checklist

  • Passing tests LOCALLY
  • New tests added (if applicable)

Please note that, in order to pass the tests, you'll need to set up your Tastytrade credentials as repository secrets on your local fork. Read more at CONTRIBUTING.md.

Copy link
Contributor

@Graeme22 Graeme22 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great, thanks for the contribution! A couple small comments. Also, if you could add a test for this, that would be great!

@@ -269,16 +295,10 @@ class ComplexOrder(TastytradeJsonDataclass):
"""
Dataclass containing information about a complex order.
"""
id: str
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe these lines are needed for previously placed complex orders, you can confirm by calling Account.get_history after placing a complex order. If so, this could be renamed to PlacedComplexOrder to maintain the naming convention.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These lines are no longer in the return value of placing complex orders. If there are other complex orders than OTOCO and OCO, then maybe, but those 2 are the only one I found.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check the value after calling Account.get_history with a complex order in the history, that's why this is there in the first place

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like it's for pairs trades!

@@ -45,3 +46,11 @@ def validate_response(response: Response) -> None: # pragma: no cover
error_message += f"\n{error['code']}: {error['message']}"

raise TastytradeError(error_message)


def object_to_dict(obj):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I'm missing where you're using this but I didn't see any usages? Also it may be repeating the functionality of pydantic's BaseModel.to_dict.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a ghost I forgot about. Will roll back the change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested with both get_history and get_order_history and made a $0.10 profit in the process, as the complex order doesn't work in the certification environment.

In get_history it is absolutely not visible that the transactions once were part of a complex order.
In get_order_history it is visible, but there is no "dictionary" complex_order, just id's, which are attributes of the class.

Quenos and others added 6 commits November 27, 2023 18:10
* streamers use async context managers; prod session in testing

* fix lint

* Update CONTRIBUTING.md

* no tests on main branch

* update tests
* Make market metrics optional

* Enable manual workflow triggering
@Graeme22
Copy link
Contributor

Please note you can run tests locally with make test; also, you'll need to add your TT credentials as secrets to your repo fork (explained in CONTRIBUTING.md).

@Quenos
Copy link
Contributor Author

Quenos commented Nov 27, 2023

@Graeme22 The tests now run locally, but 95% is not reached. I think this is mainly because streamer.py has a low coverage, but that was not touched.

@Graeme22
Copy link
Contributor

@Graeme22 The tests now run locally, but 95% is not reached. I think this is mainly because streamer.py has a low coverage, but that was not touched.

It's because you added new code, but no tests for it! If you're not comfortable writing tests, lmk and I'll do it.

@Quenos
Copy link
Contributor Author

Quenos commented Nov 28, 2023

It seems impossible to test the complex orders.

  1. Testing in the certification environment is not possible
  2. Testing with a LIMIT order gives the error that it's not allowed
    E tastytrade.utils.TastytradeError: preflight_check_failure: One or more preflight checks failed
    E illegal_buy_and_sell_on_same_symbol: Cannot buy and sell against the same symbol.
  3. Using a MARKET order is possible, but only during market hours, and will buy the stock
  4. To test the OCO you need to have the stock
  5. I tested it with buying stock and it works. However, buying in automated testing is not ideal for obvious reasons
  6. I need to test cancelling orders, because that I might have overseen.

@Graeme22
Copy link
Contributor

It seems impossible to test the complex orders.

  1. Testing in the certification environment is not possible
  2. Testing with a LIMIT order gives the error that it's not allowed
    E tastytrade.utils.TastytradeError: preflight_check_failure: One or more preflight checks failed
    E illegal_buy_and_sell_on_same_symbol: Cannot buy and sell against the same symbol.
  3. Using a MARKET order is possible, but only during market hours, and will buy the stock
  4. To test the OCO you need to have the stock
  5. I tested it with buying stock and it works. However, buying in automated testing is not ideal for obvious reasons
  6. I need to test cancelling orders, because that I might have overseen.

I was able to test successfully in a certification account!

@Graeme22 Graeme22 merged commit 77a0fc5 into tastyware:master Nov 28, 2023
1 check failed
@Graeme22
Copy link
Contributor

Thanks for this, I went ahead and merged! For a OCO order, you create a NewComplexOrder and pass in two NewOrders to the orders argument; for an OTOCO order, it's the same but you also add another NewOrder as the trigger_order argument, this makes it pretty straightforward.

Appreciate the contributions!

@Quenos
Copy link
Contributor Author

Quenos commented Nov 28, 2023

You're most welcome. Always fun to contribute to this kind of projects.
Also good to know that it's running in the certification environment. Need to have a look where I went wrong.

@kaidaniel82
Copy link
Contributor

kaidaniel82 commented Nov 28, 2023

Hi Graeme,

i was trying several times using the certification account class. I was not able to place a simpel order. Could you pls provide a code snippet for this.

My API Sandbox account is up and running.

@Graeme22
Copy link
Contributor

Hi Graeme,

i was trying several times using the certification account class. I was not able to place a simpel order. Could you pls provide a code snippet for this.

My API Sandbox account is up and running.

Is the code example in #63 not working for you?

@kaidaniel82
Copy link
Contributor

The general usage of the certification class.

@Graeme22
Copy link
Contributor

The general usage of the certification class.

Just make a certification session, here's the docs on that: https://tastyworks-api.readthedocs.io/en/latest/sessions.html

@kaidaniel82
Copy link
Contributor

Its working as expected. I was running into an Issue because I never got filled due to using a realistic! market price on a symbol.

Sandbox Environment
The base url for all sandbox api endpoints is api.cert.tastyworks.com
The websocket url for account streamer updates is streamer.cert.tastyworks.com
Orders in the sandbox environment all remain within the sandbox system. They never go to a real market.
Orders statuses in the sandbox system are all determined by the order type and order price. The logic is as follows:
Market orders will be Live and never fill.
Day orders with a price <= $3 will fill immediately.
Day orders with a price between >= $3 will be Live and never fill.

You are logged in as xyz

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

Successfully merging this pull request may close these issues.

Missing feature: Bracket Orders
4 participants