Machine Learning: Perceptron-based strategy

Jan 21, 2021

Static chart image
Price Action Based
Dynamic Overlays
Volume Based
Signals
Machine Learning
Moving Averages
Trailing-Stop
Volatility
Repainting Functionality

The Machine Learning: Perceptron-based strategy indicator utilizes a single-layer artificial neural network to classify price action and generate trend-following signals based on learned data patterns. It aims to identify the relationship between historical inputs and current price direction to provide actionable buy and sell entries.

Usage

The Usage section describes how the script can be used to identify market trends. The indicator functions by retraining a perceptron on every new bar, attempting to classify whether the current price action is bullish or bearish based on historical sample data.

  • Signal Generation: Buy signals are triggered when the perceptron classifies the price action as positive (outcome > 0) while the DMI is above the specified threshold. Sell signals occur when the classification is negative and the DMI is below the threshold.
  • Filtering: Users can apply Volatility or Volume filters to reduce noise. For example, selecting "Volatility" ensures signals only trigger when market range (ATR) is expanding.
  • Holding Period: The script includes a built-in exit mechanism based on a "Holding Period." Once the specified number of bars has passed since a signal, a stop cross is plotted, indicating the trade should be closed.
  • Repainting Note: Because the perceptron retrains and updates its weights dynamically on each bar, signals may change their appearance or position in a live environment. Using the TradingView playback feature is recommended to observe this behavior.

Details

The script implements a Learning Perceptron, the simplest form of an artificial neural network (ANN). It consists of a single neuron that processes inputs, applies weights and a bias, and passes the result through a step function activation: $f(z) = 1$ if $z > 0$, else $-1$.

The training process involves:

  1. Initialization: Weights and bias are initially set.
  2. Learning Rule: The script iterates through a "Sample Size" of historical bars (Epochs) to update weights based on the error of each observation.
  3. Optimization: The weight parameters are adjusted to move the classifier’s decision boundary toward a correct classification of the last observation.
  4. Convergence: Through repeated sweeps (epochs), the weights converge to a state that best models the recent price relationship.

Settings

General Settings

  • Time Frame: Defines the timeframe from which the data is pulled for calculation.
  • Sample Size: The number of historical bars used for training the perceptron.
  • Period: The lookback period used for the internal Moving Average and DMI calculations.
  • Threshold [0..100]: Sets the sensitivity level for the DMI filter.
  • Filter: Choose between Volatility, Volume, Both, or None to qualify signals.
  • Holding Period: The maximum number of bars to stay in a trade before a stop signal is generated.

Visual Settings

  • Ma: Toggles the visibility of the internal Moving Average curve.
  • Bar Color: Colors the price bars based on the current detected trend.
  • Trailing: Displays a line based on linear regression and MG calculation to assist in trailing stops.
  • Info: Displays a label on the chart showing current DMI values and the sum of errors from the perceptron training.

FAQ

How does the perceptron "learn" in this script?

The script uses a for-loop to iterate through historical data points (epochs). During each iteration, it calculates the prediction error and updates the weights ($weight1$, $weight2$) and the bias to minimize that error.

Why do the signals repaint?

The signals repaint because the neural network's weights are recalculated on every new bar using the most recent data. As the training set shifts forward, the optimized boundary for previous bars may change.

How do I access this indicator?

You can get access on the LuxAlgo Library for charting platforms like TradingView, MetaTrader (MT4/MT5), and NinjaTrader for free.

Free access on the following platforms
tradingviewSymbolTradingView
Open in your browser

This in-app browser is not compatible with Get Access from the library.

Tap the menu (...) at the top of your screen and select "Open in Browser", "Open in Safari", or "Open in Chrome" to continue.

ninjatraderNinjaTrader
Open in your browser

This in-app browser is not compatible with Get Access from the library.

Tap the menu (...) at the top of your screen and select "Open in Browser", "Open in Safari", or "Open in Chrome" to continue.

metatrader4MetaTrader 4/5
Open in your browser

This in-app browser is not compatible with Get Access from the library.

Tap the menu (...) at the top of your screen and select "Open in Browser", "Open in Safari", or "Open in Chrome" to continue.

thinkorswimThinkorswim
Open in your browser

This in-app browser is not compatible with Get Access from the library.

Tap the menu (...) at the top of your screen and select "Open in Browser", "Open in Safari", or "Open in Chrome" to continue.

Trading & investing are risky and many will lose money in connection with trading and investing activities. All content on this site is not intended to, and should not be, construed as financial advice. Decisions to buy, sell, hold or trade in securities, commodities and other investments involve risk and are best made based on the advice of qualified financial professionals. Past performance does not guarantee future results.

Hypothetical or Simulated performance results have certain limitations. Unlike an actual performance record, simulated results do not represent actual trading. Also, since the trades have not been executed, the results may have under-or-over compensated for the impact, if any, of certain market factors, including, but not limited to, lack of liquidity. Simulated trading programs in general are designed with the benefit of hindsight, and are based on historical information. No representation is being made that any account will or is likely to achieve profit or losses similar to those shown.

Testimonials appearing on this website may not be representative of other clients or customers and is not a guarantee of future performance or success.

As a provider of technical analysis tools and strategies, we do not have access to the personal trading accounts or brokerage statements of our customers. As a result, we have no reason to believe our customers perform better or worse than traders as a whole based on any content, tool, or platform feature we provide.

Charts used on this site are by TradingView in which the majority of our technical indicators are built on. TradingView® is a registered trademark of TradingView, Inc. www.TradingView.com TradingView® has no affiliation with the owner, developer, or provider of the Services described herein.

Market data is provided by CBOE, CME Group, BarChart, Massive, CoinAPI. Select U.S. equities data is provided through Massive. CBOE BZX real-time U.S. equities data is licensed from CBOE and provided through BarChart. Real-time futures data is licensed from CME Group and provided through BarChart. Select cryptocurrency data, including major coins, is provided through CoinAPI. All data is provided “as is” and should be verified independently for trading purposes.

This does not represent our full Disclaimer. Please read our full disclaimer.

© 2026 LuxAlgo Global, LLC.