Back to Community
Overspending portfolio

I have this algorithm that is working fairly well but the only reason that I think it's doing well is that it's overspending its portfolio and taking risky moves with the "borrowed" money. I've seen several posts going over it, but I'm still struggling to see what is causing it to spend so much.

I posted comments in the code to show what the program is doing. I believe the problem might be related to shorting stocks instead of selling them. If you see what I'm doing wrong please tell me!


57,000% isn't bad :)

Clone Algorithm
Backtest from to with initial capital
Total Returns
Max Drawdown
Benchmark Returns
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 5918f275cb78ba618ddc37f5
There was a runtime error.
4 responses

I barely know anything about python, but if you look at your handle_data method, it looks like once a minute you're telling it to place a new order to short three stocks, spending 75% of your total portfolio value on each of those orders. So each day you're spending more than 800x your total portfolio value, none of which you even have in cash any more (since your first order already more than double spent your cash).
You're spending massive amounts of money you don't have, which is not possible in the real world.

Try closing positions to free up cash or at least check whether you have any before placing another order. Consider using order_target_percent or order_target_value taking into account how much cash you have free. Or consider deleting the handle_data method entirely, since it doesn't seem related to anything.

I understand what you're saying, but before I sell the stock I check to see if the Simple Moving Average 7 day period is above or below the Simple Moving Average 14 day period. If sma7 is below the sma14 sell 75 percent of that stock. I guess what I'm trying to say is how do I sell 75 percent of a stock in my current portfolio.


Something like this?

target_amount = context.portfolio.positions[context.stock].amount * 0.25  
# selling 75% is like keeping 25%  
order_target(context.stock, target_amount)  

That's exactly what I needed!