Skip to content

Commit

Permalink
updated tsmom
Browse files Browse the repository at this point in the history
  • Loading branch information
Ravpritpal Kohli authored and Ravpritpal Kohli committed Sep 12, 2019
1 parent 2efbf9d commit a68cbaf
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 30 deletions.
2 changes: 1 addition & 1 deletion GARCH_code.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -3448,7 +3448,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.2"
"version": "3.6.3"
}
},
"nbformat": 4,
Expand Down
2 changes: 1 addition & 1 deletion MomentumIndia.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -5109,7 +5109,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.2"
"version": "3.6.3"
}
},
"nbformat": 4,
Expand Down
31 changes: 23 additions & 8 deletions YahooFinance.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import pandas as pd
class YahooDailyReader():
def __init__(self, symbol=None, start=None, end=None):
import datetime, time
import datetime as dt, time
self.symbol = symbol

# initialize start/end dates if not provided
if end is None:
end = datetime.datetime.today()
end = dt.datetime.today()
if start is None:
start = datetime.datetime(2010,1,1)
start = dt.datetime(2010,1,1)

self.start = start
self.end = end
Expand All @@ -20,19 +20,20 @@ def __init__(self, symbol=None, start=None, end=None):

url = 'https://finance.yahoo.com/quote/{}/history?'
url += 'period1={}&period2={}'
url += '&filter=history'
url += '&interval=1d'
url += '&frequency=1d'
url += '&filter=div'
self.url = url.format(self.symbol, unix_start, unix_end)

def read(self):
def base(self):
import requests, re, json

r = requests.get(self.url)

ptrn = r'root\.App\.main = (.*?);\n}\(this\)\);'
txt = re.search(ptrn, r.text, re.DOTALL).group(1)
jsn = json.loads(txt)
return jsn

