Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
843 views
in Technique[技术] by (71.8m points)

pine script - strategy.exit should be wait after strategy.entry

I have a script, this should be called every 5 minute only one time. And after strategy.entry should wait 30 second and check if it can be canceled.

My script looks so, but it does not work, as i wish.

 
//@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


Thanks in advance

Gül

question from:https://stackoverflow.com/questions/65643772/strategy-exit-should-be-wait-after-strategy-entry

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Thanks for your answer, i changed my code,

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   

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...