if 6 < row_index > 0:
时间: 2023-12-14 19:39:33 浏览: 18
这个语句是错误的,因为 Python 不支持连续比较。如果你想判断 row_index 的值在 0 和 6 之间,正确的写法应该是:
```
if 0 < row_index < 6:
# 执行操作
```
这样写会首先判断 row_index 是否大于 0,如果是,则继续判断是否小于 6,如果也满足,则执行操作。
相关问题
if 0 < row_index < 4 or 8 <= row_index <= 10:
This conditional statement checks if the value of `row_index` is greater than 0 and less than 4, or if it is greater than or equal to 8 and less than or equal to 10. If `row_index` satisfies either of these conditions, the statement will evaluate to `True`, otherwise it will evaluate to `False`.
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里不报错
这段代码的错误可能是由于缺少依赖库或文件。请确保您已经安装了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)
```
阅读全文