Strategy: Have 2 take profits and move stoploss to entry

I’m trying since weeks now, to solve a problem and it seems like I’m not able to do it on my own.

I want a strategy, that contains:

  • two TakeProfit levels (TP1 – 50% | TP2 – 100%)
  • one main stoploss, when TP1 is NOT hit
  • move stoploss to entry, after TP1 WAS hit

What I was able to achieve is, that both TakeProfits are being taken correctly.

Problem still is:

  • When TP1 was NOT hit, but main StopLoss is hit, it will only StopLoss with 50%
  • When TP1 was hit, it instantly triggers the StopLoss at entry, which doesn’t makes sense to me

In the source code below, I just added for now the strategy for a short, as it will be easy to finalize it for the long position when everything works as expected:

//@version=5
strategy("Long/Short Signal [Strategy]", overlay=true, initial_capital = 100000, max_bars_back = 1000) 

// 257  LETZTE RICHTIGE!!!!!!!!!!!!!
///////////////////////////
// Global Variables
////////////////////////
enableRsiRegular =      true 
rsiColorPos =           color.new(color.lime, 0)
rsiColorNeg =           color.new(color.lime, 0)

enableRsiTrendlines =   true
trendlineRcolor =       color.white
trendlineScolor =       color.white

enableDivergences =     input           (   true,                               "    ◉ ON/OFF - DIV",                  group="Show/Hide", inline="Divergence")
showhidden        =     input           (   true,                               "Show hidden DIVs",                    group="Show/Hide", inline="Divergence")

////////////////////////
// Sources
////////////////////////
sourceCLOSE =   close
sourceOPEN =    open
sourceHLC3 =    hlc3
sourceHIGH =    high
sourceLOW =     low

///////////////////////////////////////////////////////////////////////////////////////
//                                                                                   //
//                       -------------- RSI --------------                           //
//                                                                                   //
///////////////////////////////////////////////////////////////////////////////////////
rsiLengthInput =    input.int       (   14,     "RSI",      1,      group="RSI",        inline="RSILength")

var rsiOutput = 0.0
color rsiColor = na
if enableRsiRegular
    // calculation
    rsiOutput := ta.rsi(sourceCLOSE, rsiLengthInput)
    rsiColor := rsiOutput > 50 ? rsiColorPos : rsiColorNeg

// SCALED
rsi_Y =                      50
rsi_Multiplicator =          3  
rsiOutputScaled =   ((rsiOutput-50)*rsi_Multiplicator)+rsi_Y

///////////////////////////////////////////////////////////////////
//----------------------------- FINAL OUTPUTS --------------------
//////////////////////////////////////////////////////////////////
rsiFinalOutput = rsiOutputScaled

////////////////////////////////////////////////////////////////////////////////
//                                                                            //
//                        ====== DIVERGENCES ======                           //
//                                                                            //
////////////////////////////////////////////////////////////////////////////////
import iBeardy/iBeardyDivergenceLib/22 as divergenceIndicators

var isDivergence = divergenceIndicators.Object.new()
var isRegularBearDiv = false
var isRegularBullDiv = false
var isHiddenBearDiv = false
var isHiddenBullDiv = false

if enableDivergences
    isRegularBearDiv := divergenceIndicators.CheckRegularBearDivergences(isDivergence, "RegularBear", false, "0")

////////////////////////////////////////////////////////////////////////////////
//                                                                            //
//                      ====== TRENDLINE RSI ======                           //
//                                                                            //
////////////////////////////////////////////////////////////////////////////////

import HoanGhetti/SimpleTrendlines/3 as tl

g_trendlines    = 'Trendline Settings', g_conditions = 'Conditions',  g_styling = 'Styling', g_timeframe = 'Timeframe'
input_pLen      = input.int             (   3,              'Lookback',               group = "RSI Trendlines", inline="RSI Trendlines",   minval = 1, tooltip = 'How many bars to determine when a swing high/low is detected.')
input_repaint   = input.string          (   'On',           'Repaint',                   group = "RSI Trendlines", inline="RSI Trendlines",   options = ['On', 'Off: Bar Confirmation'], tooltip = 'Bar Confirmation: Generates alerts when candle closes. (1 Candle Later)')
input_rsiDiff   = input.int             (   3,              'RSI Difference',               group = "RSI Trendlines", inline="RSI Trendlines",   tooltip = 'The difference between the current RSI value and the breakout value.nnHow much higher in value should the current RSI be compared to the breakout value in order to detect a breakout?')
input_override  = input.bool            (   false,          'Override Text Color',          group = "RSI Trendlines")
input_overCol   = input.color           (   color.white,  '',                             group = "RSI Trendlines")
input_checkDivs = input.bool            (   true,          "With regular DIV check?",  group = "RSI Trendlines")
input_checkHiddenDivs = input.bool      (   false,          "With hidden DIV check?",  group = "RSI Trendlines")

lblText = 'Br'

repaint = switch input_repaint
    'On' => true
    'Off: Bar Confirmation' => false


rsiX = rsiFinalOutput
outputFinal =  rsiX

pl = fixnan(ta.pivotlow(outputFinal, 1, input_pLen))
ph = fixnan(ta.pivothigh(outputFinal, 1, input_pLen))

