/Alpaca-CPPI

A constant proportion portfolio insurance (CPPI) trading algorithm on top of Alpaca's Trading API.

Primary LanguageJupyter Notebook

Alpaca-CPPI

A constant proportion portfolio insurance (CPPI) trading algorithm on top of Alpaca's Trading API.

Installation

The algorithm was tested on the Alpaca Trade API version mentioned the requirements file and is considered as the stable version for this project. User may try different versions but author doesn't guarantee that will work.

pip install -r requirements.txt

Usage

Trading Strategy

To run the strategy user is need to initialize the algorithm with the risky asset you want to invest in, a safe asset if you have one else it keeps the safe allocation as cash in the trading account, the initial investment capital, the floor percentage, the multiplier/leverage and the rebalancing frequency. If you are not sure what parameters to choose, I recommend playing with the parameters in the backtesting notebook. Once you are settled with the parameters, a strategy instance can be created from the CPPI class as shown in the below example. User can create multiple instances of the strategy by creating that many CPPI class instances. Remember, that if for any reason the strategy is interrupted by the user, it can be restarted without any need of closing the residual positions (the positions left open after the interruption), the strategy will detect any open position in the asset and rebalance them accordingly.

#import the CPPI class
from CPPI import CPPI

#set the strategy params
r_asset = 'SPY'#risky_asset
s_asset = None #safe_asset
capital = 1000
rebalance_freq = 1 #days or daily
floor_pct = 0.8 #80%
m = 3 #asset_muliplier

#create a instance
spy_cppi = CPPI(risky_asset=r_asset, cppi_budget=capital, safe_asset=s_asset,
               floor_percent=floor_pct, asset_muliplier=m)
#start the strategy
spy_cppi.run(period_in_days=reblance_freq)

Research Notebook

The research notebook can be used for backtesting CPPI strategies both with and without the drawdown constraint. It also provides various example's of different CPPI settings that was used for the testing, along with the backtest report and chart.

  • CPPI without drawdown constraint

    CPPI without drawdown constraint

  • CPPI with drawdown constraint

    CPPI with drawdown constraint

Disclaimer

The trading strategy discussed here is for educational purpose only doesn't guarantee to make profit. Trading involves a high risk of losing money. Use the code provided here at your own risk. The author and AlpacaDB, Inc. are not responsible for your trading results i.e. any profit or loss caused by the algorithm. User is advised to run the code on paper trading account only to understand the risk involved.