Usage¶
Command Line¶
Most functions have been wired into command line arguments. For a full list of scrilla’s functionality,
scrilla help
The main usage of scrilla is detailed below.
Syntax¶
scrilla [COMMAND] [TICKERS] [OPTIONS]
Commands: asset,cvar,var,capm-equity,capm-beta,clear-cache,clear-static,clear-common,close,correlation,correlations,discount-dividend-model,dividends,efficient-frontier,help,interest,watchlist,max-return,mov-averages,optimize-portfolio,optimize-cvar,plot-correlations,plot-dividends,plot-efficient-frontier,plot-moving-averages,plot-returns,plot-risk-profile,plot-yield-curve,prices,purge,risk-free,risk-profile,screen,sharpe-ratio,stat,stats,store,version,watch,yield-curve
Tickers: space-separated list of asset tickers/statistic symbols/interest maturities (depending on the command)
Options: command-specific flags and configuration.
Portfolio Optimization¶
Volatility Minimization & Sharpe-Ratio Maximization
A portfolio of consisting of the equities ALLY, BX and SONY can be optimized with the following command,
scrilla optimize-portfolio ALLY BX SONY
By default, scrilla will optimize over the last 100 trading days. If you wish to optimize over a different time period, you may use the -start
and -end
argument flags to provide starting and ending dates in the YYYY-MM-DD
format.
Also by default, the optimization function will minimize the portfolio variance. You can also specify the portfolio should be maximized with respect to the Sharpe ratio,
scrilla optimize-portfolio ALLY BX SONY -sh
There are several other arguments you may use to configure your optimization program. The full list of arguments is shown below,
scrilla optimize-portfolio [TICKERS] -sh \
-start <YYYY-MM-DD> \
-end <YYYY-MM-DD> \
-save <absolute path to json file> \
-target <float> \
-invest <float>
-target
will optimize the portfolio with the additional constraint that its rate of return must equal target
. Note the target return must be between the minimum rate of return and maximum rate of return in a basket of equities. For example, if ALLY had a rate of return of 10%, BX 15%, SONY 20%, the frontier of possible rates of returns resides in the range [10%, 20%]. It is impossible to combine the equities in such a way to get a rate of return less than 10% or one greater than 20%. Note, this assumes shorting is not possible. A future release will relax this assumption and allow portfolio weights to be negative.
-invest
represents the total amount of money invested in a portfolio.
For example, the following command,
scrilla optimize-portfolio ALLY BX SONY -sh \
-save <path-to-json-file> \
-target 0.25 \
-invest 10000 \
-start 2020-01-03 \
-end 2021-05-15
Will optimize a portfolio consisting of ALLY, BX and SONY using historical data between the dates of January 1st, 2020 and May 15th, 2021. The portfolio will be constrained to return a rate of 25%. A total $10,000 will be invested into this portfolio (to the nearest whole share). The output of this command will look like this,
———————————————- Results ———————————————-
———————————– Optimal Percentage Allocation ———————————–
ALLY = 22.83 %
BX = 19.26 %
SONY = 57.91 %
————————————– Optimal Share Allocation ————————————–
ALLY = 42
BX = 15
SONY = 56
————————————– Optimal Portfolio Value ————————————–
>> Total = $ 9893.98
—————————————- Risk-Return Profile —————————————-
>> Return = 0.25
>> Volatility = 0.201
—————————————————————————————————-
Note the optimal share allocation does not allow fractional shares. scrilla will attempt to get as close to the total investment inputted without going over using only whole shares. Also note the return of this portfolio is 25%, as this was inputted into the target return constraint.
Conditional Value at Risk Minimization
The portfolio optimization can also be done by minimizing its conditional value at risk. Because the underlying calculations are a bit different, this function is accessed through a different command and requires different arguments.
The two new arguments are prob
and expiry
. prob
, in essence, represents the percentile of the portfolio’s distribution on which the value at risk will be conditioned. In other words, if the portfolio value is represented by a random variable P, for a given value of P=p, the prob
is the probability such that Probability(P<p)=prob
.
expiry
represents the time horizon over which the value at risk will be calculated, i.e. the point in time in which the hypothetical loss occurs.
With these two new arguments, a portfolio’s conditional value at risk can be optimized using the following,
scrilla optimize-cvar ALLY BX SONY -prob 0.05 -expiry 0.5
The command given above will optimize the portfolio’s value at risk consisting of ALLY, BX and SONY over the next half year (expiry
= 0.5) conditioned the value at risk being in the 5th percentile.
Other Notable Features¶
Distribution Modes
scrilla will assume an asset price process and therefore a probability distribution for the population of asset returns. The model scrilla assumes is determined by the environment variable ANALYSIS_MODE. Currently, only one model is available: geometric
, which corresponds to an assume price process that follows geometric brownian motion and thus a probability distribution for the asset returns that is log-normal.
In the near future, a mean reversion model will implemented.
Estimation Modes
scrilla can estimate model parameters in a number of ways. The default estimation method is defined by the environment variable DEFAULT_ESTIMATION_METHOD, but all statistical functions can have their estimation overridden with a flag. scrilla supports three estimation modes: moments
, percents
and likely
.
moments
will use the method of moment matching, where the moments of a sample of data are equated to the moments of the assumed distribution in order to determine the distribution parameters. percents
will use the method of percentile matching, where the first and third quartile of the sample are equated to the theoretical distribution percentiles to determine the distribution parameters. likely
will use maximum likelihood estimation, where the probability of each observation given the assumed distribution is calculated and then the intersection of the probabilities is minimized with respect to the distribution parameters. (Note: the underlying distribution can be configured through the ANALYSIS_MODE environment variable; see Environment for more information)
For example, the following command will return the risk profile of ACI using the method of moment matching,
scrilla risk-profile ACI -moments
Where as the following command will return the risk profile of ACI using maximum likelihood estimation,
scrilla risk-profile ACI -likelihood
And the following command will return the risk profile of ACI using the method of percentile matching,
scrilla risk-profile ACI -percentiles
Note, the following command,
scrilla risk-profile ACI
will return the risk profile of ACI using the method set in the DEFAULT_ESTIMATION_METHOD environment variable. If this variable is not set, it will default to a value of moments
.
Discount Dividend Model
scrilla will pull an equity’s dividend payment history, regress the payment amount against its date and infer a simple linear regression model from this time series. It will use this model to project future dividend payments and then calculate the current cost of equity and use that to discount the sum of dividend payments back to the present. The following command will perform this action,
scrilla ddm ALLY
Alternatively, you can visualize the dividend payments against the regression model with a matplotlib graphic,
scrilla plot-divs ALLY
Financial Statistics
Beta:
scrilla capm-beta [TICKERS] [OPTIONS]
Correlation Matrix:
scrilla cor [TICKERS] [OPTIONS]
Conditional Value At Risk
scrilla cvar [TICKERS] -prob PROB -expiry EXP [OPTIONS]
Cost Of Equity:
scrilla capm-equity [TICKERS] [OPTIONS]
Risk-Return Profile:
scrilla risk-profile [TICKERS] [OPTIONS]
Sharpe Ratio:
scrilla sharpe-ratio [TICKERS] [OPTIONS]
Value At Risk:
scrilla var [TICKERS] -prob PROB -expiry EXP [OPTIONS]
Stock Watchlist and Screening
Stocks can be added to your watchlist with,
scrilla watch [TICKERS]
You can then screen stocks according to some criteria. For example, the following command will search your watchlist for stock prices that are less than their Discount Dividend Model (very rare this happens…),
scrilla screen -criteria DDM
Visualizations
Discount Dividend Model:
scrilla plot-divs [TICKER] [OPTIONS]
NOTE: THIS FUNCTION ONLY ACCEPTS ONE TICKER AT A TIME.
Efficient Fronter:
scrilla plot-ef [TICKERS] [OPTIONS]
Moving Averages:
scrilla plot-mas [TICKERS] [OPTIONS]
Risk Return Profile:
scrilla plot-rp [TICKERS] [OPTIONS]
Yield Curve:
scrilla plot-yield
QQ Plot of Returns:
scrilla plot-rets [TICKER] [OPTIONS]
NOTE: THIS FUNCTION ONLY ACCEPTS ONE TICKER AT A TIME
Correlation Time Series
scrilla plot-cors [TICKERS] [OPTIONS]
NOTE: THIS FUNCTION ACCEPTS EXACTLY TWO TICKERS
Programmatic¶
You can import and use scrilla’s function in a Python script. You must ensure the API keys have been set. See Configuration for more information. If the keys have not been configured through environment variables or set through the CLI, you must set the keys through Python’s os
library before importing any functions or modules from scrilla,
import os
os.environ.setdefault('ALPHA_VANTAGE_KEY', 'key')
os.environ.setdefault('QUANDL_KEY', 'key')
os.environ.setdefault('IEX_KEY', 'key')
Replace key
with the appropriate API key. Append this to the top of the script before scrilla’s modules are imported.
Retrieve Price Data¶
import os
os.environ.setdefault('ALPHA_VANTAGE_KEY', 'key')
os.environ.setdefault('QUANDL_KEY', 'key')
os.environ.setdefault('IEX_KEY', 'key')
from scrilla.services import get_daily_price_history
from scrilla.util import dater
start = dater.parse('2021-01-01')
end = dater.parse('2021-04-05')
prices = get_daily_price_history(ticker='ALLY', start_date=start, end_date=end)
Risk Return¶
import os
os.environ.setdefault('ALPHA_VANTAGE_KEY', 'key')
os.environ.setdefault('QUANDL_KEY', 'key')
os.environ.setdefault('IEX_KEY', 'key')
from scrilla.analysis.models.geometric.statistics import calculate_risk_return
from scrilla.util import dater
start = dater.parse('2021-01-01')
end = dater.parse('2021-04-05')
prices = calulate_risk_return(ticker='BTC', start_date=start, end_date=end)
Portfolio Optimization¶
import os
os.environ.setdefault('ALPHA_VANTAGE_KEY', 'key')
os.environ.setdefault('QUANDL_KEY', 'key')
os.environ.setdefault('IEX_KEY', 'key')
from scrilla.analysis.objects.portfolio import Portfolio
from scrilla.analysis.optimizer import optimize_portfolio_variance, maximize_sharpe_ratio
from scrilla.util import dater
start = dater.parse('2021-01-01')
end = dater.parse('2021-04-05')
port = Portfolio(tickers=['BTC','ALLY','SPY','GLD'], start_date=start, end_date=end)
# calculate minimum variance portfolio & its risk profile
min_vol_allocation = optimize_portfolio_variance(port)
min_vol_port_ret = port.return_function(min_vol_allocation)
min_vol_port_vol = port.volatility_function(min_vol_allocation)
# calculate maximum sharpe ratio portfolio & its risk profile
max_sh_allocation = maximize_sharpe_ratio(port)
max_sh_port_ret = port.return_function(max_sh_allocation)
max_sh_port_vol = port.volatility_function(max_sh_allocation)