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

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

      //@version=5
strategy('Renko', overlay=true, currency=currency.USD, initial_capital=500, 
process_orders_on_close=false, default_qty_type=strategy.percent_of_equity, 
default_qty_value=200, margin_long=1, margin_short=1)

entry_atr = float(0.0)  //set float
entry_price = float(0.0)  //set float
entry_atr := strategy.position_size == 0 or entry_long ? atr : entry_atr[1]
entry_price := strategy.position_size == 0 or entry_long ? close : entry_price[1]

enter code here`if entry_long
    strategy.entry(id='Long Entry', direction=strategy.long)
if entry_short
    strategy.entry(id='Short Entry', direction=strategy.short)

nLoss = entry_atr * atrMulti_Loss
nProfit = entry_atr * atrMulti_Profit

long_profit_level = float(0.0)  //set float
long_stop_level = float(0.0)  //set float
long_profit_level := entry_price + nProfit
long_stop_level := entry_price - nLoss

short_profit_level = float(0.0)  //set float
short_stop_level = float(0.0)  //set float
short_profit_level := entry_price - nProfit
short_stop_level := entry_price + nLoss


plot(strategy.position_size <= 0 or entry_long or entry_short ? na : long_stop_level, 
color=color.new(color.red, 0), style=plot.style_linebr, linewidth=2)
plot(strategy.position_size <= 0 or entry_long or entry_short ? na : long_profit_level, 
color=color.new(color.green, 0), style=plot.style_linebr, linewidth=2)
plot(strategy.position_size >= 0 or entry_long or entry_short ? na : short_stop_level, 
color=color.new(color.red, 0), style=plot.style_linebr, linewidth=2)
plot(strategy.position_size >= 0 or entry_long or entry_short ? na : short_profit_level, 
color=color.new(color.green, 0), style=plot.style_linebr, linewidth=2)

strategy.close(id='Long Entry', comment='Exit 1 L1', when=ta.crossunder(renko_close, 
renko_open) or long_stop_level)
strategy.close(id='Short Entry', comment='Exit 1 S1', when=ta.crossover(renko_open, 
renko_close))

strategy.exit('TP/SL 1', from_entry='Long Entry', stop=long_stop_level, 
limit=long_profit_level, qty_percent=50)
strategy.exit('TP/SL 2', from_entry='TP/SL 1', stop=entry_price) //, qty_percent=100)

strategy.exit('TP/SL 1', 'Short Entry', stop=short_stop_level, limit=short_profit_level, 
qty_percent=50)
strategy.exit('TP/SL 2', 'Short Entry', stop=short_stop_level) //, qty_percent=100)

2 ответа

У меня другая проблема с этим скриптом. Я не могу переместить стоп-лосс после фиксации прибыли. SL может двигаться только на нулевых уровнях. Мне нужно, например, чтобы переместить SL съели TP2 после достижения TP3

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

//@version=5
strategy("strategy rsi - SL to BE", overlay=true, initial_capital=10000, default_qty_value = 1000, default_qty_type= strategy.fixed) 
eg1 = ta.ema(close, 9)
eg2 = ta.ema(close, 50)
long = ta.crossover(eg1, eg2)
closelong = ta.crossunder(eg1, eg2)
longSL = (close * 0.005) / syminfo.mintick
longTP0 = (close * 0.005) / syminfo.mintick
longTP1 = (close * 0.01) / syminfo.mintick
longTP2 = (close * 0.015) / syminfo.mintick
longTP3 = (close * 0.02) / syminfo.mintick
longTP4 = (close * 0.025) / syminfo.mintick

DefaultP = 0.5
default_qty_value = 1000

qty2 = default_qty_value - (default_qty_value / 2)
qty3 = qty2 - (qty2 / 2)


if (long)
    strategy.entry("Long", strategy.long)
    longSL = (close * 0.005) / syminfo.mintick
    longTP0 = (close * 0.005) / syminfo.mintick
    longTP1 = (close * 0.01) / syminfo.mintick
    longTP2 = (close * 0.015) / syminfo.mintick
    longTP3 = (close * 0.02) / syminfo.mintick
    longTP4 = (close * 0.025) / syminfo.mintick
    strategy.exit("Exit long 1","Long", profit=longTP1, loss=longSL, qty_percent=50)
    strategy.exit("Exit long 2", "Long", loss=longSL, profit=longTP2, qty=qty2*DefaultP )
    strategy.exit("Exit long 3", "Long", loss=longSL, profit=longTP3, qty=qty3*DefaultP )
    strategy.exit("Exit long 4", "Long", loss=longSL, profit=longTP4, qty_percent=100 )
if(ta.change(strategy.closedtrades) and strategy.openprofit > 0)
    strategy.cancel("Exit long 2")
    longTP4 = (strategy.opentrades.entry_price(0) * 0.015) / syminfo.mintick
    strategy.exit("Exit long - Amended2", "Long", loss=0, profit=longTP2, qty=qty2*DefaultP)
if(ta.change(strategy.closedtrades) and strategy.openprofit > 0)
    strategy.cancel("Exit long 3")
    longTP4 = (strategy.opentrades.entry_price(0) * 0.02) / syminfo.mintick
    strategy.exit("Exit long - Amended3", "Long", loss=longSL, profit=longTP3, qty=qty3*DefaultP)
if(ta.change(strategy.closedtrades) and strategy.openprofit > 0)
    strategy.cancel("Exit long 4")
    longTP4 = (strategy.opentrades.entry_price(0) * 0.025) / syminfo.mintick
    strategy.exit("Exit long - Amended4", "Long", loss=0, profit=longTP4, qty_percent=100)

 

    // Plot take profit values for confirmation
plot(series=(strategy.position_size > 0) ? longTP1 : na, color=color.green, style=plot.style_circles, linewidth=1, title="Take Profit 1")
plot(series=(strategy.position_size > 0) ? longTP2 : na, color=color.green, style=plot.style_circles, linewidth=1, title=" Take Profit 2")
plot(series=(strategy.position_size > 0) ? longTP3 : na, color=color.green, style=plot.style_circles, linewidth=1, title=" Take Profit 3")
plot(series=(strategy.position_size > 0) ? longTP4 : na, color=color.green, style=plot.style_circles, linewidth=1, title=" Take Profit 4")
plot(series=(strategy.position_size > 0) ? longSL : na, color=color.red, style=plot.style_circles, linewidth=1, title="Stop Loss")

Я решил это в этой стратегии. Условия входа только для тестирования. Но удается продать половину позиции, достигнув 1% прибыли, переместить SL к цене входа и установить новый TP в 2%

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

//@version=5
strategy("strategy rsi - SL to BE", overlay=true, initial_capital=100, default_qty_value = 100, default_qty_type= strategy.percent_of_equity) 
ema200 = ta.ema(close, 200)
rsi = ta.rsi(close,14)
timePeriod = time >= timestamp(syminfo.timezone, 2021, 1, 1, 0, 0)
notInTrade = strategy.position_size <= 0
longCondition1 = close > ema200
longCondition2 = rsi <= 30

if (longCondition1 and longCondition2)
    strategy.entry("Long", strategy.long)
    longSL = (close * 0.01) / syminfo.mintick
    longTP1 = (close * 0.01) / syminfo.mintick
    longTP2 = (close * 0.02) / syminfo.mintick
    strategy.exit("Exit long 1","Long", profit=longTP1, loss=longSL, qty_percent=50)
    strategy.exit("Exit long 2", "Long", loss=longSL, profit=longTP2, qty_percent=100)
if(ta.change(strategy.closedtrades) and strategy.openprofit > 0)
    strategy.cancel("Exit long 2")
    longTP2 = (strategy.opentrades.entry_price(0) * 0.02) / syminfo.mintick
    strategy.exit("Exit long - Amended", "Long", loss=0, profit=longTP2, qty_percent=100)
Другие вопросы по тегам