首页 > 解决方案 > strategy.exit 应该在 strategy.entry 之后等待

问题描述

我有一个脚本,应该每 5 分钟调用一次。在 strategy.entry 之后应该等待 30 秒并检查是否可以取消。

我的脚本看起来如此,但它不起作用,如我所愿。

 
//@version=4
strategy("My Strategy", overlay=true)


// create a variable with time of exit
timeLong = 0.0
timeLong := nz(timeLong[1])

msToHours(timeMs) =>
    timeMs / 1000 

isMoreThan() =>
    msToHours(time - timeLong) >= 30

// detect the exit
if strategy.position_size < strategy.position_size[1]
    timeLong := time
    
if (time >  timestamp(2021, 1, 9, 00, 00) )
    strategy.entry("Long Entry", strategy.long, comment="Long")


// === STRATEGY RISK MANAGEMENT EXECUTION ===
// finally, make use of all the earlier values we got prepped
if  isMoreThan()
    strategy.close("Long Entry", comment="Exit")
    timeLong := na


提前致谢

居尔

标签: pine-script

解决方案


感谢您的回答,我更改了我的代码,

Thanks for your answer, i changed my code, 
//@version=4
strategy("My Strategy", overlay=true, calc_on_every_tick = true)


// create a variable with time of long start
timeLong = 0.0
timeLong := nz(timeLong[1])

msToSeconds(timeMs) =>
    timeMs / 1000 

isMoreThan() =>
    msToSeconds(timenow - timeLong) >= 30

if (timenow >  timestamp(2021, 1, 9, 00, 00) ) and strategy.position_size == 0
    strategy.entry("Long Entry", strategy.long, comment="Long")
    timeLong := timenow


   
if  isMoreThan() and strategy.position_size > 0
    strategy.close("Long Entry", comment="Exit")
    timeLong := na   

但它不像我希望的那样工作,它不会在买卖之间等待 30 秒。

我从我的警报中向您发送屏幕截图,

来自警报的屏幕截图


推荐阅读