The indicator we are building is based on logic that needs certain levels to be broken. There are two types of price levels. This should happen first on CTF and then the same logic would be used on several HTFs. Because the same logic is needed for all TFs, we build a function that has, among other inputs, the previous bar levels that need to be broken. Using this function works fine on CTF, but once we use it as input in the "security" function nothing works properly anymore. Even if the outputs of the function differed from CTF to HTF, once the function was used as input in the security function, the resulting price levels from the HTF would overlap the ones from the CTF (it acts as if the security function is not calling the HTF).
Having these problems, we tried some simpler code to test what is happening when using custom-made functions as input for the security function. None of the situations that we could think off gave the expected results. We attached the code where we made these attempts to get the proper outputs from the security function.
What the simple code tries to do is to grab the first bar from a higher timeframe as it relates to the current timeframe.
Does anyone know a way around this limitation?
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
//@version=5
indicator("Test HTF", overlay = true)
sHtfGroup = "HTF Settings"
useHTF1 = input.bool(title='Use HTF', defval=true, group=sHtfGroup, tooltip='Check this if you want to use the higher time frame.')
htfRes1 = input.timeframe(title=' HTF Resolution', defval='5', group=sHtfGroup, tooltip='Choose the higher time frame.')
//1st try - DOESN'T WORK
firstbar0()=>
var barIndex = 0
if(barIndex == 0)
barIndex := bar_index
barIndex
//2nd try - DOESN'T WORK
var TheBarNumber1 = 0
firstbar1(barNumber)=>
barIndex = 0
if(barNumber == 0)
barIndex := bar_index
barIndex
//3rd try - DOESN'T WORK
var TheBarNumber2 = 0
firstbar2(barNumber)=>
var barIndex = 0
if(barNumber == 0)
barIndex := bar_index
barIndex
//4th try - DOESN'T WORK
IsFirstBar = false
if barstate.isfirst
IsFirstBar := true
firstbar3(firstBar)=>
barIndex = 0
if(firstBar)
barIndex := bar_index
barIndex
//5th try - DOESN'T WORK
HtfBarIndex = request.security(syminfo.tickerid, htfRes1, bar_index)
mn = 0
if barstate.isfirst
mn := HtfBarIndex
firstbar4(firstBar)=>
barIndex = 0
if(firstBar == bar_index)
barIndex := bar_index
barIndex
//6th try - DOESN'T WORK
TheTime = 0
if barstate.isfirst
TheTime := time
firstbar5(Time)=>
barIndex = 0
if(Time == time)
barIndex := bar_index
barIndex
//7th try - DOESN'T WORK
var IsFirstBar6 = false
if barstate.isfirst
IsFirstBar6 := true
else
IsFirstBar6 := false
firstbar6(firstBar)=>
barIndex = 0
if(firstBar)
barIndex := bar_index
barIndex
[HtfIsFirstBar, HtfFirstBarNumber0, HtfFirstBarNumber1, HtfFirstBarNumber2, HtfFirstBarNumber3, HtfFirstBarNumber4, HtfFirstBarNumber6, HtfBarNumber] = request.security(syminfo.tickerid, htfRes1, [barstate.isfirst, firstbar0(), firstbar1(TheBarNumber1), firstbar2(TheBarNumber2), firstbar3(IsFirstBar), firstbar4(mn), firstbar6(IsFirstBar6), bar_index])
plot(HtfIsFirstBar ? 1 : 0)
plot(HtfFirstBarNumber0)
plot(TheBarNumber1, color = color.red)
plot(HtfFirstBarNumber1, color = color.red)
plot(TheBarNumber2, color = color.lime)
plot(HtfFirstBarNumber2, color = color.lime)
plot(HtfFirstBarNumber3, color = color.purple)
plot(HtfFirstBarNumber4, color = color.fuchsia)
plot(HtfFirstBarNumber6, color = color.aqua)
plot(HtfBarNumber)
TheBarNumber1 := HtfFirstBarNumber1
TheBarNumber2 := HtfFirstBarNumber2
EDIT:
From the Telegram discussion with Fikira
Fikira, [9/6/2022 03:05] Either way, when you have a ticker with just 10 bars on the weekly, this would be 70 daily bars (continuous market), the idea that you would fetch the second W bar for example and place it on the daily, is not correct. The second daily bar is in the FIRST week, so you should not mix bar indexes between TF's, instead take a HTF bool, then calculate its place
Adrian T, [9/6/2022 03:08] thanks for the answer, we used this example as we have a rather larger function that we use in a security call. In this function we would need to store and reuse some values that we will need to stay the same until the next function call from the security. What we were trying to basically to do here is to find a way to store those values in the function during a security call
Fikira, [9/6/2022 03:09] Always best to put as much you can in a function first, then place that function in security call
Adrian T, [9/6/2022 03:13] thanks for the suggestion. So then how can we store data from one bar to another in a security function call if the var modifier doesn't hold it's value from one call to the next? (see example 1 in the code)
EDIT 2:
Let's replace barindex with high. The premise of this next function is to find the highest high of the chart but by storing the previous high and checking it against the current high
GetHTFHighestHigh()=>
var previousHigh = 0
if(high > previousHigh)
previousHigh := high
previousHigh
All this again in a higher timeframe, this still doesn't work. How can we make this function work?