stock_code = ["股票__{}".format(i+1) for i in range(stock_rise.shape[0])]
时间: 2024-03-04 13:54:09 浏览: 23
这段代码是用来生成一个股票代码的列表,其中:
- `stock_rise.shape[0]` 表示股票数据的行数,即股票的数量
- `["股票__{}".format(i+1) for i in range(stock_rise.shape[0])]` 表示通过循环遍历股票数量的范围,生成一个列表,其中每个元素为 `"股票__i"` 的格式化字符串,其中 `i` 从 1 开始递增,一直到股票数量。这样就可以得到一个形如 `["股票__1", "股票__2", ...]` 的列表,其中包含了所有股票的代码。
相关问题
pro = ts.pro_api() df = pro.daily(fields = 'ts_code,trade_date,open,close') def get_stock_poll(df): stock_pool=[] for code in df['ts_code'].unique(): temp_df = df[df['ts_code'] == code ] for i in range(len(temp_df)-3): if (temp_df.iloc[i+3]['close']>temp_df.iloc[i+2]['close']) and\(temp_df.iloc[i+2]['close']>temp_df.iloc[i+2]['open']) and\(temp_df.iloc[i+1]['close']>temp_df.iloc[i+1]['open']) and\(temp_df.iloc[i]['close']>temp_df.iloc[i]['open']) stock_pool.append(code) break return stock_pool def buy_stock(stock_pool,df): buy_list = [] for code in stock_pool: temp_df = df[df['ts_code']==code] for i in range(len(temp_df-2)): if(temp_df.iloc[i + 2]['close'] < temp_df.iloc[i + 2]['open']) and \ (temp_df.iloc[i + 1]['close'] < temp_df.iloc[i + 1]['open']) and \ (temp_df.iloc[i]['close'] < temp_df.iloc[i]['open'])and \ (temp_df.iloc[i + 2]['close'] < temp_df.iloc[i + 1]['close'])and \ (temp_df.iloc[i + 1]['close'] < temp_df.iloc[i]['close']): buy_list.append(code) break return buy_list def sell_stock(buy_stock,df): sell_list = [] for stock in buy_list: buy_date=dateime.datetime.strptime(stock[1],"%Y-%m-%d") current_date = datetime.datetime.today() days_held = (current_date - buy_date).days if days_held >=3: sell_list.append(stock[0]) print("sell list:",sell_list)修改上述代码
以下是修改后的代码:
```python
import datetime
pro = ts.pro_api()
df = pro.daily(fields='ts_code,trade_date,open,close')
def get_stock_pool(df):
stock_pool = []
for code in df['ts_code'].unique():
temp_df = df[df['ts_code'] == code]
for i in range(len(temp_df)-3):
if (temp_df.iloc[i+3]['close'] > temp_df.iloc[i+2]['close']) and \
(temp_df.iloc[i+2]['close'] > temp_df.iloc[i+2]['open']) and \
(temp_df.iloc[i+1]['close'] > temp_df.iloc[i+1]['open']) and \
(temp_df.iloc[i]['close'] > temp_df.iloc[i]['open']):
stock_pool.append(code)
break
return stock_pool
def buy_stock(stock_pool, df):
buy_list = []
for code in stock_pool:
temp_df = df[df['ts_code'] == code]
for i in range(len(temp_df)-2):
if (temp_df.iloc[i+2]['close'] < temp_df.iloc[i+2]['open']) and \
(temp_df.iloc[i+1]['close'] < temp_df.iloc[i+1]['open']) and \
(temp_df.iloc[i]['close'] < temp_df.iloc[i]['open']) and \
(temp_df.iloc[i+2]['close'] < temp_df.iloc[i+1]['close']) and \
(temp_df.iloc[i+1]['close'] < temp_df.iloc[i]['close']):
buy_list.append((code, temp_df.iloc[i+2]['trade_date']))
break
return buy_list
def sell_stock(buy_list, df):
sell_list = []
for stock in buy_list:
buy_date = datetime.datetime.strptime(stock[1], "%Y%m%d")
current_date = datetime.datetime.today()
days_held = (current_date - buy_date).days
if days_held >= 3:
sell_list.append(stock[0])
print("sell list:", sell_list)
```
我对代码进行了几个修改:
1. 在 `get_stock_pool`、`buy_stock` 和 `sell_stock` 的函数定义之后,我在每个函数调用的括号中添加了参数。这些参数是调用函数所需的数据。
2. 在 `buy_stock` 和 `sell_stock` 中,我将 `buy_list.append(code)` 和 `sell_list.append(stock[0])` 分别改为 `buy_list.append((code, temp_df.iloc[i+2]['trade_date']))` 和 `sell_list.append(stock[0])`。这是因为在 `buy_stock` 中,列表中的元素需要包含股票代码和买入日期,而在 `sell_stock` 中,列表中的元素只需要包含股票代码。
3. 在 `sell_stock` 中,我将 `for stock in buy_list:` 改为 `for stock in buy_list:`,因为在 `buy_stock` 函数中返回的是一个元组列表,而不是一个股票代码列表。同时,我将 `buy_list` 改为 `buy_list`,因为这是函数参数的名称。
希望这些修改能够帮助你解决问题!
for i in range(14): df = dfs[i] stock_name = df['name'].iloc[0] # 获取股票名字 label = '{} Close'.format(stock_name) # 构造标签 ax.plot(df['date'], df['close'], label=label)如何将label标签改为两列,一列7个
如果要将 label 标签改为两列,每列显示 7 个股票名称,可以分别构造左侧和右侧的标签,然后将它们拼接起来。具体实现如下:
```
for i in range(14):
df = dfs[i]
stock_name = df['name'].iloc[0] # 获取股票名字
if i % 7 == 0: # 每 7 个股票换一行
label_left = '{}\n'.format(stock_name) # 左侧标签
else:
label_left = '{}\n'.format(stock_name)
label_right = 'Close\n' # 右侧标签
label = label_left + label_right # 拼接左右标签
ax.plot(df['date'], df['close'], label=label)
# 设置标签格式,使其显示为两列
handles, labels = ax.get_legend_handles_labels()
labels_new = ['\n'.join(labels[i:i+14]) for i in range(0, len(labels), 14)]
ax.legend(handles, labels_new, loc='upper left')
```
这样,每 7 个股票就会换一行显示,标签就会呈现出两列的效果。注意,在最后需要设置标签格式,将标签显示为两列。这里使用了列表推导式将标签分组,每个组里包含 14 个标签。然后,使用 `join()` 方法将每个组内的标签拼接起来,形成一个带有换行符的字符串。最后,将拼接好的标签字符串传递给 `legend()` 方法,就可以显示为两列的标签。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)