Environment
Language
Python 3.7
Alpaca SDK Version
Using Python requests and json library. I am not using the Alpaca API library and do not want to, if possible.
Problem
Summary
I am passing a request for a list of orders to the Alpaca API. When I run it and don’t pass any parameters, the default response works perfectly (connects, authorizes, returns the values no problem).
However, when I try to pass parameters (e.g., “status”:“all”,“limit”:5), the query completely ignores the parameters and returns a response with the default selections.
For comparison, I am 100% able to pass parameters when placing an order (ticker, quantity, etc.) with no problem.
Paper or Live Tradng?
Paper
Example Code
import json, requests
from config import *
ORDERS_URL = “{}/v2/orders”.format(PAPER_ENDPOINT)
HEADERS = {‘APCA-API-KEY-ID’: API_KEY, ‘APCA-API-SECRET-KEY’: SECRET_KEY}
def get_orders():
data = {
“status”:“all”,
“limit”:5
}
r = requests.get(ORDERS_URL, json=data, headers=HEADERS)
return json.loads(r.content)
print(get_orders())
SOLVED. Figured out the problem was the parameters being loaded into the module “json” instead of “params”. I use “json” in the request line to pass parameters for trades so no idea why it works there and not here.
Hi,
just out of curiosity - why aren’t you using the python sdk?
Haha fair question! I started development using Part Time Larry’s videos who uses the requests and json libraries instead of the Alpaca python SDK.
When confronted with the problem in this thread, I tried the REST query with the SDK to confirm it worked (and it did) but felt it was considerably simplified. For me, the goal is to understand the tech along with the finance and I feel I can get better at both by doing things more bare bones. Example, I couldn’t have told you different between a GET or POST HTTP request before this.
That said, still definitely on the newer side of these sorts of projects. If you have any guidance on it I’m definitely open to it!
I do recommend using the sdk and not directly do rest calls.
learning is great (and definitely do that) but when you work with the sdk you work with a package that was tested by hundreds of users.
so, if you do plan on taking this live with real money - use the sdk
Hi!!
I’m having the opposite problem than you and I cannot crack it!!
I’m interacting perfectly with all of the endpoints of the API, with json and requests (also learning) but I cannot place an order!!
This is the code:
base_url = 'https://paper-api.alpaca.markets/v2'
orders_url = base_url + '/orders'
def place_order(symbol, qty, side, type='stop_limit',
time_in_force='gtc', order_class='bracket'):
params = {
'symbol': symbol,
'qty': str(qty),
'side': side,
'type': type,
'time_in_force': time_in_force,
'order_class': order_class,
'take_profit': take_and_stop(symbol)[0],
'stop_loss': take_and_stop(symbol)[1]
}
r = requests.post(orders_url,
params=params,
headers=headers)
place_order('BABA', 10, 'buy')
(the take_and_stop() function calculates take profit and stop limit prices, and returns a tuple of dictionaries.)
This gives me a 400 error every time. My API keys are valid. Do you see the mistake I’m making?
Thanks!!
Yep I think yours is the same problem just in reverse. I think you need to change the “params” argument to “json”. So change this:
r = requests.post(orders_url, params=params, headers=headers)
To this:
r = requests.post(orders_url, json=params, headers=headers)
I’m not yet familiar with the “take profit” and “stop loss” trade arguments so can’t confirm if those are good or not, but everything else (except the above code example) matches my code. Hope this helps!
1 Like
Thanks for your response. I had already tried that and it didn’t solve it either…
For the moment, I’m using the SDK for placing orders (every other interaction works fine). I’ll see if I can solve it later or I’ll end up migrating the whole thing to the SDK.