Back to Community
Lunar phase
Clone Algorithm
108
Loading...
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
def initialize(context):
    context.DIA = sid(2174)
    context.max_notional = 100000.1
    context.min_notional = -100000.0
    context.longstop = 0.0
    set_commission(commission.PerShare(cost=0.01))
    
def handle_data(context, data):
    
    price = data[context.DIA].price 
    low = data[context.DIA].low
    quoteDate = data[context.DIA].datetime
    index = GetPhaseOfMoonEighth(quoteDate.month, quoteDate.day, quoteDate.year)
    ma = data[context.DIA].mavg(5)
    
    if index == 7:
        if context.portfolio.positions[context.DIA].amount == 0:
            order(context.DIA, +context.portfolio.cash/price)
        elif low < ma:
            order(context.DIA, -context.portfolio.positions[context.DIA].amount) 
    elif index == 1:
        order(context.DIA, -context.portfolio.positions[context.DIA].amount) 
        
def GetPhaseOfMoonEighth(month, day, year):
    
    ages = [18, 0, 11, 22, 3, 14, 25, 6, 17, 28, 9, 20, 1, 12, 23, 4, 15, 26, 7]
    offsets = [-1, 1, 0, 1, 2, 3, 4, 5, 7, 7, 9, 9]
    if day == 31:
        day = 1
    days_into_phase = ((ages[(year + 1) % 19] + ((day + offsets[month-1]) % 30) + (year < 1900)) % 30)
    index = int((days_into_phase + 2) * 16/59.0)
    if index > 7:
        index = 7
    index += 1
    return index

This backtest was created using an older version of the backtester. Please re-run this backtest to see results using the latest backtester. Learn more about the recent changes.
There was a runtime error.
12 responses

It's pretty loony =)

I think it got lucky around the crash of 2008. Change the timeframe, and the result is very different.

Fun share, though. Thanks!

Clone Algorithm
14
Loading...
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
def initialize(context):
    context.DIA = sid(2174)
    context.max_notional = 100000.1
    context.min_notional = -100000.0
    context.longstop = 0.0
    set_commission(commission.PerShare(cost=0.01))
    
def handle_data(context, data):
    
    price = data[context.DIA].price 
    low = data[context.DIA].low
    quoteDate = data[context.DIA].datetime
    index = GetPhaseOfMoonEighth(quoteDate.month, quoteDate.day, quoteDate.year)
    ma = data[context.DIA].mavg(5)
    
    if index == 7:
        if context.portfolio.positions[context.DIA].amount == 0:
            order(context.DIA, +context.portfolio.cash/price)
        elif low < ma:
            order(context.DIA, -context.portfolio.positions[context.DIA].amount) 
    elif index == 1:
        order(context.DIA, -context.portfolio.positions[context.DIA].amount) 
        
def GetPhaseOfMoonEighth(month, day, year):
    
    ages = [18, 0, 11, 22, 3, 14, 25, 6, 17, 28, 9, 20, 1, 12, 23, 4, 15, 26, 7]
    offsets = [-1, 1, 0, 1, 2, 3, 4, 5, 7, 7, 9, 9]
    if day == 31:
        day = 1
    days_into_phase = ((ages[(year + 1) % 19] + ((day + offsets[month-1]) % 30) + (year < 1900)) % 30)
    index = int((days_into_phase + 2) * 16/59.0)
    if index > 7:
        index = 7
    index += 1
    return index

This backtest was created using an older version of the backtester. Please re-run this backtest to see results using the latest backtester. Learn more about the recent changes.
There was a runtime error.
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.

I plead ignorance - what's an MFI? Have a link I can read about it?

I haven't looked at the code but I assume it's Money Flow Index. See: http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:money_flow_index_mfi

P.

It's an interesting exercise but I'm really not sure I would bet a single dollar on this in real life. However, recently I wrote a star-sign calculator for my gf's astrology website, that calculates where the planets are positioned at a given time: https://www.leinenbock.com/astro-calculations/
I've seen an astrology plugin for ami-broker before and so I thought I share it for anyone who's insane (or genius) enough to try it. My gf believes that you can obviously make money with this :-) Let me know if you have any luck, it would shake my world view if you did.

