Sorry but I have read this statement several times and I cannot understand how this gets computed:
difference between open vs high / low is not greater than 2%
You will need to provide an actual mathematical formula. If I interpret your statement literally this is what I end up with:
open - (high / low)
Let's take an example from yesterdays daily candle of AAPL:
124.37 - (124.98 / 123.09) = 123.35
Obviously this is not correct. You need to provide the math to compute this so-called "tight price range".
Edit: In the comments section below we now have a full set of specifications for how to compute a stock that has a "tight range". Here is the basic description of how this should be computed for this particular request. "for example AAPL is open at 125$ and its high for the day is 127$ and low for the day is 123$ then AAPL will get selected since it meets the 2% range i.e. (127-125)/125 = 1.6% for high and (125-123)/125 = 1.6% for low which is below or equal to 2% range".
So we need to compute this value twice and the code must check that both values are within the specificied amount. I have included two user inputs to allow folks to adjust the percent thresholds to suite their own needs.
The volume portion of the reuqest will not be included in this solution because it is already provided as a built-in stock filter on Thinkorswim Stock Hacker.
I blieve this should do the trick but I have not taken the time to test it.
input percentThresholdHigh = 2.0;
input percentThresholdLow = 2.0;
def range = high - low;
def percentHighFromOpen = 100 * (high - open) / open;
def percentLowFromOpen = 100 * (open - low) / open;
plot scan = percentHighFromOpen < percentThresholdHigh and percentLowFromOpen and percentThresholdLow;