[英]sleep/pause function in Pinescript
我有一個關於松腳本的問題。 我需要一個睡眠/暫停 function,我需要這個因為我有一個延遲 1 秒的交易機器人。 當 tradingview 發送 webhook 通知時,機器人需要 1 秒才能在我的經紀人處購買。 因此,tradingview 給我的過去一年的結果並不准確,我想要更准確的結果。 你可以忽略策略代碼,這純粹是為了讓它更具體我想要什么。
//@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()
我不認為這是可以做到的,因為 Pine 處理每根柱子的柱子,並且每根柱子的最大執行時間限制為 200 毫秒(根據我得到的最大循環時間錯誤判斷)。
下面的代碼是嘗試等待 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()
我在想這樣的事情。 當然,這是行不通的。 但這可能是某人可以進一步編輯的內容。
試試這個代碼:
//@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
您還可以創建 barstoWait 作為輸入,然后您可以在測試策略時即時快速更改它。 我像這樣使用它並且它有效。
像這樣:
barsToWait = input.int(2,"Bars to Wait for Trades",1,20,1)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.