Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Quantify trades as a % of portfolio value

Hello quantopian community!

I am new to programming and i find quantopian to be an awesome tool to work it.

I am building an algo that rebalance my portfolio 1 minute before close. I would like the add a constraint that says that if the trade value is less than 5% of the portfolio value at the time of the trade, we should not reblance. Is there any ways that could be done in quantopian?

Sorry if that has been answered eslewhere, i tried searching or it but could not find anything.
(English is not my 1st language. Sorry if the post is somewhat confusing)

Thanks a lot

Mathieu

4 responses

Bump

The solution rather depends upon what order method you are using (eg 'order_target_percent', 'order_value', etc). If you are using the 'order_value' method then it's pretty straightforward to check if your order is less than x% of the portfolio.

MIN_TRADE_PERCENT = .05  
if order_value > abs(MIN_TRADE_PERCENT * context.portfolio.portfolio_value):  
    order_value(stock, order_value)


The absolute value is needed to account for both open and close orders (ie positive and negative order_value). Using some other order method would be similar but you would just need to calculate the order_value somehow.

Hello Dan,

Thanks for your help. Im actually using order_target_percent to send orders. Any ways i can run it without using order_value like you suggested ?

Thank you

Maybe something like this...

for stock in context.securities:  
        if data.can_trade(stock):  
            current_amount = context.portfolio.positions[stock].amount  
            current_price = context.portfolio.positions[stock].last_sale_price  
            current_value = current_amount * current_price  
            target_value =  TARGET_PERCENT * context.portfolio.portfolio_value  
            trade_value = abs(target_value - current_value)  
            if trade_value > MIN_TRADE_PERCENT * context.portfolio.portfolio_value:  
                order_target_percent(stock, TARGET_PERCENT)  
                log.info('ordered')  
            else:  
                log.info('skipped order')

It appears the '.amount' and '.last_sale_price' are pretty forgiving and return 0 if there aren't any holdings. So don't need to check for that case. Simple algo is attached.