尝试将复杂的策略转换为警报

时间:2019-09-27 16:15:05

标签: pine-script

我正在尝试将此策略转换为警报,以便通过警报以3逗号自动执行该策略。

我已尝试将策略转换为研究,但由于条件中包含不允许在研究中使用的strategy.closetrades,因此出现了错误。我不确定如何使用其他方法。

$data = [ ['string' => 'Something xxx something', 'value' => 10],
    ['string' => 'Something yyy something', 'value'  => 20],
    ['string' => 'Something xxx something', 'value'  => 30],
    ['string' => 'Something zzz something', 'value'  => 40]];

$total = 0;
foreach ( $data as $row )   {
    if (strpos($row['string'], 'xxx') !== false){
        $total += $row['value'];
    }
}
echo $total;

1 个答案:

答案 0 :(得分:0)

您可以使用两个变量,一个作为买入信号,一个作为卖出信号。

这是Tradingview上的默认策略示例:

//@version=4
strategy("My Strategy", overlay=true)

longCondition = crossover(sma(close, 14), sma(close, 28))
if (longCondition)
    strategy.entry("My Long Entry Id", strategy.long)

shortCondition = crossunder(sma(close, 14), sma(close, 28))
if (shortCondition)
    strategy.entry("My Short Entry Id", strategy.short)

您可以将其转换为指标,如下所示:

//@version=4
study("My Script", overlay=true)

var isLong = false
var isShort = false

buySignal = not isLong and crossover(sma(close, 14), sma(close, 28))        // only buy if we are not already long
sellSignal = not isShort and crossunder(sma(close, 14), sma(close, 28))     // only sell if we are not already short

if (buySignal)
    isLong := true
    isShort := false

if (sellSignal)
    isLong := false
    isShort := true

alertcondition(condition=buySignal, title="BUY", message="BUY")
alertcondition(condition=sellSignal, title="SELL", message="SELL")

plotshape(series=buySignal, text="BUY", style=shape.triangleup, color=color.green, location=location.belowbar, size=size.small)
plotshape(series=sellSignal, text="SELL", style=shape.triangledown, color=color.red, location=location.abovebar, size=size.small)

注意:您需要按照here所述手动设置警报。

enter image description here