When attempting to open a "bear_put_vertical_open" using the order template I get this error
traderbilln opened this issue · 5 comments
When attempting to open a "bear_put_vertical_open" using the order template I get this error "Your order was canceled. It was entered with an incorrect credit or debit amount". I can open a "bear_call_vertical_open" as a net_credit as shown in your docs but the docs show a net_debit to open a "bear_put_vertical_open" which is where I think the problem is. The docs show a net_debit to open the bear_put_vertical_open and net_credit for the bear_put_vertical_close. Since I can open a bear_call_vertical_open with net_credit with no error but cannot open a bear_put_vertical_open without an error as a net_debit= or net_credit=. I also noticed that in the error log that the low strike price option is not the first in the order leg collection which your docs clam should be sent low strike first. I receice this error when tring to force it to a market order "Market order may result in paying a debit for a credit spread. Use a limit order." In a put vertical spread the long strike is the low strike(sell the hi price buy the low price =-$). To close the spread would be net_debit(buy back). Hope this helps solve the error I am having opening and closing vertical put spreads. Thank you for some outstanding App software.
Code:
import httpx
from tda import auth, client,orders
import json
import datetime
import os
import time
import parm
import cert
import tda.orders.options
import httpx
import tda
from tda.orders.options import bear_call_vertical_open,bear_put_vertical_open,OptionSymbol
from tda.orders.common import Duration, Session, OrderType
from tda.orders.generic import OrderBuilder
import cert
import logging
logging.getLogger('').addHandler(logging.StreamHandler())
tda.debug.enable_bug_report_logging()
cama = ','
dquote = '"'
running = True
#while running:
token_path = cert.tkpath
api_key = cert.apkey
redirect_uri = cert.redir
try:
c = auth.client_from_token_file(token_path, api_key)
except FileNotFoundError:
from selenium import webdriver
with webdriver.Chrome() as driver:
c = auth.client_from_login_flow(
driver, api_key, redirect_uri, token_path)
casymb = 'SPXW_101321C4400'
cbsymb = 'SPXW_101321C4405'
parm.contracts = 2
corder = tda.orders.options.bear_call_vertical_open(casymb,cbsymb, parm.contracts, net_credit=.30)
print(corder)
#r = c.place_order(parm.accno, corder)
#assert r.status_code == httpx.codes.OK, r.raise_for_status()
casymb = 'SPXW_101321P4290'
cbsymb = 'SPXW_101321P4285'
porder = tda.orders.options.bear_put_vertical_open(casymb,cbsymb, parm.contracts, net_debit=.30)
print(porder)
r = c.place_order(parm.accno, porder)
error Log1:
D:\tda\MopBot>python3 test.py
tda-api version 1.3.7
Loading token from file \d: emp
Loaded metadata aware token with creation timestamp 1633959071
<tda.orders.generic.OrderBuilder object at 0x000002A9D2485AC0>
<tda.orders.generic.OrderBuilder object at 0x000002A9D2550310>
Updating refresh token:
- Current timestamp is 1634143785
- Token creation timestamp is 1633959071
- Update interval is 7344000 seconds
Skipping refresh token update
Req 1: POST to https://api.tdameritrade.com/v1/accounts/#########/orders, json={
"session": "NORMAL",
"duration": "DAY",
"orderType": "NET_DEBIT",
"complexOrderStrategyType": "VERTICAL",
"quantity": 2,
"price": "0.3000",
"orderLegCollection": [
{
"instruction": "SELL_TO_OPEN",
"instrument": {
"assetType": "OPTION",
"symbol": "SPXW_101321P4290"
},
"quantity": 2
},
{
"instruction": "BUY_TO_OPEN",
"instrument": {
"assetType": "OPTION",
"symbol": "SPXW_101321P4285"
},
"quantity": 2
}
],
"orderStrategyType": "SINGLE"
}
Req 1: GET response: 400, content={
"error" : "Your order was canceled. It was entered with an incorrect credit or debit amount.; Your order was canceled. It was entered with an incorrect credit or debit amount."
}
Traceback (most recent call last):
File "D:\tda\MopBot\test.py", line 57, in
assert r.status_code == httpx.codes.OK, r.raise_for_status()
File "C:\Users\bnels\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\httpx_models.py", line 1105, in raise_for_status
raise HTTPStatusError(message, request=request, response=self)
httpx.HTTPStatusError: 400 Client Error: for url: https://api.tdameritrade.com/v1/accounts/#########/orders
For more information check: https://httpstatuses.com/400
BEGIN REDACTED LOGS
[debug.py:163:_enable_bug_report_logging] tda-api version 1.3.7
[auth.py:37:load_token] Loading token from file \d: emp
[auth.py:161:from_loaded_token] Loaded metadata aware token with creation timestamp 1633959071
[auth.py:215:ensure_refresh_token_update] Updating refresh token:
- Current timestamp is 1634143785
- Token creation timestamp is 1633959071
- Update interval is 7344000 seconds
[auth.py:225:ensure_refresh_token_update] Skipping refresh token update
[synchronous.py:28:_post_request] Req 1: POST to https://api.tdameritrade.com/v1/accounts/#########/orders, json={
"session": "NORMAL",
"duration": "DAY",
"orderType": "NET_DEBIT",
"complexOrderStrategyType": "VERTICAL",
"quantity": 2,
"price": "0.3000",
"orderLegCollection": [
{
"instruction": "SELL_TO_OPEN",
"instrument": {
"assetType": "OPTION",
"symbol": "SPXW_101321P4290"
},
"quantity": 2
},
{
"instruction": "BUY_TO_OPEN",
"instrument": {
"assetType": "OPTION",
"symbol": "SPXW_101321P4285"
},
"quantity": 2
}
],
"orderStrategyType": "SINGLE"
}
[base.py:59:_log_response] Req 1: GET response: 400, content={
"error" : "Your order was canceled. It was entered with an incorrect credit or debit amount.; Your order was canceled. It was entered with an incorrect credit or debit amount."
assert r.status_code == httpx.codes.OK, r.raise_for_status()
error log2:
D:\tda\MopBot>python3 test.py
tda-api version 1.3.7
Loading token from file \d: emp
Loaded metadata aware token with creation timestamp 1633959071
<tda.orders.generic.OrderBuilder object at 0x000001D293101B80>
<tda.orders.generic.OrderBuilder object at 0x000001D2931D03D0>
Updating refresh token:
- Current timestamp is 1634149454
- Token creation timestamp is 1633959071
- Update interval is 7344000 seconds
Skipping refresh token update
Req 1: POST to https://api.tdameritrade.com/v1/accounts/870168348/orders, json={
"session": "NORMAL",
"duration": "DAY",
"orderType": "MARKET",
"complexOrderStrategyType": "VERTICAL",
"quantity": 2,
"orderLegCollection": [
{
"instruction": "SELL_TO_OPEN",
"instrument": {
"assetType": "OPTION",
"symbol": "SPXW_101321P4290"
},
"quantity": 2
},
{
"instruction": "BUY_TO_OPEN",
"instrument": {
"assetType": "OPTION",
"symbol": "SPXW_101321P4285"
},
"quantity": 2
}
],
"orderStrategyType": "SINGLE"
}
Req 1: GET response: 400, content={
"error" : "This symbol has no quote. Please use a limit order or contact us.; Market order may result in paying a debit for a credit spread. Use a limit order."
}
Traceback (most recent call last):
File "D:\tda\MopBot\test.py", line 57, in
assert r.status_code == httpx.codes.OK, r.raise_for_status()
File "C:\Users\bnels\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\httpx_models.py", line 1105, in raise_for_status
raise HTTPStatusError(message, request=request, response=self)
httpx.HTTPStatusError: 400 Client Error: for url: https://api.tdameritrade.com/v1/accounts/#########/orders
For more information check: https://httpstatuses.com/400
BEGIN REDACTED LOGS
[debug.py:163:_enable_bug_report_logging] tda-api version 1.3.7
[auth.py:37:load_token] Loading token from file \d: emp
[auth.py:161:from_loaded_token] Loaded metadata aware token with creation timestamp 1633959071
[auth.py:215:ensure_refresh_token_update] Updating refresh token:
- Current timestamp is 1634149454
- Token creation timestamp is 1633959071
- Update interval is 7344000 seconds
[auth.py:225:ensure_refresh_token_update] Skipping refresh token update
[synchronous.py:28:_post_request] Req 1: POST to https://api.tdameritrade.com/v1/accounts/#########/orders, json={
"session": "NORMAL",
"duration": "DAY",
"orderType": "MARKET",
"complexOrderStrategyType": "VERTICAL",
"quantity": 2,
"orderLegCollection": [
{
"instruction": "SELL_TO_OPEN",
"instrument": {
"assetType": "OPTION",
"symbol": "SPXW_101321P4290"
},
"quantity": 2
},
{
"instruction": "BUY_TO_OPEN",
"instrument": {
"assetType": "OPTION",
"symbol": "SPXW_101321P4285"
},
"quantity": 2
}
],
"orderStrategyType": "SINGLE"
}
[base.py:59:_log_response] Req 1: GET response: 400, content={
"error" : "This symbol has no quote. Please use a limit order or contact us.; Market order may result in paying a debit for a credit spread. Use a limit order."
}
Oh boy, you clearly understand this spread way better than I do. My best guess is that this is a bug in my code. Would you mind joining the discord server and chatting with me there?
Yes I would be happy to join you on discord. Please give me a date and time and I can join you then. I am located is Sacramento ca.
Sounds good, let's discuss on Discord? I'm passively checking in and responding to messages, so go ahead and drop your question there and I'll reach out and schedule a time with you.
One recommendation: have you tried passing in the price as a string? tda-api
attempts to translate floating point prices to strings, but it doesn't always get it right.
@traderbilln It appears you are trying to sell a put that would trade for more than the put you are buying for a net debit.
casymb = 'SPXW_101321P4290'
cbsymb = 'SPXW_101321P4285'
porder = tda.orders.options.bear_put_vertical_open(casymb,cbsymb, parm.contracts, net_debit=.30)
The legs are not in the correct places in your call. A bear put vertical would mean the higher strike should be bought, the lower strike sold. The higher strike should always trade for more than the lower strike resulting in a net debit.
The function definition: https://github.com/alexgolec/tda-api/blob/master/tda/orders/options.py#L417
omswindles on the discord