This project is a Google Cloud Function that interacts with an Alpaca trading profile to buy or sell random stocks. It is designed to be used with the Apple Shortcuts app.
Disclaimer: This code is shared for academic purposes under the MIT education license. Nothing herein is financial advice, and NOT a recommendation to trade real money. Please use common sense and always first consult a professional before trading or investing.
Note that Alpaca has the ability to trade with "paper" accounts (i.e. fake money). The developer strongly advises against using this code as a trading strategy with actual funds.
This code has only been tested on Python 3.11 - no guarantees are made regarding its functionality.
The main functionality of this project is encapsulated in the alpaca_interfacer
class, which is defined in alpacaInterfacer/__init__.py
. This class provides methods to buy or sell random stocks, and to execute a random action (either buying or selling).
The alpaca_interfacer
class uses several helper classes to interact with the Alpaca API:
alpaca_base
inalpacaInterfacer/alpacaBase.py
account_handler
inalpacaInterfacer/accountHandler.py
asset_handler
inalpacaInterfacer/assetHandler.py
order_handler
inalpacaInterfacer/orderHandler.py
position_handler
inalpacaInterfacer/positionHandler.py
The project also includes a logger that uses Google Cloud's logging service, defined in alpacaLogger/gcpLogger.py
.
The main entry point of the cloud function is defined in main.py
.
- Clone this repository.
- Install the required Python packages by running
pip install -r requirements.txt
. - Set up your Alpaca API keys and other configuration variables in a
.env
file. You can use the providedsample.env
as a template. - Deploy the cloud function to Google Cloud using the provided script in
scripts/deploy.sh
. Note that you will need to have GCP CLI installed with the proper APIs exposed in your project.
Once the cloud function is deployed, you can trigger it by making a HTTP request to the function's URL. If you're using the Apple Shortcuts app (the original intention for the repo), you can set up a shortcut to make this request. Note that the application uses bearer authentication.
The function will randomly choose to either buy or sell a stock from your Alpaca profile. The decision is logged to Google Cloud's logging service.
The sample.env
file contains a template for the environmental variables needed for the project. Rename this file to .env
and replace the placeholders with your actual values. Here's what each variable is used for:
alpaca_key
: Your Alpaca API key. This is used to authenticate with the Alpaca API.alpaca_secret
: Your Alpaca secret key. This is used to authenticate with the Alpaca API.secret_key
: A secret key for your application. This is used for secure operations within your application.max_percent_to_spend
: The maximum percentage of your buying power that you're willing to spend on a single stock purchase. This should be a decimal number between 0 and 1 (e.g., 0.1 for 10%).truncated_asset_name_length
: The number of words to use from the asset name when logging or displaying asset names. For example, if this is set to 3, "Standard BioTools Inc. Common Stock" would be truncated to "Standard BioTools Inc.".
Unit tests for the alpaca_interfacer
class are provided in tests/test_alpaceInterfacer.py
. You can run these tests using pytest.
The notebooks
directory contains Jupyter notebooks that you can use to interactively test and explore the functionality of the alpaca_interfacer
class.