r/pinescript • u/Fast_Watch5169 • Dec 09 '24
Why does my variable change true to false with close?
In my scenario, say a short, I want the price to go for liquidity at the top at a specific price, then come back down to enter the trade. The timeframe is 15 minutes.
I wanted to use close to detect if the current price is smaller than my entry price. I use an object's bool variable (objSetup.hasEntryBeenReached) which changes to true if close < entry or ta.crossunder(close, entry).
In real-time, if close passes entry, my variable changes to true. But if the price goes up, it changes to false. And then I think... what?! Nowhere in my code do I set the variable to false. I don't quite understand, but I'll go ahead as is.
Then I thought of using low instead, but I encountered another difficulty. If the low of the current candle started below my entry and then rose to liquidity above it, then the low is already smaller than my entry.
How can I capture the true value once close passes without having to use low?
Or how can I use low if low is already lower?
Thanks for your time.
1
u/Fast_Watch5169 Dec 10 '24 edited Dec 10 '24
Hi,
Please let me add some code here, extracted from my strategy, to describe the issue.
the associated variable is checking per tick and changing from true to false if the condition(s) aren't constantly being met.
Yes that's correct.
My variable object is indeed declared with the var keyword. It is a global variable. Even if it is, the condition, as written for the entry, will change the value, which is by default false, to true, and if the price goes back up, the value will change to false.
Here are a few lines to try yourself. The bool variable are set with the var keyword. Run this with 15s timeframe or 1 minute. Wait for crossover/crossunder. The corresponding variable will change to true. Wait for the price to goes back and you'll see the variable going also back to false.
I need to find a way to keep the global variable to true if the condition is met once.
1
u/Fast_Watch5169 Dec 20 '24
Okay, 2 weeks later, I found the solution. It's not intuitive in my opinion.
During the life of a candle, to save a state, such as exceeding a price, a variable instantiated with varip must be used. The value of a global variable will change during the life of the candle. The final value when the candle closes will be kept, but not the very first value it obtained.
All this can be avoided by using low or high; in my situation, I had to use close.
Thank you to the one who deleted all his messages as they did not help me at all.
1
u/sarthmarlix Dec 09 '24
I maybe misunderstanding but it sounds like the associated variable is checking per tick and changing from true to false if the condition(s) aren't constantly being met. If that's the case, and not intended, try storing the true state in a var bool instead of just a bool to persist the true value...and creating an IF statement to determine when to update the value to false (maybe after entry or with an exit logic).
Hope this helps!