1. Trang chủ
  2. » Luận Văn - Báo Cáo

Luận văn automatic stock market analysis and prediction using reinforcement learning model vs high frequency trading algorithm

47 0 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Automatic stock market analysis and prediction using reinforcement learning model vs high-frequency trading algorithm
Tác giả Tran Anh Dung
Người hướng dẫn Dr. Trương Tuấn Anh, MCS. Mai Đức Trung
Trường học Ho Chi Minh City University of Technology
Chuyên ngành Computer Science
Thể loại Thesis
Năm xuất bản 2024
Thành phố Ho Chi Minh City
Định dạng
Số trang 47
Dung lượng 7,64 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Cấu trúc

  • Chapter 1: Introduction (11)
  • Chapter 2: Automated Trading (12)
    • 2.1 Overview & Terms (12)
    • 2.2 Motivation (13)
  • Chapter 3: Machine Learning Methods (15)
    • 3.1. Brief Introduction (15)
    • 3.2. Machine learning in trading (16)
  • Chapter 4: Prediction Model (20)
    • 4.1. Stochastic oscillator (20)
      • 4.1.1 Introduce (20)
      • 4.1.2 Stochastic oscillator Settings (22)
    • 4.2. Heikin Ashi formula (23)
      • 4.2.1 Introduce (24)
      • 4.2.2 Heikin ashi meaning (25)
  • Chapter 5: Trading Strategy (26)
    • 5.1. Old Trading Strategy: Stochastic Slow Strategy (26)
    • 5.2. Old Trading Strategy: Stochastic Strategy By Massive Tortoise (27)
    • 5.3. Suggested New Trading Strategy: Combination of Stochastic and Heikin Ashi Strategy (29)
  • Chapter 6: Trading Strategy Implementation (33)
    • 6.1 Strategy (33)
    • 6.2 Market Simulator (34)
  • Chapter 7: Strategy optimization (36)
    • 7.1 MQL5 language (36)
    • 7.2 Optimize the code based on MQL5 language (37)
    • 7.3 Optimize strategy backtest and performance (39)
  • Chapter 8: Reinforcement Learning Methods vs Automated trading (43)
    • 8.1 Reinforcement learning applied to Forex trading by Joóo Carapuỗo, Rui Neves, Nuno Horta vs (43)
    • 8.2 An automated FX trading system using adaptive reinforcement learning by M.A.H.Dempster vs (43)
  • Chapter 9: SUMMARY (45)
    • 9.1 Thesis review (45)
    • 9.2 Future work (45)

Nội dung

VIETNAM NATIONAL UNIVERSITY HO CHI MINH CITY HO CHI MINH CITY UNIVERSITY OF TECHNOLOGY FACULTY OF COMPUTER SCIENCE AND ENGINEERING CAPSTONE PROJECT REPORT Automatic Stock Market Analy

Introduction

Financial investment has produced a notable share of the world’s wealth, with Forbes listing 471 billionaires born in the financial investment industry—about 15% of the richest people on the planet and Warren Buffett as a classic example—yet statistics from the American Securities Association show that roughly 99% of investors lose money while only about 1% profit In the technology 4.0 era, automated trading via computer algorithms has transformed finance, with algorithmic trading often accounting for 60%–80% or more of activity in developed markets; these systems use advanced quantitative models to make trading decisions, submit orders automatically, and monitor trades, removing emotion, lowering costs, and increasing liquidity Building an automated trading system typically proceeds in three steps: develop models that forecast the stock market, generate trading orders using a strategy that uses model predictions, and backtest the system on historical data to assess viability The discussion covers what automatic trading is, how to build it with machine learning, the available algorithms, the author’s own algorithm with comparisons to existing ones, and optimization strategies, including deployment environments such as MQL5, ending with a comparison of automated trading and reinforcement learning methods to highlight differences in performance and efficiency.

Automated Trading

Overview & Terms

Shares of publicly traded companies are bought and sold on the stock market, and when a company launches an IPO, it opens its stock to investors worldwide who can buy and trade the shares Each share signals ownership in the company, with prices reflecting investors’ expectations for future value, and those prices are determined by supply and demand in the market Investors typically choose between a buy order to go long, betting the stock will rise and pushing up the price as more buyers join in, or a short sale for those who expect a decline, selling shares they may or may not own and later buying them back to cover the position; short sellers profit from price drops by purchasing back at lower prices The market features a diverse mix of participants—from individual traders to institutional players such as mutual funds, ETFs, and hedge funds, plus algorithmic traders—all competing to profit by correctly predicting future prices and executing buy-low, sell-high strategies Core decisions for everyone involved revolve around trading frequency and the trading universe, i.e., how often to trade and which securities to trade.

