0

//I am trying to close a position with more than one exit but multiple exits seem not working.

//If I comment the first exit the other works and viceversa.

//I can't understand why they don't work together:

//Sell Condition 2 and //Sell Condition 3 Can't work together ??? if commented they work well. // If you find a solution please help me understand the problem :)

//@version=5

strategy("Cancellare Bob Qta", overlay=true)

// Impostazioni delle bande di Bollinger
length = input.int(20, title="BB Length")
mult = input.float(2.0, title="BB StdDev")

// Calcolo delle bande di Bollinger
source = close
basis = ta.sma(source, length)
dev = mult * ta.stdev(source, length)
upper = basis + dev
lower = basis - dev

plot_basis = plot(basis, color = color.orange)
plot_upper = plot(upper, color = color.blue)
plot_lower = plot(lower, color = color.blue)
fill(plot_upper, plot_lower, color = color.blue, transp = 90)

// Variabili di strategia
risk_reward_ratio = input.float(40, title="Risk-Reward Ratio %")
stop_loss_price_distance = input.float(20, title="stop_loss_price_distance")
budget = input.float(5000, title="budget") // Set the desired budget here
buy_price = 0.0
stop_loss_price = 0.0
exit_BB_percent = input.bool(false, title="exit_BB_percent")

// Calcolo del numero di azioni da comprare
shares_to_buy = budget / close

// Condizioni di acquisto
buy_condition = ta.crossover(source, lower)

// Condizioni di vendita
sell_condition1 = ta.crossunder(source, upper)

// Buy Condition
if buy_condition
    buy_price := source
    strategy.entry("Buy", strategy.long, qty=shares_to_buy)

// Sell Condition 1
if sell_condition1
    strategy.close("Buy", comment="sell_condition1")

//Sell Condition 2
if strategy.opentrades != 0 //and not exit_BB_percent
    stop_loss_price := strategy.opentrades.entry_price(0) - stop_loss_price_distance
    strategy.exit("sell_condition2", from_entry = "Buy", stop=stop_loss_price)

//Sell Condition 3
var stop_2 = 0.0

if (strategy.opentrades[1] == 0 and strategy.opentrades == 1) //and exit_BB_percent
    stop_2 := strategy.opentrades.entry_price(0) - (upper - lower) / 100 * risk_reward_ratio
    strategy.exit("sell_condition3", from_entry = "Buy", stop=stop_2)

if strategy.opentrades == 0
    stop_2 := 0

plot(stop_loss_price, title="stop_loss_price", color=color.purple)
plot(stop_2, title="stop_2", color=color.red)
plot((upper - lower), title="distance")
plot(strategy.opentrades.entry_price(0), title="entry_price")
plot((upper - lower) / 100 * risk_reward_ratio , title="range stop %")
plot(strategy.opentrades)

//Sell Condition 2 and //Sell Condition 3 Can't work together ??? if commented they work well. // If you find a solution please help me understand the problem :)

1 Answers1

0

You try to create multiple stops for the same contracts. If you use half the quantity for the first and half for the second then it will likely work.

It only creates exits until there are no contracts to be accounted for anymore. The same goes for TP's.

You need to use strategy.order to solve this.

https://www.tradingview.com/pine-script-docs/en/v5/concepts/Strategies.html#strategy-oca-reduce