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.