Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Beginner Question regarding CustomFactors

Hi Community,

I'm trying to implement a Gap-Trading-Algorithm, where the logic is quite simple: Buy, when the Close of the previous day is higher than n% compared to Open today.

For that I've defined the following custom factor:

class CloseOpenGap(CustomFactor):  
    #defaults  
    inputs = [USEquityPricing.close, USEquityPricing.open]  
    window_length = 2  
    def compute(self, today, asset_ids, out, close, open):  
        out[:] = ( open[0] / close[-1] ) - 1

The screen-filter is pretty simple:
gap_minus_5 = (gap < context.LONG_THRESHOLD)

However, the results are confusing. I'm buying on Market Open
schedule_function(my_market_open, date_rules.every_day(), time_rules.market_open())
but the price is sowhat different to the expected gap price.

My questions:

  1. Is the CustomFactor compute is right to calc the gap between yesterday.close and todays.open? ( out[:] = ( open[0] / close[-1] ) - 1 ) refers the index 0 to the current day in the pipeline? I can't find any docs
  2. Is there any difference between USEquityPricing and the historical prices in Yahoo? I've tried to match the them, but there are slightly different - so I'm a bit confused

Thank you for your help!

best regards,

Peter

5 responses

My experience was -1 was the current day (which is empty if you do the calculation before market open) and -2 is the previous day

I've just figured out, that the pipeline contains only data for the previous day and the day before. Is there any chance to get the open price of the current day in the CustomFactor method?

@Peter: Pipelines run well before the open on each trading day, and only then. So it's not possible for them to get data for the day that they run.

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.

Nathan, so how would you handle this pseudo code (two pieces of logic really):

Macro:

If price is lower than the closing price X days ago by some Y percentage, and if the current price at 1000AM is higher than the closing price Z days ago, then buy, else pass

Micro:
If price at 1000AM is lower than yesterday closing price by XX%, then buy, else pass

Thanks in advance!

BT, I'm happy to help you with specific elements, but I hesitate to write code outright. Let me know if there are specific points you'd like help with.

That said, for your pseudocode, I would start by using a pipeline to filter down to a good universe. You can have the pipeline output specific historical price data that you mentioned: closing price X days ago, yesterday's closing price, etc.

Then you can have a scheduled function execute at 10:00 AM. This function could compare the current price for all the equities selected by your pipeline with their historical prices outputted by the pipeline. You could then execute the trades depending on your specified percentage thresholds.