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



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

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

LongExit变量依赖于ta.crossunder(close, out13EMA)。此条件将在每个刻度上计算,因为这是一个带有calc_on_every_tick=truestrategy。在横盘期间(横盘仍然打开,尚未关闭(,一旦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

它的工作原理你可以在这里看到

最新更新