Overall Statistics
Total Trades
360
Average Win
3.65%
Average Loss
-3.71%
Compounding Annual Return
2.383%
Drawdown
60.200%
Expectancy
0.055
Net Profit
41.019%
Sharpe Ratio
0.218
Loss Rate
47%
Win Rate
53%
Profit-Loss Ratio
0.98
Alpha
0.059
Beta
0.005
Annual Standard Deviation
0.275
Annual Variance
0.076
Information Ratio
-0.141
Tracking Error
0.398
Treynor Ratio
12.316
Total Fees
$13846.37
# https://quantpedia.com/Screener/Details/61
from QuantConnect.Data import SubscriptionDataSource
from QuantConnect.Python import PythonData
from datetime import date, timedelta, datetime
from decimal import Decimal
import numpy as np
import pandas as pd


class TradeWtiBrentSpreadAlgorithm(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2004, 1, 1)
        self.SetEndDate(2018, 8, 1)
        self.SetCash(100000)
        # import the custom data 
        self.AddData(MoonPhase, "phase", Resolution.Daily)
        self.AddEquity("EEM", Resolution.Daily)
        self.SetBenchmark("EEM")
    
    def OnData(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"].IsLong:
            self.SetHoldings("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"].IsShort:
            self.SetHoldings("EEM", -1)
        

class MoonPhase(PythonData):
    "Class to import Phases of the Moon data from Dropbox"
        
    def GetSource(self, config, date, isLiveMode):
        return SubscriptionDataSource("https://www.dropbox.com/s/q9rt06tpfjlvymt/MoonPhase.csv?dl=1", SubscriptionTransportMedium.RemoteFile)

    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