# TOS Industry Ranking
# Using TOS Public List of S&P 500 Sectors & Industries
# bulusufinances
# v0.1
declare lower;
input Industrial_Conglomerates = "$SP500#201050";
input Wireless_Telecommunication_Services = "$SP500#501020";
input Health_Care_REITs = "$SP500#601050";
input Household_Durables = "$SP500#252010";
input Tobacco = "$SP500#302030";
input Telecommunication_Services_TR = "$SP500#5010TR";
input Utilities_TR = "$SP500#5510TR";
input Electric_Utilities = "$SP500#551010";
input Aerospace_Defense = "$SP500#201010";
input Consumer_Staples_Distribution_Retail_TR = "$SP500#3010TR";
input Technology_Hardware_Storage_Peripherals = "$SP500#452020";
input Commercial_Professional_Services_TR = "$SP500#2020TR";
input Technology_Hardware_Equipment_TR = "$SP500#4520TR";
input Commercial_Services_Supplies = "$SP500#202010";
input Insurance_TR = "$SP500#4030TR";
input Technology_Hardware_Equipment = "$SP500#4520";
input Consumer_Staples_Distribution_Retail = "$SP500#3010";
input Consumer_Staples_Distribution_Retail_Industry = "$SP500#301010";
input Commercial_Professional_Services = "$SP500#2020";
input Biotechnology = "$SP500#352010";
input Utilities = "$SP500#5510";
input Insurance = "$SP500#4030";
input Insurance_Industry = "$SP500#403010";
input Consumer_Finance = "$SP500#402020";
input Building_Products = "$SP500#201020";
input Financial_Services_TR = "$SP500#4020TR";
input Banks_TR = "$SP500#4010TR";
input Telecommunication_Services = "$SP500#5010";
input Financial_Services = "$SP500#4020";
input Interactive_Media_Services = "$SP500#502030";
input Equity_REITs_TR = "$SP500#6010TR";
input Life_Sciences_Tools_Services = "$SP500#352030";
input Banks = "$SP500#4010";
input Banks_Industry = "$SP500#401010";
input Specialized_REITs = "$SP500#601080";
input Gas_Utilities = "$SP500#551020";
input Media_Entertainment = "$SP500#5020";
input Media_Entertainment_TR = "$SP500#5020TR";
input Semiconductors_Equipment_TR = "$SP500#4530TR";
input Multi_Utilities = "$SP500#551030";
input Construction_Materials = "$SP500#151020";
input Semiconductors_Equipment = "$SP500#4530";
input Semiconductor_Equipment = "$SP500#453010";
input Containers_Packaging = "$SP500#151030";
input Equity_REITs = "$SP500#6010";
input Real_Estate_Management_Development = "$SP500#6020";
input Real_Estate_Management_Development_Industry = "$SP500#602010";
input Financial_Services_Industry = "$SP500#402010";
input Capital_Goods_TR = "$SP500#2010TR";
input Health_Care_Equipment_Services_TR = "$SP500#3510TR";
input Pharmaceuticals_Biotechnology_Life_Sciences_TR = "$SP500#3520TR";
input Diversified_Telecommunication_Services = "$SP500#501010";
input Water_Utilities = "$SP500#551040";
input Retail_REITs = "$SP500#601070";
input Capital_Goods = "$SP500#2010";
input Trading_Companies_Distributors = "$SP500#201070";
input Health_Care_Equipment_Services = "$SP500#3510";
input Specialty_Retail = "$SP500#255040";
input Health_Care_Providers_Services = "$SP500#351020";
input Residential_REITs = "$SP500#601060";
input Capital_Markets = "$SP500#402030";
input Professional_Services = "$SP500#202020";
input Construction_Engineering = "$SP500#201030";
input Food_Beverage_Tobacco_TR = "$SP500#3020TR";
input Pharmaceuticals_Biotechnology_Life_Sciences = "$SP500#3520";
input Materials_TR = "$SP500#1510TR";
input Household_Products = "$SP500#303010";
input Chemicals = "$SP500#151010";
input Office_REITs = "$SP500#601040";
input Food_Beverage_Tobacco = "$SP500#3020";
input Beverages = "$SP500#302010";
input Household_Personal_Products_TR = "$SP500#3030TR";
input IT_Services = "$SP500#451020";
input Materials = "$SP500#1510";
input Industrial_REITs = "$SP500#601025";
input Broadline_Retail = "$SP500#255030";
input Household_Personal_Products = "$SP500#3030";
input Leisure_Products = "$SP500#252020";
input Consumer_Discretionary_Distribution_Retail_TR = "$SP500#2550TR";
input Entertainment = "$SP500#502020";
input Consumer_Discretionary_Distribution_Retail = "$SP500#2550";
input Automobiles = "$SP500#251020";
input Automobiles_Components = "$SP500#2510";
input Automobiles_Components_TR = "$SP500#2510TR";
input Consumer_Durables_Apparel = "$SP500#2520";
input Consumer_Durables_Apparel_TR = "$SP500#2520TR";
input Health_Care_Equipment_Supplies = "$SP500#351010";
input Pharmaceuticals = "$SP500#352020";
input Communications_Equipment = "$SP500#452010";
input Media = "$SP500#502010";
input Metals_Mining = "$SP500#151040";
input Machinery = "$SP500#201060";
input Distributors = "$SP500#255010";
input Food_Products = "$SP500#302020";
input Electrical_Equipment = "$SP500#201040";
input Software = "$SP500#451030";
input Software_Services_TR = "$SP500#4510TR";
input Ground_Transportation = "$SP500#203040";
input Fertilizers_Agricultural_Chemicals = "$SP500#15101030";
input Software_Services = "$SP500#4510";
input Independent_Power_Renewable_Electricity_Producers = "$SP500#551050";
input Energy_TR = "$SP500#1010TR";
input Oil_Gas_Consumable_Fuels = "$SP500#101020";
input Air_Freight_Logistics = "$SP500#203010";
input Energy = "$SP500#1010";
input Transportation_TR = "$SP500#2030TR";
input Energy_Equipment_Services = "$SP500#101010";
input Transportation = "$SP500#2030";
input Automobile_Components = "$SP500#251010";
input Personal_Care_Products = "$SP500#303020";
input Consumer_Services_TR = "$SP500#2530TR";
input Textiles_Apparel_Luxury_Goods = "$SP500#252030";
input Consumer_Services = "$SP500#2530";
input Hotels_Restaurants_Leisure = "$SP500#253010";
input Passenger_Airlines = "$SP500#203020";
input Electronic_Equipment_Instruments_Components = "$SP500#452030";
input Hotel_Resort_REITs = "$SP500#601030";
# Function to calculate the least-squares curve fit slope
script leastSquaresFit {
input data = close;
def n = 252; # Number of days for the fit
def sumX = Sum(1, n);
def sumY = Sum(data, n);
def sumXY = Sum(data * (1 + n), n);
def sumXX = Sum(Sqr(1 + n), n);
def slope = (n * sumXY - sumX * sumY) / (n * sumXX - Sqr(sumX));
plot slopeValue = slope;
}
# Calculate the least-squares slope for each industry
def slope_Industrial_Conglomerates = leastSquaresFit(close((Industrial_Conglomerates)));
def slope_Wireless_Telecommunication_Services = leastSquaresFit(close((Wireless_Telecommunication_Services)));
def slope_Health_Care_REITs = leastSquaresFit(close((Health_Care_REITs)));
def slope_Household_Durables = leastSquaresFit(close((Household_Durables)));
def slope_Tobacco = leastSquaresFit(close((Tobacco)));
def slope_Telecommunication_Services_TR = leastSquaresFit(close((Telecommunication_Services_TR)));
def slope_Utilities_TR = leastSquaresFit(close((Utilities_TR)));
def slope_Electric_Utilities = leastSquaresFit(close((Electric_Utilities)));
def slope_Aerospace_Defense = leastSquaresFit(close((Aerospace_Defense)));
def slope_Consumer_Staples_Distribution_Retail_TR = leastSquaresFit(close((Consumer_Staples_Distribution_Retail_TR)));
def slope_Technology_Hardware_Storage_Peripherals = leastSquaresFit(close((Technology_Hardware_Storage_Peripherals)));
def slope_Commercial_Professional_Services_TR = leastSquaresFit(close((Commercial_Professional_Services_TR)));
def slope_Technology_Hardware_Equipment_TR = leastSquaresFit(close((Technology_Hardware_Equipment_TR)));
def slope_Commercial_Services_Supplies = leastSquaresFit(close((Commercial_Services_Supplies)));
def slope_Insurance_TR = leastSquaresFit(close((Insurance_TR)));
def slope_Technology_Hardware_Equipment = leastSquaresFit(close((Technology_Hardware_Equipment)));
def slope_Consumer_Staples_Distribution_Retail = leastSquaresFit(close((Consumer_Staples_Distribution_Retail)));
def slope_Consumer_Staples_Distribution_Retail_Industry = leastSquaresFit(close((Consumer_Staples_Distribution_Retail_Industry)));
def slope_Commercial_Professional_Services = leastSquaresFit(close((Commercial_Professional_Services)));
def slope_Biotechnology = leastSquaresFit(close((Biotechnology)));
def slope_Utilities = leastSquaresFit(close((Utilities)));
def slope_Insurance = leastSquaresFit(close((Insurance)));
def slope_Insurance_Industry = leastSquaresFit(close((Insurance_Industry)));
def slope_Consumer_Finance = leastSquaresFit(close((Consumer_Finance)));
def slope_Building_Products = leastSquaresFit(close((Building_Products)));
def slope_Financial_Services_TR = leastSquaresFit(close((Financial_Services_TR)));
def slope_Banks_TR = leastSquaresFit(close((Banks_TR)));
def slope_Telecommunication_Services = leastSquaresFit(close((Telecommunication_Services)));
def slope_Financial_Services = leastSquaresFit(close((Financial_Services)));
def slope_Interactive_Media_Services = leastSquaresFit(close((Interactive_Media_Services)));
def slope_Equity_REITs_TR = leastSquaresFit(close((Equity_REITs_TR)));
def slope_Life_Sciences_Tools_Services = leastSquaresFit(close((Life_Sciences_Tools_Services)));
def slope_Banks = leastSquaresFit(close((Banks)));
def slope_Banks_Industry = leastSquaresFit(close((Banks_Industry)));
def slope_Specialized_REITs = leastSquaresFit(close((Specialized_REITs)));
def slope_Gas_Utilities = leastSquaresFit(close((Gas_Utilities)));
def slope_Media_Entertainment = leastSquaresFit(close((Media_Entertainment)));
def slope_Media_Entertainment_TR = leastSquaresFit(close((Media_Entertainment_TR)));
def slope_Semiconductors_Equipment_TR = leastSquaresFit(close((Semiconductors_Equipment_TR)));
def slope_Multi_Utilities = leastSquaresFit(close((Multi_Utilities)));
def slope_Construction_Materials = leastSquaresFit(close((Construction_Materials)));
def slope_Semiconductors_Equipment = leastSquaresFit(close((Semiconductors_Equipment)));
def slope_Semiconductor_Equipment = leastSquaresFit(close((Semiconductor_Equipment)));
def slope_Containers_Packaging = leastSquaresFit(close((Containers_Packaging)));
def slope_Equity_REITs = leastSquaresFit(close((Equity_REITs)));
def slope_Real_Estate_Management_Development = leastSquaresFit(close((Real_Estate_Management_Development)));
def slope_Real_Estate_Management_Development_Industry = leastSquaresFit(close((Real_Estate_Management_Development_Industry)));
def slope_Financial_Services_Industry = leastSquaresFit(close((Financial_Services_Industry)));
def slope_Capital_Goods_TR = leastSquaresFit(close((Capital_Goods_TR)));
def slope_Health_Care_Equipment_Services_TR = leastSquaresFit(close((Health_Care_Equipment_Services_TR)));
def slope_Pharmaceuticals_Biotechnology_Life_Sciences_TR = leastSquaresFit(close((Pharmaceuticals_Biotechnology_Life_Sciences_TR)));
def slope_Diversified_Telecommunication_Services = leastSquaresFit(close((Diversified_Telecommunication_Services)));
def slope_Water_Utilities = leastSquaresFit(close((Water_Utilities)));
def slope_Retail_REITs = leastSquaresFit(close((Retail_REITs)));
def slope_Capital_Goods = leastSquaresFit(close((Capital_Goods)));
def slope_Trading_Companies_Distributors = leastSquaresFit(close((Trading_Companies_Distributors)));
def slope_Health_Care_Equipment_Services = leastSquaresFit(close((Health_Care_Equipment_Services)));
def slope_Specialty_Retail = leastSquaresFit(close((Specialty_Retail)));
def slope_Health_Care_Providers_Services = leastSquaresFit(close((Health_Care_Providers_Services)));
def slope_Residential_REITs = leastSquaresFit(close((Residential_REITs)));
def slope_Capital_Markets = leastSquaresFit(close((Capital_Markets)));
def slope_Professional_Services = leastSquaresFit(close((Professional_Services)));
def slope_Construction_Engineering = leastSquaresFit(close((Construction_Engineering)));
def slope_Food_Beverage_Tobacco_TR = leastSquaresFit(close((Food_Beverage_Tobacco_TR)));
def slope_Pharmaceuticals_Biotechnology_Life_Sciences = leastSquaresFit(close((Pharmaceuticals_Biotechnology_Life_Sciences)));
def slope_Materials_TR = leastSquaresFit(close((Materials_TR)));
def slope_Household_Products = leastSquaresFit(close((Household_Products)));
def slope_Chemicals = leastSquaresFit(close((Chemicals)));
def slope_Office_REITs = leastSquaresFit(close((Office_REITs)));
def slope_Food_Beverage_Tobacco = leastSquaresFit(close((Food_Beverage_Tobacco)));
def slope_Beverages = leastSquaresFit(close((Beverages)));
def slope_Household_Personal_Products_TR = leastSquaresFit(close((Household_Personal_Products_TR)));
def slope_IT_Services = leastSquaresFit(close((IT_Services)));
def slope_Materials = leastSquaresFit(close((Materials)));
def slope_Industrial_REITs = leastSquaresFit(close((Industrial_REITs)));
def slope_Broadline_Retail = leastSquaresFit(close((Broadline_Retail)));
def slope_Household_Personal_Products = leastSquaresFit(close((Household_Personal_Products)));
def slope_Leisure_Products = leastSquaresFit(close((Leisure_Products)));
def slope_Consumer_Discretionary_Distribution_Retail_TR = leastSquaresFit(close((Consumer_Discretionary_Distribution_Retail_TR)));
def slope_Entertainment = leastSquaresFit(close((Entertainment)));
def slope_Consumer_Discretionary_Distribution_Retail = leastSquaresFit(close((Consumer_Discretionary_Distribution_Retail)));
def slope_Automobiles = leastSquaresFit(close((Automobiles)));
def slope_Automobiles_Components = leastSquaresFit(close((Automobiles_Components)));
def slope_Automobiles_Components_TR = leastSquaresFit(close((Automobiles_Components_TR)));
def slope_Consumer_Durables_Apparel = leastSquaresFit(close((Consumer_Durables_Apparel)));
def slope_Consumer_Durables_Apparel_TR = leastSquaresFit(close((Consumer_Durables_Apparel_TR)));
def slope_Health_Care_Equipment_Supplies = leastSquaresFit(close((Health_Care_Equipment_Supplies)));
def slope_Pharmaceuticals = leastSquaresFit(close((Pharmaceuticals)));
def slope_Communications_Equipment = leastSquaresFit(close((Communications_Equipment)));
def slope_Media = leastSquaresFit(close((Media)));
def slope_Metals_Mining = leastSquaresFit(close((Metals_Mining)));
def slope_Machinery = leastSquaresFit(close((Machinery)));
def slope_Distributors = leastSquaresFit(close((Distributors)));
def slope_Food_Products = leastSquaresFit(close((Food_Products)));
def slope_Electrical_Equipment = leastSquaresFit(close((Electrical_Equipment)));
def slope_Software = leastSquaresFit(close((Software)));
def slope_Software_Services_TR = leastSquaresFit(close((Software_Services_TR)));
def slope_Ground_Transportation = leastSquaresFit(close((Ground_Transportation)));
def slope_Fertilizers_Agricultural_Chemicals = leastSquaresFit(close((Fertilizers_Agricultural_Chemicals)));
def slope_Software_Services = leastSquaresFit(close((Software_Services)));
def slope_Independent_Power_Renewable_Electricity_Producers = leastSquaresFit(close((Independent_Power_Renewable_Electricity_Producers)));
def slope_Energy_TR = leastSquaresFit(close((Energy_TR)));
def slope_Oil_Gas_Consumable_Fuels = leastSquaresFit(close((Oil_Gas_Consumable_Fuels)));
def slope_Air_Freight_Logistics = leastSquaresFit(close((Air_Freight_Logistics)));
def slope_Energy = leastSquaresFit(close((Energy)));
def slope_Transportation_TR = leastSquaresFit(close((Transportation_TR)));
def slope_Energy_Equipment_Services = leastSquaresFit(close((Energy_Equipment_Services)));
def slope_Transportation = leastSquaresFit(close((Transportation)));
def slope_Automobile_Components = leastSquaresFit(close((Automobile_Components)));
def slope_Personal_Care_Products = leastSquaresFit(close((Personal_Care_Products)));
def slope_Consumer_Services_TR = leastSquaresFit(close((Consumer_Services_TR)));
def slope_Textiles_Apparel_Luxury_Goods = leastSquaresFit(close((Textiles_Apparel_Luxury_Goods)));
def slope_Consumer_Services = leastSquaresFit(close((Consumer_Services)));
def slope_Hotels_Restaurants_Leisure = leastSquaresFit(close((Hotels_Restaurants_Leisure)));
def slope_Passenger_Airlines = leastSquaresFit(close((Passenger_Airlines)));
def slope_Electronic_Equipment_Instruments_Components = leastSquaresFit(close((Electronic_Equipment_Instruments_Components)));
def slope_Hotel_Resort_REITs = leastSquaresFit(close((Hotel_Resort_REITs)));
# Display the top 20 slopes
AddLabel(yes, "Top 20 Industry Slopes:", Color.YELLOW);
# Add labels for the top 20 industries based on manually checking their slopes
AddLabel(yes, "Wireless Telecommunication Services: " + slope_Wireless_Telecommunication_Services, Color.GREEN);
AddLabel(yes, "Health Care REITs: " + slope_Health_Care_REITs, Color.GREEN);
AddLabel(yes, "Tobacco: " + slope_Tobacco, Color.GREEN);
AddLabel(yes, "Telecommunication Services TR: " + slope_Telecommunication_Services_TR, Color.GREEN);
AddLabel(yes, "Utilities TR: " + slope_Utilities_TR, Color.GREEN);
AddLabel(yes, "Electric Utilities: " + slope_Electric_Utilities, Color.GREEN);
AddLabel(yes, "Aerospace & Defense: " + slope_Aerospace_Defense, Color.GREEN);
AddLabel(yes, "Technology Hardware, Storage & Peripherals: " + slope_Technology_Hardware_Storage_Peripherals, Color.GREEN);
AddLabel(yes, "Commercial & Professional Services TR: " + slope_Commercial_Professional_Services_TR, Color.GREEN);
AddLabel(yes, "Technology Hardware & Equipment TR: " + slope_Technology_Hardware_Equipment_TR, Color.GREEN);
AddLabel(yes, "Commercial Services & Supplies: " + slope_Commercial_Services_Supplies, Color.GREEN);
AddLabel(yes, "Insurance TR: " + slope_Insurance_TR, Color.GREEN);
AddLabel(yes, "Technology Hardware & Equipment: " + slope_Technology_Hardware_Equipment, Color.GREEN);
AddLabel(yes, "Consumer Staples Distribution & Retail: " + slope_Consumer_Staples_Distribution_Retail, Color.GREEN);
AddLabel(yes, "Biotechnology: " + slope_Biotechnology, Color.GREEN);
AddLabel(yes, "Life Sciences Tools & Services: " + slope_Life_Sciences_Tools_Services, Color.GREEN);
AddLabel(yes, "Banks Industry: " + slope_Banks_Industry, Color.GREEN);
AddLabel(yes, "Specialized REITs: " + slope_Specialized_REITs, Color.GREEN);
AddLabel(yes, "Gas Utilities: " + slope_Gas_Utilities, Color.GREEN);
AddLabel(yes, "Media & Entertainment: " + slope_Media_Entertainment, Color.GREEN);