def read(self):
jsn = self.base()
df = pd.DataFrame(
jsn['context']['dispatcher']['stores']
['HistoricalPriceStore']['prices']
Expand All @@ -49,3 +50,17 @@ def read(self):
df.sort_index(ascending = True, inplace = True)
return df

def read_div(self):
jsn = self.base()
df = pd.DataFrame(jsn['context']
['dispatcher']
['stores']
['HistoricalPriceStore']
['eventsData'])
df.insert(0, 'symbol', self.symbol)
df['date'] = pd.to_datetime(df.date, unit= 's').dt.date
df.set_index('date', inplace = True)
df.sort_index(inplace = True)
df = df[['amount']]
return df

50 changes: 49 additions & 1 deletion momentum.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -501,6 +501,54 @@
"df_ts_df = get_ts(bonds)"
]
},
{
"cell_type": "code",
"execution_count": 63,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<matplotlib.collections.PathCollection at 0x12ee7b240>"
]
},
"execution_count": 63,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAYYAAAD8CAYAAABzTgP2AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvNQv5yAAAIABJREFUeJzt3X+MHOWd5/H31+O2M44ijwnebBhwbBICF0TA6wlBhy4KzoK5SwIWkECEVuQuK+5XdAq7i3ZQuPBjs8I5K0oibaSETbJKbnNgApF38kPrI8GcIt/CMo5NiBN8GAjgDnvnxR6S4AFmxs/90VXj6pr62VXdXdX9eUmWp7uru6urZ+pbz/f5Ps9jzjlERER8y/q9AyIiUi0KDCIi0kaBQURE2igwiIhIGwUGERFpo8AgIiJtFBhERKSNAoOIiLRRYBARkTbLy3gRM7sc+BIwAnzNObct9Pj7gC8C7wauc87dH3hsAXjCu/m8c+6KtPc79dRT3fr168vYdRGRobF3795/ds6tTduucGAwsxHgy8ClwGHgMTObcs79IrDZ88DHgT+LeIlZ59wFed5z/fr1TE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Z5FQ1NVLkex2k34nSFZorqQxVWe+hCrp8LK53zjXN7E3AA7SWWv1WZ3sqNfUisM4595KZbQJ2mtm5zrnf9HvHpFr6Hhic1ntYVMKxaALvD9w+nVbfAs65pvf/b83sf9BqhtcpMDSBMwK3o75Pf5vDZrYcWE3r9yDLc+ui4+PgWsn11wCcc3vN7GngncB01/e6O4p8r7F/K1L9VJLWezgpy7HYBVxmZmu8qqXLgF1mttzMTgUwswbwIeDnPdjnMj0GnOVVmq2g1ak6FdomeIyuAR7yfg+mgOu8ap0NwFnAP/Zov8vW8XEws7VmNgJgZmfSOg7P9Gi/uyHLsYgT+bfSpf2sn373fif9o5UX/THwFPAj4BTv/gnga4HtfgIcAWZp5Qq3ePefSesEcIjWehAr+/2ZenAs/p33eQ8B/9a7743AXuBnwAHgS9SwKgf4N8D/oVWJ8mnvvjuBK7yf3+B9z4e87/3MwHM/7T3vIPCv+/1Z+nEcgKu9738/8FPgw/3+LD04Fu/xzgmv0Go9Hgg8d8nfiv61/mnks4iItKl6KklERHpMgUFERNooMIiISBsFBhERaaPAICIibRQYRESkjQKDiIi0UWAQEZE2/x8f1GGKWbNi1wAAAABJRU5ErkJggg==\n",
"text/plain": [
"<matplotlib.figure.Figure at 0x12eb8d748>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"ausie = bonds.iloc[:, 5].dropna()\n",
"t = 1\n",
"x = ausie[t:]\n",
"y = ausie[:-t]\n",
"plt.scatter(x, y)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down
21 changes: 2 additions & 19 deletions tsmom.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pandas as pd
import numpy as np
#from pandas_datareader import data as web
from pandas_datareader import data as web
import plotly.plotly as py
import plotly.tools as tls
from plotly.graph_objs import *
Expand Down Expand Up @@ -577,10 +577,8 @@ def tsmom(series, mnth_vol, mnth_cum, tolerance = 0, vol_flag = False, scale = 0
"""Function to calculate Time Series Momentum returns on a time series.
params:
series: used for name purpose only, provide a series with the name of the ticker
mnth_vol: ex-ante volatility which is a monthly timeseries
mnth_cum: cumulative monthly returns.
tolerance: (optional) -1 < x < 1, for signal processing, x < 0 is loss thus short the asst and vice-versa
vol_flag: (optional) Boolean default is False. Wherether we are using vol scaling or not
vol_flag: (optional) Boolean default is False,
scale: (optional) volatility scaling parameter
lookback: (optional) int, lookback months
Expand Down Expand Up @@ -627,7 +625,6 @@ def tsmom(series, mnth_vol, mnth_cum, tolerance = 0, vol_flag = False, scale = 0
return new_longs, new_shorts, new_lev

def get_long_short(mnth_cum, lookback = 12):
"""Function to calculate number of longs and shorts in a particular month"""
lback_ret = mnth_cum.pct_change(lookback)
lback_ret = lback_ret.dropna(how = 'all')
nlongs = lback_ret[lback_ret > 0].count(axis = 1)
Expand Down Expand Up @@ -676,20 +673,6 @@ def get_ts(df):
return df_ts_df

def get_tsmom(mnth_vol, mnth_cum, flag = False, scale = 0.20, lookback = 12):
"""Calculates the tsmom returns for longside of the portfolio and short side of
the portfolio
params:
mnth_vol: volatility, monthly timeseries
mnth_cum: cumulative monthly returns
flag: Volatility flag, whether or not we are using leverage by scaling returns
scale: scaling factor
lookback: return look, in this case 12 mo returns
returns:
long port returns, short portfolio returns, and leverage
"""
total = mnth_cum.apply(lambda x: tsmom(x, mnth_vol, mnth_cum, scale = scale, vol_flag= flag, lookback= lookback))
pnl_long = pd.concat([i[0] for i in total], axis = 1)
pnl_short = pd.concat([i[1] for i in total], axis = 1)
Expand Down

0 comments on commit a68cbaf

Please sign in to comment.