Hi all,
I've been on and off Quantopian for a while now and I still find myself writing algorithms that do things I don't expect. The two main things that seem to occur are: the leverage goes above 1x and the number of positions is lower than expected.
So, I thought maybe the best way to solve this is to start a thread where we list all the tips for how to write algorithms that don't have these problems.
I'll start with three I've seen:
- In the initialize() function, you can add a trading guard to avoid leveraged ETFs: set_do_not_order_list(security_lists.leveraged_etf_list). BTW, does anybody know how to exclude all ETFs?
- To avoid over-ordering, we can look at open orders using get_open_orders() which returns a dictionary of open orders keyed by assets. We can use this to ensure that we don't have an open order for a security before we place a new order for it. Also the data.can_trade() method can be used to check if a security can be traded (see: https://www.quantopian.com/tutorials/getting-started?utm_source=All+Active+Members&utm_campaign=0968811043-April_2016_Quantopian2&utm_medium=email&utm_term=0_7cca55778a-0968811043-108631197 )
- Securities have an "end_date" property that defines when the security is delisted. I've had problems in the past where the security just disappears, so I set a guard against this by selling anything that comes within say 10 days of that date. I'm not sure if this is really what happens in the real world, so if anybody has some commentary on the most realistic way to deal with this then that'd be welcome.
Please let me know if you have any other tips and tricks. I'd love to know more about how to avoid extremely illiquid securities, if there are any ways of screening out undesirable securities (like leveraged ETFs) and just in general ways to keep myself fully invested without using any leverage.