Machine Learning: Lorentzian Classification
Forums › ProRealTime foro Español › Soporte ProBuilder › Machine Learning: Lorentzian Classification
- This topic has 8 replies, 6 voices, and was last updated 1 year ago by robertogozzi.
-
-
05/11/2023 at 2:44 PM #214553
Hola a tod@s, os dejo aquí el código y la captura de pantalla de este indicador de TRADINGVIEW, el cuál me parece bastante interesante. De este modo a ver si podría trasladar a PRT. Gracias.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/// ©jdehorty// @version=5indicator('Machine Learning: Lorentzian Classification', 'Lorentzian Classification', true, precision=4, max_labels_count=500)import jdehorty/MLExtensions/2 as mlimport jdehorty/KernelFunctions/2 as kernels// ====================// ==== Background ====// ====================// When using Machine Learning algorithms like K-Nearest Neighbors, choosing an// appropriate distance metric is essential. Euclidean Distance is often used as// the default distance metric, but it may not always be the best choice. This is// because market data is often significantly impacted by proximity to significant// world events such as FOMC Meetings and Black Swan events. These major economic// events can contribute to a warping effect analogous a massive object's// gravitational warping of Space-Time. In financial markets, this warping effect// operates on a continuum, which can analogously be referred to as "Price-Time".// To help to better account for this warping effect, Lorentzian Distance can be// used as an alternative distance metric to Euclidean Distance. The geometry of// Lorentzian Space can be difficult to visualize at first, and one of the best// ways to intuitively understand it is through an example involving 2 feature// dimensions (z=2). For purposes of this example, let's assume these two features// are Relative Strength Index (RSI) and the Average Directional Index (ADX). In// reality, the optimal number of features is in the range of 3-8, but for the sake// of simplicity, we will use only 2 features in this example.// Fundamental Assumptions:// (1) We can calculate RSI and ADX for a given chart.// (2) For simplicity, values for RSI and ADX are assumed to adhere to a Gaussian// distribution in the range of 0 to 100.// (3) The most recent RSI and ADX value can be considered the origin of a coordinate// system with ADX on the x-axis and RSI on the y-axis.// Distances in Euclidean Space:// Measuring the Euclidean Distances of historical values with the most recent point// at the origin will yield a distribution that resembles Figure 1 (below).// [RSI]// |// |// |// ...:::....// .:.:::••••••:::•::..// .:•:.:•••::::••::••....::.// ....:••••:••••••••::••:...:•.// ...:.::::::•••:::•••:•••::.:•..// ::•:.:•:•••••••:.:•::::::...:..// |--------.:•••..•••••••:••:...:::•:•:..:..----------[ADX]// 0 :•:....:•••••::.:::•••::••:.....// ::....:.:••••••••:•••::••::..:.// .:...:••:::••••••••::•••....:// ::....:.....:•::•••:::::..// ..:..::••..::::..:•:..// .::..:::.....:// |// |// |// |// _|_ 0//// Figure 1: Neighborhood in Euclidean Space// Distances in Lorentzian Space:// However, the same set of historical values measured using Lorentzian Distance will// yield a different distribution that resembles Figure 2 (below).//// [RSI]// ::.. | ..:::// ..... | ......// .••••::. | :••••••.// .:•••••:. | :::••••••.// .•••••:... | .::.••••••.// .::•••••::.. | :..••••••..// .:•••••••::.........::••••••:..// ..::::••••.•••••••.•••••••:.// ...:•••••••.•••••••••::.// .:..••.••••••.••••..// |---------------.:•••••••••••••••••.---------------[ADX]// 0 .:•:•••.••••••.•••••••.// .••••••••••••••••••••••••:.// .:••••••••••::..::.::••••••••:.// .::••••••::. | .::•••:::.// .:••••••.. | :••••••••.// .:••••:... | ..•••••••:.// ..:••::.. | :.•••••••.// .:•.... | ...::.:••.// ...:.. | :...:••.// :::. | ..::// _|_ 0//// Figure 2: Neighborhood in Lorentzian Space// Observations:// (1) In Lorentzian Space, the shortest distance between two points is not// necessarily a straight line, but rather, a geodesic curve.// (2) The warping effect of Lorentzian distance reduces the overall influence// of outliers and noise.// (3) Lorentzian Distance becomes increasingly different from Euclidean Distance// as the number of nearest neighbors used for comparison increases.// ======================// ==== Custom Types ====// ======================// This section uses PineScript's new Type syntax to define important data structures// used throughout the script.type Settingsfloat sourceint neighborsCountint maxBarsBackint featureCountint colorCompressionbool showExitsbool useDynamicExitstype Labelint longint shortint neutraltype FeatureArraysarray<float> f1array<float> f2array<float> f3array<float> f4array<float> f5type FeatureSeriesfloat f1float f2float f3float f4float f5type MLModelint firstBarIndexarray<int> trainingLabelsint loopSizefloat lastDistancearray<float> distancesArrayarray<int> predictionsArrayint predictiontype FilterSettingsbool useVolatilityFilterbool useRegimeFilterbool useAdxFilterfloat regimeThresholdint adxThresholdtype Filterbool volatilitybool regimebool adx// ==========================// ==== Helper Functions ====// ==========================series_from(feature_string, _close, _high, _low, _hlc3, f_paramA, f_paramB) =>switch feature_string"RSI" => ml.n_rsi(_close, f_paramA, f_paramB)"WT" => ml.n_wt(_hlc3, f_paramA, f_paramB)"CCI" => ml.n_cci(_close, f_paramA, f_paramB)"ADX" => ml.n_adx(_high, _low, _close, f_paramA)get_lorentzian_distance(int i, int featureCount, FeatureSeries featureSeries, FeatureArrays featureArrays) =>switch featureCount5 => math.log(1+math.abs(featureSeries.f1 - array.get(featureArrays.f1, i))) +math.log(1+math.abs(featureSeries.f2 - array.get(featureArrays.f2, i))) +math.log(1+math.abs(featureSeries.f3 - array.get(featureArrays.f3, i))) +math.log(1+math.abs(featureSeries.f4 - array.get(featureArrays.f4, i))) +math.log(1+math.abs(featureSeries.f5 - array.get(featureArrays.f5, i)))4 => math.log(1+math.abs(featureSeries.f1 - array.get(featureArrays.f1, i))) +math.log(1+math.abs(featureSeries.f2 - array.get(featureArrays.f2, i))) +math.log(1+math.abs(featureSeries.f3 - array.get(featureArrays.f3, i))) +math.log(1+math.abs(featureSeries.f4 - array.get(featureArrays.f4, i)))3 => math.log(1+math.abs(featureSeries.f1 - array.get(featureArrays.f1, i))) +math.log(1+math.abs(featureSeries.f2 - array.get(featureArrays.f2, i))) +math.log(1+math.abs(featureSeries.f3 - array.get(featureArrays.f3, i)))2 => math.log(1+math.abs(featureSeries.f1 - array.get(featureArrays.f1, i))) +math.log(1+math.abs(featureSeries.f2 - array.get(featureArrays.f2, i)))// ================// ==== Inputs ====// ================// Settings Object: General User-Defined InputsSettings settings =Settings.new(input.source(title='Source', defval=close, group="General Settings", tooltip="Source of the input data"),input.int(title='Neighbors Count', defval=8, group="General Settings", minval=1, maxval=100, step=1, tooltip="Number of neighbors to consider"),input.int(title="Max Bars Back", defval=2000, group="General Settings"),input.int(title="Feature Count", defval=5, group="Feature Engineering", minval=2, maxval=5, tooltip="Number of features to use for ML predictions."),input.int(title="Color Compression", defval=1, group="General Settings", minval=1, maxval=10, tooltip="Compression factor for adjusting the intensity of the color scale."),input.bool(title="Show Default Exits", defval=false, group="General Settings", tooltip="Default exits occur exactly 4 bars after an entry signal. This corresponds to the predefined length of a trade during the model's training process.", inline="exits"),input.bool(title="Use Dynamic Exits", defval=false, group="General Settings", tooltip="Dynamic exits attempt to let profits ride by dynamically adjusting the exit threshold based on kernel regression logic.", inline="exits"))// Trade Stats Settings// Note: The trade stats section is NOT intended to be used as a replacement for proper backtesting. It is intended to be used for calibration purposes only.showTradeStats = input.bool(true, 'Show Trade Stats', tooltip='Displays the trade stats for a given configuration. Useful for optimizing the settings in the Feature Engineering section. This should NOT replace backtesting and should be used for calibration purposes only. Early Signal Flips represent instances where the model changes signals before 4 bars elapses; high values can indicate choppy (ranging) market conditions.', group="General Settings")useWorstCase = input.bool(false, "Use Worst Case Estimates", tooltip="Whether to use the worst case scenario for backtesting. This option can be useful for creating a conservative estimate that is based on close prices only, thus avoiding the effects of intrabar repainting. This option assumes that the user does not enter when the signal first appears and instead waits for the bar to close as confirmation. On larger timeframes, this can mean entering after a large move has already occurred. Leaving this option disabled is generally better for those that use this indicator as a source of confluence and prefer estimates that demonstrate discretionary mid-bar entries. Leaving this option enabled may be more consistent with traditional backtesting results.", group="General Settings")// Settings object for user-defined settingsFilterSettings filterSettings =FilterSettings.new(input.bool(title="Use Volatility Filter", defval=true, tooltip="Whether to use the volatility filter.", group="Filters"),input.bool(title="Use Regime Filter", defval=true, group="Filters", inline="regime"),input.bool(title="Use ADX Filter", defval=false, group="Filters", inline="adx"),input.float(title="Threshold", defval=-0.1, minval=-10, maxval=10, step=0.1, tooltip="Whether to use the trend detection filter. Threshold for detecting Trending/Ranging markets.", group="Filters", inline="regime"),input.int(title="Threshold", defval=20, minval=0, maxval=100, step=1, tooltip="Whether to use the ADX filter. Threshold for detecting Trending/Ranging markets.", group="Filters", inline="adx"))// Filter object for filtering the ML predictionsFilter filter =Filter.new(ml.filter_volatility(1, 10, filterSettings.useVolatilityFilter),ml.regime_filter(ohlc4, filterSettings.regimeThreshold, filterSettings.useRegimeFilter),ml.filter_adx(settings.source, 14, filterSettings.adxThreshold, filterSettings.useAdxFilter))// Feature Variables: User-Defined Inputs for calculating Feature Series.f1_string = input.string(title="Feature 1", options=["RSI", "WT", "CCI", "ADX"], defval="RSI", inline = "01", tooltip="The first feature to use for ML predictions.", group="Feature Engineering")f1_paramA = input.int(title="Parameter A", tooltip="The primary parameter of feature 1.", defval=14, inline = "02", group="Feature Engineering")f1_paramB = input.int(title="Parameter B", tooltip="The secondary parameter of feature 2 (if applicable).", defval=1, inline = "02", group="Feature Engineering")f2_string = input.string(title="Feature 2", options=["RSI", "WT", "CCI", "ADX"], defval="WT", inline = "03", tooltip="The second feature to use for ML predictions.", group="Feature Engineering")f2_paramA = input.int(title="Parameter A", tooltip="The primary parameter of feature 2.", defval=10, inline = "04", group="Feature Engineering")f2_paramB = input.int(title="Parameter B", tooltip="The secondary parameter of feature 2 (if applicable).", defval=11, inline = "04", group="Feature Engineering")f3_string = input.string(title="Feature 3", options=["RSI", "WT", "CCI", "ADX"], defval="CCI", inline = "05", tooltip="The third feature to use for ML predictions.", group="Feature Engineering")f3_paramA = input.int(title="Parameter A", tooltip="The primary parameter of feature 3.", defval=20, inline = "06", group="Feature Engineering")f3_paramB = input.int(title="Parameter B", tooltip="The secondary parameter of feature 3 (if applicable).", defval=1, inline = "06", group="Feature Engineering")f4_string = input.string(title="Feature 4", options=["RSI", "WT", "CCI", "ADX"], defval="ADX", inline = "07", tooltip="The fourth feature to use for ML predictions.", group="Feature Engineering")f4_paramA = input.int(title="Parameter A", tooltip="The primary parameter of feature 4.", defval=20, inline = "08", group="Feature Engineering")f4_paramB = input.int(title="Parameter B", tooltip="The secondary parameter of feature 4 (if applicable).", defval=2, inline = "08", group="Feature Engineering")f5_string = input.string(title="Feature 5", options=["RSI", "WT", "CCI", "ADX"], defval="RSI", inline = "09", tooltip="The fifth feature to use for ML predictions.", group="Feature Engineering")f5_paramA = input.int(title="Parameter A", tooltip="The primary parameter of feature 5.", defval=9, inline = "10", group="Feature Engineering")f5_paramB = input.int(title="Parameter B", tooltip="The secondary parameter of feature 5 (if applicable).", defval=1, inline = "10", group="Feature Engineering")// FeatureSeries Object: Calculated Feature Series based on Feature VariablesfeatureSeries =FeatureSeries.new(series_from(f1_string, close, high, low, hlc3, f1_paramA, f1_paramB), // f1series_from(f2_string, close, high, low, hlc3, f2_paramA, f2_paramB), // f2series_from(f3_string, close, high, low, hlc3, f3_paramA, f3_paramB), // f3series_from(f4_string, close, high, low, hlc3, f4_paramA, f4_paramB), // f4series_from(f5_string, close, high, low, hlc3, f5_paramA, f5_paramB) // f5)// FeatureArrays Variables: Storage of Feature Series as Feature Arrays Optimized for ML// Note: These arrays cannot be dynamically created within the FeatureArrays Object Initialization and thus must be set-up in advance.var f1Array = array.new_float()var f2Array = array.new_float()var f3Array = array.new_float()var f4Array = array.new_float()var f5Array = array.new_float()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 objectfeatureArrays =FeatureArrays.new(f1Array, // f1f2Array, // f2f3Array, // f3f4Array, // f4f5Array // f5)// Label Object: Used for classifying historical data as training data for the ML ModelLabel direction =Label.new(long=1,short=-1,neutral=0)// Derived from General SettingsmaxBarsBackIndex = last_bar_index >= settings.maxBarsBack ? last_bar_index - settings.maxBarsBack : 0// EMA SettingsuseEmaFilter = input.bool(title="Use EMA Filter", defval=false, group="Filters", inline="ema")emaPeriod = input.int(title="Period", defval=200, minval=1, step=1, group="Filters", inline="ema", tooltip="The period of the EMA used for the EMA Filter.")isEmaUptrend = useEmaFilter ? close > ta.ema(close, emaPeriod) : trueisEmaDowntrend = useEmaFilter ? close < ta.ema(close, emaPeriod) : trueuseSmaFilter = input.bool(title="Use SMA Filter", defval=false, group="Filters", inline="sma")smaPeriod = input.int(title="Period", defval=200, minval=1, step=1, group="Filters", inline="sma", tooltip="The period of the SMA used for the SMA Filter.")isSmaUptrend = useSmaFilter ? close > ta.sma(close, smaPeriod) : trueisSmaDowntrend = useSmaFilter ? close < ta.sma(close, smaPeriod) : true// Nadaraya-Watson Kernel Regression SettingsuseKernelFilter = input.bool(true, "Trade with Kernel", group="Kernel Settings", inline="kernel")showKernelEstimate = input.bool(true, "Show Kernel Estimate", group="Kernel Settings", inline="kernel")useKernelSmoothing = input.bool(false, "Enhance Kernel Smoothing", tooltip="Uses a crossover based mechanism to smoothen kernel color changes. This often results in less color transitions overall and may result in more ML entry signals being generated.", inline='1', group='Kernel Settings')h = input.int(8, 'Lookback Window', minval=3, tooltip='The number of bars used for the estimation. This is a sliding value that represents the most recent historical bars. Recommended range: 3-50', group="Kernel Settings", inline="kernel")r = input.float(8., 'Relative Weighting', step=0.25, tooltip='Relative weighting of time frames. As this value approaches zero, the longer time frames will exert more influence on the estimation. As this value approaches infinity, the behavior of the Rational Quadratic Kernel will become identical to the Gaussian kernel. Recommended range: 0.25-25', group="Kernel Settings", inline="kernel")x = input.int(25, "Regression Level", tooltip='Bar index on which to start regression. Controls how tightly fit the kernel estimate is to the data. Smaller values are a tighter fit. Larger values are a looser fit. Recommended range: 2-25', group="Kernel Settings", inline="kernel")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 SettingsshowBarColors = input.bool(true, "Show Bar Colors", tooltip="Whether to show the bar colors.", group="Display Settings")showBarPredictions = input.bool(defval = true, title = "Show Bar Prediction Values", tooltip = "Will show the ML model's evaluation of each bar as an integer.", group="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")barPredictionsOffset = input.float(0, "Bar Prediction Offset", minval=0, tooltip="The offset of the bar predictions as a percentage from the bar high or close.", group="Display Settings")// =================================// ==== Next Bar Classification ====// =================================// 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.sourcey_train_series = src[4] < src[0] ? direction.short : src[4] > src[0] ? direction.long : direction.neutralvar y_train_array = array.new_int(0)// Variables used for ML Logicvar predictions = array.new_float(0)var prediction = 0.var signal = direction.neutralvar distances = array.new_float(0)array.push(y_train_array, y_train_series)// =========================// ==== Core ML Logic ====// =========================// Approximate Nearest Neighbors Search with Lorentzian Distance:// A novel variation of the Nearest Neighbors (NN) search algorithm that ensures a chronologically uniform distribution of neighbors.// In a traditional KNN-based approach, we would iterate through the entire dataset and calculate the distance between the current bar// and every other bar in the dataset and then sort the distances in ascending order. We would then take the first k bars and use their// labels to determine the label of the current bar.// There are several problems with this traditional KNN approach in the context of real-time calculations involving time series data:// - It is computationally expensive to iterate through the entire dataset and calculate the distance between every historical bar and// the current bar.// - Market time series data is often non-stationary, meaning that the statistical properties of the data change slightly over time.// - It is possible that the nearest neighbors are not the most informative ones, and the KNN algorithm may return poor results if the// nearest neighbors are not representative of the majority of the data.// Previously, the user @capissimo attempted to address some of these issues in several of his PineScript-based KNN implementations by:// - Using a modified KNN algorithm based on consecutive furthest neighbors to find a set of approximate "nearest" neighbors.// - Using a sliding window approach to only calculate the distance between the current bar and the most recent n bars in the dataset.// Of these two approaches, the latter is inherently limited by the fact that it only considers the most recent bars in the overall dataset.// The former approach has more potential to leverage historical price action, but is limited by:// - The possibility of a sudden "max" value throwing off the estimation// - The possibility of selecting a set of approximate neighbors that are not representative of the majority of the data by oversampling// values that are not chronologically distinct enough from one another// - The possibility of selecting too many "far" neighbors, which may result in a poor estimation of price action// To address these issues, a novel Approximate Nearest Neighbors (ANN) algorithm is used in this indicator.// In the below ANN algorithm:// 1. The algorithm iterates through the dataset in chronological order, using the modulo operator to only perform calculations every 4 bars.// This serves the dual purpose of reducing the computational overhead of the algorithm and ensuring a minimum chronological spacing// between the neighbors of at least 4 bars.// 2. A list of the k-similar neighbors is simultaneously maintained in both a predictions array and corresponding distances array.// 3. When the size of the predictions array exceeds the desired number of nearest neighbors specified in settings.neighborsCount,// the algorithm removes the first neighbor from the predictions array and the corresponding distance array.// 4. The lastDistance variable is overriden to be a distance in the lower 25% of the array. This step helps to boost overall accuracy// by ensuring subsequent newly added distance values increase at a slower rate.// 5. Lorentzian distance is used as a distance metric in order to minimize the effect of outliers and take into account the warping of// "price-time" due to proximity to significant economic events.lastDistance = -1.0size = math.min(settings.maxBarsBack-1, array.size(y_train_array)-1)sizeLoop = math.min(settings.maxBarsBack-1, size)if bar_index >= maxBarsBackIndex //{for i = 0 to sizeLoop //{d = get_lorentzian_distance(i, settings.featureCount, featureSeries, featureArrays)if d >= lastDistance and i%4 //{lastDistance := darray.push(distances, d)array.push(predictions, math.round(array.get(y_train_array, i)))if array.size(predictions) > settings.neighborsCount //{lastDistance := array.get(distances, math.round(settings.neighborsCount*3/4))array.shift(distances)array.shift(predictions)//}//}//}prediction := array.sum(predictions)//}// ============================// ==== Prediction Filters ====// ============================// User Defined Filters: Used for adjusting the frequency of the ML Model's predictionsfilter_all = filter.volatility and filter.regime and filter.adx// Filtered Signal: The model's prediction of future price movement direction with user-defined filters appliedsignal := prediction > 0 and filter_all ? direction.long : prediction < 0 and filter_all ? direction.short : nz(signal[1])// Bar-Count Filters: Represents strict filters based on a pre-defined holding period of 4 barsvar int barsHeld = 0barsHeld := ta.change(signal) ? 0 : barsHeld + 1isHeldFourBars = barsHeld == 4isHeldLessThanFourBars = 0 < barsHeld and barsHeld < 4// Fractal Filters: Derived from relative appearances of signals in a given time series fractal/segment with a default length of 4 barsisDifferentSignalType = ta.change(signal)isEarlySignalFlip = ta.change(signal) and (ta.change(signal[1]) or ta.change(signal[2]) or ta.change(signal[3]))isBuySignal = signal == direction.long and isEmaUptrend and isSmaUptrendisSellSignal = signal == direction.short and isEmaDowntrend and isSmaDowntrendisLastSignalBuy = signal[4] == direction.long and isEmaUptrend[4] and isSmaUptrend[4]isLastSignalSell = signal[4] == direction.short and isEmaDowntrend[4] and isSmaDowntrend[4]isNewBuySignal = isBuySignal and isDifferentSignalTypeisNewSellSignal = isSellSignal and isDifferentSignalType// Kernel Regression Filters: Filters based on Nadaraya-Watson Kernel Regression using the Rational Quadratic Kernel// For more information on this technique refer to my other open source indicator located here:// https://www.tradingview.com/script/AWNvbPRM-Nadaraya-Watson-Rational-Quadratic-Kernel-Non-Repainting/c_green = color.new(#009988, 20)c_red = color.new(#CC3311, 20)transparent = color.new(#000000, 100)yhat1 = kernels.rationalQuadratic(settings.source, h, r, x)yhat2 = kernels.gaussian(settings.source, h-lag, x)kernelEstimate = yhat1// Kernel Rates of Changebool wasBearishRate = yhat1[2] > yhat1[1]bool wasBullishRate = yhat1[2] < yhat1[1]bool isBearishRate = yhat1[1] > yhat1bool isBullishRate = yhat1[1] < yhat1isBearishChange = isBearishRate and wasBullishRateisBullishChange = isBullishRate and wasBearishRate// Kernel Crossoversbool isBullishCrossAlert = ta.crossover(yhat2, yhat1)bool isBearishCrossAlert = ta.crossunder(yhat2, yhat1)bool isBullishSmooth = yhat2 >= yhat1bool isBearishSmooth = yhat2 <= yhat1// Kernel Colorscolor colorByCross = isBullishSmooth ? c_green : c_redcolor colorByRate = isBullishRate ? c_green : c_redcolor plotColor = showKernelEstimate ? (useKernelSmoothing ? colorByCross : colorByRate) : transparentplot(kernelEstimate, color=plotColor, linewidth=2, title="Kernel Regression Estimate")// Alert Variablesbool alertBullish = useKernelSmoothing ? isBullishCrossAlert : isBullishChangebool alertBearish = useKernelSmoothing ? isBearishCrossAlert : isBearishChange// Bullish and Bearish Filters based on KernelisBullish = useKernelFilter ? (useKernelSmoothing ? isBullishSmooth : isBullishRate) : trueisBearish = useKernelFilter ? (useKernelSmoothing ? isBearishSmooth : isBearishRate) : true// ===========================// ==== Entries and Exits ====// ===========================// Entry Conditions: Booleans for ML Model Position EntriesstartLongTrade = isNewBuySignal and isBullish and isEmaUptrend and isSmaUptrendstartShortTrade = isNewSellSignal and isBearish and isEmaDowntrend and isSmaDowntrend// Dynamic Exit Conditions: Booleans for ML Model Position Exits based on Fractal Filters and Kernel Regression FilterslastSignalWasBullish = ta.barssince(startLongTrade) < ta.barssince(startShortTrade)lastSignalWasBearish = ta.barssince(startShortTrade) < ta.barssince(startLongTrade)barsSinceRedEntry = ta.barssince(startShortTrade)barsSinceRedExit = ta.barssince(alertBullish)barsSinceGreenEntry = ta.barssince(startLongTrade)barsSinceGreenExit = ta.barssince(alertBearish)isValidShortExit = barsSinceRedExit > barsSinceRedEntryisValidLongExit = barsSinceGreenExit > barsSinceGreenEntryendLongTradeDynamic = (isBearishChange and isValidLongExit[1])endShortTradeDynamic = (isBullishChange and isValidShortExit[1])// Fixed Exit Conditions: Booleans for ML Model Position Exits based on a Bar-Count FiltersendLongTradeStrict = ((isHeldFourBars and isLastSignalBuy) or (isHeldLessThanFourBars and isNewSellSignal and isLastSignalBuy)) and startLongTrade[4]endShortTradeStrict = ((isHeldFourBars and isLastSignalSell) or (isHeldLessThanFourBars and isNewBuySignal and isLastSignalSell)) and startShortTrade[4]isDynamicExitValid = not useEmaFilter and not useSmaFilter and not useKernelSmoothingendLongTrade = settings.useDynamicExits and isDynamicExitValid ? endLongTradeDynamic : endLongTradeStrictendShortTrade = settings.useDynamicExits and isDynamicExitValid ? endShortTradeDynamic : endShortTradeStrict// =========================// ==== Plotting Labels ====// =========================// Note: These will not repaint once the most recent bar has fully closed. By default, signals appear over the last closed bar; to override this behavior set offset=0.plotshape(startLongTrade ? low : na, 'Buy', shape.labelup, location.belowbar, color=ml.color_green(prediction), size=size.small, offset=0)plotshape(startShortTrade ? high : na, 'Sell', shape.labeldown, location.abovebar, ml.color_red(-prediction), size=size.small, offset=0)plotshape(endLongTrade and settings.showExits ? high : na, 'StopBuy', shape.xcross, location.absolute, color=#3AFF17, size=size.tiny, offset=0)plotshape(endShortTrade and settings.showExits ? low : na, 'StopSell', shape.xcross, location.absolute, color=#FD1707, size=size.tiny, offset=0)// ================// ==== Alerts ====// ================// Separate Alerts for Entries and Exitsalertcondition(startLongTrade, title='Open Long ▲', message='LDC Open Long ▲ | {{ticker}}@{{close}} | ({{interval}})')alertcondition(endLongTrade, title='Close Long ▲', message='LDC Close Long ▲ | {{ticker}}@{{close}} | ({{interval}})')alertcondition(startShortTrade, title='Open Short ▼', message='LDC Open Short | {{ticker}}@{{close}} | ({{interval}})')alertcondition(endShortTrade, title='Close Short ▼', message='LDC Close Short ▼ | {{ticker}}@{{close}} | ({{interval}})')// Combined Alerts for Entries and Exitsalertcondition(startShortTrade or startLongTrade, title='Open Position ▲▼', message='LDC Open Position ▲▼ | {{ticker}}@{{close}} | ({{interval}})')alertcondition(endShortTrade or endLongTrade, title='Close Position ▲▼', message='LDC Close Position ▲▼ | {{ticker}}@[{{close}}] | ({{interval}})')// Kernel Estimate Alertsalertcondition(condition=alertBullish, title='Kernel Bullish Color Change', message='LDC Kernel Bullish ▲ | {{ticker}}@{{close}} | ({{interval}})')alertcondition(condition=alertBearish, title='Kernel Bearish Color Change', message='LDC Kernel Bearish ▼ | {{ticker}}@{{close}} | ({{interval}})')// =========================// ==== Display Signals ====// =========================atrSpaced = useAtrOffset ? ta.atr(1) : nacompressionFactor = settings.neighborsCount / settings.colorCompressionc_pred = prediction > 0 ? color.from_gradient(prediction, 0, compressionFactor, #787b86, #009988) : prediction <= 0 ? color.from_gradient(prediction, -compressionFactor, 0, #CC3311, #787b86) : nac_label = showBarPredictions ? c_pred : nac_bars = showBarColors ? color.new(c_pred, 50) : nax_val = bar_indexy_val = useAtrOffset ? prediction > 0 ? high + atrSpaced: low - atrSpaced : prediction > 0 ? high + hl2*barPredictionsOffset/20 : low - hl2*barPredictionsOffset/30label.new(x_val, y_val, str.tostring(prediction), xloc.bar_index, yloc.price, color.new(color.white, 100), label.style_label_up, c_label, size.normal, text.align_left)barcolor(showBarColors ? color.new(c_pred, 50) : na)// =====================// ==== Backtesting ====// =====================// The following can be used to stream signals to a backtest adapterbackTestStream = switchstartLongTrade => 1endLongTrade => 2startShortTrade => -1endShortTrade => -2plot(backTestStream, "Backtest Stream", display=display.none)// The following can be used to display real-time trade stats. This can be a useful mechanism for obtaining real-time feedback during Feature Engineering. This does NOT replace the need to properly backtest.// Note: In this context, a "Stop-Loss" is defined instances where the ML Signal prematurely flips directions before an exit signal can be generated.[totalWins, totalLosses, totalEarlySignalFlips, totalTrades, tradeStatsHeader, winLossRatio, winRate] = ml.backtest(high, low, open, startLongTrade, endLongTrade, startShortTrade, endShortTrade, isEarlySignalFlip, maxBarsBackIndex, bar_index, settings.source, useWorstCase)init_table() =>c_transparent = color.new(color.black, 100)table.new(position.top_right, columns=2, rows=7, frame_color=color.new(color.black, 100), frame_width=1, border_width=1, border_color=c_transparent)update_table(tbl, tradeStatsHeader, totalTrades, totalWins, totalLosses, winLossRatio, winRate, stopLosses) =>c_transparent = color.new(color.black, 100)table.cell(tbl, 0, 0, tradeStatsHeader, text_halign=text.align_center, text_color=color.gray, text_size=size.normal)table.cell(tbl, 0, 1, 'Winrate', text_halign=text.align_center, bgcolor=c_transparent, text_color=color.gray, text_size=size.normal)table.cell(tbl, 1, 1, str.tostring(totalWins / totalTrades, '#.#%'), text_halign=text.align_center, bgcolor=c_transparent, text_color=color.gray, text_size=size.normal)table.cell(tbl, 0, 2, 'Trades', text_halign=text.align_center, bgcolor=c_transparent, text_color=color.gray, text_size=size.normal)table.cell(tbl, 1, 2, str.tostring(totalTrades, '#') + ' (' + str.tostring(totalWins, '#') + '|' + str.tostring(totalLosses, '#') + ')', text_halign=text.align_center, bgcolor=c_transparent, text_color=color.gray, text_size=size.normal)table.cell(tbl, 0, 5, 'WL Ratio', text_halign=text.align_center, bgcolor=c_transparent, text_color=color.gray, text_size=size.normal)table.cell(tbl, 1, 5, str.tostring(totalWins / totalLosses, '0.00'), text_halign=text.align_center, bgcolor=c_transparent, text_color=color.gray, text_size=size.normal)table.cell(tbl, 0, 6, 'Early Signal Flips', text_halign=text.align_center, bgcolor=c_transparent, text_color=color.gray, text_size=size.normal)table.cell(tbl, 1, 6, str.tostring(totalEarlySignalFlips, '#'), text_halign=text.align_center, bgcolor=c_transparent, text_color=color.gray, text_size=size.normal)if showTradeStatsvar tbl = ml.init_table()if barstate.islastupdate_table(tbl, tradeStatsHeader, totalTrades, totalWins, totalLosses, winLossRatio, winRate, totalEarlySignalFlips)05/15/2023 at 12:21 PM #214729Bien, ese es otro promedio móvil (un promedio móvil de mínimos cuadrados pero no con una distancia euclidiana, sino con una distancia lorentziana). Por qué no, pero le aseguro que no obtendrá algo más significativo al final que un MA de regresión lineal. Una parte del código está ofuscado porque está ubicado en funciones externas (el código fuente no está completo):
importar jdehorty / MLExtensions / 2 como mlimportar jdehorty / KernelFunctions / 2 como núcleos1 user thanked author for this post.
05/15/2023 at 12:27 PM #21473105/15/2023 at 2:34 PM #214742Hola Neo
Algo que funciona de maravilla iniciado por Nicolas
// LINEAR REGRESSION CHANNEL V2 by Nicolas
// echelle lineaire//PRC_Std and Ste LinRegChannel indicator //Standard Deviation and Standard Error
//Linear Regression Channel //12.03.2019
//Nicolas @ http://www.prorealcode.com //Sharing ProRealTime knowledgedefparam drawonlastbaronly= true
if islastbarupdate then
// — settings
lookback= max(1,barindex) //channel period
ChannelType = 1 //1= Standard Deviation ; 2= Standard Erro
NbDeviation = 1 //Deviation multiplier
NbDeviation2 = 0.5 //Deviation multiplier
colorRed = 255
colorGreen = 255
colorBlue = 0
// — end of settingssumx = 0
sumy = 0
sumxy = 0
sumx2 = 0for cmpt = lookback downto 0 do
tmpx = cmpt
tmpy = close[cmpt]
sumy = sumy+tmpy
sumx = sumx+tmpx
sumx2 = sumx2 + (tmpx*tmpx)
sumxy = sumxy + (tmpy*tmpx)
nextn = lookback+1
if (sumx2 = sumx * sumx) then // protection to avoid infinite values
b = sumxy – sumx * sumy
else
b = (n * sumxy – sumx * sumy) / (n * sumx2 – sumx * sumx)
endif
a = (sumy – b * sumx) / ndrawsegment(barindex[lookback],a+b*lookback,barindex,a+b*0) coloured(“white”)style(line,5)
//channel
if ChannelType = 1 then //Standard Deviation
dat = std[lookback]*NbDeviation
dat2 = std[lookback]*NbDeviation2
dat3 = std[lookback]*NbDeviation3
else
dat = ste[lookback]*NbDeviation
dat2 = ste[lookback]*NbDeviation2
dat3 = ste[lookback]*NbDeviation3
endif
drawsegment(barindex[lookback],(a+b*lookback)+dat,barindex,a+b*0+dat) coloured(“red”) style(line,2)
drawsegment(barindex[lookback],(a+b*lookback)-dat,barindex,a+b*0-dat) coloured(“green”) style(line,2)
drawsegment(barindex[lookback],(a+b*lookback)+dat2,barindex,a+b*0+dat2) coloured(“yellow”) style(line,3)
drawsegment(barindex[lookback],(a+b*lookback)-dat2,barindex,a+b*0-dat2) coloured(“yellow”) style(line,3)
endif
/////////////////////////////////////////////////////////////////////////////
NbDeviation3 = 1.5 //Deviation multiplier
drawsegment(barindex[lookback],(a+b*lookback)+dat3,barindex,a+b*0+dat3) coloured(“red”) style(dottedline,3)
drawsegment(barindex[lookback],(a+b*lookback)-dat3,barindex,a+b*0-dat3) coloured(“green”) style(dottedline,3)
////////////////////////////////////////////////////////////////////////////////
return customclose as ” LINEAR REGRESSION CHANNEL V2 “1 user thanked author for this post.
05/15/2023 at 2:37 PM #21474305/15/2023 at 2:38 PM #21474506/14/2023 at 7:58 AM #21619408/29/2023 at 5:52 PM #220025Hola Mitchy14
Estoy intentando crear alertas con esa estrategia para que cuando me pinte los indicadores flecha verde arriba y flecha roja abajo me lance las alertas correspondientes pero TradingView no dispara las alertas
Me pueden ayudar a configurar trandigView ?
Gracias
08/30/2023 at 10:30 AM #220049Lo sentimos, pero este foro está dedicado únicamente a la plataforma ProRealTime.
Encontrará otros foros en Internet sobre diferentes plataformas comerciales.
-
AuthorPosts