Example #1
0
def calc_ewmac_forecast(price, Lfast, Lslow=None, usescalar=True):
    
    
    """
    Calculate the ewmac trading fule forecast, given a price and EWMA speeds Lfast, Lslow and vol_lookback
    
    Assumes that 'price' is daily data
    """
    ## price: This is the stitched price series
    ## We can't use the price of the contract we're trading, or the volatility will be jumpy
    ## And we'll miss out on the rolldown. See http://qoppac.blogspot.co.uk/2015/05/systems-building-futures-rolling.html

    if Lslow is None:
        Lslow=4*Lfast
    
    ## We don't need to calculate the decay parameter, just use the span directly
    
    fast_ewma=pd.ewma(price, span=Lfast)
    slow_ewma=pd.ewma(price, span=Lslow)
    raw_ewmac=fast_ewma - slow_ewma
    
    ## volatility adjustment
    stdev_returns=volatility(price)    
    vol_adj_ewmac=raw_ewmac/stdev_returns
    
    ## scaling adjustment
    if usescalar:
        f_scalar=ewmac_forecast_scalar(Lfast, Lslow)
        forecast=vol_adj_ewmac*f_scalar
    else:
        forecast=vol_adj_ewmac
    
    cap_forecast=cap_series(forecast, capmin=-20.0,capmax=20.0)
    
    return cap_forecast
Example #2
0
def calc_ewmac_forecast(price, Lfast, Lslow=None, usescalar=True):
    """
    Calculate the ewmac trading fule forecast, given a price and EWMA speeds Lfast, Lslow and vol_lookback
    
    Assumes that 'price' is daily data
    """
    ## price: This is the stitched price series
    ## We can't use the price of the contract we're trading, or the volatility will be jumpy
    ## And we'll miss out on the rolldown. See http://qoppac.blogspot.co.uk/2015/05/systems-building-futures-rolling.html

    if Lslow is None:
        Lslow = 4 * Lfast

    ## We don't need to calculate the decay parameter, just use the span directly

    fast_ewma = pd.ewma(price, span=Lfast)
    slow_ewma = pd.ewma(price, span=Lslow)
    raw_ewmac = fast_ewma - slow_ewma

    ## volatility adjustment
    stdev_returns = volatility(price)
    vol_adj_ewmac = raw_ewmac / stdev_returns

    ## scaling adjustment
    if usescalar:
        f_scalar = ewmac_forecast_scalar(Lfast, Lslow)
        forecast = vol_adj_ewmac * f_scalar
    else:
        forecast = vol_adj_ewmac

    cap_forecast = cap_series(forecast, capmin=-20.0, capmax=20.0)

    return cap_forecast
Example #3
0
def calc_carry_forecast(carrydata, price, f_scalar=30.0):
    """
    Carry calculation
    
    Formulation here will work whether we are trading the nearest contract or not
    
    For other asset classes you will have to work out nerpu (net expected return in price units) yourself
    """

    nerpu = carrydata.apply(find_datediff, axis=1)

    stdev_returns = volatility(price)
    ann_stdev = stdev_returns * ROOT_DAYS_IN_YEAR

    raw_carry = nerpu / ann_stdev

    forecast = raw_carry * f_scalar

    cap_forecast = cap_series(forecast)

    return cap_forecast
Example #4
0
def calc_carry_forecast(carrydata, price, f_scalar=30.0):

    """
    Carry calculation
    
    Formulation here will work whether we are trading the nearest contract or not
    
    For other asset classes you will have to work out nerpu (net expected return in price units) yourself
    """
    
    nerpu=carrydata.apply(find_datediff, axis=1)
    
    stdev_returns=volatility(price)
    ann_stdev=stdev_returns*ROOT_DAYS_IN_YEAR
    
    raw_carry=nerpu/ann_stdev
    
    forecast=raw_carry*f_scalar
    
    cap_forecast=cap_series(forecast)
    
    return cap_forecast
Example #5
0
data_to_plot = pd.concat([price, fast_ewma, slow_ewma], axis=1)
data_to_plot.columns = ['Price', 'Fast', 'Slow']

data_to_plot[d1:d2].plot()
plt.show()

raw_ewmac[d1:d2].plot()
plt.title("Raw EWMAC")
plt.show()

## volatility adjustment
stdev_returns = pd.ewmstd(price - price.shift(1), span=vol_lookback)
vol_adj_ewmac = raw_ewmac / stdev_returns

vol_adj_ewmac[d1:d2].plot()
plt.title("Vol adjusted")
plt.show()

## scaling adjustment
f_scalar = ewmac_forecast_scalar(Lfast, Lslow)

forecast = vol_adj_ewmac * f_scalar

cap_forecast = cap_series(forecast, capmin=-20.0, capmax=20.0)

data_to_plot = pd.concat([forecast, cap_forecast], axis=1)
data_to_plot.columns = ['Scaled Forecast', 'Capped forecast']

data_to_plot[d1:d2].plot()
plt.show()
Example #6
0
plt.show()

#d1=pd.datetime(2007,1,1)
#d2=pd.datetime(2009,12,31)

nerpu = data.apply(find_datediff, axis=1)
nerpu.plot()
plt.title("Nerpu")
plt.show()