Trading frequency is how often a trader makes a decision A day trader executes many orders daily to chase intraday profits, while an investor like Warren Buffett trades far less frequently For human investors, frequency is adjustable, whereas automated trading systems are systematic, with the frequency set by system developers before the system is designed The range of frequencies spans from once in a lifetime for buy-and-hold strategies to nanoseconds for high-frequency trading algorithms Consequently, the chosen trade frequency can drive meaningful differences in model selection and strategy design.

Motivation

Algorithmic trading makes investing more systematic by removing emotional decision-making, since gains can trigger euphoria and losses can trigger fear that undermines judgment and trading discipline While a trading plan is essential for achieving profitable results, no single strategy guarantees profits, and emotional reactions during drawdowns can erode discipline An automated trading system helps establish consistency, maintains discipline in volatile market conditions, and reduces emotional influence by following the strategy methodically.

Backtesting is a core advantage of algorithmic trading, enabling you to apply a trading strategy to historical data to see how it would have performed in a given period It gives developers insight into past performance and, through iterative adjustment of model parameters, offers a path to optimize the strategy The predictive ability of backtesting hinges on the assumption that the price series’ statistical characteristics are stable, meaning profitable histories may continue to yield gains; if macroeconomic outlook, fundamentals, or market structure change, that assumption can fail A robust evaluation uses historical data split into in-sample and out-of-sample sets: optimization uses in-sample data to tune parameters, while out-of-sample data tests resilience and helps prevent overfitting, confirming whether the strategy can be sustainable in unseen conditions.

Human investors often struggle to pace the market, whereas automated trading systems can time entries and exits with precision in milliseconds A famous Buffett principle—“be fearful when others are greedy and greedy only when others are fearful”—is used by some day traders to guide timing, selling when prices rise on greed and buying when fear drives prices down, though Buffett also warned that “our favorite holding period is forever” to remind traders not to chase short-term moves High-frequency trading firms apply this timing logic at scale, continually identifying arbitrage opportunities across thousands of stocks and executing trades far faster than any human could This is why automated trading systems and trading algorithms dominate HFT They also detect market sentiment—greed and fear—using consistent financial time series forecasting and sentiment analysis of news and social media to improve timing decisions.

Machine Learning Methods

Brief Introduction

Machine learning, a core branch of artificial intelligence, enables computers to learn from data and make predictions or decisions without explicit programming It relies on algorithms and statistical models to analyze large datasets, identify patterns, and extract actionable insights Unlike traditional software, machine learning improves performance over time through experience, adapting to new data and changing environments through an iterative learning process that continually refines predictions and decision-making capabilities.

Supervised learning trains models on labeled data to learn the relationships between input features and output labels, enabling them to generalize from examples and make predictions on new, unseen data This paradigm covers tasks like classification, where inputs are sorted into discrete classes, and regression, where the goal is to predict continuous numerical values Its applications span many domains, including image and speech recognition, spam detection, medical diagnosis, and financial forecasting.

Unsupervised learning is a key approach in machine learning that trains models on unlabeled data to uncover hidden patterns and structures By using techniques such as clustering and dimensionality reduction, systems can discover relationships and group similar data points without explicit guidance Clustering algorithms reveal natural data groupings, enabling practical applications like customer segmentation, anomaly detection, and scalable data compression Dimensionality reduction simplifies complex datasets by lowering the number of input features while preserving essential information, which improves visualization and computational efficiency.

Reinforcement learning is a machine learning paradigm that trains agents to interact with environments and learn optimal strategies through trial and error By receiving rewards or penalties based on their actions, agents learn from experience and steadily improve their decision-making over time This approach has been successfully applied to game playing, robotics, autonomous vehicle control, and resource management Algorithms such as Q-learning and deep Q-networks have achieved remarkable feats, including mastering complex games like Go and reaching human-level performance across a range of tasks.

Deep learning, a key subset of machine learning, has drawn significant attention for its ability to learn complex patterns and representations from data Inspired by the brain’s structure and function, it uses artificial neural networks with multiple layers to extract hierarchical features from raw inputs Among the most widely used architectures are convolutional neural networks (CNNs), recurrent neural networks (RNNs), and generative adversarial networks (GANs), enabling advances across computer vision, natural language processing, and speech recognition These models have propelled progress in image classification, object detection, machine translation, and medical imaging, pushing the boundaries of what artificial intelligence can achieve.

