take profit + moving stop when TP is hit

ghz 8months ago ⋅ 70 views

I have been working on a multi TP + Moving stop and now im stuck. when it hits TP the stop moves but doesnt seem to be plotting correctly as it runs after trade is closed, also the intial stop doesnt plot and sometimes doesnt trigger. im very very new to this so any help would be amazing.

//@version=5
strategy('ema moving stop - Long only',
     overlay=true, 
     initial_capital=50, 
     currency=currency.USDT,
     default_qty_type=strategy.percent_of_equity, 
     default_qty_value=100,
     commission_type=strategy.commission.percent,
     commission_value=0.05)

// Define input parameters
float initialStopLossPercent = input.float(1.5, title="Initial Stop Loss Percentage")
float takeProfit1Percent = input.float(1, title="Take Profit 1 Percentage")
float takeProfit2Percent = input.float(1.5, title="Take Profit 2 Percentage")
float takeProfit3Percent = input.float(2, title="Take Profit 3 Percentage")
float takeProfit4Percent = input.float(2.5, title="Take Profit 4 Percentage")

// Entry condition for long trades
longCondition = ta.crossover(ta.sma(close, 14), ta.sma(close, 28))

// Variable to hold the stop loss level and the highest price since the trade was opened
var float stopLossLevel = na

if longCondition
    strategy.entry("long", strategy.long)
    stopLossLevel := strategy.position_avg_price * (1 - initialStopLossPercent / 100) // Set initial stop loss level

// Calculate take profit levels
tp1 = strategy.position_avg_price * (1 + takeProfit1Percent / 100)
tp2 = strategy.position_avg_price * (1 + takeProfit2Percent / 100)
tp3 = strategy.position_avg_price * (1 + takeProfit3Percent / 100)
tp4 = strategy.position_avg_price * (1 + takeProfit4Percent / 100)

// Set take profit and stop loss levels
strategy.exit("TP1", from_entry="long", qty_percent=20, limit=tp1, stop=stopLossLevel)
strategy.exit("TP2", from_entry="long", qty_percent=20, limit=tp2, stop=stopLossLevel)
strategy.exit("TP3", from_entry="long", qty_percent=20, limit=tp3, stop=stopLossLevel)
strategy.exit("TP4", from_entry="long", limit=tp4, stop=stopLossLevel)
strategy.exit("SL", from_entry="long", stop=stopLossLevel)

// Adjust stop loss when take profit is reached
if (strategy.position_size > 0)
    if close >= tp4
        stopLossLevel := tp4
    else if close >= tp3
        stopLossLevel := tp3
    else if close >= tp2
        stopLossLevel := tp2
    else if close >= tp1
        stopLossLevel := tp1

// Store trade direction
var bool islong = na

// Check for open trades and determine the trade direction
if strategy.opentrades > 0
    islong := strategy.position_size > 0
    
// Plot only the relevant take profit levels based on the trade direction
plot(islong ? tp1 : na, color=color.rgb(65, 255, 59), style=plot.style_linebr, linewidth=2)
plot(islong ? tp2 : na, color=color.rgb(65, 255, 59), style=plot.style_linebr, linewidth=2)
plot(islong ? tp3 : na, color=color.rgb(65, 255, 59), style=plot.style_linebr, linewidth=2)
plot(islong ? tp4 : na, color=color.rgb(65, 255, 59), style=plot.style_linebr, linewidth=2)
plot(islong ? stopLossLevel : na, color=color.rgb(243, 33, 44), style=plot.style_linebr, linewidth=2)

Thank in advance

Ive tried a few things but get same results. just really stuck

Answers

It seems like there are a few issues with your script. Let's address them step by step:

  1. Stop Loss Calculation: The initial stop loss calculation seems to be correct. However, you need to update the stop loss level as the price moves in your favor. Currently, you are only adjusting the stop loss when the take profit levels are reached. You should continuously adjust the stop loss level as the price moves in your favor.

  2. Plotting Stop Loss: You should plot the initial stop loss level as well as the dynamically adjusted stop loss level. Currently, you are plotting only the dynamically adjusted stop loss level, which may not be visible on the chart until it's triggered.

  3. Stop Loss Triggering: Make sure that the stop loss level is being triggered correctly when the price moves against your position. You may need to add some debugging statements or plot markers to verify if the stop loss is working as expected.

Here's an updated version of your script with some modifications:

//@version=5
strategy('ema moving stop - Long only',
     overlay=true, 
     initial_capital=50, 
     currency=currency.USDT,
     default_qty_type=strategy.percent_of_equity, 
     default_qty_value=100,
     commission_type=strategy.commission.percent,
     commission_value=0.05)

// Define input parameters
float initialStopLossPercent = input.float(1.5, title="Initial Stop Loss Percentage")
float takeProfit1Percent = input.float(1, title="Take Profit 1 Percentage")
float takeProfit2Percent = input.float(1.5, title="Take Profit 2 Percentage")
float takeProfit3Percent = input.float(2, title="Take Profit 3 Percentage")
float takeProfit4Percent = input.float(2.5, title="Take Profit 4 Percentage")

// Entry condition for long trades
longCondition = ta.crossover(ta.sma(close, 14), ta.sma(close, 28))

// Variable to hold the stop loss level and the highest price since the trade was opened
var float stopLossLevel = na

if longCondition
    strategy.entry("long", strategy.long)
    stopLossLevel := strategy.position_avg_price * (1 - initialStopLossPercent / 100) // Set initial stop loss level

// Calculate take profit levels
tp1 = strategy.position_avg_price * (1 + takeProfit1Percent / 100)
tp2 = strategy.position_avg_price * (1 + takeProfit2Percent / 100)
tp3 = strategy.position_avg_price * (1 + takeProfit3Percent / 100)
tp4 = strategy.position_avg_price * (1 + takeProfit4Percent / 100)

// Set take profit and stop loss levels
strategy.exit("TP1", from_entry="long", qty_percent=20, limit=tp1, stop=stopLossLevel)
strategy.exit("TP2", from_entry="long", qty_percent=20, limit=tp2, stop=stopLossLevel)
strategy.exit("TP3", from_entry="long", qty_percent=20, limit=tp3, stop=stopLossLevel)
strategy.exit("TP4", from_entry="long", limit=tp4, stop=stopLossLevel)
strategy.exit("SL", from_entry="long", stop=stopLossLevel)

// Adjust stop loss when take profit is reached
if (strategy.position_size > 0)
    if close >= tp4
        stopLossLevel := tp4
    else if close >= tp3
        stopLossLevel := tp3
    else if close >= tp2
        stopLossLevel := tp2
    else if close >= tp1
        stopLossLevel := tp1

// Plot stop loss levels
plot(stopLossLevel, color=color.red)

// Store trade direction
var bool islong = na

// Check for open trades and determine the trade direction
if strategy.opentrades > 0
    islong := strategy.position_size > 0
    
// Plot take profit levels
plot(islong ? tp1 : na, color=color.green)
plot(islong ? tp2 : na, color=color.green)
plot(islong ? tp3 : na, color=color.green)
plot(islong ? tp4 : na, color=color.green)

Please review the changes and test the script again to see if it behaves as expected. If you encounter any further issues, feel free to ask for assistance.