PHP Code:
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
//@version=5
indicator(title="My Indicator", overlay=true)
// draws high label, requires index and the value that needs to be plotted
drawHighLabel(index, value) =>
label.new(index, na, str.tostring(value), yloc = yloc.abovebar, color=color.green, textcolor=color.white, style=label.style_label_down, yloc=yloc.abovebar)
// draws low label, requires index and the value that needs to be plotted
drawLowLabel(index, value) =>
label.new(index, na, str.tostring(value), yloc= yloc.belowbar, color=color.red, textcolor=color.white, style=label.style_label_up, yloc=yloc.belowbar)
// getting the close of a bar, requires index
getClose(barIndex) => close[bar_index - barIndex]
// getting the close of a bar, requires index
getOpen(barIndex) => open[bar_index - barIndex]
// getting the high of a bar, requires index
getHigh(barIndex) => high[bar_index - barIndex]
// getting the low of a bar, requires index
getLow(barIndex) => low[bar_index - barIndex]
// high swing, which is the previous two candle and the next two candle has to be lower than the indexed candle, requires index of the high candle.
highSwing(index) =>
getHigh(index) > getHigh(index+1) and getHigh(index) > getHigh(index+2) and getHigh(index) > getHigh(index-1) and getHigh(index) > getHigh(index-2)
// low swing, which is the previous two candle and the next two candle has to be higher than the indexed candle, requires index of the low candle.
lowSwing(index) =>
getLow(index) < getLow(index+1) and getLow(index) < getLow(index+2) and getLow(index) < getLow(index-1) and getLow(index) < getLow(index-2)
// Define a function to find the lowest low between two indices
findLowestLow(previousIndex, currentIndex) =>
// Initialize the lowest low and its index
float lowestLow = 10e10 // Initialize lowestLow to a very large value
int lowestLowIndex = na // Initialize lowestLowIndex to na
// Loop through the indices from previousIndex to currentIndex
for i = previousIndex to currentIndex
// Get the low for the current index
lowValue = low[bar_index - i] // Use low[i] instead of getLow(i)
// Update the lowest low if the current low is lower than the previous lowest low
if lowValue < lowestLow
lowestLow := lowValue
lowestLowIndex := i
// Return the lowest low index found between previousIndex and currentIndex
drawLowLabel(lowestLowIndex, "")
lowestLow
// Define a function to find the highest high between two indices
findHighestHigh(previousIndex, currentIndex) =>
// Initialize the highest high and its index
float highestHigh = -10e10
int highestHighIndex = 0
// Loop through the indices from previousIndex to currentIndex
for i = previousIndex to currentIndex
// Get the high for the current index
highValue = high[bar_index - i]
// Update the highest high if the current high is higher than the previous highest high
if highValue > highestHigh
highestHigh := highValue
highestHighIndex := i
// Return the highest high and its index found between previousIndex and currentIndex
drawHighLabel(highestHighIndex, "")
highestHigh
// Function to check if an array includes an index
arrayIncludesIndex(arr, index) =>
bool found = false
for i = 0 to array.size(arr) - 1
if array.get(arr, i) == index
found := true
found
if barstate.islast
// Define variables: initial_candle, final_candle where initial_candle is 108th candle and final_candle is 1st candle
var initial_candle = bar_index - 108
// Define variables: current_high, current_low which is defined on initial_candle
float current_high = getHigh(initial_candle)
float current_low = getLow(initial_candle)
int currentIndex = initial_candle
int previousIndex = initial_candle
int currentHighIndex = na
int currentLowIndex = na
int trend = 0
var int[] AllHighsIndex = array.new_int(0)
var int[] AllLowsIndex = array.new_int(0)
array.push(AllHighsIndex, initial_candle)
array.push(AllLowsIndex, initial_candle)
// Loop through all the candles from initial_candle to final_candle
for i = initial_candle to bar_index
if(i <= bar_index - 2)
if trend == 1 or getClose(i) > current_high
trend := 1
if getClose(i) < current_low
trend := -1
else if highSwing(i)
current_high := getHigh(i)
drawHighLabel(i, current_high)
previousIndex := currentIndex
currentIndex := i
if array.size(AllHighsIndex) >= 2
array.shift(AllHighsIndex)
array.push(AllHighsIndex, i)
else
array.push(AllHighsIndex, i)
// if a green candle makes a new high, the low of that candle should not be considered to draw the lowest low,except if it is the initial candle
containsHighIndex = arrayIncludesIndex(AllHighsIndex, previousIndex)
isGreenCandle = getOpen(previousIndex) < getClose(previousIndex)
if containsHighIndex and isGreenCandle and previousIndex != initial_candle
current_low := findLowestLow(previousIndex + 1, currentIndex)
else
current_low := findLowestLow(previousIndex, currentIndex)
trend:= 0
if trend == -1 or getClose(i) < current_low
trend := -1
if getClose(i) > current_high
trend := 1
else if lowSwing(i)
current_low := getLow(i)
drawLowLabel(i, current_low)
previousIndex := currentIndex
currentIndex := i
if array.size(AllLowsIndex) >= 2
array.shift(AllLowsIndex)
array.push(AllLowsIndex, i)
else
array.push(AllLowsIndex, i)
// if a red candle makes a new low, the high of that candle should not be considered to draw the highest high, except if it is the initial candle
containsLowIndex = arrayIncludesIndex(AllLowsIndex, previousIndex)
isRedCandle = getOpen(previousIndex) > getClose(previousIndex)
if containsLowIndex and isRedCandle and previousIndex != initial_candle
current_high := findHighestHigh(previousIndex + 1, currentIndex)
else
current_high := findHighestHigh(previousIndex, currentIndex)
trend := 0
////////
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © AllanDecker
//@version=5
// ATR period
atr_period = input.int(14, title="ATR Period")
// ATR multiplier
atr_multiplier = input.float(1.0, title="ATR Multiplier")
// Calculate ATR
atr_value = ta.atr(atr_period)
// Calculate upper and lower bands for ATR Bands
upper_band = ta.sma(close, 20) + (atr_multiplier * atr_value)
lower_band = ta.sma(close, 20) - (atr_multiplier * atr_value)
// Calculate midline at Fibonacci retracement level of 0.75
midline_75 = upper_band - (upper_band - lower_band) * 0.75
// Calculate midline at Fibonacci retracement level of 0.25
midline_25 = upper_band - (upper_band - lower_band) * 0.25
// Plot bands and midlines for ATR Bands
//plot(upper_band, color=color.red, title="Upper Band", style=plot.style_cross)
//plot(lower_band, color=color.red, title="Lower Band", style=plot.style_cross)
//plot(midline_75, color=color.purple, title="Midline 0.75")
//plot(midline_25, color=color.orange, title="Midline 0.25")
// Define buy and sell conditions
buy_condition = close > midline_25
sell_condition = close < midline_75
// Set candle colors
candle_color = buy_condition ? color.blue : sell_condition ? color.red : na
//barcolor(candle_color)
// Check for buy signal (price breaking above midline 0.25 of ATR Bands and CCI line color1 is blue)
buy_signal = ta.crossover(close, midline_25)
// Check for sell signal (price breaking below midline 0.75 of ATR Bands and CCI line color1 is red)
sell_signal = ta.crossunder(close, midline_75)
// Plot buy and sell signals for ATR Bands
plotshape(series=buy_signal, title="Buy Signal ATR", location=location.belowbar, color=color.rgb(41,98,255), style=shape.triangleup, size = size.tiny)
plotshape(series=sell_signal, title="Sell Signal ATR", location=location.abovebar, color=color.rgb(236,64,122), style=shape.triangledown, size = size.tiny)
// Alert conditions for ATR Bands
alertcondition(buy_signal, title="Buy", message="BUY")
alertcondition(sell_signal, title="Sell", message="SELL")
/////
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © BackQuant
//@version=5
// Kalman Filter parameters
series float pricesource = input.source(close, "Kalman Price Source", group = "Calculation")
simple float processNoise = input.float(0.01, title="Process Noise", step = 0.01, group = "Calculation")
simple float measurementNoise = input.float(3.0, title="Measurement Noise", group = "Calculation")
simple int N = input.int(5, title="Filter Order", minval=1, group = "Calculation")
simple bool showkalman = input.bool(true, "Show Filtered Price on chart?", group = "UI Settings")
simple bool paintCandles = input.bool(true, "Paint candles according to Trend?", group = "UI Settings")
var float[] stateEstimate = array.new_float(N, na)
var float[] errorCovariance = array.new_float(N, 100.0)
f_init(series float pricesource) =>
if na(array.get(stateEstimate, 0))
for i = 0 to N-1
array.set(stateEstimate, i, pricesource)
array.set(errorCovariance, i, 1.0)
f_kalman(series float pricesource) =>
// Prediction Step
predictedStateEstimate = array.new_float(N)
predictedErrorCovariance = array.new_float(N)
for i = 0 to N-1
array.set(predictedStateEstimate, i, array.get(stateEstimate, i)) // Simplified prediction
array.set(predictedErrorCovariance, i, array.get(errorCovariance, i) + processNoise)
kalmanGain = array.new_float(N)
for i = 0 to N-1
kg = array.get(predictedErrorCovariance, i) / (array.get(predictedErrorCovariance, i) + measurementNoise)
array.set(kalmanGain, i, kg)
array.set(stateEstimate, i, array.get(predictedStateEstimate, i) + kg * (pricesource - array.get(predictedStateEstimate, i)))
array.set(errorCovariance, i, (1 - kg) * array.get(predictedErrorCovariance, i))
array.get(stateEstimate, 0)
f_init(pricesource)
kalmanFilteredPrice = f_kalman(pricesource)
// Conditional Trend
var Trend = 0
if kalmanFilteredPrice>kalmanFilteredPrice[1]
Trend := 1
if kalmanFilteredPrice<kalmanFilteredPrice[1]
Trend := -1
// Colouring
var barColour = #ffffff
if Trend == 1
barColour := #33ff00
if Trend == -1
barColour := #ff0000
// Plotting
barcolor(paintCandles ? barColour : na)
plot(
showkalman ? kalmanFilteredPrice : na,
"Kalman",
color = color.new(barColour, 40),
linewidth = 4
)
/////////
Yer İmleri