Machine learning is transforming industries and streamlining business processes while enhancing daily life by learning from data, adapting to changing environments, and uncovering valuable insights This data-driven capability enables organizations to tackle complex challenges, optimize operations, and drive innovation across diverse domains As technology advances and datasets grow in scale and complexity, the impact of machine learning is set to expand further, shaping the future of society and advancing the frontiers of artificial intelligence.

Machine learning in trading

Alan Turing, a pioneering computer scientist often regarded as a founder of artificial intelligence, posed the famous question “Can machines think?” Although there have been partial answers to various versions of this issue, the full answer remains elusive A central part of this debate is machine learning—the study of how machines learn—which includes both supervised and unsupervised approaches.

According to Wikipedia, the study of pattern recognition and computational learning theory in artificial intelligence gave rise to the subfield of machine learning in computer science [13] Machine learning is a

Machine learning is a field that gives computers the ability to learn without being explicitly programmed, a definition attributed to AI pioneer Arthur Samuel Another influential view, from Tom Mitchell, a renowned expert and professor at Carnegie Mellon University, describes machine learning as the study of computer algorithms that improve automatically through experience Together, these perspectives capture how machine learning enables systems to adapt and get better by learning from data rather than relying on handcrafted rules.

Professor Mitchell provides a formal definition: a computer program learns from experience when it is exposed to a class of tasks T and evaluated by a performance measure, with its task performance P on T improving as experience E accumulates This multidisciplinary field underpins the Big Data boom and furnishes its theoretical foundations Data mining represents a key usage of machine learning, leveraging massive historical data sets to enhance decision-making Examples include turning consumer transaction histories into targeted promotional campaigns and transforming medical records into actionable medical knowledge.

Software like speech recognition and autonomous driving cannot be hand-coded, but deep learning—an area that blends cognitive science and machine learning—has made significant strides in mimicking brain-like functions Notable milestones include AlphaGo, the first program to defeat a 9-dan human world Go champion In algorithmic trading, activities span financial time-series forecasting, statistical arbitrage, and high-frequency trading This work concentrates on forecasting and explores the potential of machine learning algorithms to predict stock prices rather than master chess.

Algorithmic trading relies on computers to execute trades automatically according to a preprogrammed set of instructions, a method widely used in the trading industry This approach, often called automatic trading or algo trading, uses computer systems that follow defined rules to carry out deals Trading with algorithms has been present since the late 1980s and early 1990s, and there are several strategies traders employ to automate decision-making and execution.

Trade execution algorithms split large orders into smaller trades to minimize market impact and price slippage, enabling more favorable execution Among the most commonly used are Volume Weighted Average Price (VWAP), Time Weighted Average Price (TWAP), and Percent of Value (PoV), each optimizing execution by different mechanisms: VWAP targets the average price weighted by volume, TWAP spreads orders evenly over a specified time, and PoV routes trades as a fixed percentage of value to align with market activity.

- Strategy Implementation Algorithms: Based on signals they receive from real-time market data, they execute transactions

- Stealth/Gaming Algorithms: Stealth or gaming algorithms take advantage of price changes caused by large trades or other algorithmic tactics

Arbitrage opportunities arise when the same stock trades at different prices across two markets, creating a price discrepancy that traders can exploit In this scenario, you buy the stock where it is cheaper and sell it where it is more expensive, capturing profit from the price spread as the markets converge toward efficiency This approach hinges on rapid execution and market inefficiencies, making buy-low, sell-high actions across venues a classic arbitrage strategy.

Algorithmic trading drew traders in with high profitability at first, but as competition intensified, returns fell sharply Early approaches relied on classical, rule-based algorithms that use simple if-then logic and cannot learn from past data Today, capital markets firms increasingly employ machine learning to build adaptive, self-improving trading systems that operate without fixed rules and can autonomously discover new patterns.

High-frequency trading is a form of advanced algorithmic trading where large orders are executed in a split second The speed involved makes manual trading impractical, as reading market trends and placing bids by hand can’t keep up with the pace To capitalize on fleeting opportunities, traders rely on computers and sophisticated algorithms to automatically analyze data and execute orders in milliseconds.

AI-powered automation has spread across all industries, with high-frequency trading illustrating how machine-learning models and feature engineering techniques drive rapid decision-making in financial markets Among these approaches, support vector machines (SVMs) stand out as a prevalent choice for classifying data into distinct categories These models are trained to recognize features that signal impending changes in bid and market pricing, enabling traders to respond to evolving conditions with greater speed and precision.

Machine learning uses vast historical data to generate precise predictions about the future, a capability that directly complements trading, where traders identify time- and space-limited localized patterns and seek to exploit them for higher profits These patterns are dynamic, making their detection time-consuming and challenging, but machine learning algorithms excel at pattern recognition and speed up the process By surfacing patterns that traders can combine with intuition and experience, ML enables more accurate, data-driven decisions in algorithmic trading and predictive analytics.

