Kinito Finance Repository
Medium Artcile Link: https://medium.freecodecamp.org/how-to-scrape-websites-with-python-and-beautifulsoup-5946935d93fe
https://pbpython.com/effective-matplotlib.html
Matplotlib Tutorial: https://matplotlib.org/tutorials/introductory/lifecycle.html
Many false positives on sideways market, to be used with others
Typical made by 3 periods:
- 12 Slow Moving Averga
- 26 Fast Moving Average
- 9 Signal
Bollinger Bands made of N lines (2) from M periods (20) moving average line. Delta is wide on high volatility & short during reduced volatility
ATR range perspective as swinging moves
Momentum oscilation [0,100]
- [70,100] overbought (Emerging markets use 80)
- [0,30] oversold (Emerging markets use 80)
Trend stength [0,100] comparing successive signal, then calculating the smoothed average of the difference of these
- [0,25] abscent or weak
- [25,50] strong
- [50,75] very strong
- [75,100] exteemely strong
Volume preceeding price movement. Higher OBV price increase, lower OBV price decrease. False signals Used with MACD
OLS aproximation on actual moving average trend
https://github.com/ChillarAnand/stocktrends
Timeseries data on price movement, filtering noise to see real trend. Built on bricks. Time axis but not fix. Closing price based calculation
pip install stocktrends
TA-Lib website: http://ta-lib.org/
TA-Lib Python Wrapper Github Page: https://mrjbq7.github.io/ta-lib/
TA-Lib documentation of pattern recognition: https://mrjbq7.github.io/ta-lib/func_groups/pattern_recognition.html
Discussion on installation problems: TA-Lib/ta-lib-python#127
Command to install TA-lib for python 3.5 and 3.6:
pip install -i https://pypi.anaconda.org/masdeseiscaracteres/simple ta-lib
Good website on chart patterns: http://thepatternsite.com
Comparison of different trading strategies for asset or portfolio. Does not reflect investment risk, use with volatility measure.
Standard deviation of the returns to capture variability of returns from the mean return. Annualization by multiplying volatility with aquare root of annualization factor:
- annualize daily volatility * sqrt(252) => 252 traiding days
- annualize weekly volatility * sqrrt(52) => 52 traiding weeks
- annualize monthly volatility * sqrt(12) => 12 traiding months
Used as risk measure assuming normal distribution of returns (false) Does not capture tail risk.
Average return earned in excess of the risk free rate per unit of volatility Measure of risk adjusted return Fails to dicern between upside and downside fluctuation Used to compare funds
- [1,2] good
- [2,3] very good
- [3,more] excellent
Similar to Sharpe Ratio but uses standard deviation of only negative returns. Used to dicern between upside and downside fluctuation => consider harmful volatility
Largest percentage drop in asset price over a specified time period (peak distance difference)
Ratio of CAGR and Maximum Draw Down. Measure of risk adjusted return
First test your system/ strategy with a set of rules with trading signal mocking data with trading conditions Factor in slippage (trading/ brokerage costs) when assessing performance. Be conservative on side of caution
Find a logical subset (L/ M/ S cap, industry, factor) specific and stick to this group. Build fixed individual position sized long only portfolio by picking:
- M number of stocks based on monthly returns
- any other criteria
Rebalance every month replacing N worse stocks by top N stocks Backtest it and compare the KPIs with that simple buy and hold strategy of corresponding index
Helps figure out when stock reach resistance (temporary historical top value - upper line) Choose high volume, high activity stocks for this strategy (pre market movers, historically high volumes stocks)
Define breakout rule:
- use price breach 20 * period rolling max/ min price & vlume breaching rolling max volume (go long/ short stocks based on signals)
Define exit/ stop loss signal:
- use previous price +/- 20 * period ATR as the roling stop loss price
Calculate strategy cumulative return for each stock
Pick high volume, high activity stock (pre market movers, historically high volumes stocks)
Define Buy Signal:
- Renko Ratio bar >= 2
- 5 day OBV Slope >= 30 degrees
- exit if Renko Ratio bar < 2
Define Sell Signal:
- Renko Ratio bar <= -2
- 5 day OBV Slope <= -30 degrees
- exit if Renko Ratio bar > -2
Pick high volume, high activity stock (pre market movers, historically high volumes stocks)
Define Buy Signal:
- Renko Ratio bar >= 2
- MACD line >= signal line
- MACD line's slope (over 5 day period) >= signal line's slope (over the last 5 periods)
- exit if (MACD line < signal line) & (MACD line's slope < signal line's slope)
Define Sell Signal:
- Renko Ratio bar <= -2
- MACD line < signal line
- MACD line's slope (over 5 day period) < signal line's slope (over the last 5 periods)
- exit if (MACD line >= signal line) & (MACD line's slope >= signal line's slope)
Price is what you pay, value is what you get.
Quantitative approach if you can afford computational power. Quantitative methods:
- (Joel Greenblatt's) Magic Formula
- (Stanford Accounting Professor Joseph Piotroski) F-score
Excellent artcile on HFT based on actual events:
https://www.nytimes.com/2014/04/06/magazine/flash-boys-michael-lewis.html
Best seller ''The little book that beats the market'':
- identify wonderfull stocks at bargain price
Wonderful stocks : Return On Invested Capital (ROIC) focussing on assets generating the return
Bargain price: Earning Yield is the ratio of EBIT to Enterprise Value (like P/E ratio but capital structure independent)
Invest in the top 20-30 companies (exclude finance & insurance), accumulating 2-3 positions per month over 12 months period and rebalance each year.
Number [0-9] which assess company's financial position strength, 9 bieng best. Pick sctock [8,9] and rebalance once or twice a year.
- profiltability criteria:
- positive return on assets in the current year (1 point)
- positive perating cash flow in the current year (1 point)
- return on asset higher in the current year compared to previous year (1 point)
- cash flow from operations divided by total assets greater than ROA in current year - Accruals - (1 point)
- leverage, liquidity & source of funds criteria:
- lower ratio of long term debt in the current period, compared to previous - decreased leverage - (1 point)
- higher current ratio this year compared to previous year, more liquidity (1 point)
- no new shares were issued in the last year, lack of dilution (1 point)
- operating efficiency criteria:
- higher gross margin compared to previous year (1 point)
- higher asset turnover ratio compared to previous year (1 point)
- direct access to stock exchange
- high frequency
- colocation
- milliseconds downtime
- API trading
- RESTful
- similar to any broker
- cheap
- test performances
- FXCM demo account https://www.fxcm.com/uk/forex-trading-demo/ u=D291103820 p=1504 token=49efa2a744bfcaa80119ebb7fd12ddb777ea2e70
- running code continiously/ periodically
- crontab
- taskmgr
- Python 'timed' scripts
API-Key 8f61d5ed3ce2f93b2cbc3d753abb6f82-e292f680364fc69b424f86fe2a817b3f
-
OANDA web trading terminal https://trade.oanda.com/
-
OANDA Python wrapper installation https://pypi.org/project/oandapyV20/
pip install oandapyV20
-
OANDA API (python wrapper) documentation https://buildmedia.readthedocs.org/media/pdf/oanda-api-v20/latest/oanda-api-v20.pdf
-
OANDA developer account intro http://developer.oanda.com/
-
SMA Crossover Strategy Link https://www.babypips.com/trading/forex-system-20150605
-
Stochastic https://excellenceassured.com/trading/stochastic-oscillator-trading-indicator
Faster, reliable, try EC2 free on AWS https://ca-central-1.console.aws.amazon.com/console/home?region=ca-central-1
i-05c31a451783d16d4 ec2-15-222-12-63.ca-central-1.compute.amazonaws.com
cd /path/to/PEMfile
chmod 400 udemy_demo.pem
chmod go-rwx filename.pem
chmod u-wx filename.pem
ssh -i "udemy_demo.pem" [email protected]
link to documentation on installing python and pip in your ec2 instance https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/eb-cli3-install-linux.html
sudo yum install python37
curl -O https://bootstrap.pypa.io/get-pip.py
python3 get-pip.py --user
Basic introduction of crontab - https://www.thegeekstuff.com/2009/06/15-practical-crontab-examples/
sample crontab command to run you file every day at 9 am (monday to friday)
crontab -e
00 09 * * 1-5 /path/to/file
pip install -U nltk wordnet
Relies on underlyng sentiment, some manually constructed
- LIWC
- ANEW
- SentiWordNet
- SenticNet
- VADER
Inludes abbreviations and emoticons, Successfule in social media data. VADER [-4,4] Paper by Georgia Tech Computer Department: http://comp.social.gatech.edu/papers/icwsm14.vader.hutto.pdf
VADER Python library gitgub page: https://github.com/cjhutto/vaderSentiment
pip install --upgrade vaderSentiment textblob
Algorithm assigning weighting factor to get important features of a document
TF: number of times the term t appear in the doc / total number of words in the doc
IDF: log(total number of documents/ number of documents with the term t in it)
Evolving area Commercial product lexicon based therefore un accurate Custom ML can be a product to sell Challenges:
- label large volumes of news data accurately
- equities need industry specific classifiers
- streaming news expensive
export PATH=` echo $PATH | sed 's/:/\n/g' | grep -v mnt | xargs | sed 's/\ /\:/g' `
jupyter nbconvert --to script Section-17_98-WhatIsaNormalDistribution-Note.ipynb
Annual Return = ((Daily Return + 1)^365)*100-1
Run permutation calculations
S&P500: '^GSPC' 500 largest US companies reflects its diversity
DowJones: '^DJI' Industrial Average 30 largest
NASDAQ: '^IXIC' Composite Grouped Securities & IT Companies
FTSE100: '^FTSE' UK
DAX30: '^GDAXI' GER
NIKKEI225: JPN
MSCI: Morgan Stanley Composite International
- cov > 0 : both move in the same direction
- cov = 0 : both are independent
- cov < 0 : both move in opposite direction
- Corr(returns) : Dependence between prices at different times, focus on returns of your portfolio.
- Corr(prices) : focus on price levels
- variance = Dot Product (weights as Vector, Dot Product (252*cov(returns), weights) )
- volatility = sqrt(Dot Product (weights as Vector, Dot Product (252*cov(returns), weights) ))
- univariate regression: one variable (line = intercept + slope * x + residuals)
- multivariate regression: multiple variables
Higher percent implies more predictive is: R square = 1 - (SSR / TSS)
Distribute to have low correlation
Perform many iterations for performance graph
Beta $$ \beta_{stock,market} = \frac{\sigma_{stock,market}}{\sigma_{market}^2} $$
Beta = cov(stock, market) / variance of the market Risk that cannot be avoided using diversification
- beta > 1 aggressive - performance follows the market
- beta = 0 no relationship
- beta < 1 defensive - performance follows slowly
CAPM $$ \overline{r_{stock}} = r_{free} + \beta_{stock,market}(\overline{r_{market}} - r_{free}) $$
Equity Risk Premium
Example for a US stock
- risk free: 2.5% (10 year US government bond yield)
- beta: 0.62 (S&P500 approximation) aka average premium
- equity risk premium: [4.5%, 5.5%] (historical in US)
- r(stock) = 2.5% + 0.62 * 5% = 5.6%
Sharpe Ratio allows to compare 2 stocks or 2 portfolios
Sharpe ratio: $$ Sharpe = \frac{\overline{r_{stock}} - r_{free}}{\sigma_{stock}} $$
CAPM alpha
Univariate regression has a unique beta coefficient and unique explanatory variable
Multivariate regression has a multiple beta coefficients and multiple explanatory variables
On revenues
- cogs: Gross Profits - cost of goods sold
- opex: Operating Profits
Direction headed in the past (calculate average, standard deviation, and variance f daily returns) in the historical period.
- sigma is the historical variability
- Zeta is the number of standard deviation from mean
- Historical Data
- Log Returns: LogReturns = log(1 + returns_pct_change)
- Mean Calculation : Mean = mean(LogReturns)
- Variance Calculation : Variance = variance(LogReturns)
- Drift Calculation : Drift = Mean - (Variance / 2)
- Standard Deviation Calculation : Std = std(LogReturns)
- Drift into Array Conversion
- Set normalization at 95% norm.ppf(0.95)
- Set array 10x2 of random values
- Z Calculation : Z = norm.ppf(randomArray)
- Set intervals & interations (1000,10)
- r calculation : r = Drift + Std * Z
- Daily Returns Calculation : DailyReturns = e^r
- Price List Calculation : iterate to create matrix (first row has latest price value) m[t] = m[t-1] * DailyReturns[t]
r = drift + stdev * Z Z = norm.ppf(np.random.rand(t_intervals, iterations) dailyReturns = exp(r) = exp(drift + stdev * Z) = exp(drift + stdev * norm.ppf(np.random.rand(tIntervals, iterations))
- Forwards: price preset, one sells in N time to another
- Futures: highly standardized foward contract on market place
- Swaps: echange cashflows in N times of an asset (interest rate, stock price, bond price, commodity)
- Options: owner buys (call options) or sells (put options) asset at a given price (strike price)
- d1 : how much can we expect
- d2 : how much we must pay