如何分离收盘价以仅分析其百分位?并筛选收盘价小数位为0.001-0.009的情况以关联成交量分析
Hey there, let's tackle your two requirements with practical, actionable steps. I'll break this down clearly so you can implement it easily.
1. 分离收盘价数据,提取小数部分用于百分位分析
First, to isolate the decimal portion of your closing prices (for percentile analysis), you have a couple of reliable options depending on your data type:
Math-based extraction (for numeric data):
- Grab the integer part with
int(close_price)ormath.floor(close_price)(safe since closing prices are positive). - Calculate the decimal part using
close_price - int(close_price). - If you need to focus specifically on the hundredth percentile (two decimal places), round the decimal part to two digits:
round(close_price % 1, 2). This makes it easy to group and analyze distributions across different percentile ranges.
- Grab the integer part with
String-based extraction (avoids floating-point precision issues):
- Convert the price to a string and split on the decimal point:
parts = str(close_price).split('.') - If there's no decimal point, the decimal part is
0.0. Otherwise, take the substring after the dot and convert it back to a float (e.g.,float('.' + parts[1])).
- Convert the price to a string and split on the decimal point:
2. 筛选小数部分在0.001-0.009区间的记录并分析成交量
This is the trickier part because floating-point precision can trip you up. Here are two robust methods to identify those $12.001-style prices, followed by how to analyze their corresponding volumes:
Method 1: Use Decimal type for precise comparisons
Floating-point numbers can have hidden precision errors (e.g., 12.001 might be stored as 12.000999999999998). Using Python's Decimal type eliminates this:
from decimal import Decimal import pandas as pd # Assume your data is in a DataFrame with columns 'close' and 'volume' df = pd.read_csv('your_trading_data.csv') def matches_target_range(price): # Convert to Decimal to avoid precision issues price_dec = Decimal(str(price)) decimal_part = price_dec % Decimal('1') # Check if decimal part is between 0.001 and 0.009 inclusive return Decimal('0.001') <= decimal_part <= Decimal('0.009') # Filter the target records target_records = df[df['close'].apply(matches_target_range)]
Method 2: String parsing (直观且精准)
Parsing the price as a string lets you directly inspect the decimal digits:
def matches_target_range_str(price): price_str = str(price) if '.' not in price_str: return False # No decimal part, skip decimal_segment = price_str.split('.')[1] # Pad with zeros to ensure we have at least 3 digits to check padded_decimal = decimal_segment.ljust(3, '0')[:3] # Convert the first three decimal digits to an integer dec_int = int(padded_decimal) # Check if it's between 1 and 9 (corresponds to 0.001-0.009) return 1 <= dec_int <= 9 target_records_str = df[df['close'].apply(matches_target_range_str)]
Analyze the filtered volumes
Once you have your target records, you can run standard volume analyses:
- Calculate key stats:
target_records['volume'].agg(['mean', 'median', 'sum', 'std']) - Compare volume distributions against non-target records (e.g., using histograms or boxplots)
- Look for trends over time (e.g., group by date and calculate daily average volume for target records)
内容的提问来源于stack exchange,提问作者Jose Mozqueda

