Introduction
The lunar phase is the shape of the direct sunlight portion of the Moon as viewed from Earth. The lunar phases gradually and cyclically change over the period of a month. Lunar phases have proven effects on human biology and psychology. Investors are subject to various psychological and behavioral biases and mood fluctuations. Therefore, if lunar phases affect mood, by extension, these phases may affect investor behavior and thus asset prices. This tutorial we will build an algorithm based upon the Moon phase.
Method
The lunar calendar data is obtained from the United States Naval Observatory (USNO) website. We import the custom data as a CSV file. The four phases of the lunar cycle are New Moon, First Quarter, Full Moon, and Last Quarter. They are in string format in the data file so we convert the string names to numbers as New Moon(0), First Quarter(1), Full Moon(2) and Last Quarter(3).
class MoonPhase(PythonData):
"Class to import Phases of the Moon data from Dropbox"
def get_source(self, config, date, isLiveMode):
return SubscriptionDataSource("https://www.dropbox.com/s/q9rt06tpfjlvymt/MoonPhase.csv?dl=1", SubscriptionTransportMedium.REMOTE_FILE)
def reader(self, config, line, date, isLiveMode):
index = MoonPhase()
index.symbol = config.symbol
try:
# Example File Format: (Data starts from 01/07/2004)
# date phase
# 2004 Jan 07 15:40 Full Moon
data = line.split(',')
if data[0] == "date": return None
index.time = datetime.strptime(data[0], "%Y %b %d %H:%M").replace(hour=0, minute=0)
if data[1] == "New Moon":
index.value = 0
elif data[1] == "First Quarter":
index.value = 1
elif data[1] == "Full Moon":
index.value = 2
elif data[1] == "Last Quarter":
index.value = 3
except:
return None
return index
Research shows that the effect is strongest in emerging markets. Here we choose the iShares MSCI Emerging Markets Index ETF, EEM. The algorithm therefore goes long in emerging market index ETF 7 days before the new moon and switch to a short position on ETF 7 days before the full moon. The benchmark is set to EEM.
def initialize(self):
self.set_start_date(2004, 1, 1)
self.set_end_date(2018, 8, 1)
self.set_cash(100000)
# import the custom data
self.add_data(MoonPhase, "phase", Resolution.DAILY)
self.add_equity("EEM", Resolution.DAILY)
self.set_benchmark("EEM")
def on_data(self, data):
# long in emerging market index ETF 7 days before the new moon (It's the Last Quarter)
if self.securities["phase"].price == 3 and not self.portfolio["EEM"].is_long:
self.set_holdings("EEM", 1)
# short on emerging market index ETF 7 days before the full moon (It's the First Quarter)
elif self.securities["phase"].price == 1 and not self.portfolio["EEM"].is_short:
self.set_holdings("EEM", -1)
Derek Melchin
See the attached backtest for an updated version of the algorithm in PEP8 style.
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 QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect 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. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Jing Wu
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 QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect 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. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!