Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Only one transaction, Need help re-balancing algorithm

Hey guys I am trying to develop an algorithm that buys only when there is momentum and the volume is high.

I am only getting one transaction in my Backtest. I am not sure why.

I am also trying to get it too re-balance every 30 days. I am not sure how to do that.

Your help is greatly appreciated.

Thanks

6 responses

So you are off to a good start by using set_universe() the fewer hard coded parameters the better!

Lets talk about your for loop: by looping through data you are going to be guaranteed that there is information for this bar so Bravo!
In your for loop you compute some sort of signal off of the volume and the avg price. The way you have it implemented is lacking in structure. You call the history function but you never use the dataframe, because you reassign the variable volume to a different value immediately. So you can get rid of your history function altogether. For future reference history() will get you the history of the given field for every security in your universe so putting it in your loop and calling it over and over again is incredibly inefficient. So I think that addresses your usage of the history function.

Now lets talk about the signals you are trying to generate with the average price and the volume. When you declare your significant values vol_significant=volume*1.5 ... buy_threshold=ma5*1.05 you immediatey check to see if your, for example, buy_threshold > ma5 well of course it is you just set it to be. So what you end up with is buy/sell logic that always evaluates to true! My advice, step back, look at your algorithm (not the code) and evaluate your idea and specifically layout its behaviour, then come back and try coding it up. That's when you can tackle your monthly rebalancing.

Disclaimer

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.

In addition to what Jame Christopher said. You are putting the buy outside the loop meaning that you are only checking the last stock in the loop and its always evaluating to true for reasons James said... so you are always setting your portfolio to 100% of the last stock so nothing is bought sold.

In the code sample below I have changed it to buy equal weights of all things that have been flagged to buy with a few simple changes however your current flag is set to always buy so it will just equally weight the entire portfolio.


buy_these = []  
for stock in data :  
   volume=history(5,'1d','volume') #volume of last 5 days  
   volume=data[stock].volume  
   vol_significant=volume*1.5  

   ma5=data[stock].mavg(5)  
   buy_threshold=ma5*1.05  
   if buy_threshold>ma5 and vol_significant>volume :  
      buy_these.append(stock)

buy_count = len(buy_these)  
pct_per_stock = 1.0/buy_count  
for stock in buy_these:  
   order_target_percent(stock,pct_per_stock)  
   log.info("Buy yeah" + str(stock))  

Did you want to buy if the price is is greater than the 5 day moving average and the volume is greater than the 5 day moving average?

I have attached a back test with source code that buys if the volume is greater than 1.5*5 day average and the price is 1.05*the 5 day average and then equally buys all stocks in that category.

Note that it is making 50-100 transactions a day, which is overkill.

Thanks for all your help. I re-created the algorithm, using 4 times the average volume and a moving average of 30. It worked a little better. I am more worried about knowing the programming then the actual algorithm.

What is necessary to put in the loop for the algorithm to work?
Also, what does buy_these= [], does that include all the stocks?

Here is the backtest for the new algorithm.

Does set_universe already filter the highest volume stocks? Is it unnecessary to include the volume average?
And one more question, How does the computer know to calculate each individual stock? Is that when I declare that it is a loop?

set_universe implementation DollarVolumeUniverse ranks the securities by their DollarVolume or the share price * volume. Check out the docs for set_universe

It's necessary to include the volume average if that is what you are trying to generate signals off of.