Store a scan result in a variable and for resue


Category:
0
0

Hello Pete, Thank you for your great service, we really appreciate it. I have a question, which I really hope that you can help me answer. So, let’s say we have a scanner x that retrieves the close price of the current bar if the conditions were met. Is it possible to store that close price in a variable that we can use for comparison when the condition is met again in the future? For example,

def signal = if condition then close
def storedSgnl =singal;

def compare = if signal<storedSignal then 1 else 0;

This way, if the current scanner result is greater than the old scanner result, we would not get alerted. Please, accept my apology if I am not clear enough. I really appreciate any help you can provide.

 

Marked as spam
Posted by (Questions: 1, Answers: 1)
Asked on April 22, 2021 2:07 pm
65 views
1
Private answer

There are examples of this technique all over this Q&A Forum. But I can appreciate the concept of having a template that does this so folks inclined to write their code are able to adapt it to their own needs. The technique is called "Recursion". This is when a variable derives its current value based on its own value from a previous bar on the chart.

Here is the basic template, adapted from the example code you provided:

def signal = close crosses above Average(close, 50);
rec storedValue = if signal then close else storedValue[1];
plot checkValues = if signal then close < storedValue[1];
checkValues.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);

Explanation:

  1. Variable named "signal" is true when the close crosses above the 50 period simple moving average.
  2. Variable named "storedValue" checks each bar to see if "signal" is true and if so it captures the close of that bar and carries that value forward until it encounters another bar in which "signal" is true. This is the recursive element required to store a value for future use.
  3. Variable named "checkValues" plots an arrow when a it finds a new bar in which "signal" is true, but also checks of the close of the correct signal bar is less than the storedValue of the previous bar. We must use the [1] index with the storedValue variable because its value is also being reset on this current bar.

Screenshot shows the results. I have included some trend lines on there connecting each of the values the code is using in the comparison.

If you really want to learn how this works you will add the following line to the code so you see how the stored value is captured and carried forward, and more importantly when that value is updated:

plot test1 = storedValue;

Marked as spam
Posted by (Questions: 37, Answers: 4079)
Answered on April 22, 2021 3:43 pm
0
Hello Pete, Thank you very much. I really appreciate it. We want to use this strategy in a scanner, will the ”rec” be a problem? Thank you. Again
( at April 23, 2021 8:21 am)
0
The vast majority of examples of this technique are posted in the Stock Scanners topic. Take some time to browse existing solutions in the Stock Scanner topic and you will begin to see them almost everywhere you look.
( at April 23, 2021 10:56 am)
0
Great solution; thank you very much. I really really appreciate it as it was the missing piece of our strategy. Thank you again, and will donate.
( at April 23, 2021 12:03 pm)
0
Thanks but we discourage folks from using the word "donate". Hahn-Tech, LLC is a for-profit company and we do not solicit nor do we accept "donations". Solutions posted in the Q&A Forum are completely free and are not provided in return for a "voluntary contribution". But thanks for your contribution and your participation in this forum. It is very much appreciated.
( at April 23, 2021 1:05 pm)