[英]sleep/pause function in Pinescript
I have a question about pine script.我有一个关于松脚本的问题。 I need a sleep/pause function, I need this because I have a tradingbot that has a delay of 1 sec.
我需要一个睡眠/暂停 function,我需要这个因为我有一个延迟 1 秒的交易机器人。 As in when tradingview sends the webhook notification it takes the bot 1 sec to buy on my broker.
当 tradingview 发送 webhook 通知时,机器人需要 1 秒才能在我的经纪人处购买。 Because of this the results tradingview gives me from the past year are not accurate, and I want more accurate results.
因此,tradingview 给我的过去一年的结果并不准确,我想要更准确的结果。 You can ignore the strategy code, that's pure to make it more specific what I want.
你可以忽略策略代码,这纯粹是为了让它更具体我想要什么。
//@version=4
strategy("xx")
emaLong = ema(close, 50)
emaShort = ema(close, 20)
longCondition = crossover()
shortCondition = crossover()
if longCondition
sleep/pause of 1 sec <<<<<<<<<<<<<<<<<< That's where I want a short pause.
strategy.entry()
if shortCondition
sleep/pause of 1 sec <<<<<<<<<<<<<<<<<< That's where I want a short pause.
strategy.entry()
I don't think this can be done, because Pine processes bar per bar, and maximum execution time per bar is limited to 200ms (judging from a max loop time error I got).我不认为这是可以做到的,因为 Pine 处理每根柱子的柱子,并且每根柱子的最大执行时间限制为 200 毫秒(根据我得到的最大循环时间错误判断)。
The code below is an attempt at waiting 1 second, but can't get it working due to the mentioned 200ms error, but I'll leave it here for you to fiddle with it.下面的代码是尝试等待 1 秒,但由于提到的 200 毫秒错误而无法使其正常工作,但我会把它留在这里供您摆弄。
//@version=4
study("xx", overlay=true)
var int[] arr = array.new_int(na)
var int t = na
if barstate.islast and barstate.isconfirmed
t := timenow
array.push(arr, t)
for i = 0 to 5000000
if timenow - t >= 1000 // 1000ms = 1 second
array.push(arr, t)
break // get out of loop
label.new(bar_index, high, "#elements=" + tostring(array.size(arr)))
plot(na)
//@version=4
strategy("xx")
emaLong = ema(close, 50)
emaShort = ema(close, 20)
longCondition = crossover()
shortCondition = crossover()
var wait_time := 0
if wait_time>0
wait_time = wait_time - 1
if longCondition
wait_time = 0
strategy.entry()
if shortCondition
wait_time = 24
strategy.entry()
I am thinking somethign like this.我在想这样的事情。 Of course, this doesn't work.
当然,这是行不通的。 But it could be something that someone may further edit.
但这可能是某人可以进一步编辑的内容。
Try this code:试试这个代码:
//@Version=5
// Define the number of bars to wait:
barsToWait = 10
// Declare a variable to keep track of the last traded bar:
lastTradeBar = 0
// Get the current bar index:
currentBar = bar_index
// Check if the number of bars passed is greater than the number of bars to wait:
if currentBar > lastTradeBar + barsToWait
// If it is, then open a trade
strategy.entry("My Long Entry", strategy.long)
lastTradeBar := currentBar
You can also create the barstoWait as an input, then you can quickly change it on the fly while testing strategies.您还可以创建 barstoWait 作为输入,然后您可以在测试策略时即时快速更改它。 I am using it like this and it works.
我像这样使用它并且它有效。
Like this:像这样:
barsToWait = input.int(2,"Bars to Wait for Trades",1,20,1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.