def process_csv_file(file): df = pd.read_csv(file) output_dictionary = {'option_name': [], 'input': [], 'grid_count': [], 'beta': [], 'date': [], 'minimizer': []} output_dt = pd.DataFrame(output_dictionary) day_count = len(df) for i in range(2, day_count - 2): today = df.iat[i + 2, 0] data_block = df.iloc[i:i + 3] # data_block = data_block.apply(pd.to_numeric) option_name = 'unknown' if not data_block.isnull().values.any(): # Then the 3-day data is good for us option_ask = data_block.iloc[:, 1].values option_bid = data_block.iloc[:, 2].values volatility = data_block.iloc[:, 4].values stock_ask = math.ceil(data_block.iat[2, 5] * 100) / 100 stock_bid = math.floor(data_block.iat[2, 6] * 100) / 100 if stock_ask == stock_bid: stock_ask += 0.01 input_data = ns.DataBlock(today=today, option_ask=option_ask, option_bid=option_bid, volatility=volatility, stock_ask=stock_ask, stock_bid=stock_bid) input_data.create_system(grid, beta) res = input_data.solve() row = {'option_name': option_name, 'input': str(input_data), 'grid_count': grid, 'beta': beta, 'date': today, 'minimizer': np.array2string(res.x, max_line_width=np.inf, threshold=np.inf)} output_dt = output_dt.append(row, ignore_index=True) print(f'Solved {option_name} on {today}') else: print(f'{option_name} on {today} is skipped due to insufficient data.') filename = Path(file).stem output_file = f'{output_folder}/{filename}_out.csv' output_dt.to_csv(output_file)
def predict(file, grid_count, beta): df = pd.read_csv(file) output_dictionary = { 'option_name': [], 'input': [], 'grid_count': [], 'beta': [], 'date': [], 'estimates': [], 'real': [] } output_dt = pd.DataFrame(output_dictionary) day_count = len(df) day_count = 20 for i in range(2, day_count - 2): today = df['DATE'][i] data_block = df.iloc[i - 2:i + 1] if len(set(data_block['OPTION_NAME'])) != 1: continue else: option_name = data_block['OPTION_NAME'].iat[0] value_block = data_block[[ 'EOD_OPTION_PRICE_ASK', 'EOD_OPTION_PRICE_BID', 'IVOL_LAST', 'EOD_UNDERLYING_PRICE_ASK', 'EOD_UNDERLYING_PRICE_BID' ]] if not value_block.isnull().values.any(): # Then the 3-day data is good for us option_ask = value_block['EOD_OPTION_PRICE_ASK'].values option_bid = value_block['EOD_OPTION_PRICE_BID'].values volatility = value_block['IVOL_LAST'].values stock_ask = float(value_block['EOD_UNDERLYING_PRICE_ASK'].iat[2]) stock_bid = float(value_block['EOD_UNDERLYING_PRICE_BID'].iat[2]) input_data = ns.DataBlock(today=today, option_ask=option_ask, option_bid=option_bid, volatility=volatility, stock_ask=stock_ask, stock_bid=stock_bid) input_data.create_system(grid_count, beta) res = input_data.solve() m = grid_count solution = res.x.reshape( (m - 1, m - 2))[[math.ceil(m / 2 - 1), m - 2], math.ceil((m - 2) / 2)] real_future = [] for j in range(2): future_price = df['EOD_OPTION_PRICE_LAST'][i + j] if np.isnan(future_price): future_price = np.mean( df[['EOD_OPTION_PRICE_ASK', 'EOD_OPTION_PRICE_BID']].iloc[i + j, :]) real_future.append(future_price) row = { 'option_name': option_name, 'input': input_data.data(), 'grid_count': grid_count, 'beta': beta, 'date': today, 'estimates': solution, 'real': real_future } output_dt = output_dt.append(row, ignore_index=True) print( f'Solved {option_name} on {today}, finished {i-1}/{day_count-4}' ) else: print( f'{option_name} on {today} is skipped due to insufficient data; finished {i-1}/{day_count-4}' ) filename = Path(file).stem output_file = f'output/prediction/{filename}_prediction.csv' output_dt.to_csv(output_file)
from model import num_solver as ns import math block = ns.DataBlock(today='10/19/2016',\ option_ask = [0.86, 0.86, 0.86],\ option_bid = [0.84, 0.85, 0.85],\ volatility = [39.456, 38.061, 37.096],\ stock_ask = 4.66,\ stock_bid = 4.65) m = 15 beta = 0.01 block.create_system(m, beta) result = block.solve() solution = result.x.reshape((m - 1, m - 2)) print('Minimizer is:') print(solution) print('Estimates for 1tau and 2tau: ', solution[[math.ceil(m / 2 - 1), m - 2], math.ceil((m - 2) / 2)])
def solve(i, df, namespace, output_lock, day_count): if 'DATE' in df: today = df['DATE'][i] else: today = None data_block = df.iloc[i - 2:i + 1] if 'OPTION_NAME' in df: if len(set(data_block['OPTION_NAME'])) > 1: return else: option_name = data_block['OPTION_NAME'].iat[0] else: option_name = None value_block = data_block[value_list] if not value_block.isnull().values.any(): # Then the 3-day data is good for us option_ask = value_block['EOD_OPTION_PRICE_ASK'].values option_bid = value_block['EOD_OPTION_PRICE_BID'].values volatility = value_block['IVOL_LAST'].values stock_ask = float(value_block['EOD_UNDERLYING_PRICE_ASK'].iat[2]) stock_bid = float(value_block['EOD_UNDERLYING_PRICE_BID'].iat[2]) input_data = ns.DataBlock(today=today, option_ask=option_ask, option_bid=option_bid, volatility=volatility, stock_ask=stock_ask, stock_bid=stock_bid) input_data.create_system(grid_count, beta) res = input_data.solve() m = grid_count solution = res.x.reshape((m - 1, m - 2))[[math.ceil(m / 2 - 1), m - 2], math.ceil((m - 2) / 2)] real_future = [] for j in range(2): if 'EOD_OPTION_PRICE_LAST' in df: future_price = df['EOD_OPTION_PRICE_LAST'][i + j] else: future_price = np.mean( df[['EOD_OPTION_PRICE_ASK', 'EOD_OPTION_PRICE_BID']].iloc[i + j, :]) real_future.append(future_price) row = { 'option_name': option_name, 'grid_count': grid_count, 'beta': beta, 'date': today, 'option_ask-2': input_data.u_a_list[0], 'option_ask-1': input_data.u_a_list[1], 'option_ask0': input_data.u_a_list[2], 'option_bid-2': input_data.u_b_list[0], 'option_bid-1': input_data.u_b_list[1], 'option_bid0': input_data.u_b_list[2], 'stock_ask0': input_data.s_a, 'stock_bid0': input_data.s_b, 'ivol-2': input_data.ivol_list[0], 'ivol-1': input_data.ivol_list[1], 'ivol-0': input_data.ivol_list[2], 'est+1': float(solution[0]), 'est+2': float(solution[1]), 'real+1': float(real_future[0]), 'real+2': float(real_future[1]) } output_lock.acquire() try: namespace.df = namespace.df.append(row, ignore_index=True) print( f'Solved {option_name} on {today}, finished {i - 1}/{day_count - 4}' ) finally: output_lock.release() else: print( f'{option_name} on {today} is skipped due to insufficient data; finished {i - 1}/{day_count - 4}' )