virgo-modules 0.0.80__py3-none-any.whl → 0.0.82__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of virgo-modules might be problematic. Click here for more details.

@@ -453,7 +453,7 @@ def ranking(data, weighted_features, top = 5, window = 5):
453
453
  top_up = list(df.sort_values('up_signas', ascending = False).index)[:top]
454
454
  top_low = list(df.sort_values('low_signas', ascending = False).index)[:top]
455
455
 
456
- return top_up, top_low
456
+ return top_up, top_low, df
457
457
 
458
458
  def ranking_first(data, weighted_features, top = 5, window = 5):
459
459
  '''
@@ -2704,7 +2704,7 @@ class signal_analyser_object:
2704
2704
  days_list (list): list of integers [3,8,10] to assess
2705
2705
  threshold (float): alpha or z threshold
2706
2706
  verbose (boolean): print metrics
2707
- signal_position (int): if true, the signal is taken at the given step in the signal
2707
+ signal_position (int): if true, the signal is taken at the given step after the signal end
2708
2708
 
2709
2709
  Returns
2710
2710
  -------
@@ -2721,6 +2721,7 @@ class signal_analyser_object:
2721
2721
  for days in days_list:
2722
2722
 
2723
2723
  feature_ = f'return_{days}d'
2724
+ days = days + signal_position if signal_position else days
2724
2725
  df[feature_] = (df['Close'].shift(-days)/df['Close']-1)*100
2725
2726
  returns_list.append(feature_)
2726
2727
 
@@ -2758,9 +2759,16 @@ class signal_analyser_object:
2758
2759
  p_scores = list()
2759
2760
  medians_down = list()
2760
2761
  validations = list()
2761
- if not signal_position: ### for now it is based on the last signal on a chain
2762
- df_melt = df[df.last_in_chain == True].melt(id_vars=['signal_type'], value_vars=returns_list, var_name='time', value_name='value')
2763
- df_melt = df_melt.dropna()
2762
+
2763
+ if signal_position:
2764
+ df['open_long'] = np.where(df.last_in_chain == True, True, np.nan)
2765
+ df['open_long'] = df['open_long'].shift(signal_position)
2766
+ else:
2767
+ df['open_long'] = np.where(df.last_in_chain == True, True, np.nan)
2768
+
2769
+ # df_melt = df[df.last_in_chain == True].melt(id_vars=['signal_type'], value_vars=returns_list, var_name='time', value_name='value')
2770
+ df_melt = df[df.open_long == True].melt(id_vars=['signal_type'], value_vars=returns_list, var_name='time', value_name='value')
2771
+ df_melt = df_melt.dropna()
2764
2772
 
2765
2773
  for evalx in returns_list:
2766
2774
 
@@ -2794,7 +2802,7 @@ class signal_analyser_object:
2794
2802
  self.mean_median_return = np.nan
2795
2803
 
2796
2804
  df2 = df.copy()
2797
- df2 = df2[df2.last_in_chain == True]
2805
+ df2 = df2[df2.open_long == True]
2798
2806
 
2799
2807
 
2800
2808
  df2['lagdate'] = df2.Date.shift(1)
@@ -2802,12 +2810,17 @@ class signal_analyser_object:
2802
2810
 
2803
2811
  fig, axs = plt.subplots(1, 3, figsize = (15,5))
2804
2812
 
2805
- sns.boxplot(data=df2, y="span",ax = axs[0])
2813
+ sns.violinplot(data=df2, y="span",ax = axs[0], color = 'lightblue', linewidth=0.7,inner="quart")
2814
+ sns.stripplot(data=df2, y="span",ax = axs[0], jitter=True, zorder=1)
2806
2815
  axs[0].set_title('span between last signals')
2807
2816
  del df2
2808
- sns.boxplot(data=df[df.last_in_chain == True], y="internal_rn",ax = axs[1])
2817
+ sns.violinplot(data=df[df.last_in_chain == True], y="internal_rn",ax = axs[1], color = 'lightblue', linewidth=0.7,inner="quart")
2818
+ sns.stripplot(data=df[df.last_in_chain == True], y="internal_rn",ax = axs[1], jitter=True, zorder=1)
2809
2819
  axs[1].set_title('signal duration distribution')
2810
- sns.boxplot(data=df_melt, x="time", y="value", hue="signal_type",ax = axs[2])
2820
+
2821
+ palette ={"go down": "tomato", "go up": "lightblue"}
2822
+ df_melt.signal_type = df_melt.signal_type.map({'up':'go down', 'down': 'go up'})
2823
+ sns.violinplot(data=df_melt, x="time", y="value", hue="signal_type",ax = axs[2], split=True, gap=0.1, inner="quart",palette = palette, linewidth=0.8)
2811
2824
  axs[2].axhline(y=0, color='grey', linestyle='--')
2812
2825
  axs[2].set_title('signal type expected returns distribution at different time lapses')
2813
2826
 
@@ -2830,7 +2843,7 @@ class signal_analyser_object:
2830
2843
  if self.return_fig:
2831
2844
  return fig
2832
2845
 
2833
- def create_backtest_signal(self,days_strategy, test_size, feature_name, high_exit = False, low_exit = False):
2846
+ def create_backtest_signal(self,days_strategy, test_size, feature_name, high_exit = False, low_exit = False, signal_position = False):
2834
2847
  """
2835
2848
  perform backtest signal analysis
2836
2849
 
@@ -2841,7 +2854,8 @@ class signal_analyser_object:
2841
2854
  feature_name (str): name of the feature to assess
2842
2855
  high_exit (float): high exit thrshold return in backtest
2843
2856
  low_exit (float): loss exit thrshold return in backtest
2844
-
2857
+ signal_position (int): if true, the signal is taken at the given step after the signal end
2858
+
2845
2859
  Returns
2846
2860
  -------
2847
2861
  fig (obj): plots
@@ -2887,19 +2901,28 @@ class signal_analyser_object:
2887
2901
  dft['chain_id'] = dft['chain_id'].fillna(method = 'ffill')
2888
2902
 
2889
2903
  dft['internal_rn'] = dft.sort_values(['Date']).groupby(['chain_id']).cumcount() + 1
2890
- dft['flag'] = np.where(dft['internal_rn'] < days_strategy, 1,0)
2891
-
2904
+
2892
2905
  dft['lrets_bench'] = np.log(dft[asset_1]/dft[asset_1].shift(1))
2893
2906
  dft['bench_prod'] = dft['lrets_bench'].cumsum()
2894
2907
  dft['bench_prod_exp'] = np.exp(dft['bench_prod']) - 1
2895
2908
 
2909
+ if signal_position:
2910
+ dft['open_long'] = np.where(dft.last_in_chain == True, True, np.nan)
2911
+ dft['open_long'] = dft.groupby(['chain_id'])['open_long'].shift(signal_position)
2912
+ dft['flag'] = np.where(dft['internal_rn'] < days_strategy + signal_position, 1,0)
2913
+ dft['flag'] = dft.groupby(['chain_id'])['flag'].shift(signal_position)
2914
+ else:
2915
+ dft['open_long'] = np.where(dft.last_in_chain == True, True, np.nan)
2916
+ dft['flag'] = np.where(dft['internal_rn'] < days_strategy, 1,0)
2917
+
2896
2918
  if high_exit and low_exit:
2897
- dft['open_strat'] = np.where(dft.last_in_chain == True, dft.Open, np.nan)
2919
+ dft['open_strat'] = np.where(dft.open_long == True, dft.Open, np.nan)
2898
2920
  dft['open_strat'] = dft['open_strat'].fillna(method = 'ffill')
2899
2921
  dft['open_strat'] = np.where(dft.flag == 1, dft.open_strat, np.nan)
2900
2922
  dft['high_strat_ret'] = (dft['High']/dft['open_strat']-1)*100
2901
2923
  dft['low_strat_ret'] = (dft['Low']/dft['open_strat']-1)*100
2902
- dft['high_exit'] = np.where(((dft['high_strat_ret'] >= high_exit) | (dft['internal_rn'] == days_strategy)), 1, np.nan)
2924
+ dft['max_step_chain'] = dft.groupby(['chain_id'])['internal_rn'].transform('max')
2925
+ dft['high_exit'] = np.where(((dft['high_strat_ret'] >= high_exit) | (dft['internal_rn'] == days_strategy) | (dft['max_step_chain'] == dft['internal_rn'])), 1, np.nan)
2903
2926
  dft['low_exit'] = np.where((dft['low_strat_ret'] <= low_exit), -1, np.nan)
2904
2927
 
2905
2928
  dft["exit_type"] = dft[["high_exit", "low_exit"]].max(axis=1)
@@ -2916,8 +2939,10 @@ class signal_analyser_object:
2916
2939
  dft['exit_step'] = np.where(dft.exit == 1, dft.internal_rn, np.nan)
2917
2940
  dft['exit_step'] = dft.sort_values(['Date']).groupby(['chain_id']).exit_step.transform('max')
2918
2941
 
2919
- dft['flag'] = np.where(dft.internal_rn <= dft.exit_step, 1, 0)
2920
- dft = dft.drop(columns = ['open_strat', 'high_strat_ret', 'low_strat_ret','exit_step', 'exit','exit_type','high_exit','low_exit', 'max_internal_rn'])
2942
+ if signal_position:
2943
+ dft['flag'] = np.where( (dft.internal_rn >= signal_position + 1) & (dft.internal_rn <= dft.exit_step) , 1,0)
2944
+ else:
2945
+ dft['flag'] = np.where(dft.internal_rn <= dft.exit_step, 1, 0)
2921
2946
 
2922
2947
  dft['lrets_strat'] = np.log(dft[asset_1].shift(-1)/dft[asset_1]) * dft['flag']
2923
2948
  dft['lrets_strat'] = np.where(dft['lrets_strat'].isna(),-0.0,dft['lrets_strat'])
@@ -2945,9 +2970,11 @@ class signal_analyser_object:
2945
2970
  print('----------------------------')
2946
2971
 
2947
2972
  fig = plt.figure(1)
2948
- plt.plot(dft.bench_prod_exp.values, label = 'benchmark')
2973
+ plt.plot(dft.bench_prod_exp.values, label = 'benchmark', color = 'steelblue')
2949
2974
  plt.scatter(range(len(dft)),np.where(dft[low_signal] == 1,dft.bench_prod_exp.values,np.nan),color = 'red', label = 'signal')
2950
- plt.plot(dft.strat_prod_exp.values, label = 'strategy')
2975
+ plt.plot(dft.strat_prod_exp.values, label = 'strategy', color = 'darksalmon')
2976
+ plt.xlabel("index")
2977
+ plt.ylabel("comulative return")
2951
2978
  plt.legend()
2952
2979
  plt.title('strategy and cumulative returns based on signal strategy')
2953
2980
  if self.show_plot:
@@ -2958,14 +2985,11 @@ class signal_analyser_object:
2958
2985
  result_plot_name = f'signals_strategy_return_{feature_name}.png'
2959
2986
 
2960
2987
  plt.savefig(self.save_path+result_plot_name)
2961
- # pickle.dump(fig, open(self.save_path+result_plot_name, 'wb'))
2962
2988
 
2963
2989
  with open(self.save_path+result_json_name, "w") as outfile:
2964
2990
  json.dump(messages, outfile)
2965
2991
 
2966
2992
  if self.save_path and self.save_aws:
2967
- # upload_file_to_aws(bucket = 'VIRGO_BUCKET', key = f'market_plots/{self.ticket_name}/'+result_json_name ,input_path = self.save_path+result_json_name)
2968
- # upload_file_to_aws(bucket = 'VIRGO_BUCKET', key = f'market_plots/{self.ticket_name}/'+result_plot_name,input_path = self.save_path+result_plot_name)
2969
2993
 
2970
2994
  upload_file_to_aws(bucket = 'VIRGO_BUCKET', key = self.save_aws + result_json_name, input_path = self.save_path + result_json_name, aws_credentials = self.aws_credentials)
2971
2995
  upload_file_to_aws(bucket = 'VIRGO_BUCKET', key = self.save_aws + result_plot_name, input_path = self.save_path + result_plot_name, aws_credentials = self.aws_credentials)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: virgo-modules
3
- Version: 0.0.80
3
+ Version: 0.0.82
4
4
  Summary: data processing and statistical modeling using stock market data
5
5
  Home-page: https://github.com/miguelmayhem92/virgo_module
6
6
  Author: Miguel Mayhuire
@@ -3,10 +3,10 @@ virgo_modules/src/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU
3
3
  virgo_modules/src/aws_utils.py,sha256=q0l7D7ofo09Lu1QQjv-esheQ06uiSy1Pdq3xMul8zvk,2571
4
4
  virgo_modules/src/edge_utils.py,sha256=ll5pRs9EE20IsE5A1vA589TKzobkeA-b0d68jNTsu1U,13268
5
5
  virgo_modules/src/pull_artifacts.py,sha256=5OPrgR7pcMSdpbevDRhf0ebk7g7ZRjff4NpTIIWAKjE,1989
6
- virgo_modules/src/re_utils.py,sha256=aOWtQTUusAjIYEQdCEWezQbfB4kOOohczIhNbWGne5Q,71586
7
- virgo_modules/src/ticketer_source.py,sha256=4J0y51Tv0dGzFU7qZarGBIJSFsFWfY_NDNWZI6VN2Ws,142729
8
- virgo_modules-0.0.80.dist-info/LICENSE,sha256=pNgFyCYgmimaw0o6V20JupZLROycAnOA_HDDh1tX2V4,1097
9
- virgo_modules-0.0.80.dist-info/METADATA,sha256=8XxXUCdtdAP4PotfMIAFshmWofJ6WKmcnTeNqEPYNUY,1429
10
- virgo_modules-0.0.80.dist-info/WHEEL,sha256=yQN5g4mg4AybRjkgi-9yy4iQEFibGQmlz78Pik5Or-A,92
11
- virgo_modules-0.0.80.dist-info/top_level.txt,sha256=ZjI-qEkDtT-8mFwGAWnXfqPOKEGlIhWRW1es1VyXc60,14
12
- virgo_modules-0.0.80.dist-info/RECORD,,
6
+ virgo_modules/src/re_utils.py,sha256=a7u7ouZ6dKvJZXzgjPLTRARbCapMC_sWf-azhExpMAk,71590
7
+ virgo_modules/src/ticketer_source.py,sha256=nDxBs0YIFwInCvk73PoU8D8oiAxElf3ERbRQXRw8k_M,144162
8
+ virgo_modules-0.0.82.dist-info/LICENSE,sha256=pNgFyCYgmimaw0o6V20JupZLROycAnOA_HDDh1tX2V4,1097
9
+ virgo_modules-0.0.82.dist-info/METADATA,sha256=n-F7T6tpDni4jLj24J_g5K0VfTXKcA_RC_rJVYpufDE,1429
10
+ virgo_modules-0.0.82.dist-info/WHEEL,sha256=yQN5g4mg4AybRjkgi-9yy4iQEFibGQmlz78Pik5Or-A,92
11
+ virgo_modules-0.0.82.dist-info/top_level.txt,sha256=ZjI-qEkDtT-8mFwGAWnXfqPOKEGlIhWRW1es1VyXc60,14
12
+ virgo_modules-0.0.82.dist-info/RECORD,,