def get_indicator_bins_frequency(indicator_df, outcomes, direction, bins=60, rank=False): coll = [] indexes = [] if not rank: for window in indicator_df.columns.tolist(): indicator = indicator_df.loc[window:, window].values arange = np.arange(indicator_df.shape[0]) hist = np.histogram(indicator, bins=bins) for i in range(bins): start = hist[1][i] end = hist[1][i + 1] if direction == 'long': index = indicator >= start else: index = indicator <= end index = np.insert(index, 0, [False] * window) group_count = get_groups(arange[index], 100).shape[0] shape = index.sum() outcomes_mean = outcomes.loc[index].mean().tolist() for each in list(zip(outcomes.columns.tolist(), outcomes_mean)): coll.append([ window, each[0], hist[1][i], i, shape, group_count, each[1] ]) indexes.append(index) # Binned Freq Results Analysis DF columns = [ 'window', 'target', 'slice', 'bin', 'shape', 'groups', 'perc' ] coll = np.array(coll) results = pd.DataFrame(coll.T, columns).T # Set Location and final currency index] return {'results': results, 'indexes': indexes} else: for window in indicator_df.columns.tolist(): val_counts = list( zip(indicator_df.loc[:, window].value_counts().index, indicator_df.loc[:, window].value_counts().values)) for vc in val_counts: index = indicator_df.loc[indicator_df.loc[:, window] \ == vc[0], window].index.values group_count = get_groups(index, 100).shape[0] shape = vc[1] long_mean = outcomes.loc[index].mean().tolist() for each in list(zip(outcomes.columns.tolist(), long_mean)): coll.append( [window, each[0], vc[0], shape, group_count, each[1]]) indexes.append(index) # Binned Freq Results Analysis DF columns = ['window', 'target', 'rank', 'shape', 'groups', 'perc'] coll = np.array(coll) results = pd.DataFrame(coll.T, columns).T # Set Location and final currency index] return {'results': results, 'indexes': indexes}
def get_indicator_bins_frequency(indicator_df, outcomes): coll = [] indexes = [] for window in indicator_df.columns.tolist(): indicator = indicator_df.loc[window:, window].values arange = np.arange(indicator_df.shape[0]) hist = np.histogram(indicator, bins=bins) for i in range(bins): start = hist[1][i] # end = hist[1][i+1] cond1 = indicator >= start cond2 = True # indicator <= end index = cond1 & cond2 index = np.insert(index, 0, [False] * window) group_count = get_groups(arange[index], 100).shape[0] shape = index.sum() long_mean = outcomes.loc[index].mean().tolist() for each in list(zip(outcomes.columns.tolist(), long_mean)): coll.append([window, each[0], i, shape, group_count, each[1]]) indexes.append(index) # Binned Freq Results Analysis DF columns = ['window', 'target', 'bin', 'shape', 'groups', 'perc'] coll = np.array(coll) results = pd.DataFrame(coll.T, columns).T # Export and return results.to_csv('/Users/user/Desktop/ratio.csv') # Set Location and final currency index] return { 'results': results, 'indexes': indexes }
def analyze_results(top_or_bottom, results, outcomes, _filter, group_interval, use_groupings, position_filter=100, range_filter=-1, filter_on_slope=False): ''' More or less verified ''' df = [] res = results.copy() if top_or_bottom == 'top': breakouts = res[(res.channel_closing_position > _filter)].index elif top_or_bottom == 'bottom': breakouts = res[(res.channel_closing_position < _filter)].index # Get only first instance in a group of breakouts (defined on interval) if use_groupings: breakouts = get_groups(breakouts, group_interval) # for all combinations of up down outcome pairs for t in outcomes.filter(regex='t').columns: for l in outcomes.filter(regex='l').columns: # Assess bottom breakout outcomes on columns pair breakout_results = outcomes.loc[breakouts].copy() wins = (breakout_results[t] < breakout_results[l]).mean() # Colect results into DataFrame df.append([t, l, wins]) # Assemble analysis into dataframes columns = ['target', 'loss', 'wins'] df = pd.DataFrame(np.array(df), columns=columns) df = df.apply(pd.to_numeric, errors='ignore') # Calculate expected values df['ex'] = (df.wins * df.target.str[1:].astype(int)) \ - (df.loss.str[1:].astype(int) * (1 - df.wins)) df['total'] = df['ex'] * breakouts.shape[0] return df, breakouts
index_2, threshold_2 = main(currency, bounded_outcomes, direction, linear_lookback, slope_lookback, mask_threshold, boundary_threshold ) # Measure Combined Results on Instrument instrument = eur_usd combined_outcomes = eur_usd_long # How did we do ? combined_indexes = np.intersect1d(index_1, index_2) groups = get_groups(combined_indexes, 120) print('Placements: {}'.format(combined_indexes.shape[0])) print('groups: {}'.format(groups.shape[0])) print('\n Winnings from all placements\n----------------------------') print(combined_outcomes.loc[combined_indexes].mean()) print('\n Winnings from groups\n----------------------------') print(combined_outcomes.loc[groups].mean()) # Plot distribution plot_index_distribution(combined_indexes, 'combined') plot_index_distribution(groups, 'groups') # Plot instrument ############################################################################### # Go through without functions - keep all in memory. Assemble DF.
# Analysis ############################################################################### ''' Currenlty using GROUP start instead of all\ ''' filter_on_slope = True analysis = [] group_gap_length = window top_filter = .7 bottom_filter = .3 # Get all values from results where closing position is outside of top_breakouts = results[results.channel_closing_position > top_filter].index bottom_breakouts = results[results.channel_closing_position < bottom_filter].index top_groups = get_groups(top_breakouts, group_gap_length) bottom_groups = get_groups(bottom_breakouts, group_gap_length) # Filter on slope if filter_on_slope: slopes_up = (results.closings_slope > 0).index slopes_down = (results.closings_slope < 0).index top_groups = np.intersect1d(top_groups, slopes_up) bottom_groups = np.intersect1d(bottom_groups, slopes_down) top_breakouts = np.intersect1d(top_breakouts, slopes_up) bottom_breakouts = np.intersect1d(bottom_breakouts, slopes_down) for d in ['d1', 'd2', 'd3'] : for u in ['u1', 'u2', 'u3']: # Assess top breakouts breakout_results = bars.loc[top_groups] top_down_wins = (breakout_results[d] < breakout_results[u]).mean() top_up_wins = (breakout_results[d] > breakout_results[u]).mean()
''' Assemble into df for hand checking, etc. ''' df = pd.DataFrame(currency).join(step_distances, lsuffix= '.')\ .join(masked_steps, lsuffix = '.') df.columns = ['eur', 'steps', 'masked_steps'] df['slopes'] = slopes df['outcomes'] = bounded_outcomes.iloc[:, 2] start = 1000 end = 10000 groups = get_groups(df.loc[(df.slopes > threshold)].index.values, 120) print('Placement Shape and Win %: \t{}\t{:.2f}'.format(df.loc[df.slopes > threshold, 'outcomes'].shape[0], df.loc[df.slopes > threshold, 'outcomes'].mean())) print('Group Shape and win % : \t{}\t{:.2f}'.format(groups.shape[0], df.loc[groups, 'outcomes'].mean())) print('Placement Shape and Win % in first start: end: \t{}\t{:.2f}'.format(df.loc[start:end].loc[df.slopes > threshold, 'outcomes'].shape[0], df.loc[start:end].loc[df.slopes > threshold, 'outcomes'].mean())) df.loc[start:end, ['steps', 'masked_steps']].plot() win_plot_index = df.loc[start:end].loc[(df.slopes > threshold) & (df.outcomes == True)].index.values lose_plot_index = df.loc[start:end].loc[(df.slopes > threshold) & (df.outcomes == False)].index.values plt.plot(win_plot_index, np.ones(win_plot_index.shape[0]) * .001, 'o', color='green') plt.plot(lose_plot_index, np.ones(lose_plot_index.shape[0]) * .001, 'o', color='red') plt.figure() currency.loc[start:end].plot() plot_index_distribution(df.loc[df.slopes > threshold].index.values, 'placement distribution')