简体   繁体   中英

About limit of strategy.exit

I'm making a DCA strategy using pyramids.

I have used strategy.close_all command to close positions so far.

But this brings me a risk as the next candle of the signal will be alerted.

So I use strategy.exit command. Here's a problem.

Because I make a pyramiding strategy, multiple orders are opened in one transaction, so when I use the strategy.exit command, an alert is generated as much as the number of opened orders.

Is there a way to receive an alert only once when the limit value corresponding to the condition is reached while using strategy.exit?

Recently, I tried to get only one alert using plot, but there was a problem.

Please advise.


// STOCHASTIC RSI //

src = input(close, title="RSI Source", group='RSI SETTINGS')
smoothK = input.int(3, "K", minval=1, group='RSI SETTINGS')
smoothD = input.int(3, "D", minval=1, group='RSI SETTINGS')
lengthRSI = input.int(14, "RSI Length", minval=1, group='RSI SETTINGS')
lengthStoch = input.int(14, "Stochastic Length", minval=1, group='RSI SETTINGS')
rsi1 = ta.rsi(src, lengthRSI)
OverBought = input.int(80, "OverBought", minval=1, group='RSI SETTINGS')
OverSold = input.int(20, "OverSold", minval=1, group='RSI SETTINGS')
k = ta.sma(ta.stoch(rsi1, rsi1, rsi1, lengthStoch), smoothK)
d = ta.sma(k, smoothD)

co = ta.crossover(k,d)
cu = ta.crossunder(k,d)

r_close = request.security(syminfo.tickerid, str.tostring(tic), close, lookahead=barmerge.lookahead_off)

last_entry_price = strategy.opentrades.entry_price(strategy.opentrades - 1)
last_entry_size  = strategy.opentrades.size(strategy.opentrades - 1)

next_price_long  =  last_entry_price * (1 - min_step) 
next_price_size  =  last_entry_size * (1 + martingale)

TP               = strategy.position_avg_price * (1 + min_profit)

// STRATEGY ORDER //

if (afterStartDate)
    if (na(strategy.position_avg_price)) and (co and k < OverSold)
        strategy.order("Long", strategy.long, qty=base_order/r_close, limit=r_close, comment="Entry")
        
    if strategy.position_size > 0 and r_close < next_price_long and strategy.opentrades < max_order
        strategy.order("Long", strategy.long, qty=next_price_size, limit=r_close, comment="Step_" + str.tostring(strategy.opentrades + 1))
        
strategy.exit("Close Long", "Long", limit = TP, comment="close long")

plot(TP, style=plot.style_cross, linewidth=6, color=color.new(color.fuchsia, 0), editable=false)

You could make custom a condition

var bool is_TP_reached = false

if high >= TP
   is_TP_reached := true

if ta.change(is_TP_reached)
   strategy.exit("Close Long", "Long", limit = TP, ...)

// If short or any other condition to reset is_TP_reached
if strategy.position_size == 0
   is_TP_reached := false

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM