This command line interface allows users to interact with their alpaca brokerage account through the terminal. Features include view current profits and positions, get asset price quotes, execute trades, run analyses, backtest strategies, and implement a pairs trading strategy all from the command line.
Clone the Pairs-Trading-Algorithm repository:
git clone https://github.com/AidanAlr/Pairs-Trading-Algorithm
Change to the project directory:
cd Pairs-trading-Algorithm
Create and enter venv
python3 -m venv .venv && source .venv/bin/activate
Install the dependencies:
pip install -r requirements.txt
Add account details to trading .FAKENV and change to .env
This script uses argparse
to handle command-line arguments for interacting with Alpaca trading functions. Below are the available options:
-t
, --ticker
: Enter an Alpaca ticker. Add -q
or -tr
for quote or trade.-tr
, --trade
: Visit the trade menu for the ticker.-p
, --positions
: Print current positions.--exitall
: Exit all positions immediately.-im
, --imenu
: Enter interactive menu.-q
, --quote
: Get a quote for the ticker.python terminal.py -t AAPL -q
This will get a quote for the ticker AAPL
.
► Analysis
Conduct the analysis required to find a suitable pair for statistical arbitrage. Submit a CSV file of stock tickers and the program will download the stock data from the last year and check the correlation between all assets. If a pair has a sufficiently high correlation, it is placed through ADF test to check the stationarity of the time series and cointegration tests for the suitability of the pair. Once a suitable pair is chosen, we use a 60-day rolling regression on the asset prices allowing us to generate the hedge ratio; we then analyse the relative spread between assets. When the spread exceeds a certain boundary, we will buy one asset and short the other expecting the spread to retract.
You will be presented with the option to backtest the strategy as well.
► Backtest strategy
Dashboard that generates graphic representations of spread, zscored spread and strategy return. Allows you to test different strategies by adjusting take profits and stop loss.
► Execute Pairs Trading Strategy
Once you have chosen a pair execute and manage a pairs trading strategy directly from your terminal.
► View current positions with profit and loss
Dashboard for viewing all important metrics about current positions.
► Manual Trade
Option to conduct a manual trade, market or limit order.
► Get Quote
Option to conduct get the market price of an asset.
► Close All Positions
Option to quickly exit all positions.
Contributions are welcome! Here are several ways you can contribute: