Alpaca Proxy Agent - clarification for users

Hi,

After reading https://alpaca.markets/learn/alpaca-proxy-agent-01/ and https://alpaca.markets/learn/alpaca-proxy-agent-02/ , I followed the instructions and attempted to setup the alpaca proxy agent, without success, for quite some time. The author is obviously quite talented and capable, but simple users may need some additional information to get the service working.

On a linux machine with alpaca-trade-api, websockets, and additional python requirements properly setup (including docker), follow the instructions from the author.

The easiest way to use the service, without taking into account security, is to pull and run the docker image:

docker pull shlomik/alpaca-proxy-agent
docker run -p 8765:8765 -e USE_POLYGON=false shlomik/alpaca-proxy-agent

This starts up a docker container as root.

Find the container name or id of the running alpaca proxy agent:

docker container ps -a

This will give you a “CONTAINER_ID” or “NAMES” parameter to use.

Here is an example output from above command:

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d9e967616212 shlomik/alpaca-proxy-agent “/bin/sh -c 'python …” 35 minutes ago Up 35 minutes 0.0.0.0:8765->8765/tcp awesome_mcnulty

I used this command to find the IP address of the alpaca proxy agent docker container:

docker inspect --format=’{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}’ d9e967616212

This command will return an IP address - for example: 172.17.0.2 (Copy this address)

Probably the easiest way to test the proxy agent is to edit the author’s websocket_example.py, which is found in the alpaca-trade-api-python/examples subdirectory.

Edit the example websocket example, enter your ALPACA_API_KEY and ALPACA_SECRET_KEY parameters, and change the data_url to the docker container IP address and proxy port number. It’s also important to change 'https" to “http”, as the proxy agent does not use secure socket layers (SSL). Here’s an example of how the changes appear:

import os

ALPACA_API_KEY = os.environ[‘APCA_API_KEY_ID’]
ALPACA_SECRET_KEY = os.environ[‘APCA_API_SECRET_KEY’]
USE_POLYGON = False

if name == ‘main’:
import logging

logging.basicConfig(format='%(asctime)s %(message)s', level=logging.INFO)
conn = StreamConn(
        ALPACA_API_KEY,
        ALPACA_SECRET_KEY,
        base_url=URL('https://paper-api.alpaca.markets'),
        # data_url=URL('https://data.alpaca.markets'),     **<--- comment this out.**
        **data_url=URL('http://172.17.0.2:8765'),**

Next export the environment variable “DATA_PROXY_WS” or place it in your shell configuration file (.bashrc for example). Be sure to source the .bashrc file after editing or open a new IDE.

export DATA_PROXY_WS=“ws://172.17.0.2:8765”

That’s it - make sure the docker container is running and execute the example websocket in a separate shell:

python websocket_example.py

You should see some authentication and logging information in the proxy server shell.

I have not tested the polygon websocket connection yet, but hopefully it will execute without issue. You should be able to run multiple instances of streaming clients, as advertised by the author’s intent.

Hope this helps other simple users, like myself.

Best Regards,

Joe O.

Hi Joe
I’m the author of the project.
Thanks for this post - it definitely adds more information for other users.
This project is still young so I hope as it grows it will become more and more simple to use.

Just one remark about the IP address - are you sure that this is not the docker internal IP address?
most of the cases the IP will be just: 127.0.0.1
so a user will need to do:

conn = StreamConn(
        ALPACA_API_KEY,
        ALPACA_SECRET_KEY,
        base_url=URL('https://paper-api.alpaca.markets'),
        data_url=URL('http://127.0.0.1:8765')
)

and set env variable:

export DATA_PROXY_WS="ws://127.0.0.1:8765"

btw, I plan on writing a third part that will show different examples on how to use it (use it with different SDKs, use it with alpaca-bactrader-api, use it with pylivetrader)

Shlomi

Hi,

Thanks very much for responding - very useful project.

I spent a lot of time trying to get this to work. It turns out the fundamental problem was not changing “https” to “http” for the proxy data address in my websocket clients.

The local IP address of 127.0.0.1 works great for the proxy - thanks very much. The proxy seems to work by cloning the github repository and executing “python main.py”, without using docker - is this also true?

Regards,

Joe O.

yes, you could definitely run it locally with cloning the repo. less convenient, but works exactly the same