When many traders in the same market employ machine learning to detect the same patterns, its advantage for pattern recognition becomes limited In a highly competitive environment, rivals can access the patterns uncovered by any single trader, reducing the uniqueness of the insight Consequently, even with ML-based pattern identification, traders must act quickly and adjust their strategies regularly to stay ahead.

Investor sentiment is a major driver of stock market movements, with emotions causing rapid fluctuations as consumer opinions shift To measure this impact, businesses increasingly rely on artificial intelligence and machine learning to assess sentiment and forecast stock prices Social media platforms provide abundant, freely expressed opinions that fuel sentiment analysis, and natural language processing (NLP) classifies these views into neutral, positive, or negative signals By turning human language into quantitative insights, NLP and ML enable more accurate readings of how public mood maps to stock value, supporting smarter investment decisions and improved market forecasting.

The stock price of the company is likely to rise if the public has a positive outlook on it

Stock prices often drop when public sentiment becomes negative, signaling a shift in investor confidence Social media activity from stock market participants—tweets, posts, and comments—can be collected and processed by machine learning algorithms An AI model trained on this data can predict stock price movements under various conditions, helping to anticipate how sentiment dynamics may impact markets.

Traders seeking to forecast the future value of equities can leverage AI-driven, machine learning-powered models to test and refine the accuracy of their predictions These algorithms take into account a variety of factors to anticipate stock prices, with neural networks helping to identify and analyze the predictors that drive changes in equity values In short, ML and neural networks enable more precise forecasts and deeper insights into the drivers of stock movements.

Prediction Model

Stochastic oscillator

George Lane, an American commodities and stock trader, developed the stochastic oscillator in the 1950s The indicator measures the closing price of a financial asset relative to its price range over a given time period, helping traders assess whether the asset is overbought or oversold Today, the stochastic oscillator is one of the most popular and widely used tools in technical analysis and stock trading.

Picture a ball thrown upward: it slows down before it reverses and returns to the ground, a simple illustration that momentum shifts before price does “Momentum always changes direction before a price does,” said George Lane, the developer of the Stochastic indicator This principle lies at the heart of momentum-based technical analysis: by monitoring momentum signals, traders can anticipate price reversals, time entries more effectively, and interpret overbought or oversold conditions to forecast market moves.

The stochastic indicator consists of two moving-average lines that oscillate between overbought, neutral, and oversold zones On the indicator chart, the oversold zone spans 0 to 20, the overbought zone spans 80 to 100, while the neutral zone sits in the middle and is considered less significant.

The stochastic oscillator is a major momentum indicator alongside MACD and RSI, signaling momentum by comparing a security’s closing price to its price range over a given period to identify overbought and oversold conditions This article defines what the stochastic oscillator is, outlines three popular approaches to using it—fast %K with %D crossovers, slow stochastic, and a smoothed version to reduce noise—and shows how each method can be applied in practice It also clarifies the key distinction between stochastic and RSI, noting that the stochastic focuses on a price’s position within its recent range while RSI assesses the strength of price moves to signal potential reversals By understanding these methods and differences, traders can better interpret momentum signals and combine stochastic with RSI for more robust entries and exits.

This is a synopsis of the article:

- A technical tool for determining a price's momentum is the stochastic oscillator

- Two distinct lines that move in unison between the oversold, overbought, and neutral zones serve as the indicator's representation

- The range of the indicator is 0 to 100 It is known as a stochastic oscillator because of its oscillations

- A stochastic oscillator is a type of leading indicator, which is an indicator of potential future changes in the market before they actually occur

The stochastic indicator is a versatile trading signal that helps traders time exits from open positions ahead of potential market corrections or reversals, while also guiding entry into new trades and offering predictive insight into future market reversals.

Like the RSI, the Stochastic Oscillator is a momentum oscillator that analyzes price momentum by focusing on the speed of price changes It is used to assess overbought and oversold conditions, but it differs from RSI by also considering the highest high and the lowest low over a specified period in addition to past prices This small but important detail captures the market's past strength and fragility, enriching the oscillator's momentum analysis.

Four variables are used for the calculation of the Stochastic Oscillator:

- %K periods This is the number of time periods used in the stochastic calculation

- %K Slowing Periods This value controls the internal smoothing of %K A value of 1 is considered a fast stochastic; a value of 3 is considered a slow stochastic

- %D periods This is the number of time periods used when calculating a moving average of %K

- %D method The method (i.e., Exponential, Simple, Smoothed, or Weighted) that is used to calculate %D

