def compute_analytics(self, compute_dict): if not self.validate_compute_dict(compute_dict): return False self.num_datapoints = int(compute_dict['time'].shape[0]) self.data['time'] = compute_dict['time'] self.data['data'] = compute_dict['data'] self.compute['tradeable'] = Analytics.market_hours( compute_dict['time']) for key, val in compute_dict.items(): if key == 'sma': if isinstance(compute_dict[key], list): self.compute[key] = [] for period in compute_dict[key]: self.compute[key].append( Analytics.moving_average(data=compute_dict['data'], period=period)) else: continue elif key == 'derivative': if isinstance(compute_dict[key], list): self.compute[key] = [] for sma_period, deriv_period in compute_dict[key]: local_sma = Analytics.moving_average( data=compute_dict['data'], period=sma_period) self.compute[key].append( Analytics.derivative(data=local_sma, period=deriv_period)) else: continue elif key == 'Bollinger': if isinstance(compute_dict[key], list): self.compute[key] = [] for anchor_period, oscillator_period, bollinger_period in compute_dict[ key]: local_anchor = Analytics.moving_average( data=compute_dict['data'], period=anchor_period) local_oscillator = Analytics.moving_average( data=compute_dict['data'], period=oscillator_period) self.compute[key].append( Analytics.bollinger_bands(data=local_oscillator, average=local_anchor, period=bollinger_period)) else: continue self.analytics_up_to_date = True return True
print('Data file does not exist!') group_choice = np.random.choice(list(datafile.keys())) time = datafile[group_choice]['datetime'][...] data_open = datafile[group_choice]['open'][...] data_high = datafile[group_choice]['high'][...] data_low = datafile[group_choice]['low'][...] datafile.close() 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=30) sma = Analytics.moving_average(data=data, period=20) sma_low_bollinger, sma_high_bollinger = Analytics.bollinger_bands( data=data, average=sma) call_buy_locs, call_buy_price, \ call_sell_locs, call_sell_price, \ put_buy_locs, put_buy_price, \ put_sell_locs, put_sell_price, \ local_minimums, local_minimums_loc, \ local_maximums, local_maximums_loc = SMA_strat(sma=sma, bollinger_down=sma_low_bollinger, bollinger_up=sma_high_bollinger, candle=data, candle_down=candle_low_bollinger, candle_up=candle_high_bollinger) cut = np.min((call_buy_price.shape[0], call_sell_price.shape[0])) call_profits = (call_buy_price[:cut] - call_sell_price[:cut])
def compute_analytics(self, data): # if not self.validate_compute_dict(self.compute_dict): # return False self.data = data self.compute[enums.ComputeKeys.datetime] = np.array( data['datetime'].tolist()) self.num_datapoints = int( self.compute[enums.ComputeKeys.datetime].shape[0]) self.compute[enums.ComputeKeys.candle] = Analytics.candle_avg( open=np.array(data['open'].tolist()), high=np.array(data['high'].tolist()), low=np.array(data['low'].tolist())) self.compute[enums.ComputeKeys.tradeable] = Analytics.market_hours( self.compute[enums.ComputeKeys.datetime]) for key, val in self.compute_dict.items(): if key is enums.ComputeKeys.sma: if isinstance(val, list): self.compute[key] = [] for period in val: self.compute[key].append( Analytics.moving_average( data=self.compute[enums.ComputeKeys.candle], period=period)) else: continue if key is enums.ComputeKeys.derivative: if isinstance(self.compute_dict[key], list): self.compute[key] = [] for sma_period, deriv_period in val: local_sma = Analytics.moving_average( data=self.compute[enums.ComputeKeys.candle], period=sma_period) self.compute[key].append( Analytics.derivative(data=local_sma, period=deriv_period)) else: continue if key is enums.ComputeKeys.Bollinger: if isinstance(val, list): self.compute[key] = [] for anchor_period, oscillator_period, Bollinger_period in val: local_anchor = Analytics.moving_average( data=self.compute[enums.ComputeKeys.candle], period=anchor_period) local_oscillator = Analytics.moving_average( data=self.compute[enums.ComputeKeys.candle], period=oscillator_period) self.compute[key].append( Analytics.bollinger_bands(data=local_oscillator, average=local_anchor, period=Bollinger_period)) self.analytics_up_to_date = True return True
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