Back to Community
Help with my algorithm using fundamental data

Hi this is the first algorithm I've written and it is very simple. I want to implement an approximation of the 'magic formula' of Joel Greenblatt. I want the algorithm to:

  1. run once a month at market open on the first trading day of the month
  2. find the list of stocks at this time that have greater than 25% return on equity
  3. sort this list to find the company with the lowest price to earnings ratio greater than five
  4. place a market order for that stock to a value of $83333
  5. after exactly one year of holding the stock, place a market sell order
  6. repeat until end of back test.

Here is my code so far:

def before_trading_start(context):  
    fundamental_df = get_fundamentals(  
        .filter(fundamentals.operation_ratios.roa > 0.25)  
        .filter(fundamentals.valuation_ratios.pe_ratio > 5)  

def initialize(context):  

def handle_data(context, data):  
    order_value(security, 83333)  

I'm pretty sure the before_trading_start(context) method will put one stock into my universe that has the required attributes but I'm not really sure where to go from here.

My questions are:

  1. How do I access the stock that I placed into the universe? i.e. how do I buy the stock that the algorithm has found

  2. How do I get the algorithm to run once at the start of every month? also if the start of the month is a day that the market is not open, like a weekend, how would that affect the algorithm?

Thanks any help appreciated.

1 response

Hi Kane,

I think this example should get you started.

You can use the API method schedule_function to check your buy screen at the start of each month, it will gracefully handle trading only on the first trading day of each month. Keeping track of your entry dates isn't supported as cleanly, for that at this point you need to create a dictionary to store the entry dates for each position.

I've also added a custom time series plot to show how many positions the algo is invested in over time, and how much of the $1mm starting capital is sitting in cash. I would think that a possible improvement on this could be make sure 100% of capital is allocated at all times, putting the extra cash into a bond or tbill fund.

Best wishes,

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: 548f1fe17b1057090a407b39
There was a runtime error.

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.