import random
import string
from datetime import datetime
def generate_random_string(length=3):
characters = string.ascii_uppercase
return ''.join(random.choice(characters) for _ in range(length))
def generate_timestamped_string(separator='_'):
timestamp = datetime.now().strftime('%y%m%d%H%M%S')
random_part = generate_random_string(length=3)
return random_part+separator+timestamp
timestamped_string = generate_timestamped_string()
print(timestamped_string)
【Talk is cheap】
#!/usr/bin/env python
# coding: utf-8
# In[59]:
import warnings
warnings.filterwarnings('ignore')
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
get_ipython().run_line_magic('matplotlib', 'inline')
# In[60]:
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
# In[61]:
import tushare as ts # 导入tushare模块 zhttps://tushare.pro
# In[62]:
# 填入个人TOKEN
# pro = ts.pro_api('******')
# In[63]:
# 填入个人TOKEN
pro = ts.pro_api('******')
# In[64]:
#查询当前所有正常上市交易的股票列表,输入参数:交易所,上市状态
#输出参数:TS代码,股票代码,地域,所属行业,上市日期
df = pro.daily(ts_code='000001.SZ',
start_date='20230101',
end_date='202312311')
df['trade_date'] = pd.to_datetime(df['trade_date'])
df.set_index('trade_date',inplace=True) # 将trade_date设置为行索引
# In[65]:
df = df[::-1] # 将数据按时间正序排列
# In[66]:
df.head()
# In[67]:
fig, ax = plt.subplots(figsize=(10, 6))
ma5 = df['close'].rolling(5).mean() # 5日均线
ma30 = df['close'].rolling(30).mean() # 30日均线
# 使用 seaborn 绘制图像
sns.lineplot(data=df['close'].iloc[30:], label='Close Price')
sns.lineplot(data=ma5.iloc[30:], label='5-day Moving Average')
sns.lineplot(data=ma30.iloc[30:], label='30-day Moving Average')
# 添加 x 轴和 y 轴的网格线
ax.grid(axis='both', linestyle='--', alpha=0.9)
plt.title('中国平安 000001.SZ')
plt.show()