## Shouldn't need changing
vol_lookback = 25
stdev_returns = pd.ewmstd(price - price.shift(1), span=vol_lookback)
ann_stdev = stdev_returns * ROOT_DAYS_IN_YEAR

raw_carry = nerpu / ann_stdev
f_scalar = 30.0

raw_carry.plot()
plt.title("Raw carry")
plt.show()

forecast = raw_carry * f_scalar

c_forecast = cap_series(forecast)

data_to_plot = pd.concat([forecast, c_forecast], axis=1)
data_to_plot.columns = ['Forecast', 'Capped forecast']
data_to_plot.plot()
plt.show()
Example #7
0
#d1=pd.datetime(2007,1,1)
#d2=pd.datetime(2009,12,31)


nerpu=data.apply(find_datediff, axis=1)
nerpu.plot()
plt.title("Nerpu")
plt.show()

## Shouldn't need changing
vol_lookback=25
stdev_returns=pd.ewmstd(price - price.shift(1), span=vol_lookback)
ann_stdev=stdev_returns*ROOT_DAYS_IN_YEAR

raw_carry=nerpu/ann_stdev
f_scalar=30.0

raw_carry.plot()
plt.title("Raw carry")
plt.show()

forecast=raw_carry*f_scalar

c_forecast=cap_series(forecast)

data_to_plot=pd.concat([forecast,c_forecast], axis=1)
data_to_plot.columns=['Forecast','Capped forecast']
data_to_plot.plot()
plt.show()

Example #8
0
    price = get_price_for_instrument(code)
    carrydata = get_carry_data(code)
    current_price = carrydata.TRADED

    if Lfast is None:
        forecast = calc_carry_forecast(carrydata, price)
        ## carry
    else:
        forecast = calc_ewmac_forecast(price, Lfast)

    stdev = volatility(price) * root_days_in_week
    price_volatility = 100 * stdev / current_price
    next_weeks_return = price.shift(-5) - price
    next_weeks_vol_norm_return = next_weeks_return / stdev
    next_weeks_vol_norm_return = cap_series(next_weeks_vol_norm_return,
                                            capmin=-50.0,
                                            capmax=50.0)

    fcastStack.append(forecast)
    vnStack.append(next_weeks_vol_norm_return)

    binary1forecast = make_binary(forecast, rounded=False)
    binary2forecast = make_binary(forecast, rounded=True)

    binary1Stack.append(binary1forecast)
    binary2Stack.append(binary2forecast)

    volStack.append(price_volatility)

    priceStack.append(price)
    currentpriceStack.append(current_price)
for code in code_list:
    price=get_price_for_instrument(code)
    carrydata=get_carry_data(code)
    current_price=carrydata.TRADED

    if Lfast is None:
        forecast=calc_carry_forecast(carrydata, price)
        ## carry
    else:
        forecast=calc_ewmac_forecast(price, Lfast)
    
    stdev=volatility(price)*root_days_in_week
    price_volatility=100*stdev/current_price
    next_weeks_return =price.shift(-5) - price
    next_weeks_vol_norm_return =next_weeks_return/stdev
    next_weeks_vol_norm_return=cap_series(next_weeks_vol_norm_return, capmin=-50.0,capmax=50.0)

    fcastStack.append(forecast)
    vnStack.append(next_weeks_vol_norm_return)
    
    binary1forecast=make_binary(forecast, rounded=False)
    binary2forecast=make_binary(forecast, rounded=True)

    binary1Stack.append(binary1forecast)
    binary2Stack.append(binary2forecast)
    
    volStack.append(price_volatility)
    
    priceStack.append(price)
    currentpriceStack.append(current_price)
    
plt.show()

raw_ewmac[d1:d2].plot()
plt.title("Raw EWMAC")
plt.show()

## volatility adjustment
stdev_returns=pd.ewmstd(price - price.shift(1), span=vol_lookback)    
vol_adj_ewmac=raw_ewmac/stdev_returns

vol_adj_ewmac[d1:d2].plot()
plt.title("Vol adjusted")
plt.show()

## scaling adjustment
f_scalar=ewmac_forecast_scalar(Lfast, Lslow)

forecast=vol_adj_ewmac*f_scalar


cap_forecast=cap_series(forecast, capmin=-20.0,capmax=20.0)

data_to_plot=pd.concat([forecast, cap_forecast], axis=1)
data_to_plot.columns=['Scaled Forecast', 'Capped forecast']

data_to_plot[d1:d2].plot()
plt.show()



Example #11
0
#d1=pd.datetime(2007,1,1)
#d2=pd.datetime(2009,12,31)


nerpu=data.apply(find_datediff, axis=1)
nerpu.plot()
plt.title("Nerpu")
plt.show()

## Shouldn't need changing
vol_lookback=25
stdev_returns=pd.ewmstd(price - price.shift(1), span=vol_lookback)
ann_stdev=stdev_returns*ROOT_DAYS_IN_YEAR

raw_carry=nerpu/ann_stdev
f_scalar=30.0

raw_carry.plot()
plt.title("Raw carry")
plt.show()

forecast=raw_carry*f_scalar

c_forecast=cap_series(forecast).to_frame()

data_to_plot=pd.concat([forecast,c_forecast], axis=1)
data_to_plot.columns=['Forecast','Capped forecast']
data_to_plot.plot()
plt.show()