import pandas as pd df = pd.read_csv('stock_data.csv') df['four_days_increase'] = df['close'].rolling(window=4).apply(lambda x: all(x[i] < x[i+1] for i in range(3))) * 1 df['three_days_decrease'] = df['close'].rolling(window=3).apply(lambda x: all(x[i] > x[i+1] for i in range(2))) * 1 capital = 1000000 max_stock_per_day = 10 max_stock_value = 100000 start_date = '2020-01-01' end_date = '2023-01-01' df = df[(df['date'] >= start_date) & (df['date'] < end_date)] df = df.reset_index(drop=True) hold_stock = [] for i, row in df.iterrows(): if len(hold_stock) > 0: sell_stock = [] for stock in hold_stock: if i - stock['buy_day'] >= 3: capital += stock['buy_price'] * stock['buy_qty'] * (1 - 0.002) sell_stock.append(stock) hold_stock = [stock for stock in hold_stock if stock not in sell_stock] df_today = df.loc[i:i+3] if i + 3 >= len(df): break if all(df_today['four_days_increase']) and all(df_today['three_days_decrease'].iloc[1:]): available_capital = capital available_stock = max_stock_per_day available_value = max_stock_value for j, stock_row in df_today.iterrows(): if available_capital > 0 and available_stock > 0 and available_value > 0: buy_qty = min(int(available_capital / (stock_row['close'] * 1.002)), available_stock, int(available_value / (stock_row['close'] * 1.002))) if buy_qty > 0: hold_stock.append({'buy_day': i, 'buy_price': stock_row['close'], 'buy_qty': buy_qty}) available_capital -= stock_row['close'] * buy_qty * 1.002 available_stock -= 1 available_value -= stock_row['close'] * buy_qty * 1.002 print('Final capital:', capital)修改上述指标使在聚宽中运行
时间: 2024-02-07 10:03:29 浏览: 148
这段代码主要是用来模拟股票交易的,包含了读取股票数据、计算股票涨跌幅、买卖股票等功能。但是这段代码是使用 pandas 库,需要在聚宽中进行修改才能运行。
首先,需要将 pandas 库替换成聚宽中的 jqdata 库。聚宽中的 jqdata 库可以用来获取股票数据。可以使用以下代码进行导入:
```python
from jqdata import get_all_trade_days, get_price
```
接下来,需要将读取股票数据的代码进行修改,改为从 jqdata 中获取股票数据。可以使用以下代码获取股票数据:
```python
start_date = '2020-01-01'
end_date = '2023-01-01'
df = get_price('000001.XSHG', start_date=start_date, end_date=end_date, frequency='1d', fields=['open', 'close', 'high', 'low', 'volume'])
df = df.reset_index()
df = df.rename(columns={'index': 'date'})
```
这段代码中,'000001.XSHG' 是股票代码,可以根据需要修改。'start_date' 和 'end_date' 分别为数据开始和结束的日期。'frequency' 表示数据频率,'1d' 表示每日数据。'fields' 表示需要获取的数据字段,包括开盘价、收盘价、最高价、最低价和成交量。
接下来,需要将计算股票涨跌幅的代码进行修改。可以使用以下代码进行计算:
```python
df['four_days_increase'] = df['close'].rolling(window=4).apply(lambda x: all(x[i] < x[i+1] for i in range(3))) * 1
df['three_days_decrease'] = df['close'].rolling(window=3).apply(lambda x: all(x[i] > x[i+1] for i in range(2))) * 1
```
这段代码中,'rolling' 函数可以计算滑动窗口内的统计值。'window' 表示窗口大小,这里分别为 4 和 3。lambda 函数用于判断股票涨跌情况,返回值为 0 或 1。
最后,需要将买卖股票的代码进行修改。可以使用以下代码进行买卖:
```python
capital = 1000000
max_stock_per_day = 10
max_stock_value = 100000
hold_stock = []
trade_days = get_all_trade_days()
for i in range(len(trade_days)):
if trade_days[i] >= start_date and trade_days[i] < end_date:
if len(hold_stock) > 0:
sell_stock = []
for stock in hold_stock:
if i - stock['buy_day'] >= 3:
capital += stock['buy_price'] * stock['buy_qty'] * (1 - 0.002)
sell_stock.append(stock)
hold_stock = [stock for stock in hold_stock if stock not in sell_stock]
df_today = df[df['date'] == trade_days[i]]
if len(df_today) == 0:
continue
if all(df[df['date'].isin(trade_days[i:i+4])]['four_days_increase']) and all(df[df['date'].isin(trade_days[i+1:i+4])]['three_days_decrease']):
available_capital = capital
available_stock = max_stock_per_day
available_value = max_stock_value
for j in range(len(df_today)):
if available_capital > 0 and available_stock > 0 and available_value > 0:
buy_qty = min(int(available_capital / (df_today.iloc[j]['close'] * 1.002)), available_stock, int(available_value / (df_today.iloc[j]['close'] * 1.002)))
if buy_qty > 0:
hold_stock.append({'buy_day': i, 'buy_price': df_today.iloc[j]['close'], 'buy_qty': buy_qty})
available_capital -= df_today.iloc[j]['close'] * buy_qty * 1.002
available_stock -= 1
available_value -= df_today.iloc[j]['close'] * buy_qty * 1.002
print('Final capital:', capital)
```
这段代码中,'get_all_trade_days' 函数可以获取所有交易日的日期。使用循环遍历每个交易日,进行买卖操作。买卖股票的逻辑与原代码相同,但需要注意一些细节的修改,例如日期的获取、数据的索引方式等。
最后,可以将以上代码整合到一个 Python 文件中,在聚宽中运行即可。
阅读全文