Wow, that's super interesting! My astrology gf would say that it's totally obvious, but for us number crunchers this is certainly not the case :-) Would you mind sharing the Sharpe ratio of your actual strategy? In terms of the astronomical calculations I use something called the Swiss Ephemeris. This is open source and the original code is written in C or C++ (not sure) but it should tie in with your C# pretty well. I just used the Python wrapper of the program. It's a pretty accurate astronomical calculator and widely used.
In terms of what the underlying reasons for this strategy are, I guess (apart from it being a lucky coincidence) perhaps you could argue that since the moon phases are linked to the both the moon and the sun, and there is a strong gravitational pull from both, there might be some influence of whatever sort.
Anyway, who knows... I have also written a piece of code for my gf that calculates a phenomenon called the 'void of course moon'. Apparently, it's a big thing in the astrology world, I'm not so sure what to make of it, but I tested it against several sources and it is accurate. if you are interested in having a go with it in trading, I'm happy to share it. It's Python but again, it's based on the Swiss Ephemeris.

Thanks for providing the insight.

:) This is a good bit of fun. It reminds me of this paper (published 2007) that predicted the 2008 crash using sunspots:

http://www.growth-dynamics.com/articles/sunspots.pdf

It also raises a great point - that correlation does not imply causation: http://en.wikipedia.org/wiki/Correlation_does_not_imply_causation
I don't think there has been much discussion about this on Quantopian - and it is something to be wary of when trying to find some "magic indicator" that tells you when to buy and sell.

For more spurious correlation examples, check this out:
Does Chocolate Consumption Really Boost Nobel Award Chances? The Peril of Over-Interpreting Correlations in Health Studies

I particularly like the part:
"We found an incredibly high correlation between the number of IKEA furniture stores and Nobel laureates..."

Obviously you are just messing around here, but one thing I've found that can happen on an algorithm run from at least '08 to '11 is that only a few days can make a huge difference. The market was really volatile at one point, I recall one day DJI went up 950 points or so, and other days it went down 500 points. If a strategy happens to trade on the right days in only two or three cases, it can send a ripple effect if the algo uses all available cash to go long when it does go long (like the one above does). Since the algo randomly went up 1000-2000 points or so, it now has more money to bet and from there an average strategy will appear to outpace the market. So that's why you see a small diversion around the recession, then your strategy starts to pull ahead. Not sure if this is obvious to you guys, but I think it's interesting.

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.

t also raises a great point - that correlation does not imply causation: http://en.wikipedia.org/wiki/Correlation_does_not_imply_causation
I don't think there has been much discussion about this on Quantopian - and it is something to be wary of when trying to find some "magic indicator" that tells you when to buy and sell.

I am very interested in any ideas/pointers/code that addresses the issue of over-fitting algorithms to historical data. It seems to me there should be some sort of Bayesian approach where you can somehow determine the likelihood that your algorithm will work on real, unseen data.

@daniel: Here is a great introduction to Bayesian methods for Python. It gives a great overview of the capability of Bayesian analysis with really nice examples.
http://spin.atomicobject.com/2013/09/09/5-unix-commands/

@daniel I suggest you look at the concept of out-of-sample data and walk forward testing. In my limited experience working with quants in large hedge funds, they have very hard and fast rules about out-of-sample data. Before they test an algo, they define what data will and won't be used, and they do out-of-sample data as the last last step. If the algo fails to win on out of sample data, then they throw it out.

@daniel: In my econometrics course we always used the Akaike information criterion and Bayesian information criterion to prevent overfitting ARMA models - I think its worth looking into these.

Dans suggestion of out-of-sample data is also known as cross validation and there are a variety of methods see wiki. Also keep in mind this:
"Cross-validation only yields meaningful results if the validation set and training set are drawn from the same population. In many applications of predictive modeling, the structure of the system being studied evolves over time. This can introduce systematic differences between the training and validation sets. For example, if a model for predicting stock values is trained on data for a certain five-year period, it is unrealistic to treat the subsequent five-year period as a draw from the same population."

Here is an article that has a good discussion on cross validation: Why every statistician should know about cross-​​validation

@Tom did you post the wrong link?