jinjia2html.py 4.55 KB
Newer Older
1 2 3 4 5
from jinja2 import PackageLoader, Environment
from app.service.result_service import UserCustomerResultAdaptor


# 准备数据
pengxiong@wealthgrow.cn's avatar
pengxiong@wealthgrow.cn committed
6 7
from app.utils.draw import draw_month_return_chart

8 9 10 11 12 13 14 15 16 17 18 19 20 21
ifa_id = 'USER_INFO15914346866762'
customer_id = '202009281545001'

user_customer = UserCustomerResultAdaptor(ifa_id, customer_id)

df = user_customer.calculate_total_data()
d = user_customer.calculate_group_result_data()

print(d)

# 几月综述部分

total_cost = df["total_cost"]/10000
now_yield = round((df['contribution_decomposition']-1)*100,2)
赵杰's avatar
赵杰 committed
22
index_yield = round((df["index_result"]["return_ratio"]-1)*100, 2)
23
now_withdrawal = round(df["max_drawdown"][0]*100,2)
赵杰's avatar
赵杰 committed
24
index_withdrawal = round(df["index_result"]["max_drawdown"][0]*100, 2)
pengxiong@wealthgrow.cn's avatar
pengxiong@wealthgrow.cn committed
25 26 27 28 29
# monthly_return_performance_pic = '_'.join((ifa_id, customer_id, '20201109', 'monthly_return_performance.png'))
# image_dir = './img/'
# monthly_return_performance_pic = image_dir + monthly_return_performance_pic
xlabels, product_list, cumulative = user_customer.get_month_return_chart()
monthly_return_performance_pic = draw_month_return_chart(xlabels, product_list, cumulative)
30

赵杰's avatar
赵杰 committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
# 组合数据
group_result = d["default"]

now_month_income = int(group_result["cur_month_profit"])   # 本月收益
now_year_income = int(group_result["cur_year_profit"])   # 今年累计收益
totoal_rate_of_return = round((group_result['contribution_decomposition']-1)*100, 2)       # 累计收益率
month_rise = round(group_result["cur_month_profit_ratio"]*100, 2)       # 本月涨幅
year_totoal_rate_of_return = round(group_result["cur_year_profit_ratio"]*100, 2)        # 今年累计收益率
annualised_return = round(group_result["return_ratio_year"]*100, 2)     # 年化收益率
volatility = round(group_result["volatility"]*100, 2)
max_withdrawal = round(group_result["max_drawdown"][0]*100, 2)
sharpe_ratio = round(group_result["sharpe"], 2)
cost_of_investment = int(group_result["total_cost"])    # 投资成本
final_balance = int(group_result["total_cost"] + group_result["cumulative_profit"])    # 期末资产
total_profit = int(group_result["cumulative_profit"])  # 累计盈利

index_section_return = round((group_result["index_result"]["return_ratio"]-1)*100, 2)
index_annualised_return = round(group_result["index_result"]["return_ratio_year"]*100, 2)     # 年化收益率
index_volatility = round(group_result["index_result"]["volatility"]*100, 2)
index_max_withdrawal = round(group_result["index_result"]["max_drawdown"][0]*100, 2)
index_sharpe_ratio = round(group_result["index_result"]["sharpe"], 2)
52

赵杰's avatar
赵杰 committed
53 54
group_nav_info = group_result["group_nav_info"]
group_hoding_info = group_result["group_hoding_info"]
55 56 57 58 59 60 61

# 全部数据
data = {'customer_name':'成龙', 'customer_gender':'女',
        'year_month':'2020年10月', 'ifa_company':'飞度工作室',
        'title':'10月综述', 'brand_name':'飞度工作室',
        'customer_old':42, 'customer_level':'平衡型',
        'position_years':'5年', 'planned_allocation_amount':2000.00,
赵杰's avatar
赵杰 committed
62 63 64 65 66 67 68 69 70 71 72 73 74
        'now_allocation_amount':total_cost, 'now_yield':now_yield, 'index_yield':index_yield, 'expected_yield':20,
        'now_withdrawal':now_withdrawal, 'index_withdrawal': index_withdrawal, 'expected_withdrawal': 20,
        'now_year_income':now_year_income,'now_month_income':now_month_income,'totoal_rate_of_return':totoal_rate_of_return,
        'month_rise':month_rise, 'year_totoal_rate_of_return':year_totoal_rate_of_return,
        'annualised_return': annualised_return, 'cost_of_investment': cost_of_investment,
        'final_balance':final_balance, 'total_profit':total_profit,
        'latest_worth_day':'2020-11-05',

        'index_comparison':{'section_return': totoal_rate_of_return, 'annualized_returns': annualised_return,
                            'volatility':volatility, 'max_withdrawal':max_withdrawal, 'sharpe_ratio': sharpe_ratio},
        'index_comparison_500':{'section_return':index_section_return, 'annualized_returns': index_annualised_return,
                            'volatility':index_volatility, 'max_withdrawal':index_max_withdrawal, 'sharpe_ratio':index_sharpe_ratio},
        'monthly_return_performance_pic':monthly_return_performance_pic,
赵杰's avatar
赵杰 committed
75 76
        'group_nav_info': group_nav_info,
        'group_hoding_info': group_hoding_info
77 78 79 80 81 82 83 84 85 86 87 88
        }


# 开始渲染html模板
env = Environment(loader=PackageLoader('app','templates'))    # 创建一个包加载器对象
template = env.get_template('monthReport.html')    # 获取一个模板文件
monthReport_html = template.render(data)  # 渲染

# 保存 monthReport_html
save_file = "app/html/monthReport.html"
with open(save_file, 'w', encoding="utf-8") as f:
    f.write(monthReport_html)