Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Backtest not reflecting stock performance

****I wrote this code to test the Quantopian API. On Augutst 9th, CSLT was $4.02 at 6:56 and $4.49 at 12:56. I wrote the code to buy at 6:46 and sell at 12:46. I expected to see something like 11% return in the backtest, but it shows zero. Could someone explain why? Also, I would like to be able to retrieve the amounts at which a stock is bought and sold. I believe there is an “order id” function, but I would like some info about breaking down the info the function returns. Sorry if I have made an egregious mistake, but I am just starting out with this. Thank you.

P.S. BACKTEST DATES: 8-8-2016 - 8-9-2016

8 responses

Have you tried using order_target_percent in your code? I think you are only ordering 100 shares using a million dollars. This won't produce 11% results.

Thank you for the reply. Shows how much learning I still need to do. I assumed the order function would simply buy 100 shares at the price they were currently trading, not spend my entire stake on 100 shares. I will post the results I get using order_target_percent. Thanks again.

Sorry, I think I have explained myself wrongly. the order() function will order a specified amount of shares at the current market price. Whereas, the order_target_percent() function orders a percent of your total portfolio value into the specified stock.

My concern was that the 11% returns on a stock is assuming that it is the only stock in your portfolio, and that it takes up all of your portfolio value. If half of your portfolio is in cash (making.losing no money) and the other half of your portfolio is in that stock, your returns will only be half (5.5%) of that if you were holding 100%.

You only ordered 100 shares which was not 100% of your total portfolio value, therefore your returns will be less.

Hope that was a bit clearer,

Max

Thank you. I think you have a point with your concern about the lone stock taking up my entire portfolio value because when I used the get_order() funtion, I get a message saying my sell order of over 7000 shares could not be filled.

So I went back to the API doc and investigated the order() functions some more. I think the one I need is order_value(), in which you give the application an amount and the security you are buying, and it will buy as many shares of that security the amount you supplied can buy. So I gave it a random value of $550 which would place a market order more or less at the current price for about 136 shares. And as the accompanying backtest result shows, my code did produce a return approximating what I was expecting.

I will spend some more time on the API doc and experimenting with the functions and code snippets provided because it looks too danged useful. Thank you again for your contributions.

EDIT: Whoops. Code only returns 10% with a $600 capital base. With a million dollars, it is still zero. Let me really go read that API doc.

Yeah that attached looks good there. It will be slightly less than 11% because either:

  • That 11% figure was rounded from 10.5%-10.9%
  • Your transaction costs ate into your profit. Quantopian does default transaction costs (cost per share bought) and slippage models.

It's probably a bit of both IMO.

I really think that the best way to learn is by doing, especially with an end goal that you really want to achieve. I think you are totally on the right track, and with dedication you can totally get there.

Good Luck!

Tahnk you. Thanks for replying a little late. This algo coding thing is a little addicting!

Two points regarding the latest shared algo:

order_target_percent(sid(46551), 100)  # Essentially, this is x100 leverage.  
order_target_percent(sid(46551), 1)    # Use decimal for 100% of your portfolio value.  

In this case when the only security is sid(46551), the end result is still likely to differ with capital base of $600 and $1M, respectively, as the entire order will not fill due to illiquidity in the latter case.