Using api.polygon.exchanges()
, I get a list of alpaca_trade_api.polygon.entity.Exchange
. It appears they are Python dictionaries, but wrapped by Exchange()
.
I am not sure how to access them. Any suggestions? Thanks.
Using api.polygon.exchanges()
, I get a list of alpaca_trade_api.polygon.entity.Exchange
. It appears they are Python dictionaries, but wrapped by Exchange()
.
I am not sure how to access them. Any suggestions? Thanks.
All Entity-based class have _raw
property that holds the raw json. But you can access field just by exchanges[0].mic
which is more friendly.
Thanks for the reply.
What should I do if I want to convert the entire list into a pandas dataframe? Like with quotes data, I can add a .df at the end of each call.
Should I use a for loop, or there is a faster way?
I believe pandas handles json/dict very well. Something like
df = pd.Dataframe(obj._raw)
Should convert it to a dataframe if the object is an array of something.
Hi hitoshi, I am trying to spend all of my buying power on one stock all at once. I really need your help with this, because if I can’t figure this out I’ll have to stop using alpaca because it isn’t helping me. I have employed this code below:
import requests, json
from chalice import Chalice
import alpaca_trade_api as tradeapi
app = Chalice(app_name='buytest')
ALPACA_API_ENDPOINT = 'https://paper-api.alpaca.markets'
ALPACA_API_KEY_ID = 'PKQR6IOUTOQ4YA8YXQU5'
ALPACA_API_SECRET_KEY = 'i1LASvEweIJ2Xy8wWH6jGWCfJriAzL5N29MiZmcZ'
ORDERS_URL = "{}/v2/orders".format(ALPACA_API_ENDPOINT)
HEADERS = {'APCA-API-KEY-ID': ALPACA_API_KEY_ID,
'APCA-API-SECRET-KEY': ALPACA_API_SECRET_KEY}
api = tradeapi.REST(ALPACA_API_KEY_ID, ALPACA_API_SECRET_KEY, ALPACA_API_ENDPOINT)
account = api.get_account()
bp = account.cash
@app.route('/testbuy', methods=['POST'])
def testbuy():
request = app.current_request
webhook_message = request.json_body
data = {
"side": "buy",
"symbol": "CRNT",
"type": "market",
"notional": bp,
"time_in_force": "day",
}
r = requests.post(ORDERS_URL, json=data, headers=HEADERS)
response = json.loads(r.content)
print(response)
print(response.keys())
return {
'message': 'I bought DAL',
'webhook_message': webhook_message
}
For some reason when I deploy the REST URL API and put it in insomnia to try the code won’t deploy.
But when I deploy it in the localhost it works.
Can you help me pleaaaase. Thank you for your response.