Back to Community
I think my algorithm is spending more money than it has.

I think my algorithm is spending more money than it has. I have done a bit of research on it but haven't been able to figure it out in over in hour. It is probably an easy fix for you guys though. Thanks

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: 531520ffd6b9f10747fedd9a
This backtest was created using an older version of the backtester. Please re-run this backtest to see results using the latest backtester. Learn more about the recent changes.
There was a runtime error.
5 responses

I'm just guessing, so it would be nice if someone else could confirm or refute this.

if you insert the following at rows 18/19, you can at least see what is happening to your cash:

def handle_data(context, data):  

As you'll see, the cash balance goes from $100k to -$651k almost immediately.

I added a counter to the loop, and it looks like your subset of the universe is around 180 stocks. When you initially cycle through the portfolio, your cash will not change after you place an order but will only be reduced once the order has been executed, which should be observable at the next bar.

Putting another counter in line 25 of your code in the rsi long loop, I can see that the initial execution was to buy 37 tickers on 2008-01-25. Given that you are buying $20k per ticker, that would be about $740k. Starting cash of $100k - $740k = -$640k, so we are in the ballpark with the -$651k observed earlier.

I'm not sure what mechanisms Quantopian has in place to fail executions once cash has been depleted.

this seems similar to my question about selling short:

you should consider using order_target_percent() instead. i changed my algo to use that (instead of an actual cash/stock amount) and everything's now working good.

I would like to see an answer from the Q staff about the handling of
I see two problems:
1) When an order long is sent, the "available cash" should be immediately decreased (yes, maybe a new field is required...). This would make thing much more realistic and comparable to what happens when working with real money.
2) For the same reason (good simulation of real operation), when a short order is sent the available cash should be immediately decreased by the margin amount (typically 50% of the value sold).
In my opinion these tasks, being always the same, should be handled by the platform, not by the algorithms.

Hi Leopoldo,

  1. That's an interesting idea - I'd like to understand why you're looking for that. Currently if you have a field in you algo called "available_cash" or "get_open_orders", checking either of these fields is the same amount of work. In your algo, the available cash is decreased once the orders are filled to mimic real-world conditions. We created this behavior because you cannot know the price until the order is filled. We have other convenient methods for cash management, including order_target_value() and order_target_percent() as Jason mentioned.

  2. A margin model is a great idea! And we've gotten a lot of requests for it. It's on our wish list and we hope to implement it in the future. Until there is a convenient built-in function, try using this code shared by David:



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 guys! Colin, thanks for examining my algorithm for me. I wasn't aware that the is only updated daily/minutely. Jason, I implemented what you recommended I set the max position I could own in any stock equal to 100/180 %. I don’t think it is working perfectly yet, but I’ll work on it.