// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ // © ak_trading_ //@version=5 strategy("Ionut MTF V2", overlay = true, max_bars_back = 5000, max_boxes_count = 5000, max_labels_count = 500) htf = input.timeframe(defval = "1", title = "HTF") show_snd = input.bool(defval = true, title = "Show HTF SnDs") show_ltf = input.bool(defval = true, title = "Show LTF Levels") show_htf = input.bool(defval = true, title = "Show HTF Levels") show_market_structure = input.bool(defval = true, title = "Show HTF Market Structure") type SND int index1 int index2 float price1 float price2 bool bearish int break_index int time1 int time2 //****************** High and Low Levels Formation ****************** opposite_level_low(open_, high_, low_, close_, bar_index_) => bi = 0 s = array.size(open_) for i = 0 to s-2 if (array.get(open_,i) < array.get(low_, i+1)) or (array.get(close_,i) < array.get(low_, i+1)) bi := array.get(bar_index_, i) break if array.get(bar_index_, 0) > 5 lowest = 999999.0 lowest_barindex = 0 for i = 0 to (array.get(bar_index_, 0) - bi) lowest := math.min(lowest, array.get(low_, i)) if array.get(low_, i) <= lowest lowest_barindex := array.get(bar_index_, i) [lowest, lowest_barindex] else [0, array.get(bar_index_, 0)] opposite_level_high(open_, high_, low_, close_, bar_index_) => bi = 0 s = array.size(open_) for i = 0 to s-2 if (array.get(open_,i) > array.get(high_, i+1)) or (array.get(close_,i) > array.get(high_, i+1)) bi := array.get(bar_index_, i) break if array.get(bar_index_, 0) > 5 highest = 0.0 highest_barindex = 0 for i = 0 to (array.get(bar_index_, 0) - bi) highest := math.max(highest,array.get(high_, i)) if array.get(high_, i) >= highest highest_barindex := array.get(bar_index_, i) [highest, highest_barindex] else [0, array.get(bar_index_, 0)] mitigation_bullish(open_, high_, low_, close_, bar_index_, index) => found = false found_index = index if index < array.get(bar_index_, 0) - 1 for j = index to (array.get(bar_index_, 0)) if array.get(bar_index_, 0) - j < 0 break if not found snd_high = array.get(high_ , array.get(bar_index_, 0) - j) snd_low = array.get(low_, array.get(bar_index_, 0) - j) for i = j + 1 to (array.get(bar_index_, 0)) if array.get(bar_index_, 0) - i - 1 < 0 break if array.get(high_, array.get(bar_index_, 0) - i) <= snd_high and array.get(low_, array.get(bar_index_, 0) -i) >= snd_low continue if array.get(close_, array.get(bar_index_, 0) -i) > snd_high and array.get(low_, array.get(bar_index_, 0) - i -1) > snd_high cond = true for k = i + 2 to array.get(bar_index_, 0) if array.get(bar_index_, 0) - k >= 0 if array.get(low_, array.get(bar_index_, 0) - k)<= snd_high cond := false if cond found := true found_index := j break [found_index, found] mitigation_bearish(open_, high_, low_, close_, bar_index_, index) => found = false found_index = index if index < array.get(bar_index_, 0) - 1 for j = index to (array.get(bar_index_, 0)) if array.get(bar_index_, 0) - j < 0 break if not found snd_high = array.get(high_, array.get(bar_index_, 0) - j) snd_low = array.get(low_,array.get(bar_index_, 0) - j) for i = j + 1 to (array.get(bar_index_, 0)) if array.get(bar_index_, 0) - i - 1 < 0 break if array.get(high_,array.get(bar_index_, 0) - i) <= snd_high and array.get(low_,array.get(bar_index_, 0) -i) >= snd_low continue if array.get(close_,array.get(bar_index_, 0) -i) < snd_low and array.get(high_,array.get(bar_index_, 0) - i -1) < snd_low cond = true for k = i + 2 to array.get(bar_index_, 0) if array.get(bar_index_, 0) - k >= 0 if array.get(high_,array.get(bar_index_, 0) - k) >= snd_low cond := false if cond found := true found_index := j break [found_index, found] main(show, open_array, high_array, low_array, close_array, barindex_array, time_array, update) => open_ = array.get(open_array, 0) low_ = array.get(low_array, 0) high_ = array.get(high_array, 0) close_ = array.get(close_array, 0) bar_index_ = array.get(barindex_array, 0) if array.size(close_array) > 100 var state = "bearish" var l = low_ var h = high_ [opposite_high, opposite_high_index] = opposite_level_high(open_array, high_array, low_array, close_array, barindex_array) [opposite_low, opposite_low_index] = opposite_level_low(open_array, high_array, low_array, close_array, barindex_array) if state == "bearish" and bar_index_ > 5 if close_ < l or open_ < l h := opposite_high l := math.min(l, low_) if array.get(low_array, 1) != array.get(low_array, 2) l := math.min(l, low_) if close_ > h or open_ > h h := high_ state := "bullish" l := opposite_low if state == "bullish" and bar_index_ > 10 if close_ > h or open_ > h l := opposite_low h := math.max(h, high_) if array.get(high_array, 1) != array.get(high_array, 2) h := math.max(h, high_) if close_ < l or open_ < l l := low_ state := "bearish" h := opposite_high // Boxes Labelling var boxes_labels = array.new