Simple Moving Average (SMA)
Momentum Trading or Trend following:
When moving average 50 falls below moving average of 200, sell (and short futures), otherwise, buy and go long.
import pandas as pd
import numpy as np
pd.DataFrame.rolling(window = 7, min_periods = 7).mean()
pd.DataFrame.rolling(window = 25).mean()
pd.DataFrame.rolling(window = 99).mean()
month_ret = pd.DataFrame.resample('M', kind = 'period').last().pct_change().dropna()
for years in [1, 3, 5, 10, 20]:
# Anualized Mean Returns For Last ... Years.
pd.DataFrame['month_ret'].rolling(years * 12).mean() * 12
# Anualized Risk For Last ... Years.
pd.DataFrame['month_ret'].rolling(years * 12).std() * np.sqrt(12)
Exponential Moving Average (EMA)
reacts faster to recent price changes
Typical short term are 12 and 26 days, long term 50 and 200 days
price.ewm(span = 10, min_periods= 10).mean()
Double Exponential Moving Average (DEMA)
DEMA = 2*EMA - EMA(EMA)
Expanding Windows
Fixed lower bound
Mean of price up to this point in time
Algorithms used in Algorithmic Trading
Logarithmic Returns
I.e. price is 130, 110, 120, 90, 80:
returns are -15%, 9%, -25%, -11% mean: -12.13%
130 np.exp(4-0.1213) = 80
Geometric Average
[(1+R1)(1+R2 )(1+R3)…*(1+Rn)]**(1/n)-1 where: R=Return n=Count of the numbers in the series
I.e. returns are respectively 30%, 10%, 20%, -10%, and -80%
The geometric average annual return == 4.3%
Other Statistic Measures