Help! Beginner that is practicing everything he's learned so far! Algorithm is blowing up!

Hello,

First off, this is my very first post and I would just like to show my appreciation for the people in this community and the employees of Quantopian for providing so many resources, posts, tutorials, lectures, the platform, data packages etc. that can allow anyone without a background in any of this stuff to become very polished in algorithmic trading.

Second, I have been on this website every since I discovered it through the Economist about three-four weeks ago. This website is absolutely amazing and have been able to learn enough where I can write the code provided in this post. Even though I have no formal background in programming (I have a degree in Finance and not computer science and math!) I think I'm coming along pretty well.

Third, now that I'm done with my excitement I want to tell you about why I need help:

Utilizing alphalens in research I was able to test my factor idea of (return on equity - cost of equity) = value creation. This ultimately led to high IC with poor returns in the bottom quantile of the basic materials sector and moderately high IC for the top quantile in the technology sector. So to test what I've been learning, I wanted to long top quantile tech and short low quantile basic materials

After putting together the algorithm, I've ran into several issues:
1) The algorithm backtest is very slow
2) I've tried to implement a beta hedge via calculating beta through regression (not sure if this is working properly because beta is going crazy)
3) Not sure if my rebalance after selling due to loss is working properly

4) If you look at the recording chart, I have HUGE spikes in cash and PV and ultimately a huge spike in returns and a crash in returns.

My questions:
What am I doing wrong?
Is my code poorly written?
What am I missing?
How can I improve my code?

Can you guys take a look and provide me with tips/inform me on how to fix these issue? Why are these issues happening?

Like I said, there may be a lot going on but I am just trying to practice to get better which is why I've implemented so many different functions, etc.

Thanks to everyone!

6
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
 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: 594ae1773fa0d16959258147
There was a runtime error.
7 responses

I think you have a lot of good ideas. What in layman's terms is the idea of your strategy? You want to long the top quantile of technology and short the bottom quantile of basic materials. Then are you trying to make Beta equal 0 between these two sectors, but if it excess beta you invest in the SPY. What do you think is wrong with the code? I see a lot of code, but it is hard to know where you went wrong.

Hi eric,

Thanks for the response.

My strategy is to long top quantile in tech sector and short bottom quantile in basic materials (as stated these had highest IC per alphalens for best/worst returns).

And yes, I want beta for my portfolio to be close to zero afterwards. I think I went wrong in implementing the beta hedge, because it seemed like it was working but the beta hedge messed me up so I'm not sure if i'm doing it right or not

I think you are right with the beta hedge making it an error. I am not sure why, but when I run it without the hedge the leverage stays under 2.0.

What does that mean?

if len(context.portfolio.positions):
I don't understand how that is an if statement, because len(context.portfolio.positions) will just be a number. So aren't you saying if 20:?

if len(context.portfolio.positions): is suppose to return true if the length of the dictionary is greater than zero. I thought this was the correct way of doing it i can also do if context.portfolio.positions. Its just a condition to make sure the code executes only after there is positions.

Logs Logs Logs. I know it sounds tedious but, Print every security that gets bought in a day, print every security sold, print every security held at the end of the day. Print the leverage 30 minutes after every method. Print all variables related to purchasing and their values as it happens. This will make it very obvious as to what is happening. We've probably all had this issue at some point. Thats how i solved it. You will figure it out.

Thanks, Jacob, I will keep that in mind going forward. Learning day by day here.

Do you have any tips on how to do research efficiently? I know about alphalens and how to utilize that if I have some ranked alphas, but just curious what else is out there.

In case you have not already heard or figured it out, try the debugger in the Quantopian IDE. It also, with the other suggestions above, can help you track down where unexpected variable changes occur. Search for "using the debugger" in https://www.quantopian.com/help .