2.5. Developing an Algorithmic Trading Bot with LVQ and Shannon Entropy
Bitcoin as a Trading Asset
Cryptocurrencies have emerged as a significant asset class in financial markets, with Bitcoin (BTC) being the most widely recognized and traded digital asset. Bitcoin operates on a decentralized blockchain network, free from centralized control, making it highly attractive to traders seeking volatility and liquidity. Unlike traditional financial assets, Bitcoin’s price movements are influenced by factors such as market sentiment, macroeconomic trends, regulatory developments, and technological advancements [
13,
23,
29].
Bitcoin trading occurs across various timeframes, ranging from short-term scalping strategies utilizing 1-minute or 3-minute charts to longer-term trend-following approaches. High-frequency traders often exploit Bitcoin’s price inefficiencies, leveraging algorithmic strategies to execute multiple trades within seconds. Additionally, Bitcoin’s correlation with macroeconomic indicators, such as inflation rates and central bank policies, plays a crucial role in defining trading strategies [
8].
Machine Learning and LVQ for Trading
The integration of machine learning (ML) techniques into financial markets has transformed traditional trading paradigms. Among various ML algorithms, the Learning Vector Quantization (LVQ) algorithm has gained attention for its ability to classify market conditions and predict potential price movements. LVQ is a supervised learning algorithm that utilizes competitive learning to optimize decision boundaries between different market states [
3,
19,
22].
In trading applications, LVQ is employed to classify bullish, bearish, or neutral market conditions based on historical data and technical indicators. By training the algorithm on a dataset containing price action, volume, and indicator values, LVQ models can refine their decision-making process, enhancing the accuracy of trade signals. Additionally, the adaptability of LVQ allows traders to incorporate dynamic market changes, thereby reducing the impact of overfitting and ensuring robustness in real-time trading scenarios [
14,
15,
26].
The indicators Relative Strength Index (RSI), Commodity Channel Index (CCI), Rate of Change (ROC), volatility, and volume were integrated into the LVQ machine learning algorithm to enhance decision-making in algorithmic trading. By analyzing these features, the model improves its accuracy in identifying optimal trading signals [
10].
The Relative Strength Index (RSI) is a momentum oscillator ranging from 0 to 100, used to identify overbought or oversold conditions by measuring the speed and change of price movements, where high values indicate strong bullish momentum and low values suggest bearish pressure. The Commodity Channel Index (CCI) assesses price deviation from its statistical average, helping traders spot cyclical trends and potential reversals, with values above +100 signaling overbought conditions and those below -100 indicating oversold markets.
The Rate of Change (ROC) measures the percentage change in price over a given period, where positive values suggest an uptrend and negative values point to downward momentum. Volatility, a key measure of market risk, reflects the degree of price variation over time and is often assessed using indicators like the Average True Range (ATR) or standard deviation. Lastly, volume represents the total number of shares or contracts traded within a specific period, with high volume typically confirming strong trends and low volume indicating weaker movements or potential reversals.
Figure 1.
Machine Learning code snippet from Tradingview platform (learning rate period).
Figure 1.
Machine Learning code snippet from Tradingview platform (learning rate period).
The LVQ (Learning Vector Quantization) algorithm, implemented in this script, is designed for machine learning tasks, particularly for classification problems such as predicting trading signals. The dataset is inputted as the ds variable, typically the closing price of an asset. The user has the flexibility to set several parameters, including the lookback window p, the learning rate lrate, the number of epochs for training the model, and the filtering type for signals. These inputs allow for a high degree of customization based on different assets or market conditions.
At the core of this model, we have a supervised learning setup where the system learns from a training period, specified by the start and stop dates. It adjusts its model over multiple epochs to minimize error and improve its ability to classify new data correctly. The norm function normalizes the features to ensure that they are on the same scale, which is crucial for training machine learning models. Normalization is performed on the inputs like RSI (Relative Strength Index), CCI (Commodity Channel Index), and ROC (Rate of Change) indicators, ensuring they fall within a consistent range.
The primary training process takes place within the loop where features are fed into the model, and weights are updated according to the algorithm’s logic. Specifically, it uses Euclidean distance to calculate which weight vector is closest to the current feature set. The function getwinner calculates this distance for two vectors—weights0 and weights1—and determines whether to generate a buy or sell signal. If the current feature vector is closer to the weights0 vector, the signal is to sell; if it is closer to the weights1 vector, the signal is to buy. If the distances are similar, the signal remains neutral, i.e., hold.
The update function is where the actual learning happens. It updates the weight vectors (weights0 and weights1) depending on whether the winning vector is for a buy or sell signal. This is achieved by adjusting the weights towards the input features, a process that allows the algorithm to refine its ability to predict future outcomes based on historical data. The learning rate (lrate) controls how much the weights change after each update.
To control the flow of the algorithm and prevent overfitting or misclassification, various filters are used. The volatilityBreak and volumeBreak functions help refine when to trigger signals based on volatility and volume, which can be key indicators in the financial market. For example, the volatilityBreak function checks the Average True Range (ATR) over a specified range to gauge market volatility.
Once training is complete, and the model has learned the relationships between the input features and the target signals (buy, sell, or hold), the algorithm moves to the testing phase. In this phase, it applies the learned weights to new data. The getwinner function is used to classify new samples, and based on the result, a trading signal (buy or sell) is generated. If the signal changes compared to the previous bar, a trade is initiated. The strategy includes a mechanism to plot buy and sell signals on the chart and alert the trader when it’s time to enter or exit a trade.
Figure 2.
Machine Learning code snippet from Tradingview platform (parameters settings).
Figure 2.
Machine Learning code snippet from Tradingview platform (parameters settings).
On the backtesting side, the script also keeps track of trades, including calculating cumulative returns, win/loss ratios, and overall performance. It does this by storing the starting price of a trade, comparing it to the exit price, and recording the profit or loss. The cumulative return is tracked over time, and statistics such as win rate and number of trades are displayed for evaluation. This allows the user to assess the effectiveness of the model in real market conditions.
Overall, this LVQ-based machine learning algorithm is a powerful tool for developing an automated trading strategy, allowing users to tune the model to their specific needs by adjusting input parameters, training periods, and filtering conditions.
The ADX Indicator for Trend Strength Analysis
The Average Directional Index (ADX) is a widely utilized technical indicator that measures the strength of a trend, rather than its direction. Developed by J. Welles Wilder, ADX is particularly beneficial for traders seeking confirmation of trend sustainability before executing trades. The indicator consists of three components: the ADX line, the Positive Directional Index (+DI), and the Negative Directional Index (-DI).An ADX value above 25 typically signifies a strong trend, while values below this threshold indicate a weak or ranging market. Traders often use ADX in conjunction with other technical indicators to validate trade setups. For instance, combining ADX with moving averages or momentum oscillators enhances the effectiveness of trend-following strategies, reducing false signals and improving overall trading performance.
Shannon Entropy-Based Indicator Using ADX Values
Entropy, a concept rooted in information theory, has been increasingly applied in financial markets to quantify market uncertainty and randomness. Shannon Entropy, in particular, measures the disorder within a given dataset, making it a valuable tool for assessing market stability. By incorporating ADX values into an entropy-based indicator, traders can gain insights into the predictability of market trends.
Figure 3.
Shannon Entropy Indicator values from Tradingview platform.
Figure 3.
Shannon Entropy Indicator values from Tradingview platform.
High and low entropy values provide critical insights into market behavior when represented on a trading chart. High entropy indicates a state of increased randomness and uncertainty, often observed during periods of sideways movement, consolidation, or volatile price action without a clear direction. On a graph, this would typically coincide with choppy price behavior, where trends frequently reverse, and traditional indicators struggle to provide reliable signals. Conversely, low entropy suggests a more structured and predictable market environment, often aligning with strong directional trends. Visually, this would appear as smoother price movements with sustained bullish or bearish momentum, where trading signals tend to be more effective. By overlaying the entropy indicator on a chart, traders can identify periods of stability and trend formation, using low entropy zones as potential entry points for trend-following strategies while avoiding high entropy conditions that could lead to false signals and increased trading risk.By analyzing the relationship between ADX and Shannon Entropy, traders can refine their entry and exit strategies, optimizing risk management in volatile market conditions. The fusion of these methodologies enables the development of sophisticated algorithmic trading models capable of adapting to complex market dynamics.
The evolution of cryptocurrency trading has necessitated the integration of advanced analytical tools to navigate the complexities of digital asset markets. Bitcoin remains a highly liquid and volatile trading instrument, providing numerous opportunities for algorithmic traders. The application of machine learning techniques, such as LVQ, facilitates improved market classification, while the ADX indicator serves as a robust tool for assessing trend strength. Furthermore, the incorporation of Shannon Entropy into ADX-based models enhances the ability to quantify market uncertainty, leading to more informed trading decisions. As technology and financial innovation continue to progress, these analytical methodologies will play an increasingly pivotal role in the future of algorithmic trading.
Figure 4.
Shannon Entropy code snippet from Tradingview platform.
Figure 4.
Shannon Entropy code snippet from Tradingview platform.
The provided Pine Script function calcEntropy(src) calculates the Shannon entropy of a given price-related data series, which serves as a measure of uncertainty or randomness in market movements. This approach helps filter out low-confidence trading conditions by identifying whether price action follows a structured trend or exhibits chaotic behavior.
The function begins by initializing an array, priceDist, with a fixed number of bins, all set to zero. This array acts as a histogram, storing the frequency distribution of price values over a defined lookback period. Next, the script determines the lowest and highest values of the input source over the last entropyLength bars, storing them in minVal and maxVal, respectively. The difference between these values, maxrange, defines the range of price movements during this period. If maxrange is greater than zero, meaning the market has experienced some variation, the script proceeds to calculate the distribution of price occurrences.
To achieve this, a loop iterates over the last entropyLength bars, normalizing each price value within the predefined range and assigning it to a corresponding bin. This process effectively segments the price data into discrete categories, allowing the function to construct an empirical probability distribution. The bin index for each price is determined by scaling the difference between the price and minVal relative to maxrange, then mapping it to one of the bins. Whenever a price falls into a bin, the script increments the corresponding value in priceDist, capturing the frequency of price occurrences across the period.
Once the distribution is established, another loop computes the Shannon entropy by iterating through all bins. The probability of each bin is calculated by dividing its frequency by the total number of samples. If a bin’s probability is greater than zero, its contribution to entropy is computed using the formula -prob * log(prob), summing over all bins to obtain the total entropy. The final value is normalized by dividing it by log(bins) and multiplying by 100, ensuring that entropy values are scaled into a more interpretable range.
To smooth out fluctuations and create a more stable entropy signal, the script applies an Exponential Moving Average (EMA) to the computed entropy values. In this case, entropy is calculated based on adxValue, meaning it is applied to the Average Directional Index (ADX), a widely used indicator for measuring trend strength. The result is a refined entropy measure that adjusts dynamically to changing market conditions.
This entropy-based approach has significant implications for algorithmic trading. Higher entropy values indicate greater uncertainty and randomness in price action, suggesting a choppy or ranging market where trading signals may be unreliable. Conversely, lower entropy values suggest more structured and directional price movements, providing stronger confirmation for trend-following strategies. By integrating entropy as a filter, traders can refine their trade selection process, avoiding entries in volatile, unpredictable conditions while prioritizing high-confidence market trends.
Algorithmic Trading Bot Development
Integrating Machine Learning with Entropy-Based Filtering
The proposed algorithmic trading bot is designed to leverage the Learning Vector Quantization (LVQ) machine learning algorithm in conjunction with Shannon entropy to refine trading signals based on the Average Directional Index (ADX). The core principle of this approach is to enhance the precision of long and short trade decisions by filtering out low-confidence signals, thereby improving overall trading accuracy.
Figure 5.
Input Properties for the strategy.
Figure 5.
Input Properties for the strategy.
The strategy properties are configured to optimize risk management and trading execution. Initially, the strategy starts with a capital of $1000, and the base currency is set to USD, meaning that all trades will be calculated and executed in US dollars. The order size is set to 100% of the equity, which means that the entire account balance is used for each trade, effectively maximizing exposure to the market with every signal. This approach may increase potential returns but also magnifies risk. Pyramiding is set to 1, which limits the strategy to a single open position in either direction at any given time, preventing overexposure. The commission is fixed at 0.05%, which means each trade incurs a small fee, slightly impacting the profitability but ensuring realistic trading costs are accounted for in the backtesting phase. These settings provide a clear and focused risk profile, allowing the user to evaluate how the strategy performs under different market conditions while maintaining control over leverage and costs.
The strategy is implemented within the Pine Script framework on the TradingView platform. The LVQ algorithm is utilized for signal classification, determining whether market conditions favor a long or short position. It does so by training on historical price data and identifying patterns that correlate with profitable trades. The model updates dynamically based on incoming data, allowing for adaptive learning over time.
Shannon entropy serves as a supplementary filtering mechanism, measuring the level of uncertainty in ADX values. The entropy calculation assesses market volatility and directional movement, filtering out signals with excessive randomness. This ensures that the bot only acts on robust trading signals where the likelihood of trend continuation is higher.
Strategy Execution and Signal Filtering
Figure 6.
Short position executed by the strategy.
Figure 6.
Short position executed by the strategy.
The bot executes trades based on a two-step decision process. First, the LVQ algorithm generates buy or sell signals by evaluating market features such as RSI, CCI, and ROC. Second, Shannon entropy is applied to these signals to eliminate trades that exhibit high uncertainty. Only signals where entropy remains below a defined threshold are considered for execution, thereby reducing exposure to unreliable trade conditions.
Figure 7.
Long position executed by the strategy.
Figure 7.
Long position executed by the strategy.
ADX values are incorporated to further refine trade entry criteria. The bot monitors the direction and strength of market trends, ensuring that positions are only entered when the ADX indicator confirms trend strength. The combination of these techniques aims to optimize trade selection and improve the profitability of the algorithm.
Optimized Strategy Design: Parameters, Logic, and Risk Management
The algorithmic trading strategy is designed to leverage a combination of Learning Vector Quantization (LVQ) and Shannon entropy filtering to refine trading signals. The optimization of parameters plays a critical role in enhancing decision-making, reducing market noise, and maximizing profitability. This section provides an in-depth analysis of the strategy’s logic, including the rationale behind learning rate selection, training period configuration, Shannon entropy threshold determination, risk management settings, and trade execution rules.
Learning Rate Selection and Training Period Configuration
The learning rate is a fundamental hyperparameter in LVQ that determines how quickly the model adapts to new data. After extensive testing, a learning rate of 0.01 was chosen as the optimal value. A higher learning rate (e.g., 0.05 or 0.1) led to excessive volatility in trade decisions, causing erratic position reversals and overfitting to short-term market fluctuations. Conversely, a lower learning rate (e.g., 0.001) resulted in a sluggish response to emerging trends, reducing the strategy’s ability to capitalize on profitable market movements. The selected learning rate balances adaptability and stability, allowing the model to recognize reliable patterns without excessive sensitivity to market noise.
The training period was set from January 1, 2020, to January 1, 2025, to ensure the model captures various market regimes, including bull markets, bear markets, and consolidation phases. This extended period enhances the model’s robustness, enabling it to generalize effectively to different financial environments.
Shannon Entropy Threshold: Filtering Uncertain Market Conditions
Shannon entropy serves as a dynamic filtering mechanism, distinguishing between high-confidence and low-confidence trading signals. The entropy period was set to 30, based on rigorous backtesting. This value ensures that entropy calculations capture medium-term market behavior while filtering out short-lived price fluctuations.
The entropy threshold was optimized to prevent the bot from executing trades in highly volatile and unpredictable conditions. Through empirical testing, it was observed that entropy values above a certain threshold led to an increased frequency of false signals, resulting in unnecessary losses. By applying a threshold filter, the strategy only initiates trades when market conditions exhibit clear directional trends, reducing exposure to choppy, range-bound price movements.
ADX Smoothing and Trend Confirmation
The Average Directional Index (ADX) smoothing period was configured at 14, as this value provided the most reliable trend confirmation. ADX is critical in determining market strength, and an excessively high smoothing period (e.g., 25 or 30) resulted in delayed entries, while lower values (e.g., 7 or 10) increased the risk of reacting to false breakouts. The chosen ADX smoothing factor ensures that trend signals remain robust and actionable without excessive lag.
Risk Management and Capital Allocation
A disciplined risk management framework was integrated to ensure capital preservation and long-term strategy sustainability:
Position Sizing: The bot allocates 100% of available capital per trade, ensuring maximum capital utilization and compounding benefits. However, this aggressive approach was balanced with stringent filtering mechanisms to prevent overexposure to poor-quality trades.
Lot Size: The minimum lot size was set at 0.01, aligning with forex and CFD trading constraints while maintaining precision in order execution.
Stop-Loss and Take-Profit Strategy: Unlike conventional trading strategies that rely on fixed stop-loss and take-profit levels, this bot employs dynamic trade exits, where positions are closed upon the detection of an opposite trading signal. This approach enhances trade efficiency by allowing profits to run in trending markets while preventing premature exits in volatile conditions.
Comparative Analysis of Parameter Variations
To validate the effectiveness of the selected parameters, alternative configurations were tested:
Higher entropy thresholds (>40) resulted in excessive trade filtering, leading to missed opportunities.
Lower entropy thresholds (<20) increased trade frequency but led to excessive losses in choppy markets.
Alternative learning rates (0.001 vs. 0.05) demonstrated that lower values caused lagging trade decisions, while higher values increased overfitting risks.
Shorter training periods (<2 years) failed to capture market cycles effectively, reducing the model’s predictive accuracy.
The final selection of parameters was based on a balance between precision, adaptability, and risk control, ensuring optimal strategy performance under various market conditions.