pivot(float pType) =>
    pivot = pType == pl ? pl : ph
    xAxis = ta.valuewhen(ta.change(pivot), bar_index, 0) - ta.valuewhen(ta.change(pivot), bar_index, 1)
    prevPivot = ta.valuewhen(ta.change(pivot), pivot, 1)
    pivotCond = ta.change(pivot) and (pType == pl ? pivot > prevPivot : pivot < prevPivot)
    pData = tl.new(x_axis = xAxis, offset = input_pLen, strictMode = true, strictType = pType == pl ? 0 : 1)
    pData.drawLine(pivotCond, prevPivot, pivot, outputFinal)
    pData

trendlineRcolor :=  enableRsiTrendlines ? trendlineRcolor : na
trendlineScolor := enableRsiTrendlines ? trendlineScolor : na

strategyDetails(direction, entryPrice, TP1, TP2, SL)=>
    textOutput = "ENTRY: "+str.tostring(entryPrice)   +    "nTP1: "+str.tostring(TP1)    +   "nTP2: "+str.tostring(TP2)     +   "nSL Main: "+str.tostring(SL)   +   "nSL Moved: "+str.tostring(entryPrice)
    if direction == "short"
        short1 = label.new(bar_index, na, yloc = yloc.abovebar, text = textOutput, color = color.new(color.red, 50) ,size = size.small, textcolor = input_override ? color.new(trendlineScolor, 50) : color.new(trendlineScolor, 50)) 
    if direction == "long"
        long1 = label.new(bar_index, na, yloc=yloc.belowbar, text = textOutput, color = color.new(color.green, 50), size = size.small, textcolor = input_override ? color.new(trendlineScolor, 50) : color.new(trendlineScolor, 50), style=label.style_label_up)

breakout(tl.Trendline this, float pType) =>
    regularBearDivs = isRegularBearDiv[4] or isRegularBearDiv[3] or isRegularBearDiv[2] or isRegularBearDiv[1] or isRegularBearDiv
    regularBullDivs = isRegularBullDiv[4] or isRegularBullDiv[3] or isRegularBullDiv[2] or isRegularBullDiv[1] or isRegularBullDiv
    hiddenBearDivs = isHiddenBearDiv[4] or isHiddenBearDiv[3] or isHiddenBearDiv[2] or isHiddenBearDiv[1] or isHiddenBearDiv
    hiddenBullDivs = isHiddenBullDiv[4] or isHiddenBullDiv[3] or isHiddenBullDiv[2] or isHiddenBullDiv[1] or isHiddenBullDiv
    
    var bool hasCrossed = false
    if ta.change(this.lines.startline.get_y1())
        hasCrossed := false
    this.drawTrendline(not hasCrossed)
    condType = (pType == pl ? rsiX < this.lines.trendline.get_y2() - input_rsiDiff : rsiX > this.lines.trendline.get_y2() + input_rsiDiff) and not hasCrossed
    condition = repaint ? condType : condType and barstate.isconfirmed

    highest = ta.highest(high, 5)
    lowest = ta.lowest(low, 5)
    float SL = na
    float entryPrice = na
    float TP1 = na
    float TP2 = na

    if condition and pType == pl
        hasCrossed := true
        this.lines.startline.set_xy2(this.lines.trendline.get_x2(), this.lines.trendline.get_y2())
        this.lines.trendline.set_xy2(na, na)
        this.lines.startline.copy()
        
        // --------------------- Calculation for strategy
        // Entry
        entryPrice := close
        // Main Stoploss
        SL := highest
        // Moved Stoploss after TP1 hit
        movedSL = entryPrice
        // Calculation for the target Price
        calc1 = SL - entryPrice
        calc2 = calc1 * 1.5
        calc3 = calc2 / 2
        // TP1
        TP1 := entryPrice - calc3
        // TP2 (not used yet)
        TP2 := entryPrice - calc2
        // Max loss (not used yet)
        maxLoss = 50
        // Calculation for the quantity of the entered trade
        calc4 = SL - entryPrice
        calc5 = 50*1/calc4

        if regularBearDivs and not regularBullDivs
            textOutput = strategyDetails("short", entryPrice, TP1, TP2, SL)  
            strategy.entry("short1", strategy.short, calc4, comment = "SHORT regular")
            strategy.exit("TP1", "short1",  limit=TP1, qty_percent = 50, stop=SL, comment_profit = "TP1 Short", comment_loss = "Stoploss2 Short")
            strategy.exit("TP2", "short1",  limit=TP2, stop=SL,   comment_profit = "TP2 Short", comment_loss = "Stoploss1 Short")


    hasCrossed

method style(tl.Trendline this, color col) =>
    this.lines.startline.set_color(col)
    this.lines.startline.set_width(1)
    this.lines.trendline.set_color(col)
    this.lines.trendline.set_width(1)
    this.lines.trendline.set_style(line.style_solid)

plData = pivot(pl)
phData = pivot(ph)
plData.style(trendlineRcolor)
phData.style(trendlineScolor)
cu = breakout(plData, pl)
co = breakout(phData, ph)

I tried different approaches but they never work as expected. Also looking for solutions online didn’t help me solve the problem.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật