def holtpc( data, yearly=256, alpha=hw_alpha, beta=hw_beta ): '''Annualized percentage growth dataframe from H-W growth model.''' # yearly is the multiplier to annualize Growth. # # MOST VALUABLE H-W function <= !! # It contains the HISTORY of FORECASTED RATES! # holtdf = holt( data, alpha, beta ) level = todf( holtdf['Level'] ) grow = todf( holtdf['Growth'] ) growan = todf( grow * yearly ) return todf( 100 * ( growan / level ) )
def holtpc(data, yearly=256, alpha=hw_alpha, beta=hw_beta): '''Annualized percentage growth dataframe from H-W growth model.''' # yearly is the multiplier to annualize Growth. # # MOST VALUABLE H-W function <= !! # It contains the HISTORY of FORECASTED RATES! # holtdf = holt(data, alpha, beta) level = todf(holtdf['Level']) grow = todf(holtdf['Growth']) growan = todf(grow * yearly) return todf(100 * (growan / level))
def stock_one( slang, maxi=3650, col='Close' ): '''slang string retrieves SINGLE column for said stock. Available col include: Open, High, Low, Close, Volume ''' df = stock_all( slang, maxi ) # return just a single column dataframe: return tools.todf( df[[ col ]] )
def cotr_position_metals(): '''Market position for precious metals from COTR of GC and SI.''' pos1 = cotr_position( 'GC' ) # Gold Comex pos2 = cotr_position( 'SI' ) # Silver Comex # # _Average reading between two contracts. return tools.todf( (pos1 + pos2) / 2.0 )
def simu_prices( N, yarray ): '''Convert bootstrap returns to price time-series into pandas DATAFRAME.''' # Initial price implicitly starts at 1. # Realize that its history is just the products of the returns. ret = bootstrap( N, yarray ) # Cumulative product of array elements: # cumprod is very fast, and keeps interim results! # http://docs.scipy.org/doc/numpy/reference/generated/numpy.cumprod.html return todf( np.cumprod( ret ) )
def cotr_position_bonds(): '''Market position for bonds from COTR of TY and ED.''' pos1 = cotr_position( 'TY' ) # TY is 10-years. pos2 = cotr_position( 'ED' ) # Eurodollar strips. # # _Average reading between two contracts. return tools.todf( (pos1 + pos2) / 2.0 )
def cotr_position_metals(): '''Market position for precious metals from COTR of GC and SI.''' pos1 = cotr_position('GC') # Gold Comex pos2 = cotr_position('SI') # Silver Comex # # _Average reading between two contracts. return tools.todf((pos1 + pos2) / 2.0)
def cotr_position_bonds(): '''Market position for bonds from COTR of TY and ED.''' pos1 = cotr_position('TY') # TY is 10-years. pos2 = cotr_position('ED') # Eurodollar strips. # # _Average reading between two contracts. return tools.todf((pos1 + pos2) / 2.0)
def cotr_position_equities(): '''Market position for equities from COTR of both SP and ES.''' pos1 = cotr_position('SP') # SP better for options reading. pos2 = cotr_position('ES') # Minis better for reading futures. # # _Average reading between two contracts. return tools.todf((pos1 + pos2) / 2.0)
def cotr_position_equities(): '''Market position for equities from COTR of both SP and ES.''' pos1 = cotr_position( 'SP' ) # SP better for options reading. pos2 = cotr_position( 'ES' ) # Minis better for reading futures. # # _Average reading between two contracts. return tools.todf( (pos1 + pos2) / 2.0 )
def cotr_position_usd(): '''Market position for USD from COTR of JY and EC.''' # We ignore USD index DX from ICE. pos1 = cotr_position( 'JY' ) # JPY futures. pos2 = cotr_position( 'EC' ) # EUR futures. # # _Inverts position relative to quotation styles. # _Average reading between two contracts. return tools.todf( 1 - ((pos1 + pos2) / 2.0) )
def holtforecast( holtdf, h=12 ): '''Given a dataframe from holt, forecast ahead h periods.''' # N.B. - holt forecasts by multiplying latest growth # by the number of periods ahead. Somewhat naive... # notice that the growth is based on smoothed levels. last = holtdf[-1:] y, l, b = last.values.tolist()[0] # df to array to list, but extract first element :-( forecasts = [y] + [ l + (b*(i+1)) for i in range(h) ] # ^last actual point return todf( forecasts, 'Forecast' )
def holtforecast(holtdf, h=12): '''Given a dataframe from holt, forecast ahead h periods.''' # N.B. - holt forecasts by multiplying latest growth # by the number of periods ahead. Somewhat naive... # notice that the growth is based on smoothed levels. last = holtdf[-1:] y, l, b = last.values.tolist()[0] # df to array to list, but extract first element :-( forecasts = [y] + [l + (b * (i + 1)) for i in range(h)] # ^last actual point return todf(forecasts, 'Forecast')
def cotr_position_usd(): '''Market position for USD from COTR of JY and EC.''' # We ignore USD index DX from ICE. pos1 = cotr_position('JY') # JPY futures. pos2 = cotr_position('EC') # EUR futures. # # _Inverts position relative to quotation styles. # _Average reading between two contracts. return tools.todf(1 - ((pos1 + pos2) / 2.0))
def holt(data, alpha=hw_alpha, beta=hw_beta): '''Holt-Winters growth (linear) model outputs workout dataframe.''' # holt is an EXPENSIVE function, so retain its output for later. holtdf = todf(data).dropna() # 'Y' ^else: # "ValueError: Length of values does not match length of index" y = holtdf.values # Convert to array. l, b = holt_winters_growth(y, alpha, beta) holtdf['Level'] = l holtdf['Growth'] = b # In effect, additional columns 'Level' and 'Growth' # for smoothed data and local slope, # along side the original index and given data: return holtdf
def holt( data, alpha=hw_alpha, beta=hw_beta ): '''Holt-Winters growth (linear) model outputs workout dataframe.''' # holt is an EXPENSIVE function, so retain its output for later. holtdf = todf( data ).dropna() # 'Y' ^else: # "ValueError: Length of values does not match length of index" y = holtdf.values # Convert to array. l, b = holt_winters_growth( y, alpha, beta ) holtdf['Level'] = l holtdf['Growth'] = b # In effect, additional columns 'Level' and 'Growth' # for smoothed data and local slope, # along side the original index and given data: return holtdf
def getfut(slang, maxi=512, col='Settle'): '''slang string retrieves single column for one futures contract. The string consists of a key from fut_dict concatenated with 'yym' where yy is shorthand for year and m is the month symbol all in lower case, e.g. 'f4xau15z' for December 2015 Comex Gold. Available col are: Open, High, Low, Last, Change, Settle, Volume, 'Open Interest' ''' # Other than Eurodollars, we should not need more than 512 days # of data due to finite life of a futures contract. # 2015-09-11 quandl default seems to be maxi around 380. # fut = quandl(fut_decode(slang), rows=maxi) # return just a single column dataframe: return tools.todf(fut[[col]])
def getfut( slang, maxi=512, col='Settle' ): '''slang string retrieves single column for one futures contract. The string consists of a key from fut_dict concatenated with 'yym' where yy is shorthand for year and m is the month symbol all in lower case, e.g. 'f4xau15z' for December 2015 Comex Gold. Available col are: Open, High, Low, Last, Change, Settle, Volume, 'Open Interest' ''' # Other than Eurodollars, we should not need more than 512 days # of data due to finite life of a futures contract. # 2015-09-11 quandl default seems to be maxi around 380. # fut = quandl( fut_decode( slang ), rows=maxi ) # return just a single column dataframe: return tools.todf( fut[[ col ]] )
def cotr_position(futures='GC'): '''Extract market position from CFTC Commitment of Traders Report.''' cotr = cotr_get(futures) # Report for both futures and options requested by implicit "FO". # # For directionality we use these categories: try: longs = cotr['Asset Manager Longs'] shorts = cotr['Asset Manager Shorts'] # "Leveraged Funds" for FINANCIALS appear short-term, whereas # "Asset Manager" takes longer term perspective. except: longs = cotr['Money Manager Longs'] shorts = cotr['Money Manager Shorts'] # "Money Manager" for COMMODITIES. # The report is structured differently than financials. # # _Scale-free between 0 and 1 indicating bullishness. return tools.todf(longs / (longs + shorts))
def cotr_position( futures='GC' ): '''Extract market position from CFTC Commitment of Traders Report.''' cotr = cotr_get( futures ) # Report for both futures and options requested by implicit "FO". # # For directionality we use these categories: try: longs = cotr['Asset Manager Longs'] shorts = cotr['Asset Manager Shorts'] # "Leveraged Funds" for FINANCIALS appear short-term, whereas # "Asset Manager" takes longer term perspective. except: longs = cotr['Money Manager Longs'] shorts = cotr['Money Manager Shorts'] # "Money Manager" for COMMODITIES. # The report is structured differently than financials. # # _Scale-free between 0 and 1 indicating bullishness. return tools.todf( longs / (longs + shorts ))
def holtgrow( data, alpha=hw_alpha, beta=hw_beta ): '''Just the Growth dataframe from Holt-Winters growth model.''' # In terms of units expressed in data. return todf( holt( data, alpha, beta )['Growth'] )
def holtlevel(data, alpha=hw_alpha, beta=hw_beta): '''Just smoothed Level dataframe from Holt-Winters growth model.''' # Useful to filter out seasonals, e.g. see X-11 method: # http://www.sa-elearning.eu/basic-algorithm-x-11 return todf(holt(data, alpha, beta)['Level'])
def holtgrow(data, alpha=hw_alpha, beta=hw_beta): '''Just the Growth dataframe from Holt-Winters growth model.''' # In terms of units expressed in data. return todf(holt(data, alpha, beta)['Growth'])
def holtlevel( data, alpha=hw_alpha, beta=hw_beta ): '''Just smoothed Level dataframe from Holt-Winters growth model.''' # Useful to filter out seasonals, e.g. see X-11 method: # http://www.sa-elearning.eu/basic-algorithm-x-11 return todf( holt( data, alpha, beta )['Level'] )