pine-script - 如何在 pine 脚本策略中每天在特定时间强制退出所有未平仓头寸?
问题描述
我想知道是否有一种方法可以在每个交易日的特定时间在 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)
推荐阅读
- html - 我的 HTML 链接不会将我带到我的其他 HTML 文件
- sql - SQL:固定行和行号计算
- regex - 在 macOS 上使用 Perl 在 bash 中匹配多行模式
- pandas - 就地删除蒙面熊猫多索引切片似乎不起作用
- jenkins - 使用类加载器 AntClassLoader 找不到 Ant CompileTask
- sails.js - 如何在 Sails.js 中访问不同控制器的模型属性?
- google-app-engine - Google Cloud 中的不同 App Engine 应用程序
- agora.io - Agora SDK 与 Safari 的兼容性 - macos 和 ios
- c# - 从列表中选择数据
其中类属性是 List 并在 DataTable 中显示结果 - sql-server - 在打开查询中强制转换 DB2 列以接受多语言字符