Overall Statistics |
Total Trades 432 Average Win 0.01% Average Loss 0.00% Compounding Annual Return 0.266% Drawdown 0.100% Expectancy 0.282 Net Profit 0.210% Sharpe Ratio 1.45 Probabilistic Sharpe Ratio 61.233% Loss Rate 51% Win Rate 49% Profit-Loss Ratio 1.61 Alpha 0.002 Beta 0.001 Annual Standard Deviation 0.001 Annual Variance 0 Information Ratio 0.573 Tracking Error 0.207 Treynor Ratio 2.347 Total Fees $550.00 Estimated Strategy Capacity $0 Lowest Capacity Asset SPY Y2N4X35NVDK6|SPY R735QTJ8XC9X |
#region imports from AlgorithmImports import * #endregion import time USE_FOMC_MEETINGS = True FOMC_MEETINGS = [ Time(2022, 1, 25).strftime("%Y%-m%-d"), Time(2022, 1, 26).strftime("%Y%-m%-d"), Time(2022, 3, 15).strftime("%Y%-m%-d"), Time(2022, 3, 16).strftime("%Y%-m%-d"), Time(2022, 5, 3).strftime("%Y%-m%-d"), Time(2022, 5, 4).strftime("%Y%-m%-d"), Time(2022, 6, 14).strftime("%Y%-m%-d"), time(2022, 6, 15).strftime("%Y%-m%-d"), time(2022, 7, 26).strftime("%Y%-m%-d"), time(2022, 7, 27).strftime("%Y%-m%-d"), time(2022, 9, 20).strftime("%Y%-m%-d"), time(2022, 9 ,21).strftime("%Y%-m%-d"), time(2022, 11, 1).strftime("%Y%-m%-d"), time(2022, 11, 2).strftime("%Y%-m%-d"), time(2022, 12, 13).strftime("%Y%-m%-d"), time(2022, 12, 14).strftime("%Y%-m%-d"), ] USE_FOMC_MINUTES = True FOMC_MINUTES = [ time(2022, 2 ,16).strftime("%Y%-m%-d"), time(2022, 4, 6).strftime("%Y%-m%-d"), time(2022, 5, 25).strftime("%Y%-m%-d"), time(2022, 7, 6).strftime("%Y%-m%-d"), time(2022, 8, 17).strftime("%Y%-m%-d"), time(2022, 10, 12).strftime("%Y%-m%-d"), ] USE_CPI = True CPI = [ time(2022, 1, 12).strftime("%Y%-m%-d"), time(2022, 2, 10).strftime("%Y%-m%-d"), time(2022, 3, 10).strftime("%Y%-m%-d"), time(2022, 4, 12).strftime("%Y%-m%-d"), time(2022, 5, 11).strftime("%Y%-m%-d"), time(2022, 6, 10).strftime("%Y%-m%-d"), time(2022, 7, 13).strftime("%Y%-m%-d"), time(2022, 8, 10).strftime("%Y%-m%-d"), time(2022, 9, 13).strftime("%Y%-m%-d"), time(2022, 10, 13).strftime("%Y%-m%-d"), time(2022, 11, 13).strftime("%Y%-m%-d"), time(2022, 12, 13).strftime("%Y%-m%-d"), ]
#region imports from AlgorithmImports import * #endregion from datetime import* USE_FOMC_MEETINGS = False FOMC_MEETINGS = [ datetime(2022, 1, 25).strftime("%Y%-m%-d"), datetime(2022, 1, 26).strftime("%Y%-m%-d"), datetime(2022, 3, 15).strftime("%Y%-m%-d"), datetime(2022, 3, 16).strftime("%Y%-m%-d"), datetime(2022, 5, 3).strftime("%Y%-m%-d"), datetime(2022, 5, 4).strftime("%Y%-m%-d"), datetime(2022, 6, 14).strftime("%Y%-m%-d"), datetime(2022, 6, 15).strftime("%Y%-m%-d"), datetime(2022, 7, 26).strftime("%Y%-m%-d"), datetime(2022, 7, 27).strftime("%Y%-m%-d"), datetime(2022, 9, 20).strftime("%Y%-m%-d"), datetime(2022, 9 ,21).strftime("%Y%-m%-d"), datetime(2022, 11, 1).strftime("%Y%-m%-d"), datetime(2022, 11, 2).strftime("%Y%-m%-d"), datetime(2022, 12, 13).strftime("%Y%-m%-d"), datetime(2022, 12, 14).strftime("%Y%-m%-d"), ] USE_FOMC_MINUTES = False FOMC_MINUTES = [ datetime(2022, 2 ,16).strftime("%Y%-m%-d"), datetime(2022, 4, 6).strftime("%Y%-m%-d"), datetime(2022, 5, 25).strftime("%Y%-m%-d"), datetime(2022, 7, 6).strftime("%Y%-m%-d"), datetime(2022, 8, 17).strftime("%Y%-m%-d"), datetime(2022, 10, 12).strftime("%Y%-m%-d"), ] USE_CPI = False CPI = [ datetime(2022, 1, 12).strftime("%Y%-m%-d"), datetime(2022, 2, 10).strftime("%Y%-m%-d"), datetime(2022, 3, 10).strftime("%Y%-m%-d"), datetime(2022, 4, 12).strftime("%Y%-m%-d"), datetime(2022, 5, 11).strftime("%Y%-m%-d"), datetime(2022, 6, 10).strftime("%Y%-m%-d"), datetime(2022, 7, 13).strftime("%Y%-m%-d"), datetime(2022, 8, 10).strftime("%Y%-m%-d"), datetime(2022, 9, 13).strftime("%Y%-m%-d"), datetime(2022, 10, 13).strftime("%Y%-m%-d"), datetime(2022, 11, 13).strftime("%Y%-m%-d"), datetime(2022, 12, 13).strftime("%Y%-m%-d"), ] USE_LOWER_THRESHOLD = True MINIMUM_DISTANCE = 0.03 MINIMUM_GAP = 1 MAXIMUM_GAP = 5 LIST_OF_PERCENTAGES = [0.01, 0.02, 0.03] LIST_OF_GAPS = [4,5,6] PREMIUM_THRESHOLD = 0.07 PREMIUM_MULTIPLIER = 1.5 STOP_LOSS_MULTIPLIER = 8 PROFIT_PERCENT = 50000000 AMOUNT_OF_CONTRACTS = 10 PREMIUM_RANGE_RATIO = 10 AVG_DAYS = 14 USE_5_5 = False NEW_THRESHOLD = 0.15 ALLOW_RE_ENTER = False USE_AVG = False RANGE_MAX = 0.02 STRIKE_MAX = 5 RANGE_WEIGHT = 0.05 STRIKE_WEIGHT = 0.2
from AlgorithmImports import * from collections import deque import config import statistics as stats import requests class SmoothFluorescentYellowCaribou(QCAlgorithm): def Initialize(self): self.SetStartDate(2022, 1, 20) # Set Start Date #self.SetEndDate(2022, 1, 27) self.SetCash(1000000) # Set Strategy Cash #self.Settings.DataSubscriptionLimit = 300 self.Spy = self.AddEquity("SPY", Resolution.Minute, extendedMarketHours=False).Symbol self.option = self.AddOption(self.Spy, Resolution.Minute) self.option.SetLeverage(1.0) self.Premium_Multiplier = config.PREMIUM_MULTIPLIER self.List_Of_Perentages = config.LIST_OF_PERCENTAGES self.List_Of_Gaps = config.LIST_OF_GAPS self.option_symbol = self.option.Symbol self.option.SetFilter(self.UniverseFunc) self.Iron_Condor_Counter = 0 self.Friday_Window = False self.Wednesday_Window = True self.Monday_Window = False self.Indicator_Average_Call_Deque = deque(maxlen=10) self.Indicator_Average_Put_Deque = deque(maxlen=10) self.Indicator_Average_Call = 0 self.Indicator_Average_Put = 0 self.Schedule.On(self.DateRules.Every(DayOfWeek.Friday), self.TimeRules.At(9, 30), self.Open_Friday_Window) self.Schedule.On(self.DateRules.Every(DayOfWeek.Friday), self.TimeRules.At(15, 59), self.Close_Friday_Window) self.Schedule.On(self.DateRules.Every(DayOfWeek.Monday), self.TimeRules.At(9, 30), self.Open_Monday_Window) self.Schedule.On(self.DateRules.Every(DayOfWeek.Monday), self.TimeRules.At(15, 59), self.Close_Monday_Window) self.Schedule.On(self.DateRules.Every(DayOfWeek.Wednesday), self.TimeRules.At(9, 30), self.Open_Wednesday_Window) self.Schedule.On(self.DateRules.Every(DayOfWeek.Wednesday), self.TimeRules.At(15, 59), self.Close_Wednesday_Window) self.Schedule.On(self.DateRules.On(2022, 2, 23), self.TimeRules.AfterMarketOpen("SPY", 1), self.Close_It) self.Schedule.On(self.DateRules.On(2022, 5, 18), self.TimeRules.At(9,35), self.May_Debug) self.Schedule.On(self.DateRules.Every(DayOfWeek.Monday), self.TimeRules.AfterMarketOpen("SPY", 1), self.Get_Open_Price) self.Schedule.On(self.DateRules.Every(DayOfWeek.Wednesday), self.TimeRules.AfterMarketOpen("SPY", 1), self.Get_Open_Price) self.Schedule.On(self.DateRules.Every(DayOfWeek.Friday), self.TimeRules.AfterMarketOpen("SPY", 1), self.Get_Open_Price) self.Schedule.On(self.DateRules.Every(DayOfWeek.Monday), self.TimeRules.AfterMarketOpen("SPY", 2), self.Get_Open_Price_2) self.Schedule.On(self.DateRules.Every(DayOfWeek.Wednesday), self.TimeRules.AfterMarketOpen("SPY", 2), self.Get_Open_Price_2) self.Schedule.On(self.DateRules.Every(DayOfWeek.Friday), self.TimeRules.AfterMarketOpen("SPY", 2), self.Get_Open_Price_2) self.Schedule.On(self.DateRules.Every(DayOfWeek.Monday), self.TimeRules.BeforeMarketClose("SPY", 1), self.Check_At_Loss) self.Schedule.On(self.DateRules.Every(DayOfWeek.Wednesday), self.TimeRules.BeforeMarketClose("SPY", 1), self.Check_At_Loss) self.Schedule.On(self.DateRules.Every(DayOfWeek.Friday), self.TimeRules.BeforeMarketClose("SPY", 1), self.Check_At_Loss) self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.AfterMarketOpen("SPY", 5), self.Get_Average) self.Traded_Today = False self.Minimum_Distance = config.MINIMUM_DISTANCE self.Minimum_Gap = config.MINIMUM_GAP self.Maximum_Gap = config.MAXIMUM_GAP self.Premium_Threshold = config.PREMIUM_THRESHOLD self.Range_Dictionary = {} self.Iron_Condors = {} self.Counter = 0 self.Put_Buy_Strike = None self.Put_Sell_Strike = None self.Call_Buy_Strike = None self.Call_Sell_Strike = None self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.Every(timedelta(minutes=31)), self.Reset_Counter) self.List_Of_Len = [] self.Previous_Len = 0 self.Stop_Loss_Multiplier = config.STOP_LOSS_MULTIPLIER self.Profit_Percent = 1 - (config.PROFIT_PERCENT/100) self.Amount_Of_Contracts = config.AMOUNT_OF_CONTRACTS self.Invested_Condor = None self.Previous_Fees = None self.Max_Unrealized_Loss = 0 self.Max_Loss_Dollar = 0 self.Unrealized_Losses = {} self.List_Of_Max_Unrealized = [] self.List_Of_Losses = [] self.Max_Loss = 0 self.Avg_Loss = 0 self.Launched_After_Open = False self.Open_Price = None self.Allow_Getting_Average = False self.Populated_List = False self.Len_List = None self.Average_Premium = None self.Premium_At_Open = None self.Premium_List = deque(maxlen=config.AVG_DAYS) self.Current_Average = None self.Appended_Today = False self.Ran_Universe_Func_Today = False self.Put_Combinations = {} self.Call_Combinations = {} self.Created_Combinations_Today = False self.Previous_Average = None self.Monday_Premium_Queue = deque(maxlen=5) self.Wednesday_Premium_Queue = deque(maxlen=5) self.Friday_Premium_Queue = deque(maxlen=5) self.Dont_Enter = False self.Allow_Re_enter = config.ALLOW_RE_ENTER self.Entered_With_PT = False self.Condor_List = [] self.Sorted_Today = False def Get_Average(self): self.Allow_Getting_Average = True def Check_At_Loss(self): if self.Invested_Condor is not None: if self.Securities[self.Spy].High >= self.Invested_Condor.Call_Sell.Strike: self.Liquidate() elif self.Securities[self.Spy].Low <= self.Invested_Condor.Put_Sell.Strike: self.Liquidate() def Get_Open_Price(self): self.Open_Price = self.Securities[self.Spy].Open #self.Debug(self.Time) def Get_Open_Price_2(self): if self.Open_Price is None: self.Open_Price = self.Securities[self.Spy].Open def Close_It(self): self.Monday_Window = False self.Wednesday_Window = False self.Friday_Window = False def Record_Premium_At_Open(self, premium): if self.Time.weekday() == 0: self.Monday_Premium_Queue.appendleft(premium) elif self.Time.weekday() == 2: self.Wednesday_Premium_Queue.appendleft(premium) elif self.Time.weekday() == 4: self.Friday_Premium_Queue.appendleft(premium) def Reset_Counter(self): for x in self.Iron_Condors.keys(): condor = self.Iron_Condors[x] condor.Sent_Already = False condor.Scanned_Already = False self.Counter = 0 def Open_Monday_Window(self): self.Appended_Today = False self.Put_Buy_Strike = None self.Put_Sell_Strike = None self.Call_Buy_Strike = None self.Call_Sell_Strike = None self.Call_Strike = None self.Put_Strike = None self.Monday_Window = True self.Previous_Fees = self.Portfolio.TotalFees self.Invested_Condor = None self.Traded_Today = False self.Previous_AccountValue = self.Portfolio.TotalPortfolioValue if config.USE_FOMC_MINUTES: if self.Time.strftime("%Y%-m%d") in config.FOMC_MINUTES: self.Dont_Enter = True if config.USE_FOMC_MEETINGS: if self.Time.strftime("%Y%-m%d") in config.FOMC_MEETINGS: self.Dont_Enter = True if config.USE_CPI: if self.Time.strftime("%Y%-m%d") in config.CPI: self.Dont_Enter = True def Open_Wednesday_Window(self): self.Appended_Today = False self.Put_Buy_Strike = None self.Put_Sell_Strike = None self.Call_Buy_Strike = None self.Call_Sell_Strike = None self.Call_Strike = None self.Put_Strike = None self.Wednesday_Window = True self.Previous_Fees = self.Portfolio.TotalFees self.Invested_Condor = None self.Traded_Today = False self.Previous_AccountValue = self.Portfolio.TotalPortfolioValue if config.USE_FOMC_MINUTES: if self.Time.strftime("%Y%-m%d") in config.FOMC_MINUTES: self.Dont_Enter = True if config.USE_FOMC_MEETINGS: if self.Time.strftime("%Y%-m%d") in config.FOMC_MEETINGS: self.Dont_Enter = True if config.USE_CPI: if self.Time.strftime("%Y%-m%d") in config.CPI: self.Dont_Enter = True def Open_Friday_Window(self): self.Appended_Today = False self.Put_Buy_Strike = None self.Put_Sell_Strike = None self.Call_Buy_Strike = None self.Call_Sell_Strike = None self.Call_Strike = None self.Put_Strike = None self.Friday_Window = True self.Previous_Fees = self.Portfolio.TotalFees self.Invested_Condor = None self.Traded_Today = False self.Previous_AccountValue = self.Portfolio.TotalPortfolioValue if config.USE_FOMC_MINUTES: if self.Time.strftime("%Y%-m%d") in config.FOMC_MINUTES: self.Dont_Enter = True if config.USE_FOMC_MEETINGS: if self.Time.strftime("%Y%-m%d") in config.FOMC_MEETINGS: self.Dont_Enter = True if config.USE_CPI: if self.Time.strftime("%Y%-m%d") in config.CPI: self.Dont_Enter = True # def OnEndOfAlgorithm(self): # self.Debug(self.Unrealized_Losses) # avg_loss = stats.mean(self.List_Of_Losses) # max_realized_loss = min(self.List_Of_Losses) # max_unrealized_loss = max(self.List_Of_Max_Unrealized) # loss_count = len(self.List_Of_Losses) # self.Debug(f"avg loss {avg_loss} max loss {max_realized_loss} max unrealized loss {max_unrealized_loss} loss count {loss_count}") # self.Debug(self.List_Of_Losses) # self.Debug(f"Counter {self.Iron_Condor_Counter}") def Close_Monday_Window(self): time = self.Time.strftime("%y%b%d%a") string_1 = str(self.Max_Unrealized_Loss) string_2 = str(self.Max_Loss_Dollar) string_3 = string_1 + "/" + string_2 self.Unrealized_Losses[time] = string_3 self.Monday_Window = False self.Sent_Already = [] self.Iron_Condors.clear() self.List_Of_Max_Unrealized.append(self.Max_Loss_Dollar) self.Max_Unrealized_Loss = 0 self.Max_Loss_Dollar = 0 account_value = self.Portfolio.TotalPortfolioValue pnl = account_value - self.Previous_AccountValue close_price = self.Securities[self.Spy].Close self.Len_List = None self.Average_Premium = None self.Populated_List = False self.Open_Price = None self.Premium_At_Open = None self.Ran_Universe_Func_Today = False self.Put_Combinations.clear() self.Call_Combinations.clear() self.Created_Combinations_Today = False self.Dont_Enter = False self.Entered_With_PT = False self.Sorted_Today = False self.Condor_List.clear() self.Debug(f"Call {self.Indicator_Average_Call} Put {self.Indicator_Average_Put} {self.Time}") if pnl < 0: self.List_Of_Losses.append(pnl) def Close_Wednesday_Window(self): time = self.Time.strftime("%y%b%d%a") string_1 = str(self.Max_Unrealized_Loss) string_2 = str(self.Max_Loss_Dollar) string_3 = string_1 + "/" + string_2 self.Unrealized_Losses[time] = string_3 self.Wednesday_Window = False self.Sent_Already = [] self.Iron_Condors.clear() self.List_Of_Max_Unrealized.append(self.Max_Loss_Dollar) self.Max_Unrealized_Loss = 0 self.Max_Loss_Dollar = 0 account_value = self.Portfolio.TotalPortfolioValue pnl = account_value - self.Previous_AccountValue close_price = self.Securities[self.Spy].Close self.Len_List = None self.Average_Premium = None self.Populated_List = False self.Open_Price = None self.Premium_At_Open = None self.Ran_Universe_Func_Today = False self.Put_Combinations.clear() self.Call_Combinations.clear() self.Created_Combinations_Today = False self.Dont_Enter = False self.Entered_With_PT = False self.Sorted_Today = False self.Condor_List.clear() self.Debug(f"Call {self.Indicator_Average_Call} Put {self.Indicator_Average_Put} {self.Time}") if pnl < 0: self.List_Of_Losses.append(pnl) def Close_Friday_Window(self): time = self.Time.strftime("%y%b%d%a") string_1 = str(self.Max_Unrealized_Loss) string_2 = str(self.Max_Loss_Dollar) string_3 = string_1 + "/" + string_2 self.Unrealized_Losses[time] = string_3 self.Friday_Window = False self.Sent_Already = [] #self.Debug(len(self.Iron_Condors)) self.Iron_Condors.clear() self.List_Of_Max_Unrealized.append(self.Max_Loss_Dollar) self.Max_Unrealized_Loss = 0 self.Max_Loss_Dollar = 0 account_value = self.Portfolio.TotalPortfolioValue pnl = account_value - self.Previous_AccountValue close_price = self.Securities[self.Spy].Close self.Len_List = None self.Average_Premium = None self.Populated_List = False self.Open_Price = None self.Premium_At_Open = None self.Ran_Universe_Func_Today = False self.Put_Combinations.clear() self.Call_Combinations.clear() self.Created_Combinations_Today = False self.Dont_Enter = False self.Entered_With_PT = False self.Sorted_Today = False self.Condor_List.clear() self.Debug(f"Call {self.Indicator_Average_Call} Put {self.Indicator_Average_Put} {self.Time}") if pnl < 0: self.List_Of_Losses.append(pnl) def May_Debug(self): self.Debug(f"{self.Time} Premium {self.Premium_At_Open}") def Get_Max_Unrealized_Loss(self, current_profit): if self.Max_Unrealized_Loss == 0: if self.Invested_Condor.Profit == 0: return if current_profit > self.Invested_Condor.Profit: self.Max_Unrealized_Loss = abs(current_profit)/self.Invested_Condor.Profit if self.Max_Unrealized_Loss != 0: x = abs(current_profit)/self.Invested_Condor.Profit if x > self.Max_Unrealized_Loss: self.Max_Unrealized_Loss = x self.Max_Loss_Dollar = abs(current_profit) - self.Invested_Condor.Profit def Profit_Taker(self): if self.Invested_Condor.Current_Profit <= self.Profit_Percent * self.Invested_Condor.Entry_Premium: if self.Portfolio.Invested: self.Liquidate(tag="PROFIT TAKER") self.Entered_With_PT = False self.Invested_Condor.Profit_Taken = True self.Debug(f"{self.Time} PROFIT TAKEN") def Stop_Loss(self): pnl = self.Portfolio.TotalUnrealizedProfit max_loss = -(self.Invested_Condor.Profit * self.Stop_Loss_Multiplier) if pnl <= max_loss: if self.Portfolio.Invested: self.Liquidate(tag=f"STOP LOSS {pnl} {max_loss} {self.Invested_Condor.Profit} {self.Invested_Condor.Entry_Premium} {self.Invested_Condor.Current_Profit}") self.Invested_Condor.Stopped_Out = True def OnData(self, data: Slice): if self.Portfolio.Invested: if self.Invested_Condor.Profit is None: fees = self.Portfolio.TotalFees - self.Previous_Fees initial_profit = (self.Invested_Condor.Entry_Premium * 100) * self.Amount_Of_Contracts self.Invested_Condor.Profit = initial_profit - fees fees = self.Portfolio.TotalFees - self.Previous_Fees current_premium = self.Invested_Condor.Calculate_Premium(self.Securities[self.Invested_Condor.Put_Buy.Symbol].AskPrice, self.Securities[self.Invested_Condor.Put_Sell.Symbol].BidPrice, self.Securities[self.Invested_Condor.Call_Buy.Symbol].AskPrice, self.Securities[self.Invested_Condor.Call_Sell.Symbol].BidPrice) current_premium = current_premium self.Invested_Condor.Current_Profit = current_premium if self.Invested_Condor.Entry_Fees is None: self.Invested_Condor.Entry_Fees = fees self.Get_Max_Unrealized_Loss(self.Invested_Condor.Current_Profit) #self.Profit_Taker() #self.Stop_Loss() # if self.Portfolio.Invested: # if self.Invested_Condor.Profit is None: # fees = self.Portfolio.TotalFees - self.Previous_Fees # profit = (self.Invested_Condor.Entry_Premium * 100) * self.Amount_Of_Contracts # self.Invested_Condor.Profit = profit - fees # self.Get_Max_Unrealized_Loss() # self.Profit_Taker() # self.Stop_Loss() if self.Portfolio["SPY"].Invested: self.Liquidate("SPY") if self.Portfolio.Invested: return for i in data.OptionChains: # Checking if we are at the right symbol (SPY) if i.Key != self.option_symbol: continue optionchain = i.Value # if option chain is empty return if (data.OptionChains.Count == 0): return # if self.Portfolio.Invested: # if self.Invested_Condor.Profit is None: # fees = self.Portfolio.TotalFees - self.Previous_Fees # initial_profit = (self.Invested_Condor.Entry_Premium * 100) * self.Amount_Of_Contracts # self.Invested_Condor.Profit = initial_profit - fees # fees = self.Portfolio.TotalFees - self.Previous_Fees # current_premium = self.Invested_Condor.Calculate_Premium(self.Securities[self.Invested_Condor.Put_Buy.Symbol].AskPrice, self.Securities[self.Invested_Condor.Put_Sell.Symbol].BidPrice, self.Securities[self.Invested_Condor.Call_Buy.Symbol].AskPrice, self.Securities[self.Invested_Condor.Call_Sell.Symbol].BidPrice) # current_premium = (current_premium * 100) * self.Amount_Of_Contracts # self.Invested_Condor.Current_Profit = current_premium # if self.Invested_Condor.Entry_Fees is None: # self.Invested_Condor.Entry_Fees = fees # self.Get_Max_Unrealized_Loss(self.Invested_Condor.Current_Profit) # self.Profit_Taker() # #self.Stop_Loss() Price = self.Securities[self.Spy].Price Price = math.floor(Price) if self.Friday_Window or self.Wednesday_Window or self.Monday_Window: # List_Of_Distances = [] # #self.Debug("IN WINDOW") # # Grab all put and all call contracts and store them in a list each # put_3_expiry = [x for x in optionchain if x.Right == OptionRight.Put and ((x.Expiry - self.Time).days) == -1] # call_3_expiry = [x for x in optionchain if x.Right == OptionRight.Call and ((x.Expiry - self.Time).days) == -1] # expiry_date = "TODAY" if self.Open_Price is None: self.Open_Price = self.Securities[self.Spy].Open # if len(put_3_expiry) == 0 and len(call_3_expiry) == 0: # expiry_date = "TOMORROW" # put_3_expiry = [x for x in optionchain if x.Right == OptionRight.Put and ((x.Expiry - self.Time).days) == 0] # call_3_expiry = [x for x in optionchain if x.Right == OptionRight.Call and ((x.Expiry - self.Time).days) == 0] # if (len(put_3_expiry) + len(call_3_expiry)) != self.Previous_Len: # self.Debug(f"{len(call_3_expiry)} {len(put_3_expiry)}") # self.List_Of_Len.append((len(put_3_expiry) + len(call_3_expiry))) # self.Previous_Len = len(put_3_expiry) + len(call_3_expiry) # Calculate distance of how much lower than current price the strike price for put contracts has to be # # Using minimum percentage distance and then rounding the number down # put_3_min_distance = Price*1 # put_3_min_distance = math.floor(put_3_min_distance) # # Adding all put contracts that are far enough away from current price to a new list # put_3_min_distances_sell = [x for x in put_3_expiry] # put_3_min_distances_buy = [x for x in put_3_expiry] # # Calculate distance of how much lower than current price the strike price for call contracts has to be # # Using minimum percentage distance and then rounding the number up # call_3_min_distance = Price*1 # call_3_min_distance = math.ceil(call_3_min_distance) # # Adding all call contracts that are far enough away from current price to a new list # call_3_min_distances_sell = [x for x in call_3_expiry] # call_3_min_distances_buy = [x for x in call_3_expiry] # # Sorting both lists by Strike price, put list will start with the highest puts and call list with the lowest call # put_3_min_distances_sorted_sell = sorted(put_3_min_distances_sell, key=lambda x: x.Strike, reverse=True) # call_3_min_distances_sorted_sell = sorted(call_3_min_distances_sell, key=lambda x: x.Strike, reverse=False) # put_3_min_distances_sorted_buy = sorted(put_3_min_distances_buy, key=lambda x: x.Strike, reverse=True) # call_3_min_distances_sorted_buy = sorted(call_3_min_distances_buy, key=lambda x: x.Strike, reverse=False) expiry_date = "TODAY" if not self.Created_Combinations_Today: List_Of_Distances = [] #self.Debug("IN WINDOW") # Grab all put and all call contracts and store them in a list each put_3_expiry = [x for x in optionchain if x.Right == OptionRight.Put and ((x.Expiry - self.Time).days) == -1 and (x.Strike - Price) < 1] call_3_expiry = [x for x in optionchain if x.Right == OptionRight.Call and ((x.Expiry - self.Time).days) == -1 and (Price - x.Strike) < 1] put_3_min_distance = Price*1 put_3_min_distance = math.floor(put_3_min_distance) # Adding all put contracts that are far enough away from current price to a new list put_3_min_distances_sell = [x for x in put_3_expiry] put_3_min_distances_buy = [x for x in put_3_expiry] # Calculate distance of how much lower than current price the strike price for call contracts has to be # Using minimum percentage distance and then rounding the number up call_3_min_distance = Price*1 call_3_min_distance = math.ceil(call_3_min_distance) # Adding all call contracts that are far enough away from current price to a new list call_3_min_distances_sell = [x for x in call_3_expiry] call_3_min_distances_buy = [x for x in call_3_expiry] # Sorting both lists by Strike price, put list will start with the highest puts and call list with the lowest call put_3_min_distances_sorted_sell = sorted(put_3_min_distances_sell, key=lambda x: x.Strike, reverse=True) call_3_min_distances_sorted_sell = sorted(call_3_min_distances_sell, key=lambda x: x.Strike, reverse=False) put_3_min_distances_sorted_buy = sorted(put_3_min_distances_buy, key=lambda x: x.Strike, reverse=True) call_3_min_distances_sorted_buy = sorted(call_3_min_distances_buy, key=lambda x: x.Strike, reverse=False) # for put in put_3_min_distances_sorted_buy: # if put.Strike < 376: # self.Debug(f"put {put.Strike}") # for call in call_3_min_distances_sorted_buy: # if call.Strike > 399: # self.Debug(f"call {call.Strike}") put_list = [] call_list = [] for x in put_3_min_distances_sorted_sell: for y in put_3_min_distances_sorted_buy: strike_diff = x.Strike - y.Strike if x.Strike > y.Strike and strike_diff >= 1 and strike_diff <= 5: combo = (x, y) put_list.append(combo) #if strike_diff < 5: #self.Debug(f"{self.Time} {strike_diff}") for x in call_3_min_distances_sorted_sell: for y in call_3_min_distances_sorted_buy: strike_diff = y.Strike - x.Strike if y.Strike > x.Strike and strike_diff >= 1 and strike_diff <= 5: combo = (x, y) call_list.append(combo) #self.Debug(len(call_list)) #self.Debug(len(put_list)) # for call in self.Call_Combinations.values(): # self.Debug(f"call {call.S.Strike} {call.B.Strike}") # for put in self.Put_Combinations.values(): # self.Debug(f"put {put.S.Strike} {put.B.Strike}") for ps, pb in put_list: #self.Debug(f"{put.S.Strike} {put.B.Strike}") for cs, cb in call_list: # if call.S.Strike > 395 and put.S.Strike < 379: # self.Debug(f"{call.S.Strike} {put.S.Strike}") p_diff = ps.Strike - pb.Strike c_diff = cb.Strike - cs.Strike # if p_diff <=5: # self.Debug(f"p {p_diff} {self.Time}") if cs.Strike > ps.Strike and p_diff == c_diff: Strikes = f"{pb.Strike} {ps.Strike} {cs.Strike} {cb.Strike}" if Strikes not in self.Iron_Condors and self.Open_Price is not None: open_call_range = ((cs.Strike - self.Open_Price) / self.Open_Price) open_put_range = ((self.Open_Price - ps.Strike) / ps.Strike) self.Iron_Condors[Strikes] = Iron_Condor(self, pb, ps, cb, cs, open_call_range, open_put_range) #self.Debug(f"call {open_call_range} put {open_put_range}") #self.Debug(Strikes) #if p_diff < 5: #self.Debug(f"{self.Time} {p_diff}") #self.Debug(f"ps {ps_strikes} ----------------- pb {pb_strikes} -----------------cs {cs_strikes} -----------------cb {cb_strikes}") weight = self.Assign_Weighting(open_call_range, open_put_range, p_diff) self.Iron_Condors[Strikes].Weight = weight if open_call_range >= 0.024 and open_call_range < 0.027 and open_put_range >= 0.024 and open_put_range < 0.027: #self.Debug(f"{open_call_range} {open_put_range}") condor = self.Iron_Condors[Strikes] premium = self.Iron_Condors[Strikes].Calculate_Premium(self.Securities[condor.Put_Buy.Symbol].AskPrice, self.Securities[condor.Put_Sell.Symbol].BidPrice, self.Securities[condor.Call_Buy.Symbol].AskPrice, self.Securities[condor.Call_Sell.Symbol].BidPrice) self.Premium_At_Open = premium if not self.Appended_Today: self.Premium_List.appendleft(premium) self.Appended_Today = True self.Record_Premium_At_Open(premium) # for call in self.Call_Combinations.values(): # #self.Debug(f"{put.S.Strike} {put.B.Strike}") # for put in self.Put_Combinations.values(): # # if call.S.Strike > 395 and put.S.Strike < 379: # # self.Debug(f"{call.S.Strike} {put.S.Strike}") # p_diff = put.S.Strike - put.B.Strike # c_diff = call.B.Strike - call.S.Strike # # if p_diff <=5: # # self.Debug(f"p {p_diff} {self.Time}") # if call.S.Strike > put.S.Strike and p_diff == c_diff: # Strikes = f"{put.B.Strike} {put.S.Strike} {call.S.Strike} {call.B.Strike}" # if Strikes not in self.Iron_Condors and self.Open_Price is not None: # open_call_range = ((call.S.Strike - self.Open_Price) / self.Open_Price) # open_put_range = ((self.Open_Price - put.S.Strike) / put.S.Strike) # self.Iron_Condors[Strikes] = Iron_Condor(self, put.B, put.S, call.B, call.S, open_call_range, open_put_range) # #self.Debug(f"call {open_call_range} put {open_put_range}") # #self.Debug(Strikes) # #if p_diff < 5: # #self.Debug(f"{self.Time} {p_diff}") # #self.Debug(f"ps {ps_strikes} ----------------- pb {pb_strikes} -----------------cs {cs_strikes} -----------------cb {cb_strikes}") # if open_call_range >= 0.024 and open_call_range < 0.027 and open_put_range >= 0.024 and open_put_range < 0.027: # #self.Debug(f"{open_call_range} {open_put_range}") # condor = self.Iron_Condors[Strikes] # premium = self.Iron_Condors[Strikes].Calculate_Premium(self.Securities[condor.Put_Buy.Symbol].AskPrice, self.Securities[condor.Put_Sell.Symbol].BidPrice, self.Securities[condor.Call_Buy.Symbol].AskPrice, self.Securities[condor.Call_Sell.Symbol].BidPrice) # self.Premium_At_Open = premium # if not self.Appended_Today: # self.Premium_List.appendleft(premium) # self.Appended_Today = True #self.Debug(f"call {open_call_range} put {open_put_range} {premium}") for x in self.Iron_Condors.keys(): condor = self.Iron_Condors[x] self.Condor_List.append((condor.Weight, condor)) self.Condor_List.sort(key=lambda x: x[0], reverse = True) for x in self.Condor_List: condor = x[1] if condor.Calculate_Premium(self.Securities[condor.Put_Buy.Symbol].AskPrice, self.Securities[condor.Put_Sell.Symbol].BidPrice, self.Securities[condor.Call_Buy.Symbol].AskPrice, self.Securities[condor.Call_Sell.Symbol].BidPrice) >= 0.07: self.Indicator_Average_Call_Deque.appendleft(condor.open_call_range) self.Indicator_Average_Put_Deque.appendleft(condor.open_put_range) if len(self.Indicator_Average_Call_Deque) == 10 and len(self.Indicator_Average_Put_Deque) == 10: self.Indicator_Average_Call = sum(self.Indicator_Average_Call_Deque) / len(self.Indicator_Average_Call_Deque) self.Indicator_Average_Put = sum(self.Indicator_Average_Put_Deque) / len(self.Indicator_Average_Put_Deque) break #for x in self.Condor_List: #condor = x[1] #self.Debug(f"{condor.open_call_range} {condor.open_put_range} {x[0]} Debugged") self.Created_Combinations_Today = True put_list.clear() call_list.clear() put_3_expiry.clear() call_3_expiry.clear() put_3_min_distances_buy.clear() put_3_min_distances_sell.clear() put_3_min_distances_sorted_buy.clear() put_3_min_distances_sorted_sell.clear() call_3_min_distances_buy.clear() call_3_min_distances_sell.clear() call_3_min_distances_sorted_buy.clear() call_3_min_distances_sorted_sell.clear() #self.Debug(len(self.Iron_Condors)) premium_list = [] # for ps, cs in zip(put_3_min_distances_sorted_sell, call_3_min_distances_sorted_sell): # for pb, cb in zip(put_3_min_distances_sorted_buy, call_3_min_distances_sorted_buy): # put_gap = ps.Strike - pb.Strike # call_gap = cb.Strike - cs.Strike # # if put_gap == self.Maximum_Gap and call_gap == self.Maximum_Gap: # # self.Debug("MAX") # if put_gap >= self.Minimum_Gap and put_gap <= self.Maximum_Gap and put_gap == call_gap and call_gap >= self.Minimum_Gap and call_gap <= self.Maximum_Gap and ps.Strike < cs.Strike: # Strikes = f"{pb.Strike} {ps.Strike} {cs.Strike} {cb.Strike}" # if Strikes not in self.Iron_Condors: # if self.Open_Price is not None: # #if not self.Launched_After_Open: # #open_call_range = ((cs.Strike - self.Securities[self.Spy].Price) / self.Securities[self.Spy].Price) # #open_put_range = ((self.Securities[self.Spy].Price - ps.Strike) / ps.Strike) # open_call_range = ((cs.Strike - self.Open_Price) / self.Open_Price) # open_put_range = ((self.Open_Price - ps.Strike) / ps.Strike) # self.Iron_Condors[Strikes] = Iron_Condor(self, pb, ps, cb, cs, open_call_range, open_put_range) # self.Debug(f"call {open_call_range} put {open_put_range}") # #self.Debug(Strikes) # #self.Debug(f"ps {ps_strikes} ----------------- pb {pb_strikes} -----------------cs {cs_strikes} -----------------cb {cb_strikes}") # if open_call_range >= 0.024 and open_call_range < 0.027 and open_put_range >= 0.024 and open_put_range < 0.027: # #self.Debug(f"{open_call_range} {open_put_range}") # condor = self.Iron_Condors[Strikes] # premium = self.Iron_Condors[Strikes].Calculate_Premium(self.Securities[condor.Put_Buy.Symbol].AskPrice, self.Securities[condor.Put_Sell.Symbol].BidPrice, self.Securities[condor.Call_Buy.Symbol].AskPrice, self.Securities[condor.Call_Sell.Symbol].BidPrice) # self.Premium_At_Open = premium # if not self.Appended_Today: # self.Premium_List.appendleft(premium) # self.Appended_Today = True # if premium > 0.23: # self.Debug(f"{self.Time} {premium}") if len(self.Premium_List) == config.AVG_DAYS: self.Current_Average = stats.mean(self.Premium_List) if not self.Portfolio.Invested: if not self.Entered_With_PT and not self.Portfolio.Invested: for x in self.Condor_List: condor = x[1] if not condor.Sent_Already: # if condor.Put_Sell.Strike < 373: # self.Debug(x) # if x == "365 368 392 395": # self.Debug("HEURIKA") condor_distance_put = self.Securities[self.Spy].Price - condor.Put_Sell.Strike condor_distance_call = condor.Call_Sell.Strike - self.Securities[self.Spy].Price put_call_distance = abs(condor_distance_put - condor_distance_call) strike_Gap = condor.Call_Buy.Strike - condor.Call_Buy.Strike percentage_call = ((condor.Call_Sell.Strike - self.Securities[self.Spy].Price) / self.Securities[self.Spy].Price) percentage_put = ((self.Securities[self.Spy].Price - condor.Put_Sell.Strike) / condor.Put_Sell.Strike) if self.Counter >= 15: self.Premium_Threshold = 0.25 else: self.Premium_Threshold = config.PREMIUM_THRESHOLD percentage = ((abs(self.Securities[self.Spy].Price - condor.Put_Sell.Strike) / self.Securities[self.Spy].Price) + (abs(self.Securities[self.Spy].Price - condor.Call_Sell.Strike) / self.Securities[self.Spy].Price)) / 2 #if percentage >= 0.025 and condor.Premium > 0.05: #self.Debug(f"NOT SENT Premium {condor.Premium} // Strikes {x} // Price {Price} Percentage {percentage} {put_call_distance}") # if strike_Gap <=10 and percentage_call > self.Minimum_Distance and percentage_put > self.Minimum_Distance and condor.Calculate_Premium(self.Securities[condor.Put_Buy.Symbol].AskPrice, self.Securities[condor.Put_Sell.Symbol].BidPrice, self.Securities[condor.Call_Buy.Symbol].AskPrice, self.Securities[condor.Call_Sell.Symbol].BidPrice) >= self.Premium_Threshold: # percentage = ((abs(self.Securities[self.Spy].Price - condor.Put_Sell.Strike) / self.Securities[self.Spy].Price) + (abs(self.Securities[self.Spy].Price - condor.Call_Sell.Strike) / self.Securities[self.Spy].Price)) / 2 # percentage_call = ((condor.Call_Sell.Strike - self.Securities[self.Spy].Price) / self.Securities[self.Spy].Price) # percentage_put = ((self.Securities[self.Spy].Price - condor.Put_Sell.Strike) / condor.Put_Sell.Strike) # percentage = round(percentage, 3) # percentage_call = round(percentage_call, 3) # percentage_put = round(percentage_put, 3) # #if condor.Call_Buy.Strike - condor.Call_Sell.Strike >= 10: # Breakeven_Price_Call = condor.Call_Sell.Strike + condor.Premium # Breakeven_Price_Put = condor.Put_Sell.Strike - condor.Premium # #self.Debug(f" SEND Premium {condor.Premium} // Strikes {x} // Price {self.Securities[self.Spy].Price} Percentage {percentage} call {percentage_call} put {percentage_put} breakeven call {Breakeven_Price_Call} put {Breakeven_Price_Put}") # id_ = "-723291833" # message_ = f"Action Expiry Strike Type \n Sell {(condor.Put_Sell.Expiry).strftime('%b%d%-y')} ${condor.Put_Sell.Strike} Put \n Buy {(condor.Put_Buy.Expiry).strftime('%b%d%-y')} ${condor.Put_Buy.Strike} Put \n Buy {(condor.Call_Buy.Expiry).strftime('%b%d%-y') } ${condor.Call_Buy.Strike} Call \n Sell {(condor.Call_Sell.Expiry).strftime('%b%d%-y')} ${condor.Call_Sell.Strike} Call \n \n \n \n Price ${self.Securities[self.Spy].Price} \n Premium {condor.Premium} \n Strike diff ${abs(condor.Put_Sell.Strike - condor.Put_Buy.Strike)} \n Breakeven Price Call {Breakeven_Price_Call} \n Breakeven Price Put {Breakeven_Price_Put} \n Call %{percentage_call} \n Put %{percentage_put} \n %{percentage} \n expiry {expiry_date}" # # message_ = f"Call_Buy_Strike ${call_buy_strike} Call_Sell_Strike ${call_sell_strike} // Put_Buy_Strike ${put_buy_strike} Put_Sell_Strike ${put_sell_strike} // Premium {premium}" # token_ = "5616527268:AAGkUitVosgYOntpLb_JU5HK_SSXm86GsVs" # if not self.Portfolio.Invested: # if not condor.Stopped_Out and not condor.Profit_Taken: # if self.Counter <= 20: # self.Notify.Telegram(id=id_, message=message_, token=token_) # self.Counter = self.Counter + 1 # condor.Sent_Already = True # # self.MarketOrder(condor.Put_Buy.Symbol, self.Amount_Of_Contracts, tag =f"{percentage_put}") # # self.MarketOrder(condor.Put_Sell.Symbol, -self.Amount_Of_Contracts, tag =f"{percentage_put}") # # self.MarketOrder(condor.Call_Buy.Symbol, self.Amount_Of_Contracts, tag =f"{percentage_call}") # # self.MarketOrder(condor.Call_Sell.Symbol, -self.Amount_Of_Contracts, tag =f"{percentage_call}") # self.Put_Buy_Strike = condor.Put_Buy.Strike # self.Put_Sell_Strike = condor.Put_Sell.Strike # self.Call_Buy_Strike = condor.Call_Buy.Strike # self.Call_Sell_Strike = condor.Call_Sell.Strike # self.Invested_Condor = condor # condor.Entry_Premium = condor.Premium # condor.Margin_Required = ((condor.Put_Buy.Strike - condor.Put_Sell.Strike) * 100) * self.Amount_Of_Contracts if config.USE_5_5: if self.Time.weekday() == 0 and len(self.Monday_Premium_Queue) == 5: premiums = self.Premium_List + self.Monday_Premium_Queue self.Current_Average = stats.mean(premiums) elif self.Time.weekday() == 2 and len(self.Wednesday_Premium_Queue) == 5: premiums = self.Premium_List + self.Wednesday_Premium_Queue self.Current_Average = stats.mean(premiums) elif self.Time.weekday() == 4 and len(self.Friday_Premium_Queue) == 5: premiums = self.Premium_List + self.Friday_Premium_Queue self.Current_Average = stats.mean(premiums) if self.Current_Average != 0 and self.Current_Average is not None: self.Previous_Average = self.Current_Average if config.USE_AVG: if self.Current_Average is not None and self.Premium_At_Open is not None: if self.Premium_At_Open > self.Current_Average: if self.Current_Average != 0 and self.Premium_At_Open != 0: premium_higher_by = (abs(self.Premium_At_Open-self.Current_Average)) / self.Current_Average elif self.Current_Average == 0 and self.Premium_At_Open != 0: premium_higher_by = (abs(self.Premium_At_Open-self.Previous_Average)) / self.Previous_Average elif self.Premium_At_Open == 0: premium_higher_by = 0.0000001 distance_increase_factor = 1 + (abs(premium_higher_by) / config.PREMIUM_RANGE_RATIO) self.Minimum_Distance = min((self.Minimum_Distance * distance_increase_factor),0.034) else: self.Minimum_Distance = config.MINIMUM_DISTANCE if self.Traded_Today and config.USE_LOWER_THRESHOLD: self.Premium_Threshold = config.NEW_THRESHOLD else: self.Premium_Threshold = config.PREMIUM_THRESHOLD strike_Gap = condor.Call_Buy.Strike - condor.Call_Sell.Strike # if condor.Call_Sell.Strike > 390 and condor.Put_Sell.Strike < 379: # self.Debug(f"{strike_Gap} {condor.Calculate_Premium(self.Securities[condor.Put_Buy.Symbol].AskPrice, self.Securities[condor.Put_Sell.Symbol].BidPrice, self.Securities[condor.Call_Buy.Symbol].AskPrice, self.Securities[condor.Call_Sell.Symbol].BidPrice)} ps {condor.Put_Sell.Strike} pb {condor.Put_Buy.Strike} cs {condor.Call_Sell.Strike} cb {condor.Call_Buy.Strike}") # else: # self.Debug("NOT WORKING") # if condor.Calculate_Premium(self.Securities[condor.Put_Buy.Symbol].AskPrice, self.Securities[condor.Put_Sell.Symbol].BidPrice, self.Securities[condor.Call_Buy.Symbol].AskPrice, self.Securities[condor.Call_Sell.Symbol].BidPrice) >= 0.1: # self.Debug(f"{condor.open_call_range} {condor.open_put_range}") if strike_Gap <= 5 and condor.Calculate_Premium(self.Securities[condor.Put_Buy.Symbol].AskPrice, self.Securities[condor.Put_Sell.Symbol].BidPrice, self.Securities[condor.Call_Buy.Symbol].AskPrice, self.Securities[condor.Call_Sell.Symbol].BidPrice) >= self.Premium_Threshold and condor.open_call_range >= self.Minimum_Distance and condor.open_put_range >= self.Minimum_Distance and not self.Dont_Enter: percentage_call_ = ((condor.Call_Sell.Strike - self.Securities[self.Spy].Price) / self.Securities[self.Spy].Price) percentage_put_ = ((self.Securities[self.Spy].Price - condor.Put_Sell.Strike) / condor.Put_Sell.Strike) #self.Debug("HERE") percentage_call_ = round(percentage_call_, 3) percentage_put_ = round(percentage_put_, 3) Breakeven_Price_Call = condor.Call_Sell.Strike + condor.Premium Breakeven_Price_Put = condor.Put_Sell.Strike - condor.Premium id_ = "-723291833" message__ = f" THIS IS AN ALERT USING RANGE FROM OPEN \n PUT RANGE %{condor.open_put_range} FROM OPEN \n CALL RANGE %{condor.open_call_range} FROM OPEN \n Action Expiry Strike Type \n Sell {(condor.Put_Sell.Expiry).strftime('%b%d%-y')} ${condor.Put_Sell.Strike} Put \n Buy {(condor.Put_Buy.Expiry).strftime('%b%d%-y')} ${condor.Put_Buy.Strike} Put \n Buy {(condor.Call_Buy.Expiry).strftime('%b%d%-y') } ${condor.Call_Buy.Strike} Call \n Sell {(condor.Call_Sell.Expiry).strftime('%b%d%-y')} ${condor.Call_Sell.Strike} Call \n \n \n \n Price ${self.Securities[self.Spy].Price} \n Premium {condor.Premium} \n Strike diff ${abs(condor.Put_Sell.Strike - condor.Put_Buy.Strike)} \n Breakeven Price Call {Breakeven_Price_Call} \n Breakeven Price Put {Breakeven_Price_Put} \n Current Call %{percentage_call_} \n Current Put %{percentage_put_} \n expiry {expiry_date}" token_ = "5616527268:AAGkUitVosgYOntpLb_JU5HK_SSXm86GsVs" #self.Debug(message__) if not condor.Stopped_Out: if not condor.Profit_Taken or self.Allow_Re_enter: if not self.Traded_Today or self.Allow_Re_enter: if not self.Entered_With_PT and not self.Portfolio.Invested: #self.Notify.Telegram(id=id_, message=message__, token=token_) # condor.Sent_Open_Range = True self.MarketOrder(condor.Put_Buy.Symbol, self.Amount_Of_Contracts, tag =f"{percentage_put_} Strike {condor.Put_Buy.Strike} po {self.Premium_At_Open} Price {Price}") self.MarketOrder(condor.Put_Sell.Symbol, -self.Amount_Of_Contracts, tag =f"{percentage_put_} Strike {condor.Put_Sell.Strike} po {self.Premium_At_Open} Price {Price}") self.MarketOrder(condor.Call_Buy.Symbol, self.Amount_Of_Contracts, tag =f"{percentage_call_} Strike {condor.Call_Buy.Strike} po {self.Premium_At_Open} Price {Price}") self.MarketOrder(condor.Call_Sell.Symbol, -self.Amount_Of_Contracts, tag =f"{percentage_call_} Strike {condor.Call_Sell.Strike} po {self.Premium_At_Open} Price {Price}") self.Put_Buy_Strike = condor.Put_Buy.Strike self.Put_Sell_Strike = condor.Put_Sell.Strike self.Call_Buy_Strike = condor.Call_Buy.Strike self.Call_Sell_Strike = condor.Call_Sell.Strike self.Invested_Condor = condor condor.Entry_Premium = condor.Premium condor.Margin_Required = ((condor.Put_Buy.Strike - condor.Put_Sell.Strike) * 100) * self.Amount_Of_Contracts self.Traded_Today = True self.Counter = self.Counter + 1 self.Iron_Condor_Counter = self.Iron_Condor_Counter + 1 self.Debug(f"{condor.open_call_range} {condor.open_put_range} ENTERED") if self.Allow_Re_enter: self.Entered_With_PT = True #self.Debug(f"Range {self.Minimum_Distance} Avg {self.Current_Average} Current Open {self.Premium_At_Open}") self.Minimum_Distance = config.MINIMUM_DISTANCE # if condor.Lowest_Premium is None: # condor.Lowest_Premium = condor.Premium # if condor.Lowest_Premium > condor.Premium: # condor.Lowest_Premium = condor.Premium # put_3_min_distance_scanner = Price*0.01 # put_3_min_distance_scanner = math.floor(put_3_min_distance_scanner) # call_3_min_distance_scanner = Price*0.01 # call_3_min_distance_scanner = math.ceil(call_3_min_distance_scanner) # if condor.Lowest_Premium * self.Premium_Multiplier <= condor.Premium: # if condor.Premium >= self.Premium_Threshold: # if not condor.Scanned_Already and condor_distance_put >= put_3_min_distance_scanner and condor_distance_call >= call_3_min_distance_scanner and (put_call_distance >= 0 or put_call_distance <= 2): # if self.Counter <= 20: # percentage = ((abs(self.Securities[self.Spy].Price - condor.Put_Sell.Strike) / self.Securities[self.Spy].Price) + (abs(self.Securities[self.Spy].Price - condor.Call_Sell.Strike) / self.Securities[self.Spy].Price)) / 2 # percentage_call = abs((self.Securities[self.Spy].Price - condor.Call_Sell.Strike) / self.Securities[self.Spy].Price) # percentage_put = abs((self.Securities[self.Spy].Price - condor.Put_Sell.Strike) / self.Securities[self.Spy].Price) # percentage = round(percentage, 3) # percentage_call = round(percentage_call, 3) # percentage_put = round(percentage_put, 3) # #if condor.Call_Buy.Strike - condor.Call_Sell.Strike >= 10: # Breakeven_Price_Call = condor.Call_Sell.Strike + condor.Premium # Breakeven_Price_Put = condor.Put_Sell.Strike - condor.Premium # premium_increase = ((condor.Premium - condor.Lowest_Premium )/ condor.Lowest_Premium) * 100 # token_scanner = "5616527268:AAGkUitVosgYOntpLb_JU5HK_SSXm86GsVs" # id_scanner = "-723291833" # message_scanner = f"Premium has increased by %{premium_increase} \n Lowest Premium {condor.Lowest_Premium} \n Current Premium {condor.Premium} \n Percentage %{percentage} \n Sell {(condor.Put_Sell.Expiry).strftime('%b%d%-y')} ${condor.Put_Sell.Strike} Put \n Buy {(condor.Put_Buy.Expiry).strftime('%b%d%-y')} ${condor.Put_Buy.Strike} Put \n Buy {(condor.Call_Buy.Expiry).strftime('%b%d%-y') } ${condor.Call_Buy.Strike} Call \n Sell {(condor.Call_Sell.Expiry).strftime('%b%d%-y')} ${condor.Call_Sell.Strike} Call \n \n \n \n Price ${self.Securities[self.Spy].Price} \n Premium {condor.Premium} \n Strike diff ${abs(condor.Put_Sell.Strike - condor.Put_Buy.Strike)} \n Breakeven Price Call {Breakeven_Price_Call} \n Breakeven Price Put {Breakeven_Price_Put} \n Call %{percentage_call} \n Put %{percentage_put} \n %{percentage} \n expiry {expiry_date}" # condor.Scanned_Already = True # self.Counter = self.Counter + 1 # self.Notify.Telegram(id=id_scanner, message=message_scanner, token=token_scanner) # self.Debug(message_scanner) def Assign_Weighting(self, open_call_range, open_put_range, strike_diff): call_range_diff = open_call_range - config.RANGE_MAX put_range_diff = open_put_range - config.RANGE_MAX call_put_diff = abs(open_call_range - open_put_range) call_put_diff = math.ceil(call_put_diff) call_range_diff = round(call_range_diff, 3) put_range_diff = round(put_range_diff, 3) call_range_diff = (call_range_diff * 10) put_range_diff = (put_range_diff * 10) range_weight = ((call_range_diff * config.RANGE_WEIGHT) + (put_range_diff * config.RANGE_WEIGHT)) / call_put_diff #strike_weight = (config.STRIKE_MAX - strike_diff) * config.STRIKE_WEIGHT weight = range_weight #+ strike_weight return weight # def OnEndOfAlgorithm(self): # self.Debug(max(self.List_Of_Len)) # self.Debug(min(self.List_Of_Len)) def UniverseFunc(self, universe): Price = self.Securities[self.Spy].Price ATM = self.Securities[self.Spy].Price * 0.06 #self.Debug(self.Time) # OnlyApplyFilterAtMarketOpen() return universe.IncludeWeeklys().Strikes(-ATM, ATM).Expiration(TimeSpan.FromDays(0),TimeSpan.FromDays(0)) class Iron_Condor(): def __init__(self, algorithm, put_buy, put_sell, call_buy, call_sell, open_call_range, open_put_range): self.algorithm = algorithm self.Put_Buy = put_buy self.Put_Sell = put_sell self.Call_Buy = call_buy self.Call_Sell = call_sell self.Sent_Already = False self.Premium = None self.Entry_Premium = None self.Entry_Fees = None self.Lowest_Premium = None self.Scanned_Already = False self.Profit = None self.Stopped_Out = False self.Profit_Taken = False self.Current_Profit = None self.Margin_Required = None self.open_call_range = open_call_range self.open_put_range = open_put_range self.Sent_Open_Range = False self.Weight = 0 def Calculate_Premium(self, pb_premium, ps_premium, cb_premium, cs_premium): call_premium = cs_premium - cb_premium put_premium = ps_premium - pb_premium list_of_ba = [-pb_premium, ps_premium, -cb_premium, cs_premium] self.Premium = sum(list_of_ba) return self.Premium class Combinations(): def __init__(self, algorithm, b, s): self.algorithm = algorithm self.S = s self.B = b