Overall Statistics
Total Trades
17
Average Win
0.07%
Average Loss
-0.20%
Compounding Annual Return
-20.600%
Drawdown
1.400%
Expectancy
-0.834
Net Profit
-1.235%
Sharpe Ratio
-7.52
Probabilistic Sharpe Ratio
1.883%
Loss Rate
88%
Win Rate
12%
Profit-Loss Ratio
0.33
Alpha
-0.161
Beta
0.08
Annual Standard Deviation
0.031
Annual Variance
0.001
Information Ratio
3.214
Tracking Error
0.195
Treynor Ratio
-2.858
Total Fees
$17.00
from datetime import datetime, timedelta
from clr import AddReference
AddReference("System")
AddReference("QuantConnect.Algorithm")
AddReference("QuantConnect.Indicators")
AddReference("QuantConnect.Common")

from System import *
from QuantConnect import *
from QuantConnect.Algorithm import *
from QuantConnect.Indicators import *
from QuantConnect.Data.Market import *
from QuantConnect.Data.Consolidators import *
class DataConsolidationAlgorithm(QCAlgorithm):

    def Initialize(self):
        '''Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.'''
        self.SetStartDate(2016,1,1)  #Set Start Date
        self.SetEndDate(2016,1,20)   #Set End Date
        # Find more symbols here: http://quantconnect.com/data
        #self.AddForex("EURUSD", Resolution.Minute, Market.Oanda)
        self.AddEquity("SPY", Resolution.Minute)
        thirtyMinuteConsolidator = TradeBarConsolidator(timedelta(minutes=30))
        thirtyMinuteConsolidator.DataConsolidated += self.ThirtyMinuteBarHandler
        
        self.SubscriptionManager.AddConsolidator("SPY", thirtyMinuteConsolidator)
        self.macd = MovingAverageConvergenceDivergence(12, 26, 9, MovingAverageType.Exponential)
        self.RegisterIndicator("SPY", self.macd, thirtyMinuteConsolidator)

    def ThirtyMinuteBarHandler(self, sender, bar):
        '''This is our event handler for our 30-minute trade bar defined above in Initialize(). So each time the consolidator produces a new 30-minute bar, this function will be called automatically. The sender parameter will be the instance of the IDataConsolidator that invoked the event '''
        #self.Debug(str(self.Time) + " " + str(bar))
        if not self.macd.IsReady: 
            return
        
        if self.Portfolio["SPY"].Quantity == 0 and self.macd.Current.Value > self.macd.Signal.Current.Value:  
                self.Buy("SPY",100)
                self.Debug("BUYING SPY")
                self.Debug(f"MACD VALUE : {self.macd.Current.Value}")
                self.Debug(f"SIGNAL VALUE :{self.macd.Signal.Current.Value}")
        elif self.Portfolio["SPY"].Quantity > 0 and self.macd.Current.Value < self.macd.Signal.Current.Value:
                self.Liquidate()        
                self.Debug("Liquidating SPY")
                self.Debug(f"MACD VALUE : {self.macd.Current.Value}")
                self.Debug(f"SIGNAL VALUE :{self.macd.Signal.Current.Value}")
        
        #self.Plot("Charting", "MACD",self.macd)        
        
    def OnData(self, data):
        pass