正如标题所说......无论方向或走势如何,都在寻找一种方法来关闭交易 XX 天后的交易。
目标,希望卖出 投币后大约 45 天,我想查看盈利百分比。
到目前为止我找不到任何好的参考资料,所以我真的没有为关闭/退出编写任何代码。
谢谢!
答案 0 :(得分:0)
这未经过彻底测试,因此需要验证。使用日历日,并且应该在任何时间范围内工作:
//@version=4
strategy("Trade max n days", overlay=true, default_qty_type = strategy.percent_of_equity, default_qty_value = 10, precision = 6)
i_maxTradeLengthInDays = input(5)
// Issue long/short on bar up/dn and gap conditions.
enterLong = close > open and open > close[1]
enterShort = close < open and open < close[1]
if enterLong
strategy.entry("Long executed", strategy.long, stop = high)
if enterShort
strategy.entry("Short executed", strategy.short, stop = low)
// Detect trade length and close trade when no of days reached.
newEntry = change(strategy.position_size) and strategy.position_size != 0
var int lastEntryTime = na
if newEntry
lastEntryTime := time
else if strategy.opentrades == 0
lastEntryTime := na
daysElapsed = (time_close - lastEntryTime) / (24 * 60 * 60 * 1000)
maxTradeLengthReached = daysElapsed >= i_maxTradeLengthInDays
strategy.close("Long executed", comment = "Max Days", when = maxTradeLengthReached)
strategy.close("Short executed", comment = "Max Days", when = maxTradeLengthReached)
// For validation.
plotchar(newEntry, "newEntry", "X", location.bottom, size = size.tiny)
plotchar(maxTradeLengthReached, "maxTradeLengthReached", "•", location.bottom, size = size.tiny)
plotchar(enterLong, "enterLong", "▲", location.top, size = size.tiny)
plotchar(enterShort, "enterShort", "▼", location.top, size = size.tiny)
plotchar(daysElapsed, "daysElapsed", "", location.top, size = size.tiny)