TM
Blog

Using the Alpaca API with Python

Hoss@Hossapp /

Using the Alpaca API with Python

In this tutorial, we’ll show you how to get started with the Alpaca API when using Python. when We’ll go over installing libraries, integrating the Hoss SDK, and executing trades.

Installing and Importing Libraries

Before you start using the API, you will need to install a few libraries. This can be done via the pip package manager - so run ‘pip install requests’ in your terminal. The requests library enables us to make HTTP requests to use the API. If you haven’t already signed up for Hoss, which provides you with extensive visibility into API performance - visit the Hoss site here. To install Hoss, execute ‘pip install hoss’.

import hoss_agent
import requests

Integrate the Hoss SDK

To integrate the Hoss SDK, all you need to do is initialize the Hoss client.

You’ll need to find your Hoss keys in your settings here.

hoss_client = hoss_agent.init('<HOSS API KEY>')

Getting Started With Alpaca

Head over to your Alpaca developer portal to obtain your API public and private keys. If you haven’t done so already, you’ll have to create an Alpaca Developer account here. We’ll be using the paper trading environment since we don’t actually want to trade real money now.

We’ll define some constants since we don’t want to retype URLs and API keys several times.

#declare some constants used throughout
APCA-API-KEY-ID = "APCA-API-KEY-ID"
APCA-API-SECRET-KEY = 'APCA-API-SECRET-KEY'
BASE_URL = "https://paper-api.alpaca.markets"
ACCOUNT_URL = "{}/v2/account".format(BASE_URL)
ORDERS_URL = "{}/v2/orders".format(BASE_URL)
HEADERS = {'APCA-API-KEY-ID':'APCA-API-KEY-ID',
           'APCA-API-SECRET-KEY':'APCA-API-SECRET-KEY'}

Getting Account Information

Now, we’ll get account information.

#access account info
def get_acct_info():
	r_account = requests.get(ACCOUNT_URL, headers = HEADERS)

print(get_acct_info().json())

The response is as follows.

{
  "account_blocked": False,
  "account_number": "ACCOUNT_NUMBER",
  "buying_power": "BUYING_POWER",
  "cash": "CASH",
  "created_at": "TIME_CREATED",
  ... 
}

Order a Trade

We’ll now order a trade, and explain a few of the parameters. The ‘symbol’ specifies the asset in question - we chose Facebook for this example. The ‘qty’ is the number of shares to be traded, ‘side’ specifies whether you are buying or selling.

#access order info
def create_order(symbol, qty, side, type, time_in_force):
	PARAMS = {
    	"symbol":symbol,
    	"qty": qty,
    	"side": side,
    	"type": type,
    	"time_in_force": time_in_force
	}
	r_order = requests.post(ORDERS_URL, json=PARAMS, headers=HEADERS)
	return r_order

#make order request
print(create_order("FB",100,"buy","market","gtc").json())

The response is as follows.

{
  "id": "ID",
  "client_order_id": "CLIENT_ORDER_ID",
  "created_at": "TIME",
  "updated_at": "UPDATE_TIME",

}

View your Request History in Hoss

On the left-hand toolbar, you can specify which API to view metrics for. To view the request log, click on “Requests”. You can view individual requests’ information, such as headers and payloads, by clicking on them.

Set Up Alarms

To monitor errors, high latency, and other API integration issues, Hoss provides the ability to set up alarms. We enable integration with Slack and Pagerduty.

Masking Fields

The Alpaca API may provide PII in the responses. To help ensure that you are compliant with data privacy regulations, Hoss provides the ability to mask fields. Learn more about it here.

Next Steps

That’s it! Check out the full Alpaca API documentation here and get started building your application!

Subscribe to Hoss ModeA weekly newsletter with curated articles, community discussions, news and trends.

Using the Alpaca API with Ruby

Hoss
Read more
In this tutorial, we’ll demonstrate how to use the Alpaca API with Ruby. We’ll cover installation, making a few calls, and getting started with Hoss.

Using the Alpaca API with Node.js

Hoss
Read more
In this tutorial, we’ll show you how to get started with the Alpaca API when developing in Node.js. We’ll cover downloading the important libraries, integrating the Hoss SDK, and executing trades.
TM

Copyright © Hoss Technologies, Inc. 2020 - All rights reserved. Terms of Service & Privacy Policy