From 2943d92f82554e3237aa8f377da942e6662f0013 Mon Sep 17 00:00:00 2001 From: jack Date: Wed, 26 Nov 2025 16:07:55 +0800 Subject: [PATCH] update --- Users:.fileloc | 8 ++ config.json => ai_config.json | 0 ai_result/20251124170421.txt | 5 - ai_result/20251124172559.txt | 5 - ai_result/20251124174559.txt | 5 - ai_result/20251124175745.txt | 20 --- ai_result/20251124180630.txt | 18 --- prompt.txt => alpha_prompt.txt | 7 +- generated_alpha/20251126160706.txt | 5 + main.py | 54 ++++++-- wqb_operator.txt | 197 +++++++++++++++++++++++++++++ 11 files changed, 259 insertions(+), 65 deletions(-) create mode 100644 Users:.fileloc rename config.json => ai_config.json (100%) delete mode 100644 ai_result/20251124170421.txt delete mode 100644 ai_result/20251124172559.txt delete mode 100644 ai_result/20251124174559.txt delete mode 100644 ai_result/20251124175745.txt delete mode 100644 ai_result/20251124180630.txt rename prompt.txt => alpha_prompt.txt (94%) create mode 100644 generated_alpha/20251126160706.txt create mode 100644 wqb_operator.txt diff --git a/Users:.fileloc b/Users:.fileloc new file mode 100644 index 0000000..69e37fb --- /dev/null +++ b/Users:.fileloc @@ -0,0 +1,8 @@ + + + + + URL + file:///Users/jack/source/mySpace/mycode/my_project/py/wqb/generated_alpha + + diff --git a/config.json b/ai_config.json similarity index 100% rename from config.json rename to ai_config.json diff --git a/ai_result/20251124170421.txt b/ai_result/20251124170421.txt deleted file mode 100644 index 7a1fcd0..0000000 --- a/ai_result/20251124170421.txt +++ /dev/null @@ -1,5 +0,0 @@ -你好!😊 很高兴见到你! - -看起来你在问候后面加了一些空格,不管怎样,我都很开心能和你聊天!我是DeepSeek,一个热心的AI助手,随时准备为你提供帮助。 - -无论你想聊什么话题,需要解答问题,还是想要一些建议,我都会尽我所能地帮助你。今天有什么特别想了解或讨论的吗?✨ \ No newline at end of file diff --git a/ai_result/20251124172559.txt b/ai_result/20251124172559.txt deleted file mode 100644 index 1b7955e..0000000 --- a/ai_result/20251124172559.txt +++ /dev/null @@ -1,5 +0,0 @@ -rank(((ts_rank(close,10)-ts_rank(close,50)))*rank(ts_rank(volume,5))) -rank(ts_corr(ts_rank(close,20),ts_rank(volume,5),10)) --1*rank((close-ts_mean(close,10))/ts_std_dev(close,10)*ts_mean(volume,30)) -rank(ts_delta(close,15)*rank(ts_corr((high+low)/2,volume,15))) --1*rank(ts_rank(close,5)-ts_rank(close,60))*rank(ts_std_dev(close,20)*ts_mean(volume,40)) \ No newline at end of file diff --git a/ai_result/20251124174559.txt b/ai_result/20251124174559.txt deleted file mode 100644 index 56b845c..0000000 --- a/ai_result/20251124174559.txt +++ /dev/null @@ -1,5 +0,0 @@ -ts_mean(group_rank(ts_mean(return_60(close), 20)), 5) / ts_std_dev(return_60(close), 20) -group_rank(ts_rank(ts_mean(close/vwap, 60), 20)) - group_rank(ts_rank(close/vwap, 5)) -ts_delay(group_rank(ts_corr(ts_mean(close, 5), ts_mean(volume, 5), 10)), 5) * ts_mean(return_20(close), 5) -group_rank(ts_mean(return_20(close), 10)) / group_rank(ts_std_dev(return_5(close), 20)) -group_rank(ts_mean(return_10(close), 5)) - group_mean(ts_mean(return_10(close), 5)) \ No newline at end of file diff --git a/ai_result/20251124175745.txt b/ai_result/20251124175745.txt deleted file mode 100644 index 2f0d1b2..0000000 --- a/ai_result/20251124175745.txt +++ /dev/null @@ -1,20 +0,0 @@ - - -Here are the five industry factors using WebSim functions: - -```python -group_mean(ts_return(close, 252), industry) # Industry Momentum Factor -group_mean((close / ts_mean(close, 252) - 1), industry) # Valuation Repair Factor -group_mean(ts_sum((close - ts_lag(close, 1)) * volume, 21), industry) # Capital Flow Factor -group_mean(ts_std(ts_return(close, 1), 21), industry) # Volatility Adjustment Factor -group_mean(ts_rank(close, 252), industry) # Relative Strength Factor -``` - -These factors: -1. Measure industry momentum using 12-month returns -2. Evaluate valuation through 200-day mean reversion -3. Track capital flow with 1-month price-volume accumulation -4. Assess volatility risk with 21-day standard deviation -5. Gauge cross-sectional strength via 12-month price ranking - -Each is calculated by averaging stock-level metrics within industry groups using `group_mean()` for industry-specific signals. \ No newline at end of file diff --git a/ai_result/20251124180630.txt b/ai_result/20251124180630.txt deleted file mode 100644 index f697544..0000000 --- a/ai_result/20251124180630.txt +++ /dev/null @@ -1,18 +0,0 @@ - - -```python -# 行业动量因子(相对强度) -group_rank(ts_mean(close / close[3], 3), 'industry') - -# 行业估值修复因子(均值回归) -group_rank(ts_mean(close, 3) / close, 'industry') - -# 行业资金流向因子(量价结合) -group_rank(ts_sum(volume * (close - close[1]), 3), 'industry') - -# 行业波动率特征因子(风险调整) -group_rank(1 / (ts_std(close / close[1], 3) + 1e-8), 'industry') - -# 行业间相对强度因子(横截面) -group_rank(close / ts_max(close, 3), 'industry') -``` \ No newline at end of file diff --git a/prompt.txt b/alpha_prompt.txt similarity index 94% rename from prompt.txt rename to alpha_prompt.txt index 11e72c8..cc4618e 100644 --- a/prompt.txt +++ b/alpha_prompt.txt @@ -28,10 +28,11 @@ - 提供经济周期适应的解释 **输出格式**(一行一个, 不要输出多余的东西): -[表达式] -[表达式] -[表达式] +表达式 +表达式 +表达式 ... +表达式 请提供具体的WQ表达式。 重申:请确保所有表达式都使用WorldQuant WebSim平台函数,不要使用pandas、numpy或其他Python库函数。输出必须是一行有效的WQ表达式。 \ No newline at end of file diff --git a/generated_alpha/20251126160706.txt b/generated_alpha/20251126160706.txt new file mode 100644 index 0000000..bae9abe --- /dev/null +++ b/generated_alpha/20251126160706.txt @@ -0,0 +1,5 @@ +group_rank(ts_delta(close, 20), sector) +group_zscore(ts_mean(close, 60) / ts_mean(close, 180), industry) +group_scale(ts_sum(volume * vwap, 20) / ts_sum(volume, 20), sector) +group_neutralize(ts_std_dev(close, 30), industry) +group_rank(ts_rank(close, 60), sector) - group_rank(ts_rank(close, 180), sector) \ No newline at end of file diff --git a/main.py b/main.py index 5049549..6ac00e1 100644 --- a/main.py +++ b/main.py @@ -1,10 +1,12 @@ +# -*- coding: utf-8 -*- import os import json import openai +import httpx from datetime import datetime -def load_config(config_file="config.json"): +def load_config(config_file="ai_config.json"): try: with open(config_file, 'r', encoding='utf-8') as f: return json.load(f) @@ -17,27 +19,37 @@ def load_config(config_file="config.json"): def create_prompt_file(): - if not os.path.exists("prompt.txt"): - with open("prompt.txt", 'w', encoding='utf-8') as f: + if not os.path.exists("alpha_prompt.txt"): + with open("alpha_prompt.txt", 'w', encoding='utf-8') as f: f.write("") - print("已创建prompt.txt,请填入提示词后重新运行") + print("已创建alpha_prompt.txt,请填入提示词后重新运行") return False return True def read_prompt(): - with open("prompt.txt", 'r', encoding='utf-8') as f: + with open("alpha_prompt.txt", 'r', encoding='utf-8') as f: prompt = f.read().strip() if not prompt: - print("prompt.txt是空的,请填入提示词") + print("alpha_prompt.txt是空的,请填入提示词") return None return prompt +def read_operator(): + with open("wqb_operator.txt", 'r', encoding='utf-8') as f: + operator = f.read().strip() + if not operator: + print("operator.txt是空的,请填入操作符") + return None + return operator + def create_result_folder(): - if not os.path.exists("ai_result"): - os.makedirs("ai_result") - return "ai_result" + # 修改这里:创建 generated_alpha 文件夹 + folder_name = "generated_alpha" + if not os.path.exists(folder_name): + os.makedirs(folder_name) + return folder_name def call_siliconflow(api_key, prompt, model, base_url): @@ -90,8 +102,27 @@ def get_ai_config(config): return service_name, service_config +def get_user_info(): + # 获取用户信息 + with open('ai_config.json', 'r') as f: + config = json.load(f) + siliconflow = config['siliconflow'] + token = siliconflow['api_keys'] + + headers = {"Authorization": f"Bearer {token}"} + + url = "https://api.siliconflow.cn/v1/user/info" + + response = httpx.get(url, headers=headers) + + data = response.json()['data'] + balance = data['balance'] + print(f"余额: {balance}") + def main(): + get_user_info() + config = load_config() if not config: return @@ -107,6 +138,11 @@ def main(): if not config_result: return + operator = read_operator() + + if operator: + prompt = prompt + "\n\n以下是我的账号有权限使用的操作符以及操作符的使用方法, 请严格按照操作符, 进行生成,组合因子\n\n" + operator + service_name, service_config = config_result print(f"使用服务: {service_name}") diff --git a/wqb_operator.txt b/wqb_operator.txt new file mode 100644 index 0000000..e4cf760 --- /dev/null +++ b/wqb_operator.txt @@ -0,0 +1,197 @@ +Operator: abs(x) +Description: Absolute value of x + +Operator: add(x, y, filter = false), x + y +Description: Add all inputs (at least 2 inputs required). If filter = true, filter all input NaN to 0 before adding + +Operator: densify(x) +Description: Converts a grouping field of many buckets into lesser number of only available buckets so as to make working with grouping fields computationally efficient + +Operator: divide(x, y), x / y +Description: x / y + +Operator: inverse(x) +Description: 1 / x + +Operator: log(x) +Description: Natural logarithm. For example: Log(high/low) uses natural logarithm of high/low ratio as stock weights. + +Operator: max(x, y, ..) +Description: Maximum value of all inputs. At least 2 inputs are required + +Operator: min(x, y ..) +Description: Minimum value of all inputs. At least 2 inputs are required + +Operator: multiply(x ,y, ... , filter=false), x * y +Description: Multiply all inputs. At least 2 inputs are required. Filter sets the NaN values to 1 + +Operator: power(x, y) +Description: x ^ y + +Operator: reverse(x) +Description: - x + +Operator: sign(x) +Description: if input > 0, return 1; if input < 0, return -1; if input = 0, return 0; if input = NaN, return NaN; + +Operator: signed_power(x, y) +Description: x raised to the power of y such that final result preserves sign of x + +Operator: sqrt(x) +Description: Square root of x + +Operator: subtract(x, y, filter=false), x - y +Description: x-y. If filter = true, filter all input NaN to 0 before subtracting + +Operator: and(input1, input2) +Description: Logical AND operator, returns true if both operands are true and returns false otherwise + +Operator: if_else(input1, input2, input 3) +Description: If input1 is true then return input2 else return input3. + +Operator: input1 < input2 +Description: If input1 < input2 return true, else return false + +Operator: input1 <= input2 +Description: Returns true if input1 <= input2, return false otherwise + +Operator: input1 == input2 +Description: Returns true if both inputs are same and returns false otherwise + +Operator: input1 > input2 +Description: Logic comparison operators to compares two inputs + +Operator: input1 >= input2 +Description: Returns true if input1 >= input2, return false otherwise + +Operator: input1!= input2 +Description: Returns true if both inputs are NOT the same and returns false otherwise + +Operator: is_nan(input) +Description: If (input == NaN) return 1 else return 0 + +Operator: not(x) +Description: Returns the logical negation of x. If x is true (1), it returns false (0), and if input is false (0), it returns true (1). + +Operator: or(input1, input2) +Description: Logical OR operator returns true if either or both inputs are true and returns false otherwise + +Operator: days_from_last_change(x) +Description: Amount of days since last change of x + +Operator: hump(x, hump = 0.01) +Description: Limits amount and magnitude of changes in input (thus reducing turnover) + +Operator: kth_element(x, d, k) +Description: Returns K-th value of input by looking through lookback days. This operator can be used to backfill missing data if k=1 + +Operator: last_diff_value(x, d) +Description: Returns last x value not equal to current x value from last d days + +Operator: ts_arg_max(x, d) +Description: Returns the relative index of the max value in the time series for the past d days. If the current day has the max value for the past d days, it returns 0. If previous day has the max value for the past d days, it returns 1 + +Operator: ts_arg_min(x, d) +Description: Returns the relative index of the min value in the time series for the past d days; If the current day has the min value for the past d days, it returns 0; If previous day has the min value for the past d days, it returns 1. + +Operator: ts_av_diff(x, d) +Description: Returns x - tsmean(x, d), but deals with NaNs carefully. That is NaNs are ignored during mean computation + +Operator: ts_backfill(x,lookback = d, k=1, ignore="NAN") +Description: Backfill is the process of replacing the NAN or 0 values by a meaningful value (i.e., a first non-NaN value) + +Operator: ts_corr(x, y, d) +Description: Returns correlation of x and y for the past d days + +Operator: ts_count_nans(x ,d) +Description: Returns the number of NaN values in x for the past d days + +Operator: ts_covariance(y, x, d) +Description: Returns covariance of y and x for the past d days + +Operator: ts_decay_linear(x, d, dense = false) +Description: Returns the linear decay on x for the past d days. Dense parameter=false means operator works in sparse mode and we treat NaN as 0. In dense mode we do not. + +Operator: ts_delay(x, d) +Description: Returns x value d days ago + +Operator: ts_delta(x, d) +Description: Returns x - ts_delay(x, d) + +Operator: ts_mean(x, d) +Description: Returns average value of x for the past d days. + +Operator: ts_product(x, d) +Description: Returns product of x for the past d days + +Operator: ts_quantile(x,d, driver="gaussian" ) +Description: It calculates ts_rank and apply to its value an inverse cumulative density function from driver distribution. Possible values of driver (optional ) are "gaussian", "uniform", "cauchy" distribution where "gaussian" is the default. + +Operator: ts_rank(x, d, constant = 0) +Description: Rank the values of x for each instrument over the past d days, then return the rank of the current value + constant. If not specified, by default, constant = 0. + +Operator: ts_regression(y, x, d, lag = 0, rettype = 0) +Description: Returns various parameters related to regression function + +Operator: ts_scale(x, d, constant = 0) +Description: Returns (x - ts_min(x, d)) / (ts_max(x, d) - ts_min(x, d)) + constant. This operator is similar to scale down operator but acts in time series space + +Operator: ts_std_dev(x, d) +Description: Returns standard deviation of x for the past d days + +Operator: ts_step(1) +Description: Returns days' counter + +Operator: ts_sum(x, d) +Description: Sum values of x for the past d days. + +Operator: ts_zscore(x, d) +Description: Z-score is a numerical measurement that describes a value's relationship to the mean of a group of values. Z-score is measured in terms of standard deviations from the mean: (x - tsmean(x,d)) / tsstddev(x,d). This operator may help reduce outliers and drawdown. + +Operator: normalize(x, useStd = false, limit = 0.0) +Description: Calculates the mean value of all valid alpha values for a certain date, then subtracts that mean from each element + +Operator: quantile(x, driver = gaussian, sigma = 1.0) +Description: Rank the raw vector, shift the ranked Alpha vector, apply distribution (gaussian, cauchy, uniform). If driver is uniform, it simply subtract each Alpha value with the mean of all Alpha values in the Alpha vector + +Operator: rank(x, rate=2) +Description: Ranks the input among all the instruments and returns an equally distributed number between 0.0 and 1.0. For precise sort, use the rate as 0 + +Operator: scale(x, scale=1, longscale=1, shortscale=1) +Description: Scales input to booksize. We can also scale the long positions and short positions to separate scales by mentioning additional parameters to the operator + +Operator: winsorize(x, std=4) +Description: Winsorizes x to make sure that all values in x are between the lower and upper limits, which are specified as multiple of std. + +Operator: zscore(x) +Description: Z-score is a numerical measurement that describes a value's relationship to the mean of a group of values. Z-score is measured in terms of standard deviations from the mean + +Operator: vec_avg(x) +Description: Taking mean of the vector field x + +Operator: vec_sum(x) +Description: Sum of vector field x + +Operator: bucket(rank(x), range="0, 1, 0.1" or buckets = "2,5,6,7,10") +Description: Convert float values into indexes for user-specified buckets. Bucket is useful for creating group values, which can be passed to GROUP as input + +Operator: trade_when(x, y, z) +Description: Used in order to change Alpha values only under a specified condition and to hold Alpha values in other cases. It also allows to close Alpha positions (assign NaN values) under a specified condition + +Operator: group_backfill(x, group, d, std = 4.0) +Description: If a certain value for a certain date and instrument is NaN, from the set of same group instruments, calculate winsorized mean of all non-NaN values over last d days + +Operator: group_mean(x, weight, group) +Description: All elements in group equals to the mean + +Operator: group_neutralize(x, group) +Description: Neutralizes Alpha against groups. These groups can be subindustry, industry, sector, country or a constant + +Operator: group_rank(x, group) +Description: Each elements in a group is assigned the corresponding rank in this group + +Operator: group_scale(x, group) +Description: Normalizes the values in a group to be between 0 and 1. (x - groupmin) / (groupmax - groupmin) + +Operator: group_zscore(x, group) +Description: Calculates group Z-score - numerical measurement that describes a value's relationship to the mean of a group of values. Z-score is measured in terms of standard deviations from the mean. zscore = (data - mean) / stddev of x for each instrument within its group. \ No newline at end of file