2 years ago
#69976

Marcin Kapusta
How to write function that will compile without warnings, Exectued conditionally with close and open vars
In my code I'm collecting and calculating some values across all bars and at the end I have such condition:
if barstate.islast
float tosMin = // get it from calculated data
float tosMax = // get it from calculated data
int periodToCheck = // get if from calculated data
bool someOldState = // get it from calculated data
bool newState = someOldState
for period=periodToCheck to 1
if open[period] < tosMin or close[period] < tosMin
newState := false
if open[period] > tosMax or close[period] > tosMax
newState := true
// real-time bar
if barstate.isnew // when last bar opens
if open < tosMin
newState := false
if open > tosMax
newState := true
if barstate.isconfirmed // when last bar closes
if close < tosMin
newState := false
if close > tosMax
newState := true
// do something with newState
As You can see I have some logic inside barstate.islast
condition scope. What I would like is to rewrite this logic inside scope to function that will receve on the input: tosMin, tosMax, periodToCheck and someOldState and calculate newState but when I try to write such function I have always warning like this:
The function nameOfTheFunction should be called on each calculation for consistency. It is recommended to extract the call from this scope.
The reason why I would like to rewrite it is because I have in many places such logic that is calculated during barstate.islast
condition.
Any ideas how to make this logic reusable because it seems functions is not good way to do it?
So far I tried to do something like this:
// calculating data on every bar
f_reusableFunction(_tosMin, _tosMax, _periodToCheck, _oldState) =>
bool newState = _oldState
for period=_periodToCheck to 1
if open[period] < _tosMin or close[period] < _tosMin
newState := false
if open[period] > _tosMax or close[period] > _tosMax
newState := true
// real-time bar
if barstate.isnew // when last bar opens
if open < _tosMin
newState := false
if open > _tosMax
newState := true
if barstate.isconfirmed // when last bar closes
if close < _tosMin
newState := false
if close > _tosMax
newState := true
newState
if barstate.islast
float tosMin = // get it from calculated data
float tosMax = // get it from calculated data
int periodToCheck = // get if from calculated data
bool someOldState = // get it from calculated data
bool newState = f_reusableFunction(tosMin, tosMax, periodToCheck, someOldState) // here I have warning
// do something with newState then reuse many times f_reusableFunction with different input parameters and periods calculated based on data collected in main script context.
pine-script
trading
0 Answers
Your Answer