Checking first time a condition is met


0
0

Hello,

I have some thinkscript code that is looking at the value of a momentum oscillator across several time frames and will return a “1” if conditions are right to take a long entry and some other code that returns a “1” when time is right to exit.  I would like to plot a  Up Arrow on the plot when the condition first hits long entry criteria and another for the exit. Issue is the code returns 1 for long on multiple bars as those bars are also ideal for entry and I get multiple arrows…I would however like to see the arrow for only the first bar where this was true and then no longer see it until the exit criteria is met. Same for the exit criteria.

Is there a way to code this so once I get a long condition it does not plot an up arrow until the exit condition is met?

RESOLVED
Marked as spam
Posted by (Questions: 3, Answers: 1)
Asked on November 23, 2020 3:27 pm
346 views
1
Private answer

I found a solution on another forum that hopefully can help others here. The issue is simply this, if you are checking for multiple conditions to align to give you a signal and that signal will stay high for a while, here is an easy way to only show the signal for the first bar at which it exists, until the opposite down condition is triggered.

#def up = your condition 1;
#def down = your condition 2;
def up = close > open;
def down = close < open;

def Hold_Up_Value = if up then 1 else if !down then Hold_Up_Value[1] else 0;
def Hold_Dn_Value = if down then 1 else if !up then Hold_Dn_Value[1] else 0;

plot Signal_Up = if up and Hold_Dn_Value[1] == 1 then low else Double.NaN;
Signal_Up.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
plot Signal_Dn = if down and Hold_Up_Value[1] == 1 then high else Double.NaN;
Signal_Dn.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);

Editor's note: Thank you for taking the time to provide a solution that will serve our entire audience. The original solution could not be compiled because the first two lines did not contain valid syntax. The first two lines were meant to be replaced with specific conditions as desired by each user of this code. Many viewers are just barely learning to write code and they would not understand that, get frustrated and give up. So I commented out the first two lines, leaving them for reference. Then I added two new lines with valid syntax so the code can be copy/pasted without any changes and it will compile and work.

Marked as spam
Posted by (Questions: 3, Answers: 1)
Answered on November 23, 2020 5:18 pm
0
I have marked your solution as the best answer for this question. Thanks for taking the time to provide a solution to your question. I have made minor changes and explained that by the comments I appended to your solution.
( at November 24, 2020 9:33 am)
0
Private answer

There is no blanket solution to this that fits all cases. There is nothing in the settings either. Each case requires a custom solution and you did not provide any code so there is nothing we can do to assist you.

FYI, you will find many examples of what you are trying to do already posted here in this forum.

Marked as spam
Posted by (Questions: 37, Answers: 4079)
Answered on November 23, 2020 5:10 pm