[check image first ] https://i.stack.imgur.com/LEHZr.jpg
I want to check if marked candle is 60 % inside the zone (it is more than that).
I have OHLC values of candle and upper and lower level of zone.
open = 868.72 high = 871 low = 868.72 close = 870.74 upper_level = 870 lower_level = 868
How to do it . I just don’t know how to start?
The question is not written good enough to be answered completely. It must contain small examples to be reproduced by the proposed codes to check and evaluate. However, I create a sample and hope to be representive. We can work with the desired pairs i.e. open and close (candle bodies) or low and high (candle shadows); Using the first pair (assuming you are seeking about candle’s bodies):
open = np.array([70, 30, 100, 100, 30, 10], dtype=np.float64) close = np.array([100, 10, 50, 150, 100, 200], dtype=np.float64) upper_level = np.array(, dtype=np.float64) lower_level = np.array(, dtype=np.float64) desired_percent = 0.6 # desired body containing related percentage
This problem can be solved by python loops on all candles and check the conditions; But, as I think, this problems must be handled by arrays (due to large input volumes) in terms of performance and efficiency, so I propose the following
NumPy aided solution.
- At first, we must determine which of the elements (element-wise) in open and close are bigger or smaller.
- Also, a Boolean array is created with
Falsefilled value, which will be filled by
Trues if candles satisfy the specified condition.
- Due to the large volume of data, we need to remove extra candles (that are placed out of the limit zone (situation 1)).
For this problem, I consider 5 different situations:
- candles completely outside the limit zone
- candles completely inside the limit zone
- candles containing the limit zone (the limit zone is part of the candle body)
- candles which just their top is in the limit zone
- candles which just their bottom is in the limit zone
resulted_masked_array = np.full(shape=open.shape, fill_value=False, dtype=bool) top = np.maximum(open, close) # top of the candles bottom = np.minimum(open, close) # bottom of the candles level_height = upper_level - lower_level # reducing the arrays by removing candles outside the limit zone btm_upper_up = np.greater_equal(bottom, upper_level) # if candle is upper the limit zone top_lower_down = np.less_equal(top, lower_level) # if candle is under the limit zone candle_complete_outside = np.logical_or(btm_upper_up, top_lower_down) candle_not_complete_outside = np.invert(candle_complete_outside) top = top[candle_not_complete_outside] bottom = bottom[candle_not_complete_outside] candles_height = top - bottom # if limit zone length be a part of candle body --> needs to check if the zone height >= the desired percentage of the candles' body btm_lower_down = np.less_equal(bottom, lower_level) top_upper_up = np.greater_equal(top, upper_level) percentage_check = (level_height / candles_height) >= desired_percent # if just candle bottom be in the limit zone btm_upper_down = np.greater_equal(bottom, lower_level) btm_lower_up = np.less_equal(bottom, upper_level) up_to_btm = upper_level - bottom percentage_check_utb = (up_to_btm / candles_height) >= desired_percent # if just candle top be in the limit zone top_upper_down = np.greater_equal(top, lower_level) top_lower_up = np.less_equal(top, upper_level) top_to_down = top - lower_level percentage_check_ttd = (top_to_down / candles_height) >= desired_percent candle_complete_inside = np.logical_and(btm_upper_down, top_lower_up) body_contain_limits = np.logical_and.reduce((btm_lower_down, top_upper_up, percentage_check)) btm_contained_limits = np.logical_and.reduce((btm_upper_down, btm_lower_up, percentage_check_utb)) top_contained_limits = np.logical_and.reduce((top_upper_down, top_lower_up, percentage_check_ttd)) result = np.logical_or.reduce((candle_complete_inside, body_contain_limits, btm_contained_limits, top_contained_limits)) # filling the main False filled Boolean array by Trues (for satisfied conditions) resulted_masked_array[candle_not_complete_outside] = result # resulted_masked_array --> [ True False True False True False]
The results needs to be checked by real inputs; Here, I have checked them by a small created example.
Answered By – Ali_Sh
Answer Checked By – David Marino (BugsFixing Volunteer)