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)让上述代码在jupyter里不报错
时间: 2024-02-29 19:51:44 浏览: 116
学习pandas的数据
这段代码的错误可能是由于缺少依赖库或文件。请确保您已经安装了pandas库并且已经将stock_data.csv文件放在正确的位置。另外,您可能需要在Jupyter Notebook中使用`%matplotlib inline`来确保图表正确显示。以下是可能的修改:
```
import pandas as pd
%matplotlib inline
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)
```
阅读全文