Strategy · Pine Script v5 · On the chart

Xsodus Logistic Long Regression Strategy

by xsodus

This strategy fits a binary logistic regression classification model bar by bar on the price chart to generate long trading signals.

View on GitHub

Repaints

GitHub stars5on the repository
Pine versionv5runs as is
Lines of code212
Settings11inputs
LicenceMIT
Last update2 weeks ago11 Oct 2026

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 strategy, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart. The trades and results appear in the Strategy Tester tab; adjust the settings with the gear icon.

Source Code

212 lines of Pine Script v5 from xsodus-logistic-regression.pine, published under MIT. Keep the author's credit and licence lines when you reuse it.

xsodus-logistic-regression.pine
// SPDX-License-Identifier: MIT
// © capissimo and © xsodus

//@version=5
strategy('Xsodus Logistic Long Regression Strategy (v.3)', '', true)

// Multi-timeframe Strategy based on Logistic Regression algorithm

// Description: 

// This strategy uses a classic machine learning algorithm that came from statistics -
// Logistic Regression (LR).

// The first and most important thing about logistic regression is that it is not a 
// 'Regression' but a 'Classification' algorithm. The name itself is somewhat misleading. 
// Regression gives a continuous numeric output but most of the time we need the output in 
// classes (i.e. categorical, discrete). For example, we want to classify emails into “spam” or 
// 'not spam',  classify treatment into “success” or 'failure', classify statement into “right” 
// or 'wrong', classify election data into 'fraudulent vote' or 'non-fraudulent vote', 
// classify market move into 'long' or 'short' and so on. These are the examples of 
// logistic regression having a binary output (also called dichotomous). 

// You can also think of logistic regression as a special case of linear regression when the 
// outcome variable is categorical, where we are using log of odds as dependent variable. 
// In simple words, it predicts the probability of occurrence of an event by fitting data to a 
// logit function.

// Basically, the theory behind Logistic Regression is very similar to the one from Linear 
// Regression, where we seek to draw a best-fitting line over data points, but in Logistic 
// Regression, we don’t directly fit a straight line to our data like in linear regression. 
// Instead, we fit a S shaped curve, called Sigmoid, to our observations, that best 
// SEPARATES data points. Technically speaking, the main goal of building the model is to 
// find the parameters (weights) using gradient descent. 

// In this script the LR algorithm is retrained on each new bar trying to classify it 
// into one of the two categories. This is done via the logistic_regression function by
// updating the weights w in the loop that continues for iterations number of times.
// In the end the weights are passed through the sigmoid function, yielding a
// prediction.

// Mind that some assets require to modify the script's input parameters. For instance, 
// when used with BTCUSD and USDJPY, the 'Normalization Lookback' parameter should be set 
// down to 4 (2,...,5..), and optionally the 'Use Price Data for Signal Generation?' 
// parameter should be checked. The defaults were tested with EURUSD.

// 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

ptype = input.string('Close', 'Price Type', options=['Open', 'High', 'Low', 'Close', 'HL2', 'OC2', 'OHL3', 'HLC3', 'OHLC4'])
reso = input.timeframe('', 'Resolution')
lookback = input.int(3, 'Lookback Window Size |2..n| (2)', minval=2)
nlbk = input.int(2, 'Normalization Lookback |2..240| (120)', minval=2, maxval=240)
lrate = input.float(0.0009, 'Learning Rate |0.0001..0.01|', minval=0.0001, maxval=0.01, step=0.0001)
iterations = input.int(1000, 'Training Iterations |50..20000|', minval=50)
ftype = input.string('None', 'Filter Signals by', options=['Volatility', 'Volume', 'Both', 'None'])
curves = input(false, 'Show Loss & Prediction Curves?')
easteregg = input(true, 'Optional Calculation?')
useprice = input(true, 'Use Price Data for Signal Generation?')
holding_p = input.int(5, 'Holding Period |1..n|', minval=1)

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

var BUY = 1
var SELL = -1
var HOLD = 0
var signal = HOLD
var hp_counter = 0

//-------------------- 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

volumeBreak(thres) =>
    rsivol = ta.rsi(volume, 14)
    osc = ta.hma(rsivol, 10)
    osc > thres

