Overall Statistics
Total Trades
13
Average Win
0.83%
Average Loss
-0.30%
Compounding Annual Return
1.506%
Drawdown
5.200%
Expectancy
1.494
Net Profit
1.939%
Sharpe Ratio
0.367
Loss Rate
33%
Win Rate
67%
Profit-Loss Ratio
2.74
Alpha
-0.055
Beta
3.556
Annual Standard Deviation
0.043
Annual Variance
0.002
Information Ratio
-0.095
Tracking Error
0.043
Treynor Ratio
0.004
Total Fees
$13.00
from clr import AddReference
AddReference("System")
AddReference("QuantConnect.Algorithm")
AddReference("QuantConnect.Common")

from System import *
from QuantConnect import *
from QuantConnect.Algorithm import *
from datetime import datetime, timedelta

class bbExampleAlgorithm(QCAlgorithm):

    def Initialize(self):
        ''' Initialize the data and resolution you require for your strategy '''
        self.SetStartDate(2018, 1, 1)
        self.SetCash(25000);

        # Add SPY
        self.spy = self.AddEquity("SPY", Resolution.Daily)
        
        # Set Boilinger Bands
        self.bband = self.BB("SPY", 20, 2, MovingAverageType.Simple, Resolution.Daily)

        # Set WarmUp period
        self.SetWarmUp(20)
        
    def OnData(self, data):

        # Return if no data or if indicator is not ready
        if not (data.ContainsKey("SPY") or self.BB.IsReady): return

        # Retrieve current price
        price = self.Securities["SPY"].Price

        # Sell if price is higher than upper band
        if not self.Portfolio.Invested and price > self.bband.UpperBand.Current.Value:
            self.SetHoldings("SPY",-1)
            
        # Liquidate if price is lower than middle band        
        if self.Portfolio.Invested and price < self.bband.MiddleBand.Current.Value:
                self.Liquidate()