I developed a condition when using the "Relative Strength Index" indicator. I can't figure out how to move from the indicator to the strategy and close the position at 350, 13, 349, etc. (please see photo) candles, but the problem is that the base value, I have it is p4, it is not fixed and constantly changes.
//@version=5
indicator(title="Relative Strength Index", shorttitle="RSI", max_bars_back = 2000)
ma(source, length, type) =>
switch type
"SMA" => ta.sma(source, length)
"Bollinger Bands" => ta.sma(source, length)
"EMA" => ta.ema(source, length)
"SMMA (RMA)" => ta.rma(source, length)
"WMA" => ta.wma(source, length)
"VWMA" => ta.vwma(source, length)
rsiLengthInput = input.int(14, minval=1, title="RSI Length", group="RSI Settings")
rsiSourceInput = input.source(close, "Source", group="RSI Settings")
maTypeInput = input.string("SMA", title="MA Type", options=["SMA", "Bollinger Bands", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group="MA Settings")
maLengthInput = input.int(14, title="MA Length", group="MA Settings")
bbMultInput = input.float(2.0, minval=0.001, maxval=50, title="BB StdDev", group="MA Settings")
up = ta.rma(math.max(ta.change(rsiSourceInput), 0), rsiLengthInput)
down = ta.rma(-math.min(ta.change(rsiSourceInput), 0), rsiLengthInput)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
rsiMA = ma(rsi, maLengthInput, maTypeInput)
isBB = maTypeInput == "Bollinger Bands"
plot(rsiMA, "RSI-based MA", color=color.yellow)
rsiUpperBand = hline(70, "RSI Upper Band", color=#787B86)
midline = hline(50, "RSI Middle Band", color=color.new(#787B86, 50))
rsiLowerBand = hline(30, "RSI Lower Band", color=#787B86)
fill(rsiUpperBand, rsiLowerBand, color=color.rgb(126, 87, 194, 90), title="RSI Background Fill")
bbUpperBand = plot(isBB ? rsiMA + ta.stdev(rsi, maLengthInput) * bbMultInput : na, title = "Upper Bollinger Band", color=color.green)
bbLowerBand = plot(isBB ? rsiMA - ta.stdev(rsi, maLengthInput) * bbMultInput : na, title = "Lower Bollinger Band", color=color.green)
fill(bbUpperBand, bbLowerBand, color= isBB ? color.new(color.green, 90) : na, title="Bollinger Bands Background Fill")
midLinePlot = plot(50, color = na, editable = false, display = display.none)
// Две вершины и дно между ними
a = input.int(70, 'Линия')
var int bars = 0
var int p1 = 0
var int p2 = 0
var int p = 0
var l1 = 0
var l = 0
isCond21 = ta.crossunder(rsiMA, a)
isCond11 = ta.crossover(rsiMA, a)
isCond1 = ta.cross(a, rsiMA)
l1 := nz(ta.barssince(isCond21) + 1)
l := nz(ta.barssince(isCond11) + 1)
bars := ta.valuewhen(isCond11, bar_index, 0) - ta.valuewhen(isCond11, bar_index, 1)
p := int(math.max(1, l))
p1 := int(math.max(1, l1))
p2 := l1[1] - l[1]
p3 = bars + l[1]
p4 = int((p1[1] - p)/2)
p5 = p < 20 ? 20 : p
p6 = p3 - p5
//Цена // Price
highpclose2 = int(ta.lowest(close, int(math.max(1, nz(p1[1])))))
highpclose3 = int(ta.highest(close, int(math.max(1, nz(p3)))))
highpclose1 = int(ta.highest(close, int(math.max(1, nz(p)))))
highpclose4 = int(ta.highest(close, int(math.max(1, nz(p1[1])))))
//Индикатор //indicator
highpind2 = int(ta.lowest(rsiMA, int(math.max(1, nz(p1[1])))))
highpind3 = int(ta.highest(rsiMA, int(math.max(1, nz(p3)))))
highpind1 = int(ta.highest(rsiMA, int(math.max(1, nz(p)))))
highpind4 = int(ta.highest(rsiMA, int(math.max(1, nz(p1[1])))))
fc = highpclose3[p5] > highpclose1 and highpclose1 > highpclose2 ? 1 : na
fi = highpind3[p5] > highpind1 and highpind1 > highpind2 or highpind1 > highpind3[p5] and highpind3[p5] > highpind2 ? 1 : na
ft = (highpclose2 - (highpclose3 - highpclose1)) > 0 ? highpclose2 - (highpclose3 - highpclose1) : (highpclose2 - (highpclose3 - highpclose1)) * -1
if isCond21 and fc == 1 and fi == 1
// strategy.entry('1', strategy.short)
label.new(bar_index, rsiMA,'Open', color = color.aqua)
label.new(bar_index+p4, 0, 'Close', color = color.red)
line.new(x1 = bar_index, y1 = rsiMA, x2 = bar_index+p4, y2 = 0, color = color.red)
label.new(bar_index+p4, 20, str.tostring(p4), color = color.yellow)
So, you want to clsoe your position conditionally. In that case, you should use the strategy.close()
function.
Something like below:
close_condition = (p4 == 350) or (p4 == 13)
if (close_condition)
strategy.close("Long")