英文:
Pine Script: Moving from indicator to strategy using dynamic base value and closing position at specified values
问题
//@version=5
indicator(title="相对强度指数", shorttitle="RSI", max_bars_back = 2000)
ma(source, length, type) =>
switch type
"SMA" => ta.sma(source, length)
"布林带" => 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 长度", group="RSI 设置")
rsiSourceInput = input.source(close, "数据源", group="RSI 设置")
maTypeInput = input.string("SMA", title="MA 类型", options=["SMA", "布林带", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group="MA 设置")
maLengthInput = input.int(14, title="MA 长度", group="MA 设置")
bbMultInput = input.float(2.0, minval=0.001, maxval=50, title="BB StdDev", group="MA 设置")
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 == "布林带"
plot(rsiMA, "RSI 基于MA", color=color.yellow)
rsiUpperBand = hline(70, "RSI 上限", color=#787B86)
midline = hline(50, "RSI 中轴", color=color.new(#787B86, 50))
rsiLowerBand = hline(30, "RSI 下限", color=#787B86)
fill(rsiUpperBand, rsiLowerBand, color=color.rgb(126, 87, 194, 90), title="RSI 背景填充")
bbUpperBand = plot(isBB ? rsiMA + ta.stdev(rsi, maLengthInput) * bbMultInput : na, title = "布林带上限", color=color.green)
bbLowerBand = plot(isBB ? rsiMA - ta.stdev(rsi, maLengthInput) * bbMultInput : na, title = "布林带下限", color=color.green)
fill(bbUpperBand, bbLowerBand, color= isBB ? color.new(color.green, 90) : na, title="布林带背景填充")
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
// 价格
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])))))
// 指标
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
label.new(bar_index, rsiMA,'开仓', color = color.aqua)
label.new(bar_index+p4, 0, '平仓', 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)
英文:
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)
答案1
得分: 0
你想要有条件地关闭你的仓位。在这种情况下,你应该使用 strategy.close()
函数。
类似以下方式:
close_condition = (p4 == 350) or (p4 == 13)
if close_condition:
strategy.close("Long")
英文:
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")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论