Portfolio Weight

Hi,



I woud like to change the portfolio weight based on certain condtion. I have programmed as follows. But I am not getting what I want. Please help me.



class marketreturn(CustomFactor):

params = ('market_sid',)

inputs = [EquityPricing.close]

def compute(self, today, assets, out, close, market_sid):

returns = (close[-1] - close[0]) / close[0]

market_idx = assets.get_loc(market_sid)

out[:] = returns[market_idx]



def compute_weights(context):

Set the allocations to equal weights for each long positions and short positions

market = symbol('NIFTY50').sid

niftyreturn = marketreturn(window_length=(63), market_sid=market)

if niftyreturn>0:

weightage = 1.0/20

else:

weightage = 1.0/25

return weightage



I am not able to get the weightage correctly.

your compute_weights function calls the pipeline factor class constructor. The variable niftyreturn is an instance of the class marketreturn, NOT a number. You need to add this factor to a pipeline - preferably not in compute_weights but in a set up function that returns a new pipeline with this factor added (and use attach_pipeline in initize to attach this returned pipeline to the strategy). And then call the pipeline_output in the compute_weights to trigger the actual computation. See the pipeline template on Blueshift for details, or in the help section.



Also the underlying logic may not be suitable for pipeline. You use a single indicator (nifty return) to compute a single value (weight). Best to use the data.history function to fetch the data and compute it explicitly. Pipeline will do lots of extra computation in this case that you do not seem to use.

Hi Sir,



I programmed as following. I just got this logic while listening to your introduction vidio in youtube. In mean time you also replied. THANK YOU.



def compute_weights(context,data):

Set the allocations to equal weights for each long positions and short positions

px = data.history(symbol('NIFTY50'),'close',63,'1d')

indexreturn = ((px[-1]-px[0])/px[0]) *100

num_of_security = len(context.selected_stocks)

if indexreturn>0:

weightage = 1.0/num_of_security

else:

weightage = 1.0/25

return weightage