%K = (CLOSE - MIN (LOW (%K))) / (MAX (HIGH (%K)) - MIN (LOW (%K))) * 100 Where:

MIN (LOW (%K)) — the lowest minimum in %K periods;

MAX (HIGH (%K)) — the highest maximum in %K periods

The %D moving average is calculated according to the formula:

SMA — Simple Moving Average (SMA = SUM (CLOSE (i), N) / N)

CLOSE (i) — current period close price;

As a trader, you can tailor the stochastic oscillator's parameters to fit your trading approach, adjusting factors like sensitivity and look-back period to align with your strategy When you adopt any specific settings, testing them is critical to verify they perform as intended, using backtesting and forward-testing across varied market conditions to ensure robustness.

The oversold zone is found between 0 and 20, and the normal settings indicate an overbought zone between 80 and 100 The trading platform's overbought and oversold zones have been modified as follows:

Using a stochastic oscillator to generate signals more quickly allows earlier trade entries by lowering the overbought threshold to 70 or boosting the oversold level to 30 This setting speeds up entry opportunities but also raises the likelihood of false signals, which could lead to more losing trades.

Raising the overbought threshold to 90 and lowering the oversold level to 10 reduces signal frequency and delays their appearance Under this setup, signals for overbought and oversold conditions are triggered only when there is a significant shift in price momentum.

Two adjustable indicator parameters are %K and %D in the stochastic oscillator By default, %K is 5 and %D is 3, while a typical alternative configuration is 21 for %K, 3 for %D, and 14 for smoothing.

Changing the %K and %D settings can greatly impact your trading experience Increasing these parameters accelerates the stochastic oscillator lines, leading to faster entry into overbought and oversold zones This can generate more trade signals, but it also increases the chance of false signals, which may cause frequent losses and make long-term profitability difficult to achieve.

Reducing these parameters makes the indicators respond more slowly, so the price lines move gradually between overbought and oversold zones, requiring greater patience before entry signals appear This slower response can reduce false signals, but it may also mean missing some trading opportunities Achieving an optimal balance between these configurations is key to making well-informed, efficient trading decisions.

Heikin Ashi formula

By averaging price data, the Heikin-Ashi approach produces a Japanese candlestick chart that reduces noise from the market

Heikin-Ashi charts, created in the 1700s by Munehisa Homma, resemble regular candlestick charts in many respects but use a different set of values to build each candle Instead of the standard open, high, low, and close, the Heikin-Ashi method applies a modified formula based on two-period averages This smoothing makes trends and reversals easier to spot, but it also hides gaps and some price data.

- When analyzing and identifying trends, traders might utilize the Heikin-Ashi approach in conjunction with candlestick charts

- Heikin-Ashi charts employ five principal signals

- Any market can use Heikin-Ashi charts.

Four variables are used for the calculation of the Heikin-Ashi candle:

Close = (Open + High + Low + Close) / 4

Open = [(Open price of previous candle) + (Close price of previous candle)] / 2

Technical traders rely on the Heikin-Ashi approach to quickly identify a trend Strong uptrends are indicated by hollow white (or green) candles without lower shadows, while strong downtrends are indicated by filled black (or red) candles without upper shadows.

Heikin-Ashi reversal candles have small bodies and extended upper and lower shadows, similar to classic candlestick reversal patterns, making reversals easier to spot A Heikin-Ashi chart generates candles without gaps because each candle is calculated using information from the previous candle, smoothing price action and providing a continuous view of market reversals.

By smoothing price data over two periods, the Heikin-Ashi technique helps identify trends, price patterns, and reversal points Traditional candlestick charts can be noisy, with frequent up-and-down candles that are hard to read Heikin-Ashi charts typically feature longer stretches of candles in the same color, making it easier for traders to see historical price moves.

In sluggish and choppy markets, the Heikin-Ashi approach helps traders avoid unnecessary trades by reducing erroneous signals By smoothing price action, it minimizes false reversal candles, making it more likely to spot the true signal before a trend begins rather than reacting to multiple false reversals This leads to more reliable entries when trend momentum is established and helps traders ride the trend with greater confidence.

Trading Strategy

Old Trading Strategy: Stochastic Slow Strategy

The Stochastic Slow Strategy indicator is a price oscillator that compares a security’s closing price over a defined look-back period (n) While traders commonly use a 14-period range for the slow stochastic, the exact period can be adjusted based on individual goals and thorough analysis.

The Stochastic Slow Strategy indicator is calculated based on three major components

%K: Slow %K can be calculated using the following formula:

Definitions: n = the range decided upon by the trader

