Indicator · Pine Script v4 · On the chart

Machine Learning: kNN-Based Strategy (MTF)

by capissimo

This indicator uses a k-Nearest Neighbors (kNN) machine learning algorithm to classify current market conditions based on historical indicator pairs.

View on GitHub

Repaints

GitHub stars114on the repository
Pine versionv4older, still runs
Lines of code218
Settings14inputs
LicenceNone statedfile: MPL-2.0
Last update1 year ago7 Nov 2024

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new indicator, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.

Source Code

218 lines of Pine Script v4 from scripts/097-ML-kNN-based-Strategy-(mtf).pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

097-ML-kNN-based-Strategy-(mtf).pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © capissimo

//@version=4
study("Machine Learning: kNN-based Strategy (mtf)", '', true, precision=4, max_labels_count=200)

// kNN-based Strategy (FX and Crypto), multi-timeframe version

// Description: 
// This strategy uses a classic machine learning algorithm - k Nearest Neighbours (kNN) - 
// to let you find a prediction for the next (tomorrow's, next month's, etc.) market move. 
// Being an unsupervised machine learning algorithm, kNN is one of the most simple learning algorithms. 

// To do a prediction of the next market move, the kNN algorithm uses the historic data, 
// collected in 3 arrays - feature1, feature2 and directions, - and finds the k-nearest 
// neighbours of the current indicator(s) values. 

// The two dimensional kNN algorithm just has a look on what has happened in the past when 
// the two indicators had a similar level. It then looks at the k nearest neighbours, 
// sees their state and thus classifies the current point.

// The kNN algorithm offers a framework to test all kinds of indicators easily to see if they 
// have got any *predictive value*. One can easily add cog, wpr and others.
// Note: TradingViews's playback feature helps to see this strategy in action.
// Warning: Signals ARE repainting.

// Style tags: Trend Following, Trend Analysis
// Asset class: Equities, Futures, ETFs, Currencies and Commodities
// Dataset: FX Minutes/Hours+++/Days

//-------------------- Inputs

ds          = input(close, 'Dataset')
reso        = input('',   'Resolution',  input.resolution)
ind         = input('All','Indicator',   options=['RSI','ROC','CCI','All'])
fast        = input(14,   'Fast Period', minval=1)
slow        = input(28,   'Slow Period', minval=2)
fltr        = input(false,'To Filter Out Signals By Volatility?')

startYear   = input(2000, 'Training Start Year',  minval=2000)
startMonth  = input(1,    'Training Start Month', minval=1, maxval=12)
startDay    = input(1,    'Training Start Day',   minval=1, maxval=31)
stopYear    = input(2021, 'Training Stop Year',   minval=2000)
stopMonth   = input(12,   'Training Stop Month',  minval=1, maxval=12)
stopDay     = input(31,   'Training Stop Day',    minval=1, maxval=31)

//-------------------- System Variables

var int   BUY  =  1
var int   SELL = -1
var int   HOLD =  0
var float k    = floor(sqrt(252))  // k Value for kNN

//-------------------- Custom Functions

cAqua(g) =>  g>9?#0080FFff:g>8?#0080FFe5:g>7?#0080FFcc:g>6?#0080FFb2:g>5?#0080FF99
           : g>4?#0080FF7f:g>3?#0080FF66:g>2?#0080FF4c:g>1?#0080FF33:#00C0FF19
cPink(g) =>  g>9?#FF0080ff:g>8?#FF0080e5:g>7?#FF0080cc:g>6?#FF0080b2:g>5?#FF008099
           : g>4?#FF00807f:g>3?#FF008066:g>2?#FF00804c:g>1?#FF008033:#FF008019

//-------------------- Logic

x = security('', reso, ds)  // [barstate.ishistory ? 0 : 1]

periodStart = timestamp(startYear, startMonth, startDay, 0, 0)
periodStop  = timestamp(stopYear,  stopMonth,  stopDay, 0, 0)

// 3 pairs of predictor indicators, long and short each
rs = rsi(close, slow), rf = rsi(close, fast)
cs = cci(close, slow), cf = cci(close, fast)
os = roc(close, slow), of = roc(close, fast)
// TOADD or TOTRYOUT:
//    cmo(close, slow), cmo(close, fast)
//    mfi(close, slow), mfi(close, fast)
//    mom(close, slow), mom(close, fast)
f1 = ind=='RSI' ? rs : ind=='ROC' ? os : ind=='CCI' ? cs : avg(rs, os, cs)
f2 = ind=='RSI' ? rf : ind=='ROC' ? of : ind=='CCI' ? cf : avg(rf, of, cf)

