Pine Script:从指标转换为策略,使用动态基准值并在指定值处平仓。

huangapple go评论52阅读模式
英文:

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)   
英文:

Pine Script:从指标转换为策略,使用动态基准值并在指定值处平仓。

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=&quot;Relative Strength Index&quot;, shorttitle=&quot;RSI&quot;, max_bars_back = 2000)

ma(source, length, type) =&gt;
    switch type
        &quot;SMA&quot; =&gt; ta.sma(source, length)
        &quot;Bollinger Bands&quot; =&gt; ta.sma(source, length)
        &quot;EMA&quot; =&gt; ta.ema(source, length)
        &quot;SMMA (RMA)&quot; =&gt; ta.rma(source, length)
        &quot;WMA&quot; =&gt; ta.wma(source, length)
        &quot;VWMA&quot; =&gt; ta.vwma(source, length)

rsiLengthInput = input.int(14, minval=1, title=&quot;RSI Length&quot;, group=&quot;RSI Settings&quot;)
rsiSourceInput = input.source(close, &quot;Source&quot;, group=&quot;RSI Settings&quot;)
maTypeInput = input.string(&quot;SMA&quot;, title=&quot;MA Type&quot;, options=[&quot;SMA&quot;, &quot;Bollinger Bands&quot;, &quot;EMA&quot;, &quot;SMMA (RMA)&quot;, &quot;WMA&quot;, &quot;VWMA&quot;], group=&quot;MA Settings&quot;)
maLengthInput = input.int(14, title=&quot;MA Length&quot;, group=&quot;MA Settings&quot;)
bbMultInput = input.float(2.0, minval=0.001, maxval=50, title=&quot;BB StdDev&quot;, group=&quot;MA Settings&quot;)

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 == &quot;Bollinger Bands&quot;

plot(rsiMA, &quot;RSI-based MA&quot;, color=color.yellow)
rsiUpperBand = hline(70, &quot;RSI Upper Band&quot;, color=#787B86)
midline = hline(50, &quot;RSI Middle Band&quot;, color=color.new(#787B86, 50))
rsiLowerBand = hline(30, &quot;RSI Lower Band&quot;, color=#787B86)
fill(rsiUpperBand, rsiLowerBand, color=color.rgb(126, 87, 194, 90), title=&quot;RSI Background Fill&quot;)
bbUpperBand = plot(isBB ? rsiMA + ta.stdev(rsi, maLengthInput) * bbMultInput : na, title = &quot;Upper Bollinger Band&quot;, color=color.green)
bbLowerBand = plot(isBB ? rsiMA - ta.stdev(rsi, maLengthInput) * bbMultInput : na, title = &quot;Lower Bollinger Band&quot;, color=color.green)
fill(bbUpperBand, bbLowerBand, color= isBB ? color.new(color.green, 90) : na, title=&quot;Bollinger Bands Background Fill&quot;)

midLinePlot = plot(50, color = na, editable = false, display = display.none)

// Две вершины и дно между ними

a = input.int(70, &#39;Линия&#39;)
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 &lt; 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] &gt; highpclose1 and highpclose1 &gt; highpclose2  ? 1 : na
fi = highpind3[p5] &gt; highpind1 and highpind1 &gt; highpind2 or highpind1 &gt; highpind3[p5] and highpind3[p5] &gt; highpind2 ? 1 : na

ft = (highpclose2 - (highpclose3 - highpclose1)) &gt; 0 ? highpclose2 - (highpclose3 - highpclose1) : (highpclose2 - (highpclose3 - highpclose1)) * -1

if isCond21 and fc == 1 and fi == 1
//  strategy.entry(&#39;1&#39;, strategy.short)
    label.new(bar_index, rsiMA,&#39;Open&#39;,  color = color.aqua) 
    label.new(bar_index+p4, 0, &#39;Close&#39;, 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(&quot;Long&quot;)

huangapple
  • 本文由 发表于 2023年6月5日 16:12:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76404577.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定