Hi,
I'm a pretty newbie at this topic and all my knowledge about pine script is diced together out of available open-source scripts and what i remind from school ;). Right now i want to make an indicator where the stoploss (for the hole position) moves after takeprofit2 to breakeven, but i got pretty big problems here and tried already several versions but don't get the result i want. I would very appreciate if somebody can help me here!
TP1Perc = input.float(title=Long Take Profit 1 (%)
, minval=0.0, step=0.1, defval=2, group=TP & SL
)
TP2Perc = input.float(title=Long Take Profit 2 (%)
, minval=0.0, step=0.1, defval=4, group=TP & SL
)
TP3Perc = input.float(title=Long Take Profit 3 (%)
, minval=0.0, step=0.1, defval=4, group=TP & SL
)
TP4Perc = input.float(title=Long Take Profit 4 (%)
, minval=0.0, step=0.1, defval=4, group=TP & SL
)
TP1_Ratio = input.float(title=Sell Postion Size % @ TP1
, defval=50, step=1, group=TP & SL
, tooltip=Example: 50 closing 50% of the position once TP1 is reached
)/100
TP2_Ratio = input.float(title=Sell Postion Size % @ TP2
, defval=50, step=1, group=TP & SL
, tooltip=Example: 50 closing 50% of the position once TP2 is reached
)/100
TP3_Ratio = input.float(title=Sell Postion Size % @ TP3
, defval=50, step=1, group=TP & SL
, tooltip=Example: 50 closing 50% of the position once TP3 is reached
)/100
TP4_Ratio = input.float(title=Sell Postion Size % @ TP4
, defval=50, step=1, group=TP & SL
, tooltip=Example: 50 closing 50% of the position once TP4 is reached
)/100
// Calculate moving averages
//fastSMA = ta.sma(close, FastPeriod)
//slowSMA = ta.sma(close, SlowPeriod)
// Calculate trading conditions
//enterLong = ta.crossover(fastSMA, slowSMA)
// Plot moving averages
//plot(series=fastSMA, color=color.green, title=Fase MA
)
//plot(series=slowSMA, color=color.red, title=Slow MA
)
// STEP 2:
// Figure out take profit price
percentAsPoints(pcnt) =>
strategy.position_size != 0 ? math.round(pcnt / 100.0 * strategy.position_avg_price / syminfo.mintick) : float(na)
percentAsPrice(pcnt) =>
strategy.position_size != 0 ? ((pcnt / 100.0) + 1.0) * strategy.position_avg_price : float(na)
current_position_size = math.abs(strategy.position_size)
initial_position_size = math.abs(ta.valuewhen(strategy.position_size[1] == 0.0, strategy.position_size, 0))
TP1 = strategy.position_avg_price + percentAsPoints(TP1Perc) * syminfo.mintick * strategy.position_size / math.abs(strategy.position_size)
TP2 = strategy.position_avg_price + percentAsPoints(TP2Perc) * syminfo.mintick * strategy.position_size / math.abs(strategy.position_size)
TP3 = strategy.position_avg_price + percentAsPoints(TP3Perc) * syminfo.mintick * strategy.position_size / math.abs(strategy.position_size)
TP4 = strategy.position_avg_price + percentAsPoints(TP4Perc) * syminfo.mintick * strategy.position_size / math.abs(strategy.position_size)
// Submit entry orders
if buySignal and in_date_range == true
strategy.entry('Long', strategy.long, when=buySignal, alert_message='Open Long Position')
if sellSignal and in_date_range == true
strategy.entry('Short', strategy.short, when=sellSignal, alert_message='Open Short Position')
// STEP 3:
// Submit exit orders based on take profit price
if strategy.position_size > 0
strategy.exit(TP1
, from_entry=Long
, qty = initial_position_size * TP1_Ratio, limit = TP1)
strategy.exit(TP2
, from_entry=Long
, qty = initial_position_size * TP2_Ratio, limit = TP2)
strategy.exit(TP3
, from_entry=Long
, qty = initial_position_size * TP3_Ratio, limit = TP3)
strategy.exit(TP4
, from_entry=Long
, qty = initial_position_size * TP4_Ratio, limit = TP4)
strategy.close('Long', when=sellSignal, alert_message='Close Long Position')
if strategy.position_size < 0
strategy.exit(TP1
, from_entry=Short
, qty = initial_position_size * TP1_Ratio, limit = TP1)
strategy.exit(TP2
, from_entry=Short
, qty = initial_position_size * TP2_Ratio, limit = TP2)
strategy.exit(TP3
, from_entry=Short
, qty = initial_position_size * TP3_Ratio, limit = TP3)
strategy.exit(TP4
, from_entry=Short
, qty = initial_position_size * TP4_Ratio, limit = TP4)
strategy.close('Short', when=buySignal, alert_message='Close Short Position')
// Plot take profit values for confirmation
plot(series=(strategy.position_size > 0) ? TP1 : na, color=color.green, style=plot.style_circles, linewidth=1, title=Take Profit 1
)
plot(series=(strategy.position_size > 0) ? TP2 : na, color=color.green, style=plot.style_circles, linewidth=1, title=Take Profit 2
)
plot(series=(strategy.position_size > 0) ? TP3 : na, color=color.green, style=plot.style_circles, linewidth=1, title=Take Profit 3
)
plot(series=(strategy.position_size > 0) ? TP4 : na, color=color.green, style=plot.style_circles, linewidth=1, title=Take Profit 4
)
Thank your very much for your help upfront.