0
votes

I'm sorry if someone asked this question (if so please guide me to the answer) I have been searching and attempting to resolve this issue for almost 2 weeks now with no luck!

I'm working on a strategy and I need my strategy to close 50% at tp1 and 50% on tp2 and incase an exit condition is met, it closes the entire trade

I tried many variations but I would get one of the following issues:

1- it would hit stoploss without notifying me on the chart (with the arrow alert thing)

2- it would completely ignore the stop loss 3- it would only partially close some of the stoploss

Please help me as I have tried everything I could find and everything I could think of!

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Strategy Entry//////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Limit10 = input(false,title= " ???? Stop Loss and Take Profit ???? ")

atrLength = input(14, "Strategy ATR Length")
slMultiplier = input(4.0, "Strategy SL")
tpMultiplier = input(1.0, "Strategy TP1")
tpMultiplier2 = input(1.5, "Strategy TP2")
tpMultiplier3 = input(1.8, "Strategy TP3")
atr = atr(atrLength)
sls = (price + atr * slMultiplier)
tps = (price - atr * tpMultiplier)
sll = (price - atr * slMultiplier)
tpl = (price + atr * tpMultiplier)


// EWO Strategy 
EWOenterLong = smadif > 0.01
EWOenterShort = smadif < -0.01

// Baseline Strategy

BaselineLong = close > sma(sma(src, ceil(len / 2)), floor(len / 2) + 1)
BaselineShort = sma(sma(src, ceil(len / 2)), floor(len / 2) + 1) > close

LongMA = out < close
ShortMA = out > close

// WAE Strategy

WAELong = (t1 >= 0) ? t1 : 0
WAEShort = (t1 < 0) ? (-1*t1) : 0

// Alert Entry
AlertLong = long
AlertShort = short

//KVO Exit
KVOExitLong = kvo(lenFast, lenSlow) < ShortZone
KVOExitShort = kvo(lenFast, lenSlow) > LongZone

//RVI Entry Exit
RVIBullish = current_rvi > highest(nz(current_rvi[1]), period)
RVIBeaerish = current_rvi < lowest(nz(current_rvi[1]), period)

//////////////

//Entries
enterLong = AlertLong and BaselineLong and WAELong and EWOenterLong and LongMA
enterShort = AlertShort and BaselineShort and WAEShort and EWOenterShort and ShortMA

//Exits

LongSL = price - atr * slMultiplier
ShortSL = price + atr * slMultiplier


///

if (enterLong)
    sl1 = price - atr * slMultiplier
    tp1 = price + atr * tpMultiplier
    tp2 = price + atr * tpMultiplier2
    tp3 = price + atr * tpMultiplier3
    LongSL = price - atr * slMultiplier
    exitLong = RVIBeaerish or KVOExitLong or LongSL? 1 : 0 
    strategy.entry("Long", strategy.long)
    strategy.exit("Long TP1 -50%", "Long", qty_percent = 50 , limit = tp1, stop = LongSL)
    strategy.exit("Long TP2 -25%", "Long", qty_percent = 25 , limit = tp2, stop = LongSL)
    strategy.exit("Short TP3 -100% or SL Hit", "Short", qty_percent = 100 , limit = tp3, stop = ShortSL)
    strategy.exit("Exit Long Now", "Long", qty_percent = 100 , limit = tp3 , stop = LongSL)

if (enterShort)
    sl1 = price + atr * slMultiplier
    tp1 = price - atr * tpMultiplier
    tp2 = price - atr * tpMultiplier2
    tp3 = price - atr * tpMultiplier3
    ShortSL = price + atr * slMultiplier
    exitShort = RVIBullish or KVOExitShort or ShortSL? 0 : 1
    strategy.entry("Short", strategy.short)  
    strategy.exit("Short TP1 -50%", "Short", qty_percent = 50 , limit = tp1, stop = ShortSL)
    strategy.exit("Short TP2 -25%", "Short", qty_percent = 25 , limit = tp2, stop = ShortSL)
    strategy.exit("Short TP3 -100% or SL Hit", "Short", qty_percent = 100 , limit = tp3, stop = ShortSL)
    strategy.exit("Exit Short Now", "Short", qty_percent = 100 , stop = ShortSL)
   

Thanks in advance and please feel free to ask any questions if what I wrote above is not clear

1

1 Answers

0
votes

Your code example is missing components and is not compilable, so will take a guess.

I would change the exitLong variable to boolean type instead of integer.

exitLong = RVIBeaerish or KVOExitLong or LongSL? true : false

You initialize the tp* variables in the local scope and this could cause the issue. Initialize them in the global scope and re-assign (:=) the value in the local scope.

var float tp1 = na
var float tp2 = na
var float tp3 = na

if (enterShort) 
    tp1 := price - atr * tpMultiplier
    tp2 := price - atr * tpMultiplier2
    tp3 := price - atr * tpMultiplier3

The ShortSL variable is declared twice - in the global scope and inside the if (enterShort) statement. This would give a compilation error.