ml-finance-python

python scripts for finance machine learning

git clone https://9o.is/git/ml-finance-python.git

increasing_max_turnover_snippet

(1021B)


      1 ### This piece of code tries increasingly large turnover settings
      2 ### until it finds one that yields a feasible portfolio. It's potentially
      3 ### useful good if you have an algorithm whose turnover may be more variable.
      4 ### Includes error handling for the case of order_optimal_portfolio not 
      5 ### executing due to too low of a turnover constraint.
      6 
      7 ### Reference: 
      8 ### https://www.quantopian.com/posts/party-algo-feedback-requested-please#5afaa20eab32870043944723
      9 
     10 ### Author: Grant Kiehne
     11 
     12 # set context.init = False in initialize(context)
     13 
     14 turnover = np.linspace(0.05,0.65,num=100)  
     15 for max_turnover in turnover:  
     16     constraints.append(opt.MaxTurnover(max_turnover))  
     17     if context.init:  
     18         constraints = constraints[:-1]  
     19         context.init = False  
     20     try:  
     21         order_optimal_portfolio(  
     22             objective=objective,  
     23             constraints=constraints,  
     24             )  
     25         record(max_turnover = max_turnover)  
     26         return  
     27     except:  
     28         constraints = constraints[:-1]