Overall Statistics
Total Trades
0
Average Win
0%
Average Loss
0%
Compounding Annual Return
0%
Drawdown
0%
Expectancy
0
Net Profit
0%
Sharpe Ratio
0
Probabilistic Sharpe Ratio
0%
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0
Beta
0
Annual Standard Deviation
0
Annual Variance
0
Information Ratio
-2.219
Tracking Error
0.124
Treynor Ratio
0
Total Fees
$0.00
Estimated Strategy Capacity
$0
Lowest Capacity Asset
from datetime import datetime
from collections import *
from System import *
from QuantConnect import *
from QuantConnect.Algorithm import *
from QuantConnect.Orders import *
from QuantConnect.Algorithm.Framework import *
from QuantConnect.Algorithm.Framework.Selection import *
from QuantConnect.Algorithm.Framework.Execution import *
from QuantConnect.Algorithm.Framework.Risk import *
from Execution.StandardDeviationExecutionModel import StandardDeviationExecutionModel
import decimal as d
 
 
class ETFSimple(QCAlgorithm):
    def Initialize(self):
        self.SetStartDate(2021,1,13) 
        self.SetCash(1000000) 
        self.SetPortfolioConstruction(InsightWeightingPortfolioConstructionModel()) #we want our portfolio to be made up of insights
        self.SetExecution(ImmediateExecutionModel()) 
        self.SetBrokerageModel(BrokerageName.AlphaStreams) #required brokerage model for submission
 
 
        ############################################################################
        ## OPTIONS
        ############################################################################
        self.STRIKES = 30       # no of strikes around ATM => for uni selection
        self.MIN_EXPIRY = 45 # min num of days to expiration => for uni selection
        self.MAX_EXPIRY = 126 # max num of days to expiration => for uni selection
 
        self.MIN_DELTA = d.Decimal(0.25)
        self.MAX_DELTA = d.Decimal(0.45)
        self.MIN_PREMIUM = d.Decimal(0.05)
 
        self.optionSymbol = "SPY"
        equity = self.AddEquity(self.optionSymbol, Resolution.Minute)
        option = self.AddOption(self.optionSymbol, Resolution.Minute)
 
        # set strike/expiry filter for this option chain
        option.SetFilter(-self.STRIKES, self.STRIKES, timedelta(self.MIN_EXPIRY), timedelta(self.MAX_EXPIRY))
 
        # for greeks and pricer (needs some warmup) - https://github.com/QuantConnect/Lean/blob/21cd972e99f70f007ce689bdaeeafe3cb4ea9c77/Common/Securities/Option/OptionPriceModels.cs#L81
        option.PriceModel = OptionPriceModels.CrankNicolsonFD()  # both European & American, automatically
 
        # this is needed for Greeks calcs 
        self.SetWarmUp(TimeSpan.FromDays(self.MAX_EXPIRY))
 
 
 
 
    def OnData(self, slice):
 
        if (self.IsWarmingUp): return
 
 
        ##buy calls    
        self.TradeCallOptions(slice)
 
 
 
 
 
    def TradeCallOptions(self,slice):
 
        calls = [];
 
        for i in slice.OptionChains:
            if i.Key != self.optionSymbol: continue
 
            chain = i.Value
 
            # filter the put options contracts
            calls = [x for x in chain if x.Right == OptionRight.Call and abs(x.Greeks.Delta) > self.MIN_DELTA and abs(x.Greeks.Delta) < self.MAX_DELTA and x.BidPrice > self.MIN_PREMIUM] 
 
            # sorted the contracts according to their expiration dates
            contracts = sorted(sorted(calls, key = lambda x: x.BidPrice, reverse=True), 
                                             key = lambda x: x.Expiry)
 
            if len(contracts) == 0: continue
 
            call = contracts[0].Symbol
 
            self.EmitInsights(Insight.Price(call, timedelta(MIN_EXPIRY), InsightDirection.Up, None, None, None, 1))
 
            # buy the call options
            #ticket = self.MarketOrder(self.call, 1, asynchronous = False)     
 
            # set Take Profit order
            #self.takeProfitTicket = self.LimitOrder(self.call, 1, round(ticket.AverageFillPrice * 1.5, 2))