day04 股票分析器

股票数据地址:https://cn.investing.com/equities/moutai-historical-data

一.需要计算的

 

 最大回撤率:亏损比率,评估风险

 

 

 

 

 

 

字符串转化为时间:

pendulum库(字符串转时间,换算成周几)

 

实现代码:

import requests
import re
from datetime import datetime
import pendulum
#获取投入时的价格
headers={
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36'
,'X-Requested-With': 'XMLHttpRequest'#必须加这个,用来区分这是ajax请求还是普通请求,恰好这个是ajax请求
}
data={
'curr_id': '100673'
,'smlID': '1437916'
,'header': '600519历史数据'
,'st_date': '2019/01/01'
,'end_date': '2019/12/31'
,'interval_sec': 'Daily'
,'sort_col': 'date'
,'sort_ord': 'DESC'
,'action': 'historical_data'
}

reqs=requests.post('https://cn.investing.com/instruments/HistoricalDataAjax',headers=headers,data=data)
day=re.findall('class="first left bold noWrap">(.*?)年(.*?)月(.*?)日</td>',reqs.text,re.S)
final_price=re.findall('class="first left bold noWrap">.*?class=".*?">(.*?)</td>',reqs.text,re.S)
# print(day)
# print(final_price)
s=''
capital=0#本金
now_price=0#今日价格
buy_count=0#购买次数
price_list=[]
day_list=[]
max_draw_down=0
temp_max_value=0
for item in day:
    # print(item)
    for temp in item:
            if(len(temp)==1):
                temp='0'+temp#如果是1月1日开始为11,这里转变为0101
            s=s+temp
    temp_index=day.index(item)
    #有了下标就可以获取当前下标下的价格
    #获取数据中1680.0为1,680.0因此应该去掉逗号
    now_price=re.sub(',','',final_price[temp_index],1)
    day_list.append(s)
    # print(s)
    week = datetime(int(s[0:4]), int(s[4:6]), int(s[6:8])).weekday() + 1
    '''
    判断周四的另一种方法:
    dt=pendulum.parse(item)
    if dt.say_of_week==4    
    '''
    if(week==4):#投入本金,每周四购买
         capital=capital+100*float(now_price)
         buy_count=buy_count+1
    s=''
day_list.reverse()
#获取现在的股价
headers={
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36'
,'X-Requested-With': 'XMLHttpRequest'#必须加这个,用来区分这是ajax请求还是普通请求,恰好这个是ajax请求
}
data={
'curr_id': '100673'
,'smlID': '1437916'
,'header': '600519历史数据'
,'st_date': '2020/04/10'
,'end_date': '2020/04/10'
,'interval_sec': 'Daily'
,'sort_col': 'date'
,'sort_ord': 'DESC'
,'action': 'historical_data'
}
reqs=requests.post('https://cn.investing.com/instruments/HistoricalDataAjax',data=data,headers=headers)
last_price=re.findall('class="first left bold noWrap">.*?class=".*?">(.*?)</td>',reqs.text,re.S)
last_price=re.sub(',','',last_price[0],1)
total_value=float(last_price)*buy_count*100#最后价值
#先把所有价格放在一个列表
for item in final_price:
    today_price=re.sub(',','',item,1)#或者用today.replace(',','')
    price_list.append(float(today_price))
price_list.reverse()

# 求最大回撤率
for i in range(1,len(price_list)):
    temp_max_value=max(temp_max_value,price_list[i-1])
    max_draw_down=min(max_draw_down,price_list[i]/temp_max_value-1)
#每日收益率计算(每日收益率=(总价值-总投资)/总投资)
def profit(price_list,day_list):
    total_invest=0
    total_value=0
    stock_num=0
    profits=[]
    for item in price_list:
        profit = 0
        temp_index=price_list.index(item)
        s=day_list[temp_index]

        week = datetime(int(s[0:4]), int(s[4:6]), int(s[6:8])).weekday() + 1
        if week==4:
            stock_num=stock_num+1

            total_invest=total_invest+100*price_list[temp_index]*stock_num
        total_value=price_list[temp_index]*100*stock_num
        if total_invest!=0:
            profit=(total_value-total_invest)/total_invest
        profits.append(profit)

    return profits

print("按周定投,购买{}次,本金为:{}".format(buy_count,capital))
print('期末总资产为:{}'.format(float(last_price)*buy_count*100))
print('期末总收益为:{}'.format(total_value-capital))
print('定投收益率:{}'.format((total_value-capital)/capital*100))
print('最大回撤率:{}%'.format(max_draw_down*100))
print('每日收益率:')
print(profit(price_list,day_list))

结果:

 

posted @ 2020-04-11 11:26  记得喝牛奶  阅读(350)  评论(2)    收藏  举报