微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

自进入后特定天数后关闭交易

如何解决自进入后特定天数后关闭交易

正如标题所说......无论方向或走势如何,都在寻找一种方法关闭交易 XX 天后的交易。

目标,希望卖出 投币后大约 45 天,我想查看盈利百分比。

到目前为止我找不到任何好的参考资料,所以我真的没有为关闭/退出编写任何代码

谢谢!

解决方法

这未经过彻底测试,因此需要验证。使用日历日,并且应该在任何时间范围内工作:

//@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",when = maxTradeLengthReached)

// For validation.
plotchar(newEntry,"newEntry","X",location.bottom,size = size.tiny)
plotchar(maxTradeLengthReached,"maxTradeLengthReached","•",size = size.tiny)
plotchar(enterLong,"enterLong","▲",location.top,size = size.tiny)
plotchar(enterShort,"enterShort","▼",size = size.tiny)
plotchar(daysElapsed,"daysElapsed","",size = size.tiny)

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。