Replace “n” with the range you are monitoring (number of periods) The slow stochastic strategy can be calculated on any time frame, although the default value is 14, as mentioned above

1 Once using the formula above, the trader should then take the three period Simple Moving Average (SMA) of this value in order to calculate the %K result for the Stochastic Slow Strategy

2 %D: Slow %D is equal to the three period SMA of Slow %K that the trader calculated in Step 2

In the Stochastic Slow Strategy, closing prices are less important than the highs and lows within a defined period, a key distinction from the RSI indicator The calculation explicitly uses the period’s high and low values, which drives the oscillator’s behavior As a result, the Stochastic Slow indicator produces smoother readings and tends to switch between overbought and oversold levels more frequently than methods based on closing prices alone.

Using the Stochastic Slow Strategy indicator helps identify primary market trends by tracking period highs and lows with an oscillator that signals overbought and oversold conditions Traders achieve greater success when these signals are combined with other indicators used in technical analysis or for trend identification.

The indicator excels at predicting extreme market levels, delivering timely overbought and oversold signals, but many users find this barrage of readings noisy and distracting To trade with less clutter and smoother position management, some traders grow weary of constant alerts and decide to remove the indicator entirely, seeking a cleaner chart and reduced signal fatigue.

Although the indicator’s parameters are adjustable, the decision to use it should align with a trader’s specific goals and risk tolerance According to TradingView data, this strategy may incur negative profitability, though there is some evidence supporting aspects of the approach.

Figure 7 Stochastic Slow Strategy for VN index weekly time-frame

Figure 7 shows that this strategy cannot predict the direction of the VNIndex According to the 80/20 theory, the VNIndex is the main trend driving the entire Vietnamese stock market, and approximately 80% of Vietnamese stocks move in a pattern similar to the VNIndex If this holds true, the strategy would incur losses for about 80% of Vietnam's stock market.

Old Trading Strategy: Stochastic Strategy By Massive Tortoise

This trading strategy uses the Stochastic Oscillator (Stoch) with settings (7,2,2) to guide entry and exit decisions A long (buy) position is opened when the Stoch crosses above the 50 level from below, while a short (sell) position is opened when it crosses below the 50 level from above To maintain position hygiene, opening a long position closes any existing short position, and opening a short position closes any existing long position.

- Stochastic Oscillator Settings: Length = 7, SmoothK = 2, SmoothD = 2

- The Stochastic Oscillator (Stoch) is calculated based on the closing price, high price, and low price with a period of 7, and both the %K and %D lines are smoothed with periods of

- When the %K line crosses above the oversold level (20), it generates a long (buy) signal

- When the %K line crosses below the overbought level (80), it generates a short (sell) signal

This trading strategy marks long and short signals on the chart with upward and downward triangles, respectively, making entry opportunities easy to spot It automatically executes either a long or a short trade once the corresponding conditions are met, and it manages risk by ensuring only one direction is open at a time: opening a long position closes any existing short position, and opening a short position closes any existing long position.

According to the Tradingview system, this strategy has small profit, there is some proof of this strategy

Figure 8 Stochastic Strategy for VN index weekly time-frame

Figure 8 shows that this strategy yields only about 3% over a 13-year horizon, a return that tends to be unattractive to investors In contrast, the State Bank of Vietnam indicates that with a 6% annual interest rate—the lowest since 2008—an investor could realize at least 8% profit over 13 years.

Suggested New Trading Strategy: Combination of Stochastic and Heikin Ashi Strategy

I developed a novel method that blends Heikin Ashi candles with stochastic indicators to enhance signal detection in price action A year ago, I tested this approach across major financial markets, evaluating its robustness and practical applicability The method is designed for the month-to-month time frame and is intended for medium- to long-term trading strategies.

Instead of using numbers (14,1,3) in the stochastic oscillator, I use Fibonacci numbers Here are some examples

Figure 9 Settings for NVDA monthly time-frame

Figure 10 Settings for VN index weekly time-frame

Substituting the numbers 8, 5, and 3 with 14, 1, and 3 in the image above allows the Fibonacci set to enhance the Stochastic line’s visibility, smoothness, and accuracy Using this Fibonacci-based approach improves risk management by combining the Stochastic index with the Heikin Ashi main trend on the price chart The method offers the advantage of a small stop loss and the potential for substantial returns when the market trend is stable, though a drawback is that profits can be taken and entry signals may be delayed This illustrates the profitability potential of this strategy.

Figure 11 Settings for NVDA monthly time-frame

Figure 12 Settings for VN index weekly time-frame

