Overall Statistics
Total Trades
1
Average Win
0%
Average Loss
0%
Compounding Annual Return
8.074%
Drawdown
0.700%
Expectancy
0
Net Profit
0.312%
Sharpe Ratio
2.029
Probabilistic Sharpe Ratio
59.446%
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
-0.031
Beta
0.684
Annual Standard Deviation
0.029
Annual Variance
0.001
Information Ratio
-3.521
Tracking Error
0.02
Treynor Ratio
0.085
Total Fees
$2.50
Estimated Strategy Capacity
$7500000.00
Lowest Capacity Asset
SPY WN524ZP4K1OM|SPY R735QTJ8XC9X
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 timedelta

import pandas as pd


class AlertFluorescentOrangeJackal(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2017, 5, 1)
        self.SetEndDate(2017, 5, 15)
        self.SetCash(100000)
        equity = self.AddEquity("SPY", Resolution.Minute)
        equity.SetDataNormalizationMode(DataNormalizationMode.Raw)
        self.equity = equity.Symbol
        self.SetBenchmark(self.equity)

        option = self.AddOption("SPY", Resolution.Minute)
        self.symbol = option.Symbol
    
        # set our strike/expiry filter for this option chain
        option.SetFilter(-10, 10, timedelta(90), timedelta(100))

    def OnData(self, data):
        
        # check if we alreadz invested in option
        option_invested = [x.Key for x in self.Portfolio if x.Value.Invested and x.Value.Type==SecurityType.Option]
        
        # if option is invested and it is close to expiration (4 days till expiration), we close the position
        if option_invested:
            if self.Time + timedelta(4) > option_invested[0].ID.Date:
                self.Liquidate(option_invested[0], "Too close to expiration")
            return
        
        # buy options LATER I CAN ADD CONDITION DEPENDING OIN SPY EQUITY        
        # if self.Securities[self.equity].Price >= self.high.Current.Value:
        for i in data.OptionChains:
            chains = i.Value
            self.BuyVerticalSpread(chains)

    def BuyVerticalSpread(self, chains):
    
        expiry = sorted(chains, key = lambda x: x.Expiry)[0].Expiry
        self.Debug(f"Today: {self.Time.date()}, expiry: {expiry}")
        calls = [i for i in chains if i.Expiry == expiry and i.Right == OptionRight.Call] # we choose call option closest to 90 days
        self.Debug(f"Today: {self.Time.date()}, calls: {calls}")
        call_contracts = sorted(calls, key = lambda x: abs(x.Strike - x.UnderlyingLastPrice))
        if len(call_contracts) == 0: 
            self.Debug("There are no option contracts for given criteria.")
            return
        self.call = call_contracts[0]
        
        quantity = self.Portfolio.TotalPortfolioValue / self.call.AskPrice
        quantity = int( 0.05 * quantity / 100 ) # we want to invest 5% of portfolio in out position
        self.Buy(self.call.Symbol, quantity)
        

    def OnOrderEvent(self, orderEvent):
        self.Log(str(orderEvent))
        
    def ClosePositions(self):
        if self.Portfolio.Invested:
            self.Liquidate()