Algorithmic implementation of the VWAP Trading Strategy
Boris Kushnarev
Business Analyst at Optus | Data Analytics Consultant at The Data School | Neo4j Certified Professional | Tableau Data Analyst Certification | Alteryx Advanced Certified | [email protected]
I recently had the pleasure of meeting Talgat Baibussinov , a seasoned professional in the fields of trading and finance. His diverse interests encompass various aspects, including day trading, swing trading, investments, and teaching. While engaging in manual trading, he faces limitations in processing only a limited number of stocks during a session. However, by implementing his trading strategies, for instance, using programming languages like Python and automating them, there is the potential for not only higher profits but also a range of other benefits.
We delved into a lengthy conversation exploring the potential for automating his trading strategies and concepts, all aimed at optimizing profit and minimizing losses. I found myself particularly drawn to this opportunity, eager to take on the challenge of automation. So, we enthusiastically made the decision to give it a try and embark on creating a model for the VWAP (Volume Weighted Average Price) trading strategy.
First thing first: data collection
The initial step in our modelling journey was to locate and gather data. It is crucial to understand that various trading strategies depend on different time frames, ranging from 1, 3, 5, and 10-minute intervals to longer periods like 1 hour or daily candles. When dealing with minute candles, it's possible to aggregate and derive data for higher time frames, but the reverse isn't true. Therefore, we chose to collect 1-minute candle data. Several services offer such data; finance.yahoo.com provides it for free, although data for shorter timeframes is only accessible for the last seven days. Ultimately, we obtained one-minute timeframe data from polygon.io, covering 480 stocks within the S&P 500 index over a one-year period, and this data was accessible for free after subscribing to their free plan.
Trading strategy
This strategy exclusively focuses on 'long positions', where long positions involve buying assets with the expectation of price increases, as opposed to 'short positions,' which involve selling assets (often borrowed) with the anticipation of price declines. It employs several moving averages (MA) with varying time periods and the VWAP technical indicator. A specific condition must be met to initiate a position, which is determined by the alignment of MAs, VWAP, and stock prices.
As you may know, a Moving Average is a widely used technical indicator that calculates the average price of a security over a set number of periods, smoothing out price fluctuations to help traders identify trends. VWAP, on the other hand, serves as a trading benchmark employed by both traders and institutional investors. It signifies the average price of a security over the trading day, factoring in trade volume, thereby providing insight into price levels that consider trading activity.
In this strategy, 'Take Profit' (TP) and 'Stop Loss' (SL) are parameters used to exit positions. TP triggers the sale of stocks when the stock price reaches a predefined level, allowing profit realization. Conversely, SL is employed to sell a position if the stock price moves against the strategy, limiting potential losses.
The Stock market is open for the main session from 9:30 a.m. to 4 p.m. New York time. Pre- and Post- markets are also open for trading; however, they are not used in this strategy.
Due to the non-disclosure agreement with Talgat, I cannot reveal more information about the strategy.
Modelling
The modelling was done in Python and proved to be quite straightforward. However, certain assumptions were necessary. For instance, the strategy assumes that positions must be opened as limit orders based on real-time prices, yet since it was tested using historical data and real-time prices cannot be supplied to our algorithm, positions are initiated when the opening price of a 5-minute candle meets the criteria for entry. There are no limits on the number of positions that can be open at a given time for different stocks. Multiple positions (without limits) can be open for each stock during the day, provided that the previously opened position is closed. In real trading, the number of open positions will depend on the risk management. Finally, all positions must be closed by 2 p.m. to ensure that all stocks are sold before the market closes.
For strategy analysis and assessment, a plot was generated for each trade using the Python plotly library. An example of such a plot is displayed below.
领英推荐
In the picture, you can observe 5-minute candles of a stock throughout the day. When the conditions are met to open a position, the blue line rises when the position is opened and falls when it reaches TP and subsequently closes.
$50,000 USD was chosen as the initial capital. The stock price range is considered to be between $10 and $100. Each position contains 100 stocks if its cost is less than $50; otherwise, 300 stocks are purchased for each position. The risk for each open position is set at 1/100 of the initial deposit.
For the performance analysis of this strategy, the profit/loss dataset was generated to be used in Tableau later.
Analysis
The data analysis was conducted in Tableau, using interactive visualizations. Two dashboards have been developed. The first dashboard allows users to choose from all stocks for analysis, a group of stocks, or a specific stock. Let's consider the least realistic case when all stocks are included. In real trading, due to risk management, only a few stocks would typically be traded, not all of them. We anticipate both winning and losing trades, but the objective here is to maintain a positive mathematical expectation. This is precisely what the dashboard demonstrates, accessible here: [https://public.tableau.com/app/profile/boris4118/viz/vwap_analysis/Dashboard1].
We can observe that on most days, the daily total profit is positive. There are only a few days when it turns negative, indicating that profits outweigh losses. Therefore, we can conclude that we maintain a positive mathematical expectation.
Over the course of the year, the cumulative profit has reached $1.5 million, representing a 3000% increase compared to the initial capital.
There are other parameters to tune, such as 'Weekday' and 'N of Positions.' By adjusting the 'Weekday' parameter, we can examine which day of the week yields higher profits compared to others. The 'N of Positions' parameter allows you to set a maximum number of trades per stock per day.
Similarly, the second dashboard illustrates the performance of the top 'N' stocks for this period [https://public.tableau.com/app/profile/boris4118/viz/vwap_analysis_topN/Dashboard1]. You can choose 'N' as a parameter. For example, with 'N' set to 10, the total profit amounts to $141K, representing a 250% increase relative to the initial capital.
Conclusion
The goal of validating the computational approach of the VWAP strategy has been achieved. It demonstrates a positive mathematical expectation, leading to profitability. This involved modelling or so-called 'backtesting' of the VWAP strategy using historical data. The next step involves the implementation of this algorithm into a demo broker's account via API for further testing.