TradingView - несколько тейк-профитов для одного ордера в Pine Script - PullRequest
2 голосов
/ 28 апреля 2020

Я пытаюсь реализовать простую стратегию, в которой я открываю длинную позицию, когда получаю сигнал на покупку, затем хочу взять несколько прибылей и установить стоп-лосс:

  • Продать количество 25% с прибылью 1%
  • Продать количество 25% с прибылью 2%
  • Продать количество 25% с прибылью 3%
  • Продать количество 25% с прибылью 4%
  • Стоп-лосс на 2%

Я пробовал много вещей на основе strategy.close, strategy.exit, strategy.entry, но не нашел ничего работающего. У кого-нибудь есть опыт работы с такой стратегией?

Спасибо

1 Ответ

1 голос
/ 29 апреля 2020

Пример такой стратегии:

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © adolgov

//@version=4
strategy("Multiple %% profit exits example", overlay=false, default_qty_value = 100)

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)

percentAsPoints(pcnt) =>
    strategy.position_size != 0 ? round(pcnt / 100 * strategy.position_avg_price / syminfo.mintick) : float(na)

lossPnt = percentAsPoints(2)

strategy.exit("x1", qty_percent = 25, profit = percentAsPoints(1), loss = lossPnt)
strategy.exit("x2", qty_percent = 25, profit = percentAsPoints(2), loss = lossPnt)
strategy.exit("x3", qty_percent = 25, profit = percentAsPoints(3), loss = lossPnt)
strategy.exit("x4", profit = percentAsPoints(4), loss = lossPnt)

profitPercent(price) =>
    posSign = strategy.position_size > 0 ? 1 : strategy.position_size < 0 ? -1 : 0
    (price - strategy.position_avg_price) / strategy.position_avg_price * posSign * 100

p1 = plot(profitPercent(high), style=plot.style_linebr, title = "open profit % upper bound")
p2 = plot(profitPercent(low), style=plot.style_linebr, title = "open profit % lower bound")
fill(p1, p2, color = color.red)

Как это работает, вы можете увидеть на https://www.tradingview.com/script/kHhCik9f-Multiple-profit-exits-example/

...