// @ Julien_Eche

//version=5
indicator(‘Machine Learning Signal Filter’, ”, true)

//**** Display Options ****
displayMetrics = input(true, ‘Display Neural Metrics?’)
dataCurves = input(false, ‘Display Signal Curves?’)

//**** Signal Calculations ****
signalEaster = input(true, ‘Activate Signal Calculations?’)
useDataForSignal = input(true, ‘Use Data for Signal Generation?’)

//**** Trade Settings ****
lot_size = input.float(0.1, ‘Signal Lot Size’, options=[0.01, 0.1, 0.2, 0.3, 0.5, 1, 2, 3, 5, 10, 20, 30, 50, 100, 1000])
stasisPeriod = input.int(5, ‘Temporal Stasis Period (minimum 1)’, minval=1)

//**** Signal Filtration ****
filterMatrix = input.string(‘Volatility’, ‘Signal Filtration Mode’, options=[‘Volatility’, ‘Volume’, ‘Both’, ‘None’])

//**** Temporal and Data Settings ****
dataFlux = input.string(‘Close’, ‘Data Type’, options=[‘Open’, ‘High’, ‘Low’, ‘Close’, ‘HL2’, ‘OC2’, ‘OHL3’, ‘HLC3’, ‘OHLC4’])
chronoSync = input.timeframe(”, ‘Temporal Dimension’)
retroScan = input.int(5, ‘Chrono Window (minimum 2)’, minval=2)
chronoNorm = input.int(100, ‘Temporal Normalization (range: 2 to 240)’, minval=2, maxval=240)

//**** Neural Network Parameters ****
nanoPulse = input.float(0.001, ‘Neural Pulse Rate (range: 0.0001 to 0.01)’, minval=0.0001, maxval=0.01, step=0.0001)
chronoCycles = input.int(1500, ‘Cycle Iterations (range: 50 to 20000)’, minval=50)

//**** Table Position and Text Size Input ****
tablePosition = input.string(defval = “Top Right”, title = “Table Position”, options = [“Top Right”, “Bottom Right”, “Middle Right”, “Bottom Center”, “Middle Left”])
textSizeInput = input.string(“Normal”, “Text Size”, options=[“Normal”, “Large”, “Small”])

//**** Function to get the table position ****
getTablePosition(pos) =>
switch pos
“Top Right” => position.top_right
“Bottom Right” => position.bottom_right
“Middle Right” => position.middle_right
“Bottom Center” => position.bottom_center
“Middle Left” => position.middle_left

//**** Function to get text size ****
getTextSize(size) =>
switch size
“Normal” => size.normal
“Large” => size.large
“Small” => size.small

//**** Constants and Variables ****
var ACTIVATE = 1
var DEACTIVATE = -1
var STASIS = 0
var signalBeacon = STASIS
var timeTracker = 0
var float start_long_trade = 0.
var float long_trades = 0.
var float start_short_trade = 0.
var float short_trades = 0.
var int victories = 0
var int trade_counter = 0
var table metricsTable = na

//**** Helper Functions ****
cosmicBlue(qz) =>
qz > 9 ? #FF0000ff : qz > 8 ? #FF0000e5 : qz > 7 ? #FF0000cc : qz > 6 ? #FF0000b2 : qz > 5 ? #FF000099 : qz > 4 ? #FF00007f : qz > 3 ? #FF000066 : qz > 2 ? #FF00004c : qz > 1 ? #FF000033 : #FF000019

nebulaRed(qz) =>
qz > 9 ? #0000FFff : qz > 8 ? #0000FFe5 : qz > 7 ? #0000FFcc : qz > 6 ? #0000FFb2 : qz > 5 ? #0000FF99 : qz > 4 ? #0000FF7f : qz > 3 ? #0000FF66 : qz > 2 ? #0000FF4c : qz > 1 ? #0000FF33 : #0000FF19

quantumSurge(threshold) =>
rsiVolume = ta.rsi(volume, 14)
oscillator = ta.hma(rsiVolume, 10)
oscillator > threshold

fluxThreshold(minVol, maxVol) =>
ta.atr(minVol) > ta.atr(maxVol)

vectorDot(vectorA, vectorB, dimension) =>
math.sum(vectorA * vectorB, dimension)

bioCurve(zeta) =>
1.0 / (1.0 + math.exp(-zeta))

cyberneticRegression(inputX, inputY, dimension, learningRate, iterations) =>
weight = 0.0
entropy = 0.0
for cycle = 1 to iterations by 1
hypothesis = bioCurve(vectorDot(inputX, 0.0, dimension))
entropy := -1.0 / dimension * vectorDot(vectorDot(inputY, math.log(hypothesis) + 1.0 – inputY, dimension), math.log(1.0 – hypothesis), dimension)
gradient = 1.0 / dimension * vectorDot(inputX, hypothesis – inputY, dimension)
weight -= learningRate * gradient
weight

[entropy, bioCurve(vectorDot(inputX, weight, dimension))]

stellarNormalize(dataStream, dimension, minimum, maximum) =>
highestValue = ta.highest(dataStream, dimension)
lowestValue = ta.lowest(dataStream, dimension)
(maximum – minimum) * (dataStream – lowestValue) / (highestValue – lowestValue) + minimum

//**** Data Stream Selection ****
dataStream = switch dataFlux
‘Open’ => open
‘High’ => high
‘Low’ => low
‘Close’ => close
‘HL2’ => (high + low) / 2
‘OC2’ => (open + close) / 2
‘OHL3’ => (open + high + low) / 3
‘HLC3’ => (high + low + close) / 3
=> (open + high + low + close) / 4

