def run(prices, ts, ratio_func, start_cash): count = len(ts) orders = np.zeros((count, 2), dtype=np.int) positions = np.zeros((count, 2), dtype=np.int) cash = np.zeros(count) cash[0] = start_cash cash_delta = np.zeros(count) vxx_ratio = np.zeros(count) vxz_ratio = np.zeros(count) for i in range(1, count): ratio = ratio_func(ts[i-1]) vxx_ratio[i] = ratio[0] vxz_ratio[i] = ratio[1] orders[i] = calc_adjustments( positions[i-1], (prices['VXX'][i-1], prices['VXZ'][i-1]), ratio, cash[i-1]) positions[i] = positions[i-1] + orders[i] cash_delta[i] = calc_cash_delta( orders[i], (prices['VXX'][i-1], prices['VXZ'][i-1]), positions[i-1]) cash[i] = cash[i-1] + cash_delta[i] if DEBUG: print('vxx ratio, vxz ratio, vxx px, vxz px, vxx orders, vxz orders, ' 'vxx positions, vxz positions, cash, cash delta') for i in range(0, count): print('{}, {}, {}, {}, {}, {}, {}, {}, {}, {}'.format( vxx_ratio[i], vxz_ratio[i], prices['VXX'][i], prices['VXZ'][i], orders[i][0], orders[i][1], positions[i-1][0], positions[i-1][1], cash[i], cash_delta[i])) vxx_change = (prices['VXX'] - utils.lag(prices['VXX'])) * positions[:, 0] vxz_change = (prices['VXZ'] - utils.lag(prices['VXZ'])) * positions[:, 1] notional_value = (np.absolute(positions) * prices[['VXX', 'VXZ']]).sum(1) \ + cash returns = (vxx_change + vxz_change) / notional_value # np.save('/Users/Conor/code/blog/python/kelly/returns.npy', returns) np.savetxt('/Users/Conor/code/blog/python/returns/cash.txt', cash) # np.savetxt('/Users/Conor/code/blog/python/returns/positions.txt', positions) # np.savetxt('/Users/Conor/code/blog/python/returns/stockA.txt', prices['VXX'] / 5.) # np.savetxt('/Users/Conor/code/blog/python/returns/stockB.txt', prices['VXZ'] / 5.) return returns, cash
def population_std_dev(close_prices, lookback): N = float(lookback) prices = log(close_prices / utils.lag(close_prices)) results = np.zeros(np.size(prices)) results[:] = np.NAN for i in range(lookback, len(prices)): bounds = range(i - (lookback - 1), i + 1) results[i] = sqrt( ((prices[bounds] - prices[bounds].sum() / N)**2).sum() / (N - 1)) return annualise(results)
def population_std_dev(close_prices, lookback): N = float(lookback) prices = log(close_prices / utils.lag(close_prices)) results = zeros(size(prices)) results[:] = NAN for i in range(lookback, len(prices)): bounds = range(i-(lookback-1), i+1) results[i] = sqrt( ((prices[bounds] - prices[bounds].sum() / N)**2).sum() / (N - 1)) return annualise(results)
def get_random_intraday_returns(mu, sigma, day_count): # prices = np.random.normal(mu, sigma, 391 * day_count) prices = np.cumsum(np.random.normal(0, 1, 391 * day_count) * sigma) + mu log_returns = (np.log(prices / utils.lag(prices, np.NaN))) log_returns[0] = 0. return np.cumsum(log_returns)
def test_lag_multiple_vec(self): np_utils.assert_array_equal( np.array([[0, 0], [1, 4], [2, 5]]), utils.lag(np.array([[1, 4], [2, 5], [3, 6]])))
def test_lag_nan(self): np_utils.assert_array_equal( np.array([np.NAN, 1, 2, 3, 4]), utils.lag(np.array([1., 2., 3., 4., 5.]), np.NAN))
def test_lag(self): np_utils.assert_array_equal(np.array([0, 1, 2, 3, 4]), utils.lag(np.array([1, 2, 3, 4, 5])))
def pandas_std_dev(close_prices, lookback): prices = log(close_prices / utils.lag(close_prices)) return annualise(pd.rolling_std(prices, window=lookback))