A simple Day and Night strategy using Python
In Stock trading, traders always have a strategy. A strategy is a set of rules which we follow while trading to minimize risk and maximize profit. This helps in being a more disciplined trader and always entering the market with a plan.
In this article, we will be going through one of the trading strategies — Day and Night Strategy and show you the algorithmic implementation of it using python.
The Day and Night Strategy is based on the theory that major movements in stock prices happen overnight as compared to during trading hours.
The following strategy works because there is evidence that the opening and closing of markets have an effect on short-term price movements, trading volume, and volatility.
We will implement a computer program in python that calculates the overnight returns of stocks. Here we will be testing our strategy on Amazon and Apple Stocks.
In this program, you input the amount you want to invest in Amazon and Apple and also input the date of investment. The program gives you the profit or loss of every day until the current date and finally tells you the current investment value and total profit.
Note: This is for educational purposes only.
The whole code can be found here.
Algorithmic Trading Strategy. Contribute to momo-senpai/Day-NightStrategy development by creating an account on GitHub.
By following the 6 step process below you can build yourself a Day and Night strategy implementation algorithm
Instructions to develop the strategy :
The following are the steps involved in executing the overnight strategy :
Step 1: First we will import the necessary modules for this code. The modules used are:
- Pandas: for creating data frames
- Yfinance: for accessing stock data
- Datetime: for accessing date and time
To install these modules on your system use the pip installer.
Now import the modules.
Step 2: Now we will take in the Investment amount and date of investment as input from the user.
Step 3: We will download the stock data of Amazon and AAPL from the date of investment to the current date. To download data we use:
yf.download(ticker, start_date, end_date) where ticker is the symbol of stock for amazon it is AMZN and for Apple it is AAPL.
The data will look like this.
Step 4: Cleaning the data. For this strategy, we only require the open and close price of the stocks. So we will remove all the other columns.
Step 5: Now we will calculate the overnight return in percentage for each day. The logic for calculating overnight return is to subtract the open price of the day with the closing price of the previous day. So it is:
(Open — Prev. Close)/Prev.Close * 100
Note: Here shift(-1) is used to get the previous value.
Step 6: The last step is calculating the Profit and Loss for each day, The current investment value, and total profit %. And print it to the output.
From the table above it can be seen that in both Apple and Amazon the Day and Night strategy give better returns than the simple long hold strategy i.e buying and holding it forever. One can use this strategy by buying just before the end of market hours and sell the next day when the market opens
The day and night strategy beats the long hold strategy in ⅞ cases and gives better returns in longer timeframes. This reiterates the evidence that most stock price movements happen overnight. Though this is a great strategy it still has some disadvantages.
Pros and Cons:
Some of the pros of this strategy are:
- It is not affected by volatility during market hours.
- Momentum and liquidity during opening hours are high so no difficulty in taking trades.
- Capital is risked for short amounts of time.
Some of the cons of this strategy include:
- Is highly affected by financial reports.
- Cannot be saved from changes in price due to news.
This strategy can be improved by :
- Taking weekends as a factor in returns.
- Taking changes due to news and financial reports into consideration.
- Using Order data for backtesting.
There are many other algorithmic trading strategies that are very easy to understand and implement, similar to the day and night strategy. Some of them include:
- Trend-based strategy: Taking trades based on trends identified.
- Arbitrage Trading: Taking advantage of price differences at different markets.
- Mean reversion: Based on the theory that price returns to its mean after fluctuations.
Thank you for reading. Hope you found this article helpful and hopefully this gives you a template to learn and code your own strategies in the future.