The chart shows that profits from 2008 to the present have surged to about 2000% and are still rising because the final position remains open, with the buy entry still active This open position is driving ongoing gains and indicates strong upside potential as the position has not yet been closed The figure highlights how an open buy position can contribute to long-term profit growth and reflects the chart-based view of performance since 2008.

With $45.78 referenced alongside a current price around $1,064, the stock market’s gains appear modest, delivering roughly a 50% profit from 2008 to today By using a disciplined buy-and-sell strategy, you could achieve a similar 50% return with NVIDIA stock (NVDA).

Trading Strategy Implementation

Strategy

- Runtime : System algorithm execution time

- Symbol : Trading pairs, for example XAUUSD (gold/usd), BTCUSD,

- Lot size : Investor trading volume

Figure 14 Heikin-Ashi + Stochastic strategy

This is a code for the future model.

Market Simulator

The universe of equities is the range of stocks an investor chooses to trade An investor whose universe is a global market, for example, does not restrict stock selection to any particular region The S&P 500, the U.S stock market, or a single S&P 500 sector can also be considered universes As a benchmark for the whole U.S stock market, one of the most widely used indexes is the Standard & Poor's 500 Index, or the S&P 500.

500 stocks are selected based on criteria that include industry grouping, market size, and liquidity to represent the risk/return characteristics of the U.S large-cap universe The S&P 500 serves as the benchmark for large-cap U.S equities, and SPY—the oldest and best-known U.S exchange-traded fund—aims to replicate the S&P 500’s total return as closely as possible, providing investors with a convenient way to gain exposure to the U.S stock market.

Within the S&P 500, the market is divided into sectors such as energy, financials, healthcare, industrials, consumer staples, consumer discretionary, information technology, materials, and utilities The S&P 500 universe is used by the automated trading system proposed in this research, focusing on the Energy, IT, and Utilities sectors The system trades daily, and its performance is measured against a buy-and-hold strategy on SPY.

Here are some examples about this strategy

Figure 15 Heikin-Ashi + Stochastic strategy live

Strategy optimization

MQL5 language

The MetaQuotes Language 5 (MQL5) programming language facilitates automated forex trading, allows traders to develop their own indicators, and allows traders to test trading methods on the MetaTrader platform

MetaQuotes Language 5 (MQL5) is a high-level, object-oriented programming language designed to create automated trading strategies and unique technical indicators for financial market analysis It enables you to build custom graphical tools to support trade decisions, as well as develop a range of real-time expert systems, including expert advisors, for advanced automated trading.

Derived from C++, MQL5 expands on MQL4 by introducing enumerations, structures, classes, and robust event handling, enabling more sophisticated automation within MetaTrader It also enables seamless DLL integration, allowing executables to interface with external applications thanks to the expanded set of main types With syntax closely aligned to C++, MQL5 makes it straightforward to translate code from other contemporary programming languages into the platform, unlocking greater flexibility for developers.

MQL5 gives traders direct control over price charts and the ability to build custom control panels, move charts, display unique symbols, and adjust colors, while price charts and multi-currency tests provide debugging environments for Expert Advisors The platform supports up to 512 buffers and direct computations for indicators, offering 12 graphic styles and the option to display future forecast values You can also combine multiple indicators to construct a versatile forex trading strategy.

MQL5 uses an event-based trading approach and is fundamentally event-driven, with OnTick() handling every new tick for any currency pair to generate entry or exit signals The tick flow supplies market activity indicators that help traders identify optimal moments to enter or exit trades Once a trade decision is executed, the program can open another transaction within seconds, enabling rapid, sequential trading OnTradeTransaction() is another handler that processes data from each trading operation, supporting post-trade analysis and order management.

Therefore, you don't have to wait for information in between deals As a result, you can analyze various currency events to create trading algorithms effectively

With MQL5, you can build Expert Advisors that trade across a diverse set of symbols—XAU/USD, BTC/USD, or major world indices—unlocking diversified automated strategies The Strategy Tester is an integrated tool that lets you assess your EAs before live deployment, including the ability to backtest and optimize across multiple symbols at once By using price charts to debug and refine your Expert Advisors, you can manage a portfolio that spans many currency pairs and asset classes, improving the robustness and scalability of your automated trading.

CodeBase is the largest application library for automated trading, featuring source codes, expert advisors, scripts, and indicators compatible with MetaTrader 4 and MetaTrader 5 It provides free access to anyone who uses MetaTrader, and encourages traders to contribute by sharing their own developments to benefit the community.

MQL5 provides hundreds of functions for generalized trading, powering database creation, market-direction detection, tick removal from price history, and more From integrating economic calendar events to building technical indicators, each MQL5 function serves a specific role in algorithmic trading Commonly used features include FileReadString for importing text data into your code, iBearsPower to assess bear-market strength, MathSqrt to calculate the square root of returns, and iCustom to develop unique custom indicators.