baseStream = request.security(syminfo.tickerid, chronoSync, dataStream, lookahead=barmerge.lookahead_on)
syntheticStream = math.log(math.abs(math.pow(baseStream, 2) – 1) + .5)

baseInput = signalEaster ? time : baseStream
syntheticInput = signalEaster ? baseStream : syntheticStream

//**** Cybernetic Regression and Normalization ****
[entropy, forecast] = cyberneticRegression(baseInput, syntheticInput, retroScan, nanoPulse, chronoCycles)
normalizedEntropy = stellarNormalize(entropy, chronoNorm, ta.lowest(baseStream, chronoNorm), ta.highest(baseStream, chronoNorm))
normalizedForecast = stellarNormalize(forecast, chronoNorm, ta.lowest(baseStream, chronoNorm), ta.highest(baseStream, chronoNorm))

//**** Signal Processing ****
filter = true // Disable additional filters
signalBeacon := useDataForSignal ? baseStream < normalizedEntropy and filter ? DEACTIVATE : baseStream > normalizedEntropy and filter ? ACTIVATE : nz(signalBeacon) : ta.crossunder(normalizedEntropy, normalizedForecast) and filter ? DEACTIVATE : ta.crossover(normalizedEntropy, normalizedForecast) and filter ? ACTIVATE : nz(signalBeacon)
changed = ta.change(signalBeacon)
timeTracker := changed ? 0 : timeTracker + 1

//**** Trade Conditions ****
startLongTrade = changed and signalBeacon == ACTIVATE
startShortTrade = changed and signalBeacon == DEACTIVATE
endLongTrade = signalBeacon == ACTIVATE and timeTracker == stasisPeriod and not changed or changed and signalBeacon == DEACTIVATE
endShortTrade = signalBeacon == DEACTIVATE and timeTracker == stasisPeriod and not changed or changed and signalBeacon == ACTIVATE

//**** Plotting and Alerts ****
plot(dataCurves ? normalizedEntropy : na, ”, color.new(color.teal, 0), 3)
plot(dataCurves ? normalizedForecast : na, ”, color.new(color.orange, 0), 3)
plotshape(startLongTrade ? low : na, ‘Buy’, shape.triangleup, location.belowbar, nebulaRed(10), size=size.small)
plotshape(startShortTrade ? high : na, ‘Sell’, shape.triangledown, location.abovebar, cosmicBlue(10), size=size.small)
plot(endLongTrade ? high : na, ‘StopBuy’, nebulaRed(6), 2, plot.style_cross)
plot(endShortTrade ? low : na, ‘StopSell’, cosmicBlue(6), 2, plot.style_cross)
alertcondition(startLongTrade, ‘Buy’, ‘Go long!’)
alertcondition(startShortTrade, ‘Sell’, ‘Go short!’)
alertcondition(startLongTrade or startShortTrade, ‘Alert’, ‘Deal Time!’)

//**** Trade Calculations ****
ohl3 = (open + high + low) / 3
if startLongTrade
start_long_trade := ohl3
if endLongTrade
trade_counter := 1
ldiff = ohl3 – start_long_trade
victories := ldiff > 0 ? 1 : 0
long_trades := ldiff * lot_size
if startShortTrade
start_short_trade := ohl3
if endShortTrade
trade_counter := 1
sdiff = start_short_trade – ohl3
victories := sdiff > 0 ? 1 : 0
short_trades := sdiff * lot_size

neuralReturn = ta.cum(long_trades) + ta.cum(short_trades)
tradeGalaxy = ta.cum(trade_counter)
victoryCount = ta.cum(victories)
defeatCount = tradeGalaxy – victoryCount == 0 ? 1 : tradeGalaxy – victoryCount

//**** Display Metrics ****
timeBase = (time – time[1]) / 1000
currentTime = (timenow – time_close[1]) / 1000

if displayMetrics and barstate.islast
if na(metricsTable)
metricsTable := table.new(getTablePosition(tablePosition), 2, 6, frame_color=color.gray, frame_width=1, border_width=1, border_color=color.gray)
table.cell(metricsTable, 0, 0, “Metric”, bgcolor=color.silver, text_size=getTextSize(textSizeInput))
table.cell(metricsTable, 1, 0, “Value”, bgcolor=color.silver, text_size=getTextSize(textSizeInput))

table.cell(metricsTable, 0, 1, “Return”, text_size=getTextSize(textSizeInput), bgcolor=color.white)
table.cell(metricsTable, 1, 1, str.tostring(neuralReturn, ‘#.#’), text_size=getTextSize(textSizeInput), bgcolor=color.white)
table.cell(metricsTable, 0, 2, “Trades”, text_size=getTextSize(textSizeInput), bgcolor=color.white)
table.cell(metricsTable, 1, 2, str.tostring(tradeGalaxy, ‘#’), text_size=getTextSize(textSizeInput), bgcolor=color.white)
table.cell(metricsTable, 0, 3, “Win %”, text_size=getTextSize(textSizeInput), bgcolor=color.white)
table.cell(metricsTable, 1, 3, str.tostring(victoryCount / tradeGalaxy, ‘#.#%’), text_size=getTextSize(textSizeInput), bgcolor=color.white)
table.cell(metricsTable, 0, 4, “Win/Loss”, text_size=getTextSize(textSizeInput), bgcolor=color.white)
table.cell(metricsTable, 1, 4, str.tostring(victoryCount / defeatCount, ‘#.##’), text_size=getTextSize(textSizeInput), bgcolor=color.white)
table.cell(metricsTable, 0, 5, “Time %”, text_size=getTextSize(textSizeInput), bgcolor=color.white)
table.cell(metricsTable, 1, 5, str.tostring(currentTime / timeBase, ‘#.#%’), text_size=getTextSize(textSizeInput), bgcolor=color.white)

Shares: