Event based Strategy Analysis using python and data science

In this blog,as an example, I plan to explore the influence of Halloween on Hershey Co.’s stock price (in USD). This is because the company witnesses a rise in sales and profits during celebrations such as Halloween.

Halloween is celebrated in multiple countries around the world on 31st October. In the United States of America, Halloween is a very popular and important festival that holds a lot of cultural value and significance. There are many activities that take place during Halloween . However , the most popular and much-awaited one is ‘Trick-or-Treat’. This practice of ‘Trick-or-Treat’ involves children dressed in Halloween costumes asking for treats as they travel from house to house. One of the key ‘Treats’ given is chocolate. The most famous and children-favorite chocolate is the Hershey’s chocolate bar. Other Hershey chocolates such as ‘Hershey’s Kisses’ may also be used.

This blog also aims to provide the python and data science code that I have used to do analysis on the stock data. The stock data was obtained from Yahoo Finance, a reliable data source. Following is the step by step explanation of how I performed this analysis.

In the above image, I have imported the dataset using pandas. I used Google Colab – a cloud based python editor- to store and save my code. By uploading the files to the Google Colab folder in ‘.csv’ format , I was able to access and print the data using ‘pd.read_()’ . A variable was assigned to avoid writing the same line again and again.

In the above image , I converted HSY’s Date format using pandas and the ‘datetime’ function. This allowed me to access the date year-wise, month-wise and column-wise using datetime features and modules. In order to make the analysis process easier , I wanted to extract yearly stock price data for the month of October and specifically 24-31 October for testing purposes. I filtered dates to make a new table using dates from 24th October to 31st October. Why these dates ? Well, mainly because I wanted to test a strategy that involves buying Hershey’s shares a week before Halloween as information starts to build up in the price well in advance. I cleaned and extracted the data according to my requirements and stored it inside a new csv file ‘selected_hsy_data’.

We can observe that the new data is much easier to analyze because most of the noise has been eliminated using Data science techniques. The new data is called ‘beta’ and contains values from 24th to 31st October for every year from 2012-2022. The ‘Date’ column was divided into Year , Month and Day columns to make the analysis easier and efficient to carry out. This will avoid any execution errors arising from complex date formats such as ‘yyyy-mm-dd’.

The above code is a crucial part of this strategy. I have set the portfolio value to a 25 000 dollars to test the overall profitability of the strategy. I created two lists to append buying prices , selling prices , and selling dates so they can be used to create a new Dataframe . I bought 100 shares of HSY every 24th October every year and sold 100 shares every 31st October(every year). The above output shows the transaction book of this strategy and portfolio value after every transaction is executed.

By appending the lists , I used the values to analyze overall performance year wise.

I this step , I focused on creating a new Dataframe called ‘evaluation’ to store portfolio values and dates to create a line plot. I imported and used ‘plotly ‘ to plot the graph of portfolio value and labeled the x and y axes using layout features. Finally , I after arranging feature and label parameters , I plotted the graph.

The above graph shows that the strategy is profitable indeed. The approximate portfolio value was 26 400. This means that one can gain a total profit of approximately 1400 dollars by investing in Hersheys 7 days before every Halloween. Also, the rest of the capital can be deployed somewhere else to further enhance the returns. The strategy was unprofitable during 2019-2020 due to the COVID-19 pandemic but then performed well later. Overall, the strategy seems to generate a healthy profit. Such analysis can also be done on other celebrations such as Valentine’s Day etc. to gauge responsiveness of stock prices to such admired events. If the same strategy is used for options and futures , it can generate better returns .

The information provided by Finance Voyager is for information purposes only and is not intended for advice. Finance Voyager also does not make any recommendation or endorsement as to any investment, advisor or other service or product. The information is only for educational purposes and not buy or sell recommendations.