Optimize the code based on MQL5 language

Within this periodic trading project, core features such as position time delay, take-profit, and stop-loss were not performing optimally By switching to the MQL5 programming environment and implementing additional rules tailored to different marketplace conditions, I was able to mitigate these issues and significantly improve the project's reliability and performance.

Figure 16 Heikin-Ashi + Stochastic strategy code

Figure 17 Heikin-Ashi + Stochastic strategy code

Optimize strategy backtest and performance

This new strategy in chapter 7 has significantly improved profits and data receiving speed Below are some figures about performance of strategy:

Figure 18 Profit for XAUUSD from 2018-2024 (Demo money)

Figure 19 Some other financial investment metrics

According to the table above, I used 100,000 USD and used the lowest trading volume of

5 positions for 1 transaction From January 1, 2018 to October 23, 2024, this algorithm had a net profit of 535,232 USD, meaning the profit was 500% within 6 years Much greater than bank interest rates

Figure 22 Profit for NVIDIA from 2022-2024

Figure 23 Some other financial investment indicators

According to the table above, I used 100,000 USD and used the lowest trading volume of

5 positions for 1 transaction From January 1, 2022 to November 27, 2024, this algorithm had a net profit of 69052 USD, meaning an increase of 169% in nearly 3 years An attractive profit level for an investor

Figure 24 Chart about real money in cryptocurrency trading

The image above provides tangible proof of real money earned through cryptocurrency trading Before presenting my graduation project, I conducted a rapid, high-intensity trading exercise in the crypto market I deposited 2779.22 USD into my personal account and received 10371.56 USD.

Reinforcement Learning Methods vs Automated trading

Reinforcement learning applied to Forex trading by Joóo Carapuỗo, Rui Neves, Nuno Horta vs

Figure 25 Reinforcement learning applied to Forex trading by Joóo Carapuỗo, Rui

Neves, Nuno Horta total profit

Based on the information above and figure 18 :

Figure 25 from a widely cited study (128 citations) shows Gold's reinforcement learning-based trading strategy, described in the first table, achieving an annualized profit of 4.2% over the full year 1996 on XAU/USD pairs In contrast, my custom-designed trading strategy, illustrated in the second chart, significantly outperforms this with an annualized profit of 66.6% This substantial improvement demonstrates the effectiveness and robustness of my approach in optimizing profitability in the foreign exchange market The results highlight my strategy's strong ability to capitalize on market dynamics relative to historical benchmarks.

An automated FX trading system using adaptive reinforcement learning by M.A.H.Dempster vs

by M.A.H.Dempster vs Combination of Stochastic and Heikin Ashi Strategy performance

Figure 26 Financial Time Series Forecasting with Deep Learning : A Systematic Literature Review: 2005-2019 by Omer Berat Sezer, M Ugur Güdelek, Ahmet Murat ệzbayoğlu

Based on the information above and figure 18 :

Figure 26, from a study cited 1,246 times, compares the trading strategy developed by Gold (as shown in the table), which achieved an annualized profit of 50% over 2.5 years using DNN, RNN, FDDR, and CNN across ten currency pairs, with my custom-designed trading strategy depicted in the second chart, which significantly outperforms it by delivering an annualized profit of 66.6% This pronounced outperformance highlights the efficacy and robustness of my approach in optimizing profitability in the foreign exchange market and its capacity to capitalize on evolving market dynamics relative to historical benchmarks.

SUMMARY

Thesis review

This study introduces an automated trading system powered by adaptive reinforcement learning that aims to maximize trader utility by dynamically tuning both the machine-learning algorithm’s learning behavior and the risk-management layer The approach combines a long-term investment strategy with the potential for higher returns, likened to earning interest on savings but at an improved rate It is particularly well suited for selecting reliable stocks with a lengthy track record, making it a prudent option for investors seeking steady growth over time.

Future work

Most current research concentrates on the accuracy of market predictions and manual trading, while overlooking a systematic profit-calculation framework essential for regulating cyclical fluctuations In my graduation project, I will develop a cyclical control system and demonstrate how to calibrate the variables within the trading strategy to optimize performance and manage the cycle effectively.

Currently, the paper focuses on technical analysis, while future work will incorporate fundamental analysis to build a worldwide stock filter The method remains optimal for highly liquid stocks, reflecting the strength of big-data approaches that perform best with abundant data The key goal is to strengthen the algorithm to accommodate short- and ultra-short-term investment horizons.

Ngày đăng: 15/10/2025, 20:05