ML Lorentzian Classification Script
ML Lorentzian Classification Script
import jdehorty/MLExtensions/2 as ml
type Settings
float source
int neighborsCount
int maxBarsBack
int featureCount
int colorCompression
bool showExits
bool useDynamicExits
type Label
int long
int short
int neutral
type FeatureArrays
array<float> f1
array<float> f2
array<float> f3
array<float> f4
array<float> f5
type FeatureSeries
float f1
float f2
float f3
float f4
float f5
type MLModel
int firstBarIndex
array<int> trainingLabels
int loopSize
float lastDistance
array<float> distancesArray
array<int> predictionsArray
int prediction
type FilterSettings
bool useVolatilityFilter
bool useRegimeFilter
bool useAdxFilter
float regimeThreshold
int adxThreshold
type Filter
bool volatility
bool regime
bool adx
// ==== Helper Functions ====
switch feature_string
switch featureCount
Settings settings =
Settings.new(
FilterSettings filterSettings =
FilterSettings.new(
Filter filter =
Filter.new(
ml.regime_filter(ohlc4, filterSettings.regimeThreshold,
filterSettings.useRegimeFilter),
)
// Feature Variables: User-Defined Inputs for calculating Feature Series.
featureSeries =
FeatureSeries.new(
array.push(f1Array, featureSeries.f1)
array.push(f2Array, featureSeries.f2)
array.push(f3Array, featureSeries.f3)
array.push(f4Array, featureSeries.f4)
array.push(f5Array, featureSeries.f5)
// FeatureArrays Object: Storage of the calculated FeatureArrays into a single
object
featureArrays =
FeatureArrays.new(
f1Array, // f1
f2Array, // f2
f3Array, // f3
f4Array, // f4
f5Array // f5
// Label Object: Used for classifying historical data as training data for the ML
Model
Label direction =
Label.new(
long=1,
short=-1,
neutral=0
// EMA Settings
lag = input.int(2, "Lag", tooltip="Lag for crossover detection. Lower values result in
earlier crossovers. Recommended range: 1-2", inline='1', group='Kernel Settings')
// Display Settings
useAtrOffset = input.bool(defval = false, title = "Use ATR Offset", tooltip = "Will use
the ATR offset instead of the bar prediction offset.", group="Display Settings")
// This model specializes specifically in predicting the direction of price action over
the course of the next 4 bars.
// To avoid complications with the ML model, this value is hardcoded to 4 bars but
support for other training lengths may be added in the future.
src = settings.source
var prediction = 0.
array.push(y_train_array, y_train_series)
// ==== Core ML Logic ====
lastDistance = -1.0
lastDistance := d
array.push(distances, d)
lastDistance := array.get(distances,
math.round(settings.neighborsCount*3/4))
array.shift(distances)
array.shift(predictions)
//}
//}
//}
prediction := array.sum(predictions)
//}
// User Defined Filters: Used for adjusting the frequency of the ML Model's
predictions
filter_all = filter.volatility and filter.regime and filter.adx
signal := prediction > 0 and filter_all ? direction.long : prediction < 0 and filter_all ?
direction.short : nz(signal[1])
isHeldFourBars = barsHeld == 4
isDifferentSignalType = ta.change(signal)
yhat1 = kernels.rationalQuadratic(settings.source, h, r, x)
kernelEstimate = yhat1
// Kernel Crossovers
// Kernel Colors
// Alert Variables
barsSinceRedEntry = ta.barssince(startShortTrade)
barsSinceRedExit = ta.barssince(alertBullish)
barsSinceGreenEntry = ta.barssince(startLongTrade)
barsSinceGreenExit = ta.barssince(alertBearish)
x_val = bar_index
backTestStream = switch
startLongTrade => 1
endLongTrade => 2
startShortTrade => -1
endShortTrade => -2
init_table() =>
if showTradeStats
if barstate.islast