def slinger(ax, datafile, ticker, put_parameters, call_parameters): time = datafile[ticker]['datetime'][...] data_open = datafile[ticker]['open'][...] data_high = datafile[ticker]['high'][...] data_low = datafile[ticker]['low'][...] data_volume = datafile[ticker]['volume'][...] datafile.close() tradeable = Analytics.market_hours(t=time) print(np.sum(tradeable)) candle = Analytics.candle_avg(open=data_open, high=data_high, low=data_low) candle_low_bollinger, candle_high_bollinger = Analytics.candle_bollinger_bands( open=data_open, high=data_high, low=data_low, average=candle, period=30) period = 30 sma = Analytics.moving_average(data=candle, period=period) sma_short = Analytics.exp_moving_average(data=candle, alpha=.1, period=period // 3) sma_low_bollinger, sma_high_bollinger = Analytics.bollinger_bands( data=sma_short, average=sma) sma_d = Analytics.derivative(sma, period=period // 6) # sma_d = Analytics.moving_average(sma_d, period=period // 6) sma_dd = Analytics.second_derivative(sma, period=period) results_list = SMA_chaser.put_chaser_strat( time=time, sma=sma, bollinger_up=sma_high_bollinger, bollinger_down=sma_low_bollinger, sma_d=sma_d, candle=candle, candle_high=candle_high_bollinger, candle_low=candle_low_bollinger, parameters=put_parameters) put_buy_locs = results_list[0] put_buy_price = results_list[1] # print(put_buy_price) put_buy_option_price = results_list[2] print(put_buy_option_price) put_sell_locs = results_list[3] put_sell_price = results_list[4] # print(put_sell_price) put_sell_option_price = results_list[5] print(put_sell_option_price) put_profits = (put_sell_option_price - put_buy_option_price) put_percent = (put_sell_option_price - put_buy_option_price) / put_buy_option_price print(put_percent) put_percent_avg = np.sum(put_percent) / put_percent.shape[0] print(put_percent_avg) # return_list.append(put_percent) # return_list.append(put_percent_avg) focus_top = time.shape[0] - 60 * 48 focus_bot = time.shape[0] + 1 focus_top = 0 focus_bot = time.shape[0] + 1 candle_rescaled = candle - np.sum(candle) / sma.shape[0] candle_rescaled = candle_rescaled / np.abs(candle_rescaled).max() sma_rescaled = sma - np.sum(sma) / sma.shape[0] sma_rescaled = sma_rescaled / np.abs(sma_rescaled).max() Bollinger_oscillator = 2 * ( sma_short - sma) / np.absolute(sma_high_bollinger - sma_low_bollinger) ''' ax[0].plot(time[focus_top:focus_bot], candle_rescaled, label='candle') ax[0].plot(time[focus_top:focus_bot], sma_rescaled, label='sma') ax[0].plot(time[focus_top:focus_bot], sma_d[focus_top:focus_bot] / np.abs(sma_d).max(), label='sma_d') ax[0].plot(time[focus_top:focus_bot], sma_dd[focus_top:focus_bot] / np.abs(sma_dd).max(), label='sma_dd') ax[0].legend() ''' ################################################################################# # plt.figure(figsize=(20, 10)) # plt.suptitle('profitable trades') # ax[0].plot(time[tradeable], data_volume[tradeable], '.') ax_twin = ax[0].twinx() ax_twin.plot(time[tradeable], sma_d[tradeable]) ax[0].plot(time[tradeable], candle[tradeable], '.', label=str(put_percent_avg)) ax[0].plot(time[tradeable], sma[tradeable]) ax[0].plot(time[tradeable], sma_low_bollinger[tradeable]) ax[0].plot(time[tradeable], sma_high_bollinger[tradeable]) ax[0].plot(time[tradeable], candle_low_bollinger[tradeable]) ax[0].plot(time[tradeable], candle_high_bollinger[tradeable]) profit_put_buy_locs = put_buy_locs[put_profits >= 0] put_cut = profit_put_buy_locs[profit_put_buy_locs > focus_top] ax[0].plot(time[put_cut], candle[put_cut], '>', color='r') profit_put_sell_locs = put_sell_locs[put_profits >= 0] put_cut = profit_put_sell_locs[profit_put_sell_locs > focus_top] ax[0].plot(time[put_cut], candle[put_cut], '<', color='g') ax[0].legend() ################################################################################# # plt.figure(figsize=(20, 10)) # plt.suptitle('loss trades') # ax[1].plot(time, data_volume, '.') ax[1].plot(time[tradeable], candle[tradeable], '.', label=str(put_percent_avg)) ax[1].plot(time[tradeable], sma[tradeable]) ax[1].plot(time[tradeable], sma_low_bollinger[tradeable]) ax[1].plot(time[tradeable], sma_high_bollinger[tradeable]) ax[1].plot(time[tradeable], candle_low_bollinger[tradeable]) ax[1].plot(time[tradeable], candle_high_bollinger[tradeable]) loss_put_buy_locs = put_buy_locs[put_profits < 0] put_cut = loss_put_buy_locs[loss_put_buy_locs > focus_top] ax[1].plot(time[put_cut], candle[put_cut], '>', color='r') loss_put_sell_locs = put_sell_locs[put_profits < 0] put_cut = loss_put_sell_locs[loss_put_sell_locs > focus_top] ax[1].plot(time[put_cut], candle[put_cut], '<', color='g') ax[1].legend() results_list = SMA_chaser.call_chaser_strat( time=time, sma=sma, bollinger_up=sma_high_bollinger, bollinger_down=sma_low_bollinger, sma_d=sma_d, candle=candle, candle_high=candle_high_bollinger, candle_low=candle_low_bollinger, parameters=call_parameters) call_buy_option_price = results_list[2] print(put_buy_option_price) call_sell_option_price = results_list[5] print(put_sell_option_price) call_percent = (call_sell_option_price - call_buy_option_price) / call_buy_option_price print(call_percent) call_percent_avg = np.sum(call_percent) / call_percent.shape[0] print(call_percent_avg) percent_profits = np.concatenate((put_percent, call_percent)) print(percent_profits) average_profit = np.sum(percent_profits) / percent_profits.shape[0] print(average_profit) return call_percent_avg
def slinger(ax, datafile, ticker, parameters): time = datafile[ticker]['datetime'][...] data_open = datafile[ticker]['open'][...] data_high = datafile[ticker]['high'][...] data_low = datafile[ticker]['low'][...] data_volume = datafile[ticker]['volume'][...] datafile.close() tradeable = Analytics.market_hours(t=time) print(np.sum(tradeable)) candle = Analytics.candle_avg(open=data_open, high=data_high, low=data_low) candle_low_bollinger, candle_high_bollinger = Analytics.candle_bollinger_bands( open=data_open, high=data_high, low=data_low, average=candle, period=30) period = 30 sma = Analytics.moving_average(data=candle, period=period) sma_short = Analytics.moving_average(data=candle, period=period // 3) sma_low_bollinger, sma_high_bollinger = Analytics.bollinger_bands( data=sma_short, average=sma) sma_d = Analytics.derivative(sma, period=period // 6) # sma_d = Analytics.moving_average(sma_d, period=period // 6) sma_dd = Analytics.second_derivative(sma, period=period) day_volatility = Analytics.day_volatility(data=candle, tradeable=tradeable) print('day volatility: {}'.format(day_volatility)) # find the best strategy of given strategies: performance_list = [] # parameters['option_type'] = position_class.OptionType.PUT for parameter in parameters: parameter['VIX'] = day_volatility results_list = PutSlingerBollinger.Bollinger_strat( time=time, sma=sma, sma_short=sma_short, bollinger_up=sma_high_bollinger, bollinger_down=sma_low_bollinger, sma_d=sma_d, candle=candle, candle_high=candle_high_bollinger, candle_low=candle_low_bollinger, parameters=parameter) put_buy_option_price = results_list[2] put_sell_option_price = results_list[5] put_percent = (put_sell_option_price - put_buy_option_price) / put_buy_option_price put_percent[ put_percent > 5] = 5 # put an upper bound on the option returns. put_percent_avg = np.sum(put_percent) / put_percent.shape[0] performance_list.append(put_percent_avg) performance_array = np.array(performance_list) print('performance_array: {}'.format(performance_array)) best_perf_loc = np.where( performance_array == performance_array.max())[0][0] # hack the parameters to be the best choice for the rest of the function, this keeps recoding to a minimum: parameter = parameters[best_perf_loc] parameter = parameters[0] results_list = PutSlingerBollinger.Bollinger_strat( time=time, sma=sma, sma_short=sma_short, bollinger_up=sma_high_bollinger, bollinger_down=sma_low_bollinger, sma_d=sma_d, candle=candle, candle_high=candle_high_bollinger, candle_low=candle_low_bollinger, parameters=parameter) put_buy_locs = results_list[0] put_buy_price = results_list[1] put_buy_option_price = results_list[2] put_sell_locs = results_list[3] put_sell_price = results_list[4] put_sell_option_price = results_list[5] position_value = results_list[7] print('stock price at open: {}'.format(put_buy_price)) print('strike price: {}'.format(results_list[6])) print('stock price at close: {}'.format(put_sell_price)) print('option cost at open: {}'.format(put_buy_option_price)) print('option cost at close: {}'.format(put_sell_option_price)) put_profits = (put_sell_option_price - put_buy_option_price) put_percent = (put_sell_option_price - put_buy_option_price) / put_buy_option_price print('option % gain: {}'.format(put_percent)) put_percent[ put_percent > 5] = 5 # put an upper bound on the option returns. print('position values: {}'.format(position_value)) account_value = np.sum(position_value) print('account value ate EOD: {}'.format(account_value)) # put_percent_avg = np.sum(put_percent) / put_percent.shape[0] put_percent_avg = np.sum(account_value) - 1 print('average option % gain: {}'.format(put_percent_avg)) results_list.append(put_percent) results_list.append(put_percent_avg) focus_top = time.shape[0] - 60 * 48 focus_bot = time.shape[0] + 1 focus_top = 0 focus_bot = time.shape[0] + 1 candle_rescaled = candle - np.sum(candle) / sma.shape[0] candle_rescaled = candle_rescaled / np.abs(candle_rescaled).max() sma_rescaled = sma - np.sum(sma) / sma.shape[0] sma_rescaled = sma_rescaled / np.abs(sma_rescaled).max() Bollinger_oscillator = 2 * ( sma_short - sma) / np.absolute(sma_high_bollinger - sma_low_bollinger) minute_time = Analytics.minute_time(time) # print(minute_time) ''' ax[0].plot(time[focus_top:focus_bot], candle_rescaled, label='candle') ax[0].plot(time[focus_top:focus_bot], sma_rescaled, label='sma') ax[0].plot(time[focus_top:focus_bot], sma_d[focus_top:focus_bot] / np.abs(sma_d).max(), label='sma_d') ax[0].plot(time[focus_top:focus_bot], sma_dd[focus_top:focus_bot] / np.abs(sma_dd).max(), label='sma_dd') ax[0].legend() ''' ################################################################################# # plt.figure(figsize=(20, 10)) # plt.suptitle('profitable trades') # ax[0].plot(time[tradeable], data_volume[tradeable], '.') ax_twin = ax[0].twinx() ax_twin.plot(minute_time[tradeable], Bollinger_oscillator[tradeable]) ax_twin.plot(minute_time[tradeable], np.ones_like(minute_time[tradeable]) * parameter['Bollinger_top'], color='k') ax_twin.plot(minute_time[tradeable], np.ones_like(minute_time[tradeable]) * parameter['Bollinger_bot'], color='k') ax[0].plot(minute_time[tradeable], candle[tradeable], '.', label=str(put_percent_avg)) ax[0].plot(minute_time[tradeable], sma[tradeable]) ax[0].plot(minute_time[tradeable], sma_low_bollinger[tradeable]) ax[0].plot(minute_time[tradeable], sma_high_bollinger[tradeable]) ax[0].plot(minute_time[tradeable], candle_low_bollinger[tradeable]) ax[0].plot(minute_time[tradeable], candle_high_bollinger[tradeable]) profit_put_buy_locs = put_buy_locs[put_profits >= 0] put_cut = profit_put_buy_locs[profit_put_buy_locs > focus_top] ax[0].plot(minute_time[put_cut], candle[put_cut], '>', color='k') profit_put_sell_locs = put_sell_locs[put_profits >= 0] put_cut = profit_put_sell_locs[profit_put_sell_locs > focus_top] ax[0].plot(minute_time[put_cut], candle[put_cut], '<', color='k') ax[0].legend() ################################################################################# # plt.figure(figsize=(20, 10)) # plt.suptitle('loss trades') # ax[1].plot(minute_time, data_volume, '.') ax_twin = ax[1].twinx() ax_twin.plot(minute_time[tradeable], Bollinger_oscillator[tradeable]) ax_twin.plot(minute_time[tradeable], np.ones_like(minute_time[tradeable]) * parameter['Bollinger_top'], color='k') ax_twin.plot(minute_time[tradeable], np.ones_like(minute_time[tradeable]) * parameter['Bollinger_bot'], color='k') ax[1].plot(minute_time[tradeable], candle[tradeable], '.', label=str(put_percent_avg)) ax[1].plot(minute_time[tradeable], sma[tradeable]) ax[1].plot(minute_time[tradeable], sma_low_bollinger[tradeable]) ax[1].plot(minute_time[tradeable], sma_high_bollinger[tradeable]) ax[1].plot(minute_time[tradeable], candle_low_bollinger[tradeable]) ax[1].plot(minute_time[tradeable], candle_high_bollinger[tradeable]) loss_put_buy_locs = put_buy_locs[put_profits < 0] put_cut = loss_put_buy_locs[loss_put_buy_locs > focus_top] ax[1].plot(minute_time[put_cut], candle[put_cut], '>', color='k') loss_put_sell_locs = put_sell_locs[put_profits < 0] put_cut = loss_put_sell_locs[loss_put_sell_locs > focus_top] ax[1].plot(minute_time[put_cut], candle[put_cut], '<', color='k') ax[1].legend() return put_percent_avg, performance_array
data_open = datafile[group_choice]['open'][...] data_high = datafile[group_choice]['high'][...] data_low = datafile[group_choice]['low'][...] datafile.close() period = 30 data = Analytics.candle_avg(open=data_open, high=data_high, low=data_low) candle_low_bollinger, candle_high_bollinger = Analytics.candle_bollinger_bands(open=data_open, high=data_high, low=data_low, average=data, period=period) sma = Analytics.moving_average(data=data, period=period) sma_d = Analytics.derivative(data=sma, period=period) sma_dd = Analytics.second_derivative(data=sma, period=period) # sma = Analytics.exp_moving_average(data=data, alpha=.1, period=30) sma_low_bollinger, sma_high_bollinger = Analytics.bollinger_bands(data=data, average=sma) results_list = SMA_strat(time=time, sma=sma, sma_d=sma_d, sma_dd=sma_dd, bollinger_down=sma_low_bollinger, bollinger_up=sma_high_bollinger, candle=data, candle_down=candle_low_bollinger, candle_up=candle_high_bollinger) call_buy_locs = results_list[0] call_buy_price = results_list[1] call_sell_locs = results_list[2]
def DataAnalysis(filedirectory='../StockData/'): result_list = [] meta_result_list = [] for direntry in os.scandir(filedirectory): result_dict = {} meta_result_dict = {} if direntry.is_dir(): continue if direntry.is_file(): filepath = direntry.path print(filepath) try: datafile = h5py.File(filepath) except: print('could not open file: {}'.format(filepath)) continue time_data = datafile['SPY']['datetime'][...] if time_data.shape[0] == 0: print('no \'SPY\' data in file: {}'.format(filepath)) continue clock_list = [] market_hours_list = [] for i, t in enumerate(time_data): # gm_time = tm.gmtime(t * 1e-3) utc_time = arrow.get(t * 1e-3).to('utc') utc_time = utc_time.shift( hours=-4) # must explicitely shift time for numpy to recognize market_open = utc_time.replace(hour=9, minute=30, second=0, microsecond=0) market_close = utc_time.replace(hour=16, minute=0, second=0, microsecond=0) open_delta = utc_time - market_open close_delta = market_close - utc_time # nyt_time = utc_time.to('America/New_York') clock_list.append(open_delta.total_seconds()) if open_delta.total_seconds() >= 0 and close_delta.total_seconds( ) >= 0: market_hours_list.append(True) else: market_hours_list.append(False) if i == time_data.shape[0] // 2: meta_result_dict['date_list'] = utc_time.date() # print(clock_list) meta_result_dict['clocktime'] = np.array(clock_list) meta_result_dict['tradeable'] = np.array(market_hours_list, dtype=np.bool) open_data = datafile['SPY']['open'][...] high_data = datafile['SPY']['high'][...] low_data = datafile['SPY']['low'][...] period = 5 result_dict['candle_average'] = Analytics.candle_avg(open=open_data, high=high_data, low=low_data) result_dict['moving_average'] = Analytics.moving_average( data=result_dict['candle_average'], period=period) result_dict['mvg_avg_d'] = Analytics.derivative( data=result_dict['moving_average'], period=period) result_dict['mvg_avg_dd'] = Analytics.second_derivative( data=result_dict['moving_average'], period=period) meta_result_list.append(meta_result_dict) result_list.append(result_dict) datafile.close() print('number of \'SPY\' days found: {}'.format(len(result_list))) # print(str(date_list[0])) fig, axs = plt.subplots(nrows=len(result_list), ncols=len(result_list[0]), figsize=(20, 40)) # print(axs.shape) for i, meta_result_dict, result_dict in zip(np.arange(len(result_list)), meta_result_list, result_list): # print(result_dict.keys()) for j, key in enumerate(result_dict.keys()): # print(result_dict[key]) axs[i, j].hist(result_dict[key], bins=30) # axs[i, j].legend() if i == 0: axs[i, j].set_title(key) if j == 0: axs[i, j].set_ylabel(str(meta_result_dict['date_list']), rotation=90, size='large') # axs[i].set_xlim(left=(9 * 3600 + 30 * 60), right=(16 * 3600)) # axs[i, j].set_xlim(left=(-30 * 60), right=(6 * 3600 + 30 * 60)) # axs[i].set_xticks(np.arange(8)*3600) # axs[i].set_xticklabels(map(str, np.arange(8)+9)) plt.tight_layout() # plt.figure(figsize=(10, 10)) # for i, clocktime, candle_average in zip(np.arange(len(clocktime_list)), clocktime_list, candle_average_list): # plt.plot(clocktime, candle_average / np.max(np.abs(candle_average))) return