'Var flag doesn't seem to be working, it keeps resetting
I'm trying to put together a var cross flag to determine if the price crosses above the previous days' high.
This is for a screener so further security requests will be added later once I have figured this part out, hence why it is in a function call.
The code I have so far is below; however, it will return true on the first 15m bar that it should, then rather than stay true until the reset at change of day, it resets on the next 15 minutes bar and resets to NaN.
What I want for it to do is stay true until the reset should happen which is a new day.
The reason for this is that I want a further condition to also become true (not yet wrote) and then when both conditions are set to true, output a final condition.
rblx3()=>
rblxh=request.security("CAPITALCOM:RBLX", "D",high[1])//high of previous day.
rblx15=request.security("CAPITALCOM:RBLX", "15",high)//current high
var cross_flag_high = false
if ta.change(dayofweek)!=0
cross_flag_high:=false
if ta.cross(rblx15,rblxh) and not(cross_flag_high)
cross_flag_high := true
Solution 1:[1]
Your'e using the var
keyword inside the scope of the function, and therefore it will restart on every function call.
You'll need to create global variable and set the global variable with the var
keyword. The problem is that functions can't set global variables, so we'll have to work around that.
You have 2 options I can think of:
Option 1 - the function will return the value of cross_flag_high, and the return value will be saved as a global variable:
rblx3()=>
rblxh=request.security("CAPITALCOM:RBLX", "D",high[1])//high of previous day.
rblx15=request.security("CAPITALCOM:RBLX", "15",high)//current high
var cross_flag_high = false
if ta.change(dayofweek)!=0
cross_flag_high:=false
if ta.cross(rblx15,rblxh) and not(cross_flag_high)
cross_flag_high := true
cross_flag_high
var cross_flag_high_global = rblx3()
Option 2 - using an array in order to work around this issue:
var rblx3_array = array.new_bool(1)
rblx3()=>
rblxh=request.security("CAPITALCOM:RBLX", "D",high[1])//high of previous day.
rblx15=request.security("CAPITALCOM:RBLX", "15",high)//current high
var cross_flag_high = false
if ta.change(dayofweek)!=0
cross_flag_high:=false
if ta.cross(rblx15,rblxh) and not(cross_flag_high)
cross_flag_high := true
array.set(rblx3_array, 0, cross_flag_high)
cross_flag_high_global = array.get(rblx3_array, 0)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | mr_statler |