# -*- coding: utf-8 -*-
"""
Created on 2017-7-10

@author: cheng.li
"""

import pandas as pd
from alphamind.analysis.factoranalysis import factor_analysis
from alphamind.data.engines.sqlengine import SqlEngine
from alphamind.data.engines.universe import Universe
from alphamind.data.engines.sqlengine import industry_styles
from PyFin.api import bizDatesList

engine = SqlEngine('mssql+pymssql://licheng:A12345678!@10.63.6.220/alpha')
universe = Universe('custom', ['zz500'])

used_risk_styles = ['SIZE']

total_risks = used_risk_styles + industry_styles
build_type = 'risk_neutral'


def calculate_one_day(ref_date, factors, factor_weights):
    print(ref_date)
    codes = engine.fetch_codes(ref_date, universe)
    total_data = engine.fetch_data(ref_date, factors, codes, 905)

    factor_data = total_data['factor']
    factor_df = factor_data[['Code', 'industry', 'weight', 'isOpen'] + total_risks + factors].dropna()

    dx_return = engine.fetch_dx_return(ref_date, codes)
    factor_df = pd.merge(factor_df, dx_return, on=['Code'])

    weights, _ = factor_analysis(factor_df[factors],
                                 factor_weights,
                                 factor_df.industry.values,
                                 None,
                                 detail_analysis=False,
                                 benchmark=factor_df.weight.values,
                                 risk_exp=factor_df[total_risks].values,
                                 is_tradable=factor_df.isOpen.values.astype(bool),
                                 method=build_type)

    return ref_date, (weights.weight - factor_df.weight).dot(factor_df.dx)


if __name__ == '__main__':

    from matplotlib import pyplot as plt

    factors = ['BDTO', 'CFinc1', 'DivP', 'EPS', 'RVOL', 'DROEAfterNonRecurring']
    factor_weights = [0.10, 0.30, 0.15, 0.18, 0.11, 0.35]
    biz_dates = bizDatesList('china.sse', '2017-01-01', '2017-07-07')

    ers = []
    dates = []

    for ref_date in biz_dates:
        ref_date, er = calculate_one_day(ref_date, factors, factor_weights)
        dates.append(ref_date)
        ers.append(er)

    res = pd.Series(ers, index=dates)
    res.cumsum().plot()
    plt.show()

@@ -66,16 +66,38 @@
 li%0A%22%22%22%0A%0A
+import datetime as dt%0A
 import p
@@ -373,16 +373,51 @@
 atesList
+%0Afrom PyFin.api import makeSchedule
 %0A%0Aengine
@@ -690,16 +690,34 @@
 _weights
+, horizon_end=None
 ):%0A    p
@@ -727,24 +727,25 @@
 t(ref_date)%0A
+%0A
     codes = 
@@ -1047,16 +1047,41 @@
 e, codes
+, expiry_date=horizon_end
 )%0A    fa
@@ -1975,39 +1975,26 @@
 s = 
-bizDatesList('china.sse', 
+makeSchedule(
 '201
-7
+5
 -01-
@@ -2010,16 +2010,35 @@
 7-07-07'
+, '1w', 'china.sse'
 )%0A%0A    e
@@ -2068,16 +2068,19 @@
 %0A    for
+ i,
  ref_dat
@@ -2084,16 +2084,26 @@
 date in 
+enumerate(
 biz_date
@@ -2103,18 +2103,90 @@
 iz_dates
+%5B:-1%5D)
 :%0A
+        ref_date = ref_date.strftime(%22%25Y-%25m-%25d%22)%0A        try:%0A    
         
@@ -2251,18 +2251,50 @@
 _weights
-)%0A
+, horizon_end=biz_dates%5Bi+1%5D)%0A    
         
@@ -2324,16 +2324,20 @@
         
+    
 ers.appe
@@ -2342,16 +2342,100 @@
 pend(er)
+%0A        except Exception as e:%0A            print(str(e) + %22: %7B0%7D%22.format(ref_date))
 %0A%0A    re

update example