// Classification data, what happens on the next bar
class = x[1]<x[0] ? SELL: x[1]>x[0] ? BUY : HOLD

// Training data, normalized to the range of [0,...,100]
var feature1   = array.new_float(0)  // [0,...,100]
var feature2   = array.new_float(0)  //    ...
var directions = array.new_int(0)    // [-1; +1]

// Result data
var predictions     = array.new_int(0)
var prediction      = 0.

var startLongTrade  = false 
var startShortTrade = false
var endLongTrade    = false  
var endShortTrade   = false  

// Use particular training period
if time >= periodStart and time <= periodStop
    // Store everything in arrays. Features represent a square 100 x 100 matrix,
    // whose row-colum intersections represent class labels, showing historic directions
    array.push(feature1, f1)
    array.push(feature2, f2)
    array.push(directions, class)

// Ucomment the followng statement (if barstate.islast) and tab everything below
// between BOBlock and EOBlock marks to see just the recent several signals gradually 
// showing up, rather than all the preceding signals

//if barstate.islast   

//==BOBlock    

// Core logic of the algorithm

size   = array.size(directions)
maxdist = -999.

// Loop through the training arrays, getting distances and corresponding directions.
for i=0 to size-1
    // Calculate the euclidean distance of current point to all historic points,
    // here the metric used might as well be a manhattan distance or any other.
    d = sqrt(pow(f1 - array.get(feature1, i), 2) + pow(f2 - array.get(feature2, i), 2))
    
    if d > maxdist
        maxdist := d
        if array.size(predictions) >= k
            array.shift(predictions)
        array.push(predictions, array.get(directions, i))
        
//==EOBlock    

// Note: in this setup there's no need for distances array (i.e. array.push(distances, d)),
//       but the drawback is that a sudden max value may shadow all the subsequent values.
// One of the ways to bypass this is to:
// 1) store d in distances array,
// 2) calculate newdirs = bubbleSort(distances, directions), and then 
// 3) take a slice with array.slice(newdirs) from the end
        
// Get the overall prediction of k nearest neighbours
prediction := array.sum(predictions)   
    
// Now that we got a prediction for the next market move, we need to make use of this prediction and 
// trade it. The returns then will show if everything works as predicted.
// Over here is a simple long/short interpretation of the prediction, 
// but of course one could also use the quality of the prediction (+5 or +1) in some sort of way,
// ex. for position sizing.
    
signal = HOLD
signal := prediction > 0 ? BUY : prediction < 0 ? SELL : nz(signal[1])  // HOLD

filter           = fltr ? atr(13) > atr(40) : true 
startLongTrade  := change(signal) and signal==BUY  and filter // filter out by high volatility, 
startShortTrade := change(signal) and signal==SELL and filter // or ex. atr(1) > atr(10)...
endLongTrade    := change(signal) and signal==SELL //TOADD: stop by trade duration
endShortTrade   := change(signal) and signal==BUY  

//-------------------- Rendering

plotshape(startLongTrade  ? low  : na, 'Buy',  shape.labelup,   location.belowbar, cAqua(prediction*5),  size=size.small)  // color intensity correction
plotshape(startShortTrade ? high : na, 'Sell', shape.labeldown, location.abovebar, cPink(-prediction*5), size=size.small)
plot(endLongTrade         ? high : na, 'StopBuy',  cAqua(6), 2, plot.style_cross)
plot(endShortTrade        ? low  : na, 'StopSell', cPink(6), 2, plot.style_cross)

//-------------------- Alerting

alertcondition(startLongTrade,  'Buy',  'Go long!')
alertcondition(startShortTrade, 'Sell', 'Go short!') 
alertcondition(startLongTrade or startShortTrade, 'Alert', 'Deal Time!')

//-------------------- Backtesting (TODO)

lot_size  = input(0.01, 'Lot Size', options=[0.01,0.1,0.2,0.3,0.5,1,2,3,5,10,20,30,50,100,1000]) 

ohl3 = (open+high+low)/3

var float start_long_trade  = 0.  
var float long_trades       = 0.
var float start_short_trade = 0.  
var float short_trades      = 0.
var int   wins              = 0
var int   trade_count       = 0

if startLongTrade 
    start_long_trade := ohl3 
if endLongTrade
    trade_count  := 1
    ldiff = (ohl3 - start_long_trade)
    wins         := ldiff > 0 ? 1 : 0
    long_trades  := ldiff * lot_size   
if startShortTrade 
    start_short_trade := ohl3
