Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How to short more securities when stock price drop

I had a backtest doing both long and short. Let's say at some time I begin to short a total of $10000 value stock and the trade filled price is 100.
Assume the condition did not change so I'll continue short this stock.
My requirement is Every day I'll check one time if the price drop to some level or rise to some level. To say when the price drops to 90, I'd like to short more (10000-9000=$1000) stocks. The problem is how could I possibly record the initial portfolio value ($10000) so that I can compare it with the current portfolio value to make decisions? The thing I need is the filled price for this stock ($100) and then I can compare everything fast (100 vs data.current("sid[...]","price") ? Help plz!

Edit:
According to The IB Account Information page IB there is a Average Price Average cost of stock and securities options opening positions, including commissions. Any way I can get this field?

2 responses

The initial value of the portfolio when a backtest or algorithm starts is context.portfolio.starting_cash .
The split adjusted average price paid for a currently held security is context.portfolio.positions[my_stock].cost_basis. Where 'my_stock' is an equity object. You can loop through all positions like this.


for my_position in context.portfolio.positions:  
    the_average_price_paid = my_position.cost_basis  
    the_sid = my_position.sid  
    # do whatever logic is then needed

Hope that helps.

There's more info on the portfolio properties in the documentation https://www.quantopian.com/help#api-portfolio.

Thank you