Reset variable to false on a new day


Category:
0
0

Hello Sir is it possible to convert this pseudo code to general scan? Thank you.

  1. Set variable to false on new day,
  2. Set it to true when condition “example SMA9 crosses above SMA50” occurs, carry last value forward
  3. . Only alert when prior state was false. Or include in scan results while true.
  4. else carry last value forward

this way the scan will alert any stock only once per day, regardless of how many time the condition may be true?

Thank you.

Marked as spam
Posted by (Questions: 1, Answers: 1)
Asked on January 11, 2021 3:15 pm
217 views
0
Private answer

If you spend any amount of time browsing solutions on the forum you will find the techniques used to create this are about as common as sand on beach. Although up until now no one has requested this solution directly. Most often these requests take the form of "how to trigger condition only once per day". And rather than being a generic solution such as a moving average crossover the solutions include some custom chart study the viewer is trying to modify.

I see you have requested a custom scan, so I moved this question out of the "Alerts and Notifications" topic and into the "Stock Scanner" topic. This solution will not work as a Study Alert because it requires the use of recursion to carry the previous value foward.

The following code should accomplish what you requested.

def newDay = GetDay() <> GetDay()[1];
rec trackCondition = if newDay then 0 else if Average(close, 9) crosses above Average(close, 50) then 1 else trackCondition[1];
plot signal = trackCondition and !trackCondition[1];

Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on January 12, 2021 8:48 am
0
Hello Sir, Actually, I looked and did not manage to find a similar solution. Anyway, any reason why when we use the code in the scanner with recursion and without recursion, they give a different set of stocks? Thank you With Rec def newDay = GetDay() GetDay()[1]; rec trackCondition = if newDay then 0 else if Average(close, 9) crosses above Average(close, 50) then 1 else trackCondition[1]; plot signal = trackCondition and !trackCondition[1]; 2. without Rec def trackCondition = if Average(close, 9) crosses above Average(close, 50) then 1 else 0; plot signal = trackCondition;
( at January 13, 2021 4:14 pm)
0
The simple answer: It is because recursion is required in order to facilitate what you requested. If you remove the recursion it is not possible to complete the scan according to your specifications.
( at January 13, 2021 5:02 pm)
0
Then if stocks A, B, C, D, and E all met the condition in the recursion and none recursion scan, why only A, B, and D were alerted in the recursion scan? Thank you.
( at January 13, 2021 5:26 pm)
0
Sorry I really cannot afford to spend any more time on this post. The question you should be asking is this. Does the scan I present in my solution work? If it does, that is all I can afford to complete in this venue. I can only spend 15 min on each solution. If I don't hold to this limit I am homeless and starve to death. Please understand I must set limits on the free assistance I provide.
( at January 13, 2021 7:03 pm)
0
Thank you, Pete; I totally understand. How much it will cost me to find the answer. I am more than will to donate in advance. Please, email me. Thank you. Again thank you for your time, and sorry for any inconvenience.
( at January 13, 2021 7:26 pm)
0
You can submit a custom project request on the "About" page in this very website. I do my best not to advertise this because I already have more than enough projects to work on. Recursion is a very fundamental principle in all programming languages. Rather than pay me to teach you about this I suggest you just take some time to explore on your own. Thinkorswim developers do very good job of explaining how this works through the use of very basic examples. Check the following link: https://toslc.thinkorswim.com/center/reference/thinkScript/tutorials/Advanced/Chapter-10---Referencing-Historical-Data
( at January 13, 2021 8:21 pm)