Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Need Help With Pipeline DataFrame Manipulations

I've created a pipeline with a single column for each of a limited set of securities (via StaticAssets).  The column contains a VolatilityFactor (Price / Volatility via a CustomFactor).

Where I'm having trouble is in creating an additional column of the DataFrame which represents the sum of the VolatilityFactors across all securities for each day (ComboFactor). And perhaps a third column representing each security's optimal weight = VolatilityFactor / ComboFactor.

Where I'd like to go with this is (following risk-parity principles) use each security's optimal weight (= VolatilityFactor / ComboFactor) to create a daily:

weight_objective = opt.TargetWeights(weights)

which can be used in:
order_optimal_portfolio(objective = weight_objective, constraints = [])

I suspect I'm also facing difficulties getting the dataframe format massaged into the dictionary format that the optimal function expects.

Any assistance would be greatly appreciated.

3 responses

I was able to work through the dataframe summation issues on my own, arriving at a dataframe with allocations for each security in the limited universe:
                             
Equity(19657 [XLI])   0.123386
Equity(19662 [XLY])   0.119187
Equity(32015 [IHI])   0.223302

Then, I was able to manipulate this dataframe into dictionary format, using the .T.to_dict('list') function:

{Equity(19657 [XLI]): [0.10066003341707783], Equity(19662 [XLY]): [0.15936743400110154], Equity(32015 [IHI]): [0.25590631006228276]}

When I tried to use the dictionary formatted output directly in the Optimize Target Weights function, I got the following error message:

TypeError: TargetWeights() expected a value with dtype 'float64' or 'int64' for argument 'weights', but got 'object' instead.  

Can anyone help me get this into format required by the Optimize Target Weights function:

objective = opt.TargetWeights(weights)

Apparently, the required format for weights is:

{XLI: 0.10066003341707783, XLY: 0.15936743400110154, IHI: 0.25590631006228276}

The nice thing about Python is it isn't very strict about types. The TargetWeights objective will accept either a dict or a series (check out the docs here). The nice thing about using a pandas series is that is simply a column of a pipeline dataframe. So assume that 'weight' is defined as a column in the pipeline. Then, the following will use that weight value to order the associated weights

    # Fetch the pipeline data  
    pipe_data = pipeline_output('my_pipe')

    # Determine weights  
    # In this case the calculations were already done in the pipeline  
    # Just use the 'weight' column  
    weights = pipe_data.weight

    # Run the order_optimal_portfolio method to place orders  
    order_optimal_portfolio(objective=opt.TargetWeights(weights), constraints=[])

One could also turn the 'weights' series into a dict and get the same results but that isn't necessary.

    # The following works the same but isn't necessary  
    weights = pipe_data.weight.to_dict()

See the attached algo. Good luck.

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

Thanks very much, Dan, for your clean and elegant solution, as well as your thoughtful comments.