if endShortTrade
    trade_count  := 1
    sdiff = (start_short_trade - ohl3)
    wins         := sdiff > 0 ? 1 : 0
    short_trades := sdiff * lot_size 
    
cumreturn   = cum(long_trades) + cum(short_trades)  //-- cumulative return 
totaltrades = cum(trade_count)
totalwins   = cum(wins)  
totallosses = totaltrades - totalwins == 0 ? 1 : totaltrades - totalwins

//-------------------- Information

var label lbl = na

tbase = (time - time[1]) / 1000
tcurr = (timenow - time_close[1]) / 1000

info =  'CR='          + tostring(cumreturn,             '#.#') 
      + '\nTrades: '   + tostring(totaltrades,           '#')
      + '\nWin/Loss: ' + tostring(totalwins/totallosses, '#.##') 
      + '\nWinrate: '  + tostring(totalwins/totaltrades, '#.#%') 
      + '\nBar Time: ' + tostring(tcurr / tbase,         '#.#%')

if input(true, 'Show Information?') and barstate.islast  
    lbl := label.new(bar_index, close, info, xloc.bar_index, yloc.price, 
                     color.new(color.blue, 100), label.style_label_left, color.black, size.small, text.align_left)
    label.delete(lbl[1])

Overview

Machine Learning: kNN-Based Strategy (MTF) evaluates combinations of fast and slow periods of RSI, Rate of Change (ROC), and Commodity Channel Index (CCI) within a designated training time window to signal potential buy, sell, or hold directions. Inputs permit selecting resolution, custom indicator combinations, training start/stop dates, and optional volatility filtering. Outputs are rendered directly on the price chart. Note that this multi-timeframe script repaints on historical and live data.

Details

Published by

capissimo on GitHub

Type

Indicator, Pine Script v4

Draws

On the price chart

Category

AI / machine learning, Trend, Oscillators & momentum

Markets

Forex, Crypto

Timeframes

Any timeframe

Risk Notes

  • Signals can change on the live bar or after it closes (repainting)

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
Datasetsourcecloseds
Resolutiontimeframereso
IndicatorstringAllind
Fast Periodint14fast
Slow Periodint28slow
To Filter Out Signals By Volatility?boolfalsefltr
Training Start Yearint2000startYear
Training Start Monthint1startMonth
Training Start Dayint1startDay
Training Stop Yearint2021stopYear
Training Stop Monthint12stopMonth
Training Stop Dayint31stopDay
Show all 14 settings
SettingTypeDefaultVariable
Lot Sizefloat0.01lot_size
Show Information?booltrueShow Information?
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

257 stars

This indicator uses a Lorentzian distance k-nearest neighbors classification algorithm to predict market direction based on up to five technical features, such as RSI, WaveTrend, CCI, and ADX. It compares current feature values against historical bar patterns

Indicator v5 AI / machine learningOscillators & momentum
Updated 6 weeks ago · None stated GitHub repository of Donald - Machine Learning Options

Machine Learning: Lorentzian Classification

by artificial-intelligence-edge
75 stars

An overlay indicator that applies a k-nearest-neighbors machine learning classifier using Lorentzian distance to predict price movement. It evaluates historical market features, such as RSI, WaveTrend, CCI, and ADX, to identify similar price-time patterns and

Indicator v6 AI / machine learningOscillators & momentum
Updated 2 days ago · MIT GitHub repository of Machine Learning: Lorentzian Classification

This indicator plots an oscillator in a separate pane. It builds several SuperTrend series across a user-defined factor range and step, using ATR as the band width. On each bar it measures the distance between the close and each SuperTrend output, then groups

Indicator v5 TrendVolatility & bands
Updated 2 years ago · GPL-3.0 GitHub repository of AI SuperTrend Clustering Oscillator [LuxAlgo]
7 stars

Lorentzian Classification is a machine learning indicator that applies a k-nearest neighbors algorithm using Lorentzian distance to analyze market patterns. It processes configurable technical indicators, including RSI, WaveTrend, CCI, and ADX, as feature vect

Indicator v6 AI / machine learningOscillators & momentum
Updated 1 year ago · MPL-2.0 GitHub repository of Lorentzian Classification
948 stars

The Adaptive RSI Moving Average (ARSIMA) is an overlay indicator that dynamically adjusts its smoothing factor based on the Relative Strength Index (RSI). When the RSI moves toward extreme overbought or oversold conditions, the smoothing factor increases to ma

Indicator v4 Moving averagesOscillators & momentum
Updated 10 days ago · GPL-3.0 GitHub repository of Adaptive RSI Moving Average

More indicators

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers