analyze statistical time-series indicators of trading Parameters ---------- df : pandas.DataFrame columns: like ['pa', 'pos', 'ffr', 'deal_amount', 'value']. Necessary fields: - 'pa' is the price advantage in trade indicators - 'pos' i
(df, method="mean")
| 95 | |
| 96 | |
| 97 | def indicator_analysis(df, method="mean"): |
| 98 | """analyze statistical time-series indicators of trading |
| 99 | |
| 100 | Parameters |
| 101 | ---------- |
| 102 | df : pandas.DataFrame |
| 103 | columns: like ['pa', 'pos', 'ffr', 'deal_amount', 'value']. |
| 104 | Necessary fields: |
| 105 | - 'pa' is the price advantage in trade indicators |
| 106 | - 'pos' is the positive rate in trade indicators |
| 107 | - 'ffr' is the fulfill rate in trade indicators |
| 108 | Optional fields: |
| 109 | - 'deal_amount' is the total deal deal_amount, only necessary when method is 'amount_weighted' |
| 110 | - 'value' is the total trade value, only necessary when method is 'value_weighted' |
| 111 | |
| 112 | index: Index(datetime) |
| 113 | method : str, optional |
| 114 | statistics method of pa/ffr, by default "mean" |
| 115 | |
| 116 | - if method is 'mean', count the mean statistical value of each trade indicator |
| 117 | - if method is 'amount_weighted', count the deal_amount weighted mean statistical value of each trade indicator |
| 118 | - if method is 'value_weighted', count the value weighted mean statistical value of each trade indicator |
| 119 | |
| 120 | Note: statistics method of pos is always "mean" |
| 121 | |
| 122 | Returns |
| 123 | ------- |
| 124 | pd.DataFrame |
| 125 | statistical value of each trade indicators |
| 126 | """ |
| 127 | weights_dict = { |
| 128 | "mean": df["count"], |
| 129 | "amount_weighted": df["deal_amount"].abs(), |
| 130 | "value_weighted": df["value"].abs(), |
| 131 | } |
| 132 | if method not in weights_dict: |
| 133 | raise ValueError(f"indicator_analysis method {method} is not supported!") |
| 134 | |
| 135 | # statistic pa/ffr indicator |
| 136 | indicators_df = df[["ffr", "pa"]] |
| 137 | weights = weights_dict.get(method) |
| 138 | res = indicators_df.mul(weights, axis=0).sum() / weights.sum() |
| 139 | |
| 140 | # statistic pos |
| 141 | weights = weights_dict.get("mean") |
| 142 | res.loc["pos"] = df["pos"].mul(weights).sum() / weights.sum() |
| 143 | res = res.to_frame("value") |
| 144 | return res |
| 145 | |
| 146 | |
| 147 | # This is the API for compatibility for legacy code |