pine-script - 在 strategy.exit 之后等待
问题描述
在策略被止损(退出)后,我想在我进入新交易之前等待。
我尝试使用barssince,但找不到满足最后一个出口的条件。
我希望在退出发生后等待 4 小时,然后再进行新的交易。
//@version=3
strategy("ABC", shorttitle="ABC", initial_capital=1000, commission_type = strategy.commission.percent, commission_value = 0.075, overlay = true, linktoseries = true)
nominal = 1000/close
SL = 0.0
SLold = nz(SL[1])
SL := max(max(max(strategy.position_avg_price,ohlc4)-atr(10)*3, strategy.position_avg_price * 0.96), SLold)
buy = cci(close,40)<50
testPeriodStart = timestamp(2018,2,1,0,0)
if time >= testPeriodStart
strategy.entry("Long", strategy.long, nominal, when = buy)
strategy.exit("Exit", "Long", stop = SL)
plot(SL, color = #006400)
解决方案
尝试这个。
//@version=3
strategy("ABC", shorttitle="ABC", initial_capital=1000, commission_type = strategy.commission.percent, commission_value = 0.075, overlay = true, linktoseries = true)
nominal = 1000/close
// create a variable with time of exit
timeExit = 0.0
timeExit := nz(timeExit[1])
msToHours(timeMs) =>
timeMs / 1000 / 60 / 60
isMoreThan4H() =>
msToHours(time - timeExit) >= 4
// detect the exit
if strategy.position_size < strategy.position_size[1]
timeExit := time
SL = 0.0
SLold = nz(SL[1])
SL := max(max(max(strategy.position_avg_price,ohlc4)-atr(10)*3, strategy.position_avg_price * 0.96), SLold)
buy = cci(close,40)<50
testPeriodStart = timestamp(2018,2,1,0,0)
if time >= testPeriodStart
strategy.entry("Long", strategy.long, nominal, when = buy and isMoreThan4H()) // Here added a check that is more than 4h since last exit
strategy.exit("Exit", "Long", stop = SL)
plot(SL, color = #006400)
推荐阅读
- excel - 为什么来自工作表的调用会减慢整个例程?
- sql - 当我运行 vba 宏时没有任何反应?
- python - 如何使用原始文件名保存裁剪的图像?
- c# - 两个返回字符串方法调用另一个方法
- java - 如何在其他 Scala 类中使用处理函数?
- java - Logstash-Logback 编码器:“IllegalAccessError:无法访问类 […]”
- python-3.x - 如何通过 mqtt 发送包含不同数据类型的消息?
- nvidia - cudnn 错误 :: CUDNN_STATUS_SUCCESS (1 vs. 0) CUDNN_STATUS_NOT_INITIALIZED
- html - 防止 div 内的图标折叠到其他 div 内
- blazor - 如何将只读集合传递给 Blazor 组件?