Move stop loss to entry when price reaches 50% of the take profit percentage target

Viewed 45

I'm looking to add in a condition to my vwap strategy which would move the stop loss to the entry point when the price reaches 50% of the take profit percentage target.

For example with a take profit of 5% and a stop loss of -2.5% where the stop loss would move to 0% when the price reaches 2.5% above entry point/price.

My coding skills are very basic and most of this code is taken from templates.

I'd appreciate any help with this, thanks.

// conditions (can filter this with further conditions + logic)
buy = (close < vwap_lower) // and (k <= 20)and not (strategy.closedtrades.exit_bar_index(strategy.closedtrades - 1) == bar_index) and (val > - 1)  // and (fastEMA < slowEMA)  
sell = (close > vwap_upper)// and (k >= 80) and not (strategy.closedtrades.exit_bar_index(strategy.closedtrades - 1) == bar_index) and (val < 0.5) // and (fastEMA > slowEMA) 

// plot bands
plot(vwap_upper, title='Upper')
plot(vwap_lower, title='Lower')

// plot deviations
plotshape(show_deviations and sell, style=shape.circle, location=location.abovebar, color=color.red)
plotshape(show_deviations and buy, style=shape.circle, location=location.belowbar, color=color.green)
plot(close)

//// Stoploss and Take P

enable_long_strategy = input.bool(true, title='Enable Long Strategy', group='SL/TP For Long Strategy', inline='1')
long_stoploss_value = input.float(defval=7.5, title='Stoploss %', minval=0.1, group='SL/TP For Long Strategy', inline='2')
long_stoploss_percentage = close * (long_stoploss_value / 100) / syminfo.mintick
long_takeprofit_value = input.float(defval=2.5, title='Take Profit %', minval=0.1, group='SL/TP For Long Strategy', inline='2')
long_takeprofit_percentage = close * (long_takeprofit_value / 100) / syminfo.mintick

// Enable Short Strategy
enable_short_strategy = input.bool(true, title='Enable Short Strategy', group='SL/TP For Short Strategy', inline='3')
short_stoploss_value = input.float(defval=7.5, title='Stoploss %', minval=0.1, group='SL/TP For Short Strategy', inline='4')
short_stoploss_percentage = close * (short_stoploss_value / 100) / syminfo.mintick
short_takeprofit_value = input.float(defval=2.5, title='Take Profit %', minval=0.1, group='SL/TP For Short Strategy', inline='4')
short_takeprofit_percentage = close * (short_takeprofit_value / 100) / syminfo.mintick

// Plot Stoploss & Take Profit Levels
long_stoploss_price = strategy.position_avg_price * (1 - long_stoploss_value / 100)
long_takeprofit_price = strategy.position_avg_price * (1 + long_takeprofit_value / 100)
short_stoploss_price = strategy.position_avg_price * (1 + short_stoploss_value / 100)
short_takeprofit_price = strategy.position_avg_price * (1 - short_takeprofit_value / 100)
plot(enable_long_strategy and not enable_short_strategy ? long_stoploss_price : na, color=color.new(#ff0000, 0), style=plot.style_linebr, linewidth=2, title='Long SL Level')
plot(enable_long_strategy and not enable_short_strategy ? long_takeprofit_price : na, color=color.new(#008000, 0), style=plot.style_linebr, linewidth=2, title='Long TP Level')
plot(enable_short_strategy and not enable_long_strategy ? short_stoploss_price : na, color=color.new(#ff0000, 0), style=plot.style_linebr, linewidth=2, title='Short SL Level')
plot(enable_short_strategy and not enable_long_strategy ? short_takeprofit_price : na, color=color.new(#008000, 0), style=plot.style_linebr, linewidth=2, title='Short TP Level')

// Date Range
start_date = input.int(title='Start Date', defval=1, minval=1, maxval=31, group='Date Range', inline='1')
end_date = input.int(title='End Date', defval=1, minval=1, maxval=31, group='Date Range', inline='1')
start_month = input.int(title='Start Month', defval=1, minval=1, maxval=12, group='Date Range', inline='2')
end_month = input.int(title='End Month', defval=1, minval=1, maxval=12, group='Date Range', inline='2')
start_year = input.int(title='Start Year', defval=2022, minval=1800, maxval=3000, group='Date Range', inline='3')
end_year = input.int(title='End Year', defval=2077, minval=1800, maxval=3000, group='Date Range', inline='3')
in_date_range = time >= timestamp(syminfo.timezone, start_year, start_month, start_date, 0, 0) and time < timestamp(syminfo.timezone, end_year, end_month, end_date, 0, 0)

// Long Strategy
if buy and in_date_range and enable_long_strategy and barstate.isconfirmed == true
    strategy.entry('Long', strategy.long, when=buy, alert_message = message_long_entry)
    strategy.exit('Long SL/TP', from_entry='Long', loss=long_stoploss_percentage, profit=long_takeprofit_percentage, alert_message='Your Long SL/TP Limit As Been Triggered.')

// Short Strategy
if sell and in_date_range and enable_short_strategy and barstate.isconfirmed == true
    strategy.entry('Short', strategy.short, when=sell, alert_message = message_short_entry)
    strategy.exit('Short SL/TP', from_entry='Short', loss=short_stoploss_percentage, profit=short_takeprofit_percentage, alert_message='Your Short SL/TP Limit As Been Triggered.')
0 Answers
Related