def create_PMI_index(): start_date = datetime(2003, 11, 1) end_date = datetime.today() pmi = read_data(start_date, end_date, 'PMI') pmi["date"] = pmi.index.copy() create_PMI_RSI(pmi) create_PMI_health(pmi) return pmi.rename(columns={"Index": "PMI"})
def load_data(): def _append_last_return(x): x["Last Return"] = x["Close"].diff(1) / x["Close"].shift(1) vix = read_data(start_date, end_date, 'vix') vix["date"] = vix.index _append_last_return(vix) sp500 = read_data(start_date, end_date, 'sp500') sp500["date"] = sp500.index _append_last_return(sp500) price = pd.merge(vix, sp500, left_on='date', right_on='date', how='inner', suffixes=["_vix", "_sp500"]) price.index = price.date return price
import matplotlib.pyplot as plt import pandas as pd import numpy as np from model.vix import build_vix_sig from TVIX.data.data_util import read_data __author__ = 'junyan' holding_period = 1000 locking_period = 40 stoploss = 0.6 start_date = pd.datetime(2011, 12, 1) tvix_data = read_data().set_index('Date') sp500_data = pd.read_csv( "/Users/junyan/Desktop/tvix/data/sp500.csv", parse_dates=True).sort_values(['Date']).set_index('Date') tvix_data.columns = ["tvix_" + i for i in tvix_data.columns] sp500_data.columns = ["sp500_" + i for i in sp500_data.columns] sp500_data["sp500_60 return"] = (sp500_data["sp500_Adj Close"].diff(60) / sp500_data["sp500_Adj Close"]) sp500_data["sp500_120 return"] = (sp500_data["sp500_Adj Close"].diff(120) / sp500_data["sp500_Adj Close"]) data = pd.concat( [tvix_data, sp500_data], axis=1, join='outer')
import pandas as pd import matplotlib.pyplot as plt from TVIX.data.data_util import read_data from TVIX.model.econ import attach_PMI_index start_date = pd.datetime(2011, 1, 1) end_date = pd.datetime.today() tvix = read_data(start_date, end_date, 'tvix') spy = read_data(start_date, end_date, 'spy') tvix['return'] = tvix['Close'].diff(1) / tvix['Close'].shift(1) tvix['date'] = tvix.index tvix = tvix[tvix['return'] < 2] spy['return'] = spy['Close'].diff(1) / spy['Close'].shift(1) spy['date'] = spy.index comp = pd.merge(tvix[['Close', 'return', 'date']], spy[['Close', 'return', 'date']], on='date', suffixes=('_tvix', '_sp')) comp.index = comp.date comp['ratio'] = -comp['return_sp'] / comp['return_tvix'] comp.loc[((comp['return_sp'] < 0) & (comp['return_tvix'] > 0)) | (comp.ratio < 0.2), 'ratio'] = 0 comp[['Close_sp', 'ratio']].plot(secondary_y='ratio') plt.show()
import pandas as pd import matplotlib.pyplot as plt from TVIX.data.data_util import read_data from TVIX.model.econ import attach_PMI_index start_date = pd.datetime(2011, 1, 1) end_date = pd.datetime.today() holding_period = 10 tvix = read_data(start_date, end_date, 'tvix') tvix['date'] = tvix.index tvix['tvix_last_return'] = tvix['Close'].diff(1)/tvix['Close'].shift(1) tvix['tvix_return'] = tvix['Close'].diff(1).shift(-1)/tvix['Close'] tvix['vix_sell_sig'] = tvix['Close'].rolling(4).apply( lambda x: (x[0] < x[1]) and (x[1] > x[2]) and (x[2] > x[3]) # lambda x: (x[0] < x[1]) and (x[1] > x[2]) ) tvix = attach_PMI_index(tvix) def current_econ_is_health(background): if background.PMI_Health > 0.1 and background.PMI_RSI > -0.7: return True else: return False def compute_max_drawn_down(PNL): current_max = PNL[0]
from TVIX.data.data_util import read_data from datetime import datetime import matplotlib.pyplot as plt import pandas as pd import numpy as np start_date = pd.datetime(2010, 1, 1) end_date = datetime.today() sp500 = read_data(start_date, end_date, 'sp500') def create_day_down_reverse(price_row): mid = (price_row['Open'] + price_row['Close'])/2 ret = price_row['Close'] - price_row['Open'] down = mid - price_row['Low'] up = price_row['High'] - mid return down/up sp500['down_reverse'] = sp500.apply(create_day_down_reverse, axis=1) sp500['down_reverse_10'] = pd.rolling_mean(sp500['down_reverse'], 10) sp500[['Close', 'down_reverse_10']].plot(secondary_y='down_reverse_10') plt.show() sp500[sp500['down_reverse'] < 0]