Overall Statistics
Total Trades
1
Average Win
0%
Average Loss
0%
Compounding Annual Return
264.307%
Drawdown
2.200%
Expectancy
0
Net Profit
1.667%
Sharpe Ratio
4.41
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0.007
Beta
76.306
Annual Standard Deviation
0.193
Annual Variance
0.037
Information Ratio
4.354
Tracking Error
0.192
Treynor Ratio
0.011
Total Fees
$3.25
from collections import deque
from datetime import datetime, timedelta
from numpy import sum

### Demonstrates how to create a custom indicator and register it for automatic updated
class CustomIndicatorAlgorithm(QCAlgorithm):
    def Initialize(self):
        self.SetStartDate(2013,10,7)
        self.SetEndDate(2013,10,11)
        self.AddEquity("SPY", Resolution.Second)

        # Create a QuantConnect indicator and a python custom indicator for comparison
        self.sma = self.SMA("SPY", 60, Resolution.Minute)
        self.custom = CustomSimpleMovingAverage('custom', 60)
        self.RegisterIndicator("SPY", self.custom, Resolution.Minute)

    def OnData(self, data):
        if not self.Portfolio.Invested:
            self.SetHoldings("SPY", 1)

        if self.Time.second == 0:
            self.Log("   sma -> IsReady: {0}. Time: {1}. Value: {2}".format(self.sma.IsReady, self.sma.Current.Time, self.sma.Current.Value))
            self.Log(str(self.custom))

        # Regression test: test fails with an early quit
        diff = abs(self.custom.Value - self.sma.Current.Value)
        if diff > 1e-25:
            self.Quit("Quit: indicators difference is {0}".format(diff))


# Python implementation of SimpleMovingAverage.
# Represents the traditional simple moving average indicator (SMA).
class CustomSimpleMovingAverage:
    def __init__(self, name, period):
        self.Name = name
        self.Time = datetime.min
        self.Value = 0
        self.IsReady = False
        self.queue = deque(maxlen=period)

    def __repr__(self):
        return "{0} -> IsReady: {1}. Time: {2}. Value: {3}".format(self.Name, self.IsReady, self.Time, self.Value)

    # Update method is mandatory
    def Update(self, input):
        self.queue.appendleft(input.Close)
        count = len(self.queue)
        self.Time = input.EndTime
        self.Value = sum(self.queue) / count
        self.IsReady = count == self.queue.maxlen