Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
spectrogram generation?

Would it be feasible to generate a spectrogram in Quantopian? As I understand, for a given SID, there exists in the database a time series of prices. So, it would be natural to have a look at the frequency content of the time series. Since the frequency content may change over time, a spectrogram would be an appropriate analysis tool.

One potential trading opportunity would be if the spectrogram revealed an underlying "tone" stable in time--a minutely/daily/seasonally repeating movement in the price.

Here's an example of how to generate a spectrogram in Python.

5 responses

@Grant: I think those are great ideas to explore and it is definitely something we want to enable at Quantopian. The first step is to make it easy to build your own data transforms which I'm working on right now. The current syntax would look something like this:

@batch_transform

def spectral(data):

____Pxx, freqs, bins, im = specgram(x[SID]);

____return Pxx, freqs

(sorry for the mess but there seems to be something wrong with pasting code).

The decorator provides the functionality of a sliding window of the trading events. Data will be a pandas dataframe where each column contains the price history (the window length can be specified by you) for each sid.

You would then call this function inside handle_data(context, data):

Pxx, freqs = spectral(data, sids=[21, 1024], refresh_period=5, days=5)  

This will keep a sliding window of 5 days and recompute the FFT every 5 days. Data will contain the price for sid 21 and 1024.

What do you think of that syntax/functionality?

The second step is how to display the results which is where plotting becomes relevant which touched on in our previous discussion.

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.

@batch_transform  
def spectral(data):  
    Pxx, freqs, bins, im = specgram(x[SID]);  
    return Pxx, freqs  

@Thomas,

Thanks. I don't know Python (C/C++ years ago and MATLAB currently), so I am a bit challenged here. How would the "sliding window" come into play? Would this allow spectrograms over long time periods (days/weeks/years)? What would be the limits in frequency and time resolution?

@Grant,

sorry for not being clearer. But yes, you can compute a spectrogram over long timer periods (limit frequency would be daily and time resolution minutes) that way. The key is that it will get updated every T days over a history of N days (both are parameters). That's why we call it a sliding window.

The function to compute specgram above will be called in this manner with the appropriate data and you can return whatever you need.

Does that help?

P.S. the @batch_transform is a decorator that creates a wrapper around the following function to give it the sliding window capabilities.

Thanks Thomas...I think that I'll just need to give it a try once all the tools are ready. Please keep me posted.