'Im trying to move my stop loss when the first take profit is taken to breakeven or the entry price of where i entered from when 1/2 is taken off
im unable to move the stop loss to the entry price when i sell half of the position i have the entry price working and can get the price for the stop loss but i want it to move to break even once the profit target
//@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)
Solution 1:[1]
I solved that in this strategy. Entry conditions are for testing only. But manages to sell half of the position reached 1% profit, move the SL to the entry price, and set the new TP to 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)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Hernán Orsi |