平仓超时时间(类似止损超时时间)

Timeout for closing the position (similar to stop loss timeout)

请看下面的脚本。我想为 LongExit 变量创建一个超时函数。这将类似于 3Commas 的止损超时功能(参见下面的 link):

https://help.3commas.io/en/articles/3108979-stop-loss-timeout

LongExit 变量取决于 ta.crossunder(close, out13EMA)。此条件将在每次报价时计算,因为这是 strategycalc_on_every_tick=true。在柱线期间(柱线仍处于打开状态,尚未关闭),一旦 LongExit 条件为真(收盘价交叉 EMA 13),这不会立即触发 LongExit。相反,超时功能(以秒为单位)将生效,(例如,timeout=input.int(defval="60", title="timeout in seconds"),它将延迟 LongExit 60 秒。

如果 LongExit 条件在 60 秒后仍然有效,多头仓位将被平仓。

如果 LongExit 条件在 60 秒后不再有效(无效),多头头寸将不会平仓。

如何添加这样的超时功能?

谢谢!

//@version=5
strategy(title="Stop Loss Timeout", shorttitle="SL Timeout", overlay=true, pyramiding=0, calc_on_every_tick=true, close_entries_rule='ANY')



//////////////////////
//////  EMA //////////
//////////////////////


//EMA 5

len5EMA = input.int(defval=5, minval=1, title="EMA 5", group="EMA")
src5EMA = input(close, title="Source EMA 5")
out5EMA = ta.ema(src5EMA, len5EMA)
plot(out5EMA, title="EMA 5", color=color.green, linewidth=2)

//EMA 13

len13EMA = input.int(defval=13, minval=1, title="EMA 13", group="EMA")
src13EMA = input(close, title="Source EMA 13")
out13EMA = ta.ema(src13EMA, len13EMA)
plot(out13EMA, title="EMA 13", color=color.red, linewidth=2)


//EMA 50

len50EMA = input.int(defval=50, minval=1, title="EMA 50", group="EMA")
src50EMA = input(close, title="Source EMA 50")
out50EMA = ta.ema(src50EMA, len50EMA)
plot(out50EMA, title="EMA 50", color=color.teal, linewidth=2)

////////////////////////////////
/// ENTRY AND EXIT CONDITIONS //
////////////////////////////////

LongEntry = ta.crossover(out5EMA, out13EMA)
LongExit = ta.crossunder(close, out13EMA)

ShortEntry = ta.crossunder(out5EMA, out13EMA)
ShortExit = ta.crossover(close, out13EMA)


// Long Entry and Long Exit with SL and TP

// Long Entry

if (strategy.position_size == 0 and strategy.closedtrades == strategy.closedtrades[1])   // Only 1 long entry per bar (similar to alert.freq_once_per_bar)
    if LongEntry
        strategy.entry(id="LE", direction=strategy.long)


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////
//// Similar to 3commas stop loss timeout
////
//// https://help.3commas.io/en/articles/3108979-stop-loss-timeout
////
//// How could I add a timeout for long exit. For example, 'LongExit' variable will be calculated on every tick. 
//// Then, once 'LongExit' becomes true (=ta.crossunder(close, out13EMA), we will wait for 60 seconds before closing the long position.
//// If after 'LongExits' becomes invalide after 60 seconds waiting time, then the long position will not be closed. 
//// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// Long Exit

if LongExit
    strategy.close(id="LE")



// Short Entry and Short Exit with SL and TP


// Short Entry

if (strategy.position_size == 0 and strategy.closedtrades == strategy.closedtrades[1])   // Only 1 short entry per bar (similar to alert.freq_once_per_bar)
    if ShortEntry
        strategy.entry(id="SE", direction=strategy.short)


// Short Exit

if ShortExit
    strategy.close(id="SE")

这可能有帮助:

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

//@version=5
strategy("The strategy example. Close position by timeout", overlay=true, margin_long=100, margin_short=100, process_orders_on_close=true)

longCondition = ta.crossover(ta.sma(close, 14), ta.sma(close, 28))
if (longCondition)
    strategy.entry("My Long Entry Id", strategy.long)

shortCondition = ta.crossunder(ta.sma(close, 14), ta.sma(close, 28))
if (shortCondition)
    strategy.entry("My Short Entry Id", strategy.short)

closePositionAfter(timeoutS)=>
    if strategy.opentrades > 0
        for i = 0 to strategy.opentrades-1
            if time - strategy.opentrades.entry_time(i) >= timeoutS*1000
                entry = strategy.opentrades.entry_id(i)
                strategy.close(entry, comment = str.format("Close \"{0}\" by timeout {1}s", entry, timeoutS))
                
closePositionAfter(120) // close position after 120 sec

你可以看到它是如何工作的here