输出该股票所有收盘比开盘上涨3%以上的日期
1:输出该股票所有收盘比开盘上涨3%以上的日期
#coding=utf-8
import tushare as ts
import pandas as pd
import numpy as np#获取某支股票的历史行情数据
df=ts.get_hist_data(code='600519',start='2001-01-01')
#将互联网上的数据获取并且存储到本地
df.to_csv('./maotai.csv')
#将本地的数据读取
date_path='./maotai.csv'
df2=pd.read_csv(date_path)
#print(df2.info())#需要对读取出的数据做相关的处理
#df2.drop(labels='close',axis=1,inplace=True)
# print(df2)
# print(df2.head())
#查看每一列的数据类型
#print(df2['date'].dtype)
#print(df.head())
df2.set_index('open')
#print(df2.info())
#将date一列转成了时间序列
#print(df2)
df2['date']=pd.to_datetime(df2['date'])
df=df2.set_index('date')
#print(df)
#print(df)
#print(df2.info())
#print(df2.head())
#伪代码:(收盘-开盘)/开盘。0.3
a=df.loc[(df['close']-df['open'])/df['open']>0.03].index
print(a)
#如果对布尔值作为df的行索引,则可以取出true对应的
#然后通过.index取出所需要的时间
2:该股票开盘比前日收盘超出2%的日期
#前日收盘:df['date'].shift(1)
#coding=utf-8
import tushare as ts
import pandas as pd
import numpy as np#获取某支股票的历史行情数据
df=ts.get_hist_data(code='600519',start='2001-01-01')
#将互联网上的数据获取并且存储到本地
df.to_csv('./maotai.csv')
#将本地的数据读取
date_path='./maotai.csv'
df2=pd.read_csv(date_path)
#print(df2.info())# #将date一列转成了时间序列df2['date']=pd.to_datetime(df2['date'])
df=df2.set_index('date')
#伪代码:(开盘-前日收盘)/前日收盘<-0.02
b=df.loc[(df['open']-df['close'].shift(1))/df['close'].shift(1)<-0.02].index
print(b)
3:从2010年的1月1日开始,每月第一个交易工作日买入1手股票,每年最后一个交易工作日卖出所有股票,到今天为止,我的收益如何?
#时间切片:df[2010-01:2020-02]
df2['date']=pd.to_datetime(df2['date'])
df=df2.set_index('date')
new_df=df['2021-01':'2023-02']
print(new_df)
#买股票:
- 找出每月第一天的数据
-
df2['date']=pd.to_datetime(df2['date']) df=df2.set_index('date') new_df=df['2021-01':'2023-02'] #找出每月第一行的数据 new_df=new_df.resample('M').first() print(new_df)
- 买入股票花费的总金额
-
#找出每月第一行的数据 df_monthly=new_df.resample('M').first() #计算花费 cost=df_monthly['open'].sum()*100 print(cost)
#卖股票
-
卖出股票到手的钱
-
特殊情况:2020年的股票卖不出去(将最后一行切除) df[:-1]
-
#计算卖出去的收益 df_yearly=new_df.resample('A').last()[:-1] #print(df_yearly) income=df_yearly['close'].sum()*100 print(income)
-
卖出股票到手的钱
-
df_yearly=new_df.resample('A').last()[:-1] #print(df_yearly) income=df_yearly['open'].sum()*1200 print(income)
#最后股价剩余的价值要估算到总收益中
-
#估算2023年2个月还剩多少钱 lat_money=200*new_df['close'][-1] print(lat_money)
#计算最后的收益
-
#算出最后的钱 Finally=income+lat_money-cost print(Finally)
全部代码展现
-
#coding=utf-8 import tushare as ts import pandas as pd import numpy as np#获取某支股票的历史行情数据 df=ts.get_hist_data(code='600519',start='2001-01-01') #将互联网上的数据获取并且存储到本地 df.to_csv('./maotai.csv') #将本地的数据读取 date_path='./maotai.csv' df2=pd.read_csv(date_path) #print(df2.info())#需要对读取出的数据做相关的处理 #df2.drop(labels='close',axis=1,inplace=True) # print(df2) # print(df2.head()) #查看每一列的数据类型 #print(df2['date'].dtype) #print(df.head()) # df2.set_index('open') # #print(df2.info()) # #将date一列转成了时间序列 # #print(df2) df2['date']=pd.to_datetime(df2['date']) df=df2.set_index('date') #print(df.head(20)) #print(df.tail()) new_df=df['2021-01':'2023-02'] #找出每月第一行的数据 df_monthly=new_df.resample('M').first() #计算花费 cost=df_monthly['open'].sum()*100 #print(cost) #计算卖出去的收益 df_yearly=new_df.resample('A').last()[:-1] #print(df_yearly) income=df_yearly['open'].sum()*1200 #估算2023年2个月还剩多少钱 lat_money=200*new_df['close'][-1] #print(lat_money)#算出最后的钱 Finally=income+lat_money-cost print(Finally)