Dark_Trader-x01
Pine Script Rookie
Pine Script Rookie
Posts: 7
Joined: September 26th, 2021
Contact: TradingView Profile

"Problem" with Take-Profit in a strategy

Hello!


I have created an indicator and from it a strategy to see what profit factor could be obtained by applying it.

The problem is that the take profit order is not executed instantly as I would like. The stop loss order, however, does execute well.

Could someone throw some light on this matter?

Thanks in advance and happy new year to everyone!


P.S. I have attached some illustrative images for this topic and the code.

IMAGES:
https://imgur.com/a/35zltXz

Code: Select all

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Dark_Trader-x01

//@version=4

//########################################################################################################//
strategy(title="ENVOLVENTE_VIP_strategy_v1", overlay=true, default_qty_type=strategy.cash, default_qty_value=1000, initial_capital=1000, currency=currency.USD)
//########################################################################################################//


//########################################################################################################//
// DECLARACION DE VARIABLES
bool env_BUY = false
bool env_SELL = false

var float sl_long = na
var float sl_short = na
var float tp_long = na
var float tp_short = na
//########################################################################################################//


//########################################################################################################//
// FUNCIONES
longstop = lowest(low, 2)
shortstop = highest(high, 2)
//########################################################################################################//


//########################################################################################################//
// VELA ENVOLVENTE VIP ALCISTA
if (close > open) and (open[1] > close[1]) and (close > open[1]) and (open < close[1]) 
    env_BUY := true

// VELA ENVOLVENTE VIP BAJISTA
if (open > close) and (close[1] > open[1]) and (open > close[1]) and (close < open[1])
    env_SELL := true
//########################################################################################################//


//########################################################################################################//
// STOP-LOSS y TAKE-PROFIT
/// ALCISTA
if env_BUY and strategy.position_size == 0
    sl_long := longstop
    tp_long := close
    
/// BAJISTA        
if env_SELL and strategy.position_size == 0
    sl_short := shortstop
    tp_short := close
//########################################################################################################//


//########################################################################################################//
// ESTRATEGIA ALCISTA
strategy.entry(id="LONG", long=true, when=env_BUY)
strategy.exit(id="LONG_EXIT", from_entry="LONG", stop=sl_long)            // STOP-LOSS
strategy.close(id="LONG", when=(close > tp_long) or (open > tp_long))     // TAKE-PROFIT

// ESTRATEGIA BAJISTA
strategy.entry(id="SHORT", long=false, when=env_SELL)
strategy.exit(id="SHORT_EXIT", from_entry="SHORT", stop=sl_short)         // STOP-LOSS
strategy.close(id="SHORT", when=(close < tp_short) or (open < tp_short))  // TAKE-PROFIT
//########################################################################################################//


//########################################################################################################//
// PLOTS
plotshape(env_BUY, style=shape.arrowup, text="env_BUY", location=location.abovebar, color=color.green)
plotshape(env_SELL, style=shape.arrowdown, text="env_SELL", location=location.belowbar, color=color.red)
//########################################################################################################//

Deep-Wave
Pine Script Master
Pine Script Master
Posts: 44
Joined: September 4th, 2020
Contact: TradingView Profile

Re: "Problem" with Take-Profit in a strategy

Hi Dark Trader,
I looked at your code, you are using the strategy.exit function for your stop loss (which uses a stop order to trigger your SL) but you are using the strategy.close function to trigger your take profit. The strategy.close function uses market orders (instead of limit or stop orders) to close positions which means that with the default settings your script waits for the bar close and checks then if your take profit condition is true. If it is true on the close, the script closes the trade with the next calculation on the open of the next bar.

I hope that points you in the right direction, best of luck with your coding! :cool:

Mark

Dark_Trader-x01
Pine Script Rookie
Pine Script Rookie
Posts: 7
Joined: September 26th, 2021
Contact: TradingView Profile

Re: "Problem" with Take-Profit in a strategy

Deep-Wave wrote:
Sat Jan 15, 2022 11:21 am
Hi Dark Trader,
I looked at your code, you are using the strategy.exit function for your stop loss (which uses a stop order to trigger your SL) but you are using the strategy.close function to trigger your take profit. The strategy.close function uses market orders (instead of limit or stop orders) to close positions which means that with the default settings your script waits for the bar close and checks then if your take profit condition is true. If it is true on the close, the script closes the trade with the next calculation on the open of the next bar.

I hope that points you in the right direction, best of luck with your coding! :cool:

Mark
Wowwww!
Thank you very much Mark!. You are a Profi! :up:
Nice weekend
Regards!

Return to “Request Scripts”