TP 50% SL 100%

El espacio del Foro donde compartir indicadores y estrategias creados con el lenguaje de la plataforma TradingView.
Responder
maxg1978
Mensajes: 1
Registrado: 17 Feb 2024 14:37

TP 50% SL 100%

Mensaje por maxg1978 »

Tengo una estrategia a la cual quise añadir la funcionalidad de poder establecer un TP basado en un ratio que el usuario ingresa, al alcanzar ese TP la operación debe cerrarse en un porcentaje que el usuario tambien especifica, esto funciona correctamente, el problema es que al alcanzar el SL tambien sale solo en ese porcentaje, cuando en realidad quiero que cierre completamente la posición. Ya intenté estableciendo dos ordenes diferentes usando por separado el comando strategy.exit() uno para salir al 50% al tocar TP y otro para salir totalmente al tocar SL, ¿uno de esos comandos anula o modifica al otro? (usé identificadores diferentes tambien) Pero aun así no cierra el 100% de la posición al tocar SL. El código es el siguiente:

////////////////////////////////////////////////////
// ENTRADA EN OPERACIONES

if enterLong1 == true or cambio_to_Long
if strategy.position_size < 0
strategy.close("Venta", comment = "cambio")
cambio_to_Long := true
if strategy.position_size == 0
strategy.entry("Compra", strategy.long)
//labelSL = label.new(bar_index, stop_loss_long, str.tostring(stop_loss_long, "#.######"), color=color.green, textalign=text.align_left, size=size.small)
cambio_to_Long := false

if enterShort1 == true or cambio_to_Short
if strategy.position_size > 0
strategy.close("Compra", comment = "cambio")
cambio_to_Short := true
if strategy.position_size == 0
strategy.entry("Venta", strategy.short)
//labelSL = label.new(bar_index, stop_loss_short, str.tostring(stop_loss_short, "#.######"), color=color.red, textalign=text.align_left, size=size.small)
cambio_to_Short := false

var float tp1_long = na
var float tp1_short = na

if strategy.position_size < 0 and enterShort1 or cambio_to_Short//[1]
//strategy.exit("CompraSL", comment_loss = "SL", stop = stop_loss_short)
tp1_short := strategy.opentrades.entry_price(strategy.opentrades - 1) - ((stop_loss_short - strategy.opentrades.entry_price(strategy.opentrades - 1))*ratio)
strategy.exit("CompraSL", comment_loss = "SL", stop = stop_loss_short, comment_profit = "tp1_short", limit = tp1_short, qty_percent = cerrar_en_tp)
//strategy.exit("CompraTP", comment_profit = "tp1_short", limit = tp1_short, qty_percent = cerrar_en_tp)
//strategy.exit("CompraSL", comment_loss = "SL", stop = stop_loss_short, qty_percent = 100)
//if close >= stop_loss_short//[1]
// strategy.close_all("close all")

if strategy.position_size > 0 and enterLong1 or cambio_to_Long//[1]
//strategy.exit("VentaSL", comment_loss = "SL", stop = stop_loss_long)
tp1_long := strategy.opentrades.entry_price(strategy.opentrades - 1) + ((strategy.opentrades.entry_price(strategy.opentrades - 1) - stop_loss_long)*ratio)
strategy.exit("VentaSL", comment_loss = "SL", stop = stop_loss_long, comment_profit = "tp1_long", limit = tp1_long, qty_percent = cerrar_en_tp)
//strategy.exit("VentaTP", comment_profit = "tp1_long", limit = tp1_long, qty_percent = cerrar_en_tp)
//strategy.exit("VentaSL", comment_loss = "SL", stop = stop_loss_long, qty_percent = 100)
//if close <= stop_loss_long//[1]
// strategy.close_all("close all")


//CONDICIONES PARA SALIR DE LONG
if UTsell
exitLong := true
motivo1 := "UTSell"

//CONDICIONES PARA SALIR DE SHORT
if UTbuy
exitShort := true
motivo2 := "UTbuy"

//SALIR DE OPERACIONES
if exitLong == true //and comprado == true
strategy.close("Compra", comment = motivo1)
comprado := false

if exitShort == true //and comprado == true
strategy.close("Venta", comment = motivo2)
comprado := false


Disculpen las líneas comentadas con // las dejé para que vean lo que ya intenté y no funcionó.

Por otro lado adjunto una imagen donde se ve una operación de compra de la que sale el 50% al tocar SL y el resto al cumplir otra condición por la cual tambien puede salir. El problema es que a veces esa segunda condición no aparece y hasta eso la operación se fue en contra con una perdida muy grande.

Les agradeceré mucho su ayuda.
Adjuntos
strategy fail.jpg
gu5tavo71
Mensajes: 27
Registrado: 12 Sep 2022 00:15
Contactar:

Re: TP 50% SL 100%

Mensaje por gu5tavo71 »

Hola

Tu codigo está mal posteado. Está mal indexado y es muy dificil analizar una cadena de iff
Podrias re-postearlo?
usa la herramienta code

Imagen
@gu5tavo71
Senior PineScript Developer
TradingView | Twitter | Freelancer | Telegram
Responder

Volver a “Pine Script”