Overall Statistics
Total Trades
211
Average Win
0%
Average Loss
0%
Compounding Annual Return
813.650%
Drawdown
1.400%
Expectancy
0
Net Profit
4.652%
Sharpe Ratio
4.451
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
4.631
Beta
-163.556
Annual Standard Deviation
0.449
Annual Variance
0.202
Information Ratio
4.414
Tracking Error
0.449
Treynor Ratio
-0.012
Total Fees
$211.00
from clr import AddReference
AddReference("System")
AddReference("QuantConnect.Algorithm")
AddReference("QuantConnect.Algorithm.Framework")
AddReference("QuantConnect.Common")

from System import *
from QuantConnect import *
from QuantConnect.Orders import *
from QuantConnect.Algorithm import *
from QuantConnect.Algorithm.Framework import *
from QuantConnect.Algorithm.Framework.Alphas import *
from QuantConnect.Algorithm.Framework.Portfolio import *
from QuantConnect.Algorithm.Framework.Selection import *
from Alphas.ConstantAlphaModel import ConstantAlphaModel
from Selection.OptionUniverseSelectionModel import OptionUniverseSelectionModel
from Execution.ImmediateExecutionModel import ImmediateExecutionModel
from Risk.NullRiskManagementModel import NullRiskManagementModel
from datetime import date, timedelta

class BasicTemplateOptionsFrameworkAlgorithm(QCAlgorithmFramework):

    def Initialize(self):

        self.UniverseSettings.Resolution = Resolution.Minute

        self.SetStartDate(2018, 1, 1)
        self.SetEndDate(2018, 6, 1)
        self.SetCash(100000)

        self.SetUniverseSelection(CoarseFundamentalUniverseSelectionModel(self.CoarseSelectionFunction))
        self.SetAlpha(ConstantOptionContractAlphaModel(InsightType.Price, InsightDirection.Up, timedelta(hours = 0.5)))
        self.SetPortfolioConstruction(SingleSharePortfolioConstructionModel())
        self.SetExecution(ImmediateExecutionModel())
        self.SetRiskManagement(NullRiskManagementModel())


    def CoarseSelectionFunction(self, coarse):
        sortedByDollarVolume = sorted(coarse, key=lambda x: x.DollarVolume, reverse=True)

        self.symbols = [ x.Symbol for x in sortedByDollarVolume[:3] ]
        
        for symbol in self.symbols:
            option = self.AddOption(symbol.Value)
            option.SetFilter(-2, 2, timedelta(0), timedelta(182))
        return self.symbols

class ConstantOptionContractAlphaModel(ConstantAlphaModel):
    '''Implementation of a constant alpha model that only emits insights for option symbols'''
    def __init__(self, type, direction, period):
        super().__init__(type, direction, period)

    def ShouldEmitInsight(self, utcTime, symbol):
        # only emit alpha for option symbols and not underlying equity symbols
        if symbol.SecurityType != SecurityType.Option:
            return False

        return super().ShouldEmitInsight(utcTime, symbol)

class SingleSharePortfolioConstructionModel(PortfolioConstructionModel):
    '''Portoflio construction model that sets target quantities to 1 for up insights and -1 for down insights'''
    def CreateTargets(self, algorithm, insights):
        targets = []
        for insight in insights:
            targets.append(PortfolioTarget(insight.Symbol, insight.Direction))
        return targets