volatilityBreak(volmin, volmax) =>
    ta.atr(volmin) > ta.atr(volmax)

dot(v, w, p) =>  // dot product
    math.sum(v * w, p)

sigmoid(z) =>
    1.0 / (1.0 + math.exp(-z))

logistic_regression(X, Y, p, lr, iterations) =>
    w = 0.0
    loss = 0.0
    for i = 1 to iterations by 1
        hypothesis = sigmoid(dot(X, 0.0, p))  //-- prediction
        loss := -1.0 / p * dot(dot(Y, math.log(hypothesis) + 1.0 - Y, p), math.log(1.0 - hypothesis), p)
        gradient = 1.0 / p * dot(X, hypothesis - Y, p)
        w -= lr * gradient  //-- update weights
        w

    [loss, sigmoid(dot(X, w, p))]  //-- current loss & prediction

minimax(ds, p, min, max) =>  // normalize to price
    hi = ta.highest(ds, p)
    lo = ta.lowest(ds, p)
    (max - min) * (ds - lo) / (hi - lo) + min

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

ds = ptype == 'Open' ? open : ptype == 'High' ? high : ptype == 'Low' ? low : ptype == 'Close' ? close : ptype == 'HL2' ? (high + low) / 2 : ptype == 'OC2' ? (open + close) / 2 : ptype == 'OHL3' ? (open + high + low) / 3 : ptype == 'HLC3' ? (high + low + close) / 3 : (open + high + low + close) / 4

base_ds = request.security('', reso, ds[barstate.ishistory ? 0 : 1])
synth_ds = math.log(math.abs(math.pow(base_ds, 2) - 1) + .5)  //-- generate a synthetic dataset

base = easteregg ? time : base_ds  //-- standard and optional calculation
synth = easteregg ? base_ds : synth_ds

[loss, prediction] = logistic_regression(base, synth, lookback, lrate, iterations)
scaled_loss = minimax(loss, nlbk, ta.lowest(base_ds, nlbk), ta.highest(base_ds, nlbk))
scaled_prediction = minimax(prediction, nlbk, ta.lowest(base_ds, nlbk), ta.highest(base_ds, nlbk))

filter = ftype == 'Volatility' ? volatilityBreak(1, 10) : ftype == 'Volume' ? volumeBreak(49) : ftype == 'Both' ? volatilityBreak(1, 10) and volumeBreak(49) : true

signal := useprice ? base_ds < scaled_loss and filter ? SELL : base_ds > scaled_loss and filter ? BUY : nz(signal[1]) : ta.crossunder(scaled_loss, scaled_prediction) and filter ? SELL : ta.crossover(scaled_loss, scaled_prediction) and filter ? BUY : nz(signal[1])

changed = ta.change(signal)

hp_counter := changed ? 0 : hp_counter + 1

startLongTrade = changed and signal == BUY
startShortTrade = changed and signal == SELL
endLongTrade = signal == BUY and hp_counter == holding_p and not changed or changed and signal == SELL
endShortTrade = signal == SELL and hp_counter == holding_p and not changed or changed and signal == BUY

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

plot(curves ? scaled_loss : na, '', color.new(color.blue, 0), 3)
plot(curves ? scaled_prediction : na, '', color.new(color.lime, 0), 3)

//plotshape(startLongTrade  ? low  : na, 'Buy',  shape.labelup,   location.belowbar, cAqua(10), size=size.small)
//plotshape(startShortTrade ? high : na, 'Sell', shape.labeldown, location.abovebar, cPink(10), 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)
if strategy.position_size == 0
    strategy.entry('Long', strategy.long, when=startLongTrade)

if strategy.position_size > 0
    strategy.close('Long', when=endLongTrade)

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

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

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

//lot_size = input.float(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
//    start_long_trade
//if endLongTrade
//    trade_count := 1
//    ldiff = ohl3 - start_long_trade
//    wins := ldiff > 0 ? 1 : 0
//    long_trades := ldiff * lot_size
//    long_trades
//if startShortTrade
//    start_short_trade := ohl3
//    start_short_trade
//if endShortTrade
//    trade_count := 1
//    sdiff = start_short_trade - ohl3
//    wins := sdiff > 0 ? 1 : 0
//    short_trades := sdiff * lot_size
//    short_trades
//
//cumreturn = ta.cum(long_trades) + ta.cum(short_trades)  //-- cumulative return 
//totaltrades = ta.cum(trade_count)
//totalwins = ta.cum(wins)
//totallosses = totaltrades - totalwins == 0 ? 1 : totaltrades - totalwins

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

