繁体   English   中英

如何在 pine 脚本策略中每天在特定时间强制退出所有未平仓头寸?

[英]How do I force-exit all open positions at a particular time everyday in a pine script strategy?

我想知道是否有一种方法可以在每个交易日的特定时间在 pine 脚本上强制退出所有未平仓头寸。 我编写了以下代码,但它不会关闭我的未平仓交易,有时头寸会结转到第二天

strategy("mystrategy", overlay=true)
length = input(5)
numATRs = input(0.75)

from_day   = input(defval = 1,    title = "From Day",   minval = 1)
from_month = input(defval = 1,    title = "From Month", minval = 1)
from_year  = input(defval = 2020, title = "From Year",  minval = 1970)

to_day     = input(defval = 1,    title = "To Day",     minval = 1)
to_month   = input(defval = 1,    title = "To Month",   minval = 1)
to_year    = input(defval = 2100, title = "To Year",    minval = 1970)

time_cond = (time > timestamp(from_year, from_month, from_day, 00, 00)) and (time < timestamp(to_year, to_month, to_day, 23, 59))
atrs = sma(tr, length)*numATRs
sl_inp = input(0.5, title='Stop Loss %', type=input.float)/100
tp_inp = input(1, title='Take Profit %', type=input.float)/100
stop_level = strategy.position_avg_price * (1 - sl_inp)
take_level = strategy.position_avg_price * (1 + tp_inp)
if (time_cond and not na(close[length])and hour>9 and hour<15)
    strategy.entry("VltClsLE", strategy.long, stop=close+atrs, comment = "long")
    strategy.entry("VltClsSE", strategy.short, stop=close-atrs, comment = "short")
    strategy.exit("XL", from_entry = "VltClsLE", profit = take_level, loss = stop_level,comment="exit long")
    strategy.exit("XS", from_entry = "VltClsSE", profit = take_level, loss = stop_level,comment="exit short")
else
    strategy.cancel("VltClsLE")
    strategy.cancel("VltClsSE")
    strategy.cancel("XL")
    strategy.cancel("XS")
//plot(strategy.equity, title="equity", color=color.red, linewidth=2, style=plot.style_areabr)
strategy.close_all(when=(hour==14 and minute==30),comment="force exit")

在此处输入图像描述

例如,如果我想在周日下午 1 点离开

strategy.close("short", when =  hour == 13 and dayofweek == 1)

以下代码对我有用,测试了 TATASTEEL 1 年的 NSE 数据,没有 position 结转。 使用的图表是 5 分钟,所以显示的接近时间是 15:05

if(hour==15)
    strategy.close_all()

在此处输入图像描述

只有strategy.close_all对我有用:

exitPositions = time(timeframe.period, "1730-1900")
exitPositionsCondition = not na(exitPositions) 
strategy.close_all(when=exitPositionsCondition)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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