//show_info = input(true, 'Show Info?')
//
//var label lbl = na
//
//tbase = (time - time[1]) / 1000
//tcurr = (timenow - time_close[1]) / 1000
//
//info = 'CR=' + str.tostring(cumreturn, '#.#') + '\nTrades: ' + str.tostring(totaltrades, '#') + '\nWin/Loss: ' + str.tostring(totalwins / totallosses, '#.##') + '\nWinrate: ' + str.tostring(totalwins / totaltrades, '#.#%') + '\nBar Time: ' + str.tostring(tcurr / tbase, '#.#%')
//
//if show_info 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

Xsodus Logistic Long Regression Strategy computes model weights via iterative gradient descent based on user-defined learning rates, lookback windows, and normalization parameters. Entries trigger on positive model classifications and can be filtered by volatility or volume, while exits occur after a fixed holding period. The author explicitly notes that signals repaint, and the strategy operates without a protective stop loss.

Details

Published by

xsodus on GitHub

Type

Strategy, Pine Script v5

Draws

On the price chart

Category

AI / machine learning, Trend

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

  • Signals can change on the live bar or after it closes (repainting)
  • The strategy enters trades without a stop loss

Topics

pinescript

Settings

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

SettingTypeDefaultVariable
Price TypestringCloseptype
Resolutiontimeframereso
Lookback Window Size |2..n| (2)int3lookback
Normalization Lookback |2..240| (120)int2nlbk
Learning Rate |0.0001..0.01|float0.0009lrate
Training Iterations |50..20000|int1000iterations
Filter Signals bystringNoneftype
Show Loss & Prediction Curves?boolfalsecurves
Optional Calculation?booltrueeasteregg
Use Price Data for Signal Generation?booltrueuseprice
Holding Period |1..n|int5holding_p
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

This strategy implements a single-layer perceptron machine learning model built from scratch to generate trading signals directly on the price chart. It calculates predictions based on sample size, threshold settings, and optional volatility or volume filters.

Strategy v5 AI / machine learningTrend
Updated 1 year ago · None stated GitHub repository of Machine Learning: Perceptron-Based Strategy [5] (v.3)

Lastguru KNN A002

by ruckard
13 stars

This strategy applies k-nearest neighbors machine learning to fast and slow moving average crossovers to filter entry signals. It overlays directly on the price chart and incorporates built-in Zignaly alert triggers for automated execution. Traders can configu

Strategy v5 AI / machine learningMoving averages
Updated 1 year ago · None stated GitHub repository of Lastguru KNN A002

This strategy plots an ATR Chandelier trailing stop on the price chart and tracks a directional trend state. It marks confirmed flips, weak body-rejected flips, and wick traps, and colors bars as price nears warning or danger zones around the flip trigger. Ada

Strategy v6 TrendVolatility & bands
Updated yesterday · MIT GitHub repository of Chandelier Flip Radar [WavesUnchained]
4 stars

This overlay strategy combines a Supertrend with a k-nearest-neighbors filter. It smooths price and the Supertrend with weighted moving averages, then uses neighbor count and lookback length to form an AI trend signal. Traders can allow long, short, or both di

Strategy v5 TrendVolatility & bands
Updated 2 years ago · None stated GitHub repository of AI Volume Supertrend Strategy

Neural Network

by iceberg211
2 stars

Calculates composite scoring across multiple technical indicators including EMAs, SuperTrend, Parabolic SAR, Hull Moving Average, RSI, MACD, WaveTrend, Stochastic, Bollinger Bands, and ATR. Trading entries trigger on the price chart when cumulative weighted sc

Strategy v4 TrendOscillators & momentum
Updated 1 year ago · None stated GitHub repository of Neural Network

More strategys

Trade Your Charts With a Regulated Broker

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

Compare brokers