SubCandle
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © HeWhoMustNotBeNamed

//   __    __            __       __  __                  __       __                        __      __    __              __      _______             __    __                                          __ 
//  /  |  /  |          /  |  _  /  |/  |                /  \     /  |                      /  |    /  \  /  |            /  |    /       \           /  \  /  |                                        /  |
//  $$ |  $$ |  ______  $$ | / \ $$ |$$ |____    ______  $$  \   /$$ | __    __   _______  _$$ |_   $$  \ $$ |  ______   _$$ |_   $$$$$$$  |  ______  $$  \ $$ |  ______   _____  ____    ______    ____$$ |
//  $$ |__$$ | /      \ $$ |/$  \$$ |$$      \  /      \ $$$  \ /$$$ |/  |  /  | /       |/ $$   |  $$$  \$$ | /      \ / $$   |  $$ |__$$ | /      \ $$$  \$$ | /      \ /     \/    \  /      \  /    $$ |
//  $$    $$ |/$$$$$$  |$$ /$$$  $$ |$$$$$$$  |/$$$$$$  |$$$$  /$$$$ |$$ |  $$ |/$$$$$$$/ $$$$$$/   $$$$  $$ |/$$$$$$  |$$$$$$/   $$    $$< /$$$$$$  |$$$$  $$ | $$$$$$  |$$$$$$ $$$$  |/$$$$$$  |/$$$$$$$ |
//  $$$$$$$$ |$$    $$ |$$ $$/$$ $$ |$$ |  $$ |$$ |  $$ |$$ $$ $$/$$ |$$ |  $$ |$$      \   $$ | __ $$ $$ $$ |$$ |  $$ |  $$ | __ $$$$$$$  |$$    $$ |$$ $$ $$ | /    $$ |$$ | $$ | $$ |$$    $$ |$$ |  $$ |
//  $$ |  $$ |$$$$$$$$/ $$$$/  $$$$ |$$ |  $$ |$$ \__$$ |$$ |$$$/ $$ |$$ \__$$ | $$$$$$  |  $$ |/  |$$ |$$$$ |$$ \__$$ |  $$ |/  |$$ |__$$ |$$$$$$$$/ $$ |$$$$ |/$$$$$$$ |$$ | $$ | $$ |$$$$$$$$/ $$ \__$$ |
//  $$ |  $$ |$$       |$$$/    $$$ |$$ |  $$ |$$    $$/ $$ | $/  $$ |$$    $$/ /     $$/   $$  $$/ $$ | $$$ |$$    $$/   $$  $$/ $$    $$/ $$       |$$ | $$$ |$$    $$ |$$ | $$ | $$ |$$       |$$    $$ |
//  $$/   $$/  $$$$$$$/ $$/      $$/ $$/   $$/  $$$$$$/  $$/      $$/  $$$$$$/  $$$$$$$/     $$$$/  $$/   $$/  $$$$$$/     $$$$/  $$$$$$$/   $$$$$$$/ $$/   $$/  $$$$$$$/ $$/  $$/  $$/  $$$$$$$/  $$$$$$$/ 
//                                                                                                                                                                                                          
//                                                                                                                                                                                                          
//
//@version=5
indicator("SubCandle", overlay=true)

numberOfBacktestBars = input.int(5000, 'Backtest Bars', maxval=20000, step=1000)
import PineCoders/Time/3 as t

var ltf = int(timeframe.in_seconds(timeframe.period) / (100000/numberOfBacktestBars))
var strLtf = t.secondsToTfString(ltf)
[lo, lh, ll, lc] = request.security_lower_tf(syminfo.tickerid, strLtf, [open, high, low, close], true)
low_indices_sorted = array.sort_indices(ll, order.ascending)
high_indices_sorted = array.sort_indices(lh, order.descending)

ll_index = array.size(lh)>0? array.get(low_indices_sorted, 0) : 0
hh_index = array.size(ll)>0? array.get(high_indices_sorted, 0) : 0

high_after_low = array.size(lh)>0? array.max(array.slice(lh, ll_index>=array.size(ll)-1? ll_index : ll_index+1, array.size(lh))) : na
low_after_high = array.size(ll)>0? array.min(array.slice(ll, hh_index>=array.size(lh)-1? hh_index : hh_index+1, array.size(ll))) : na

newOpen = high_after_low == high? high_after_low: low_after_high
newHigh = math.min(high_after_low, high)
newLow = math.max(low_after_high, low)
newClose = close

nColor=newOpen>newClose?color.red : color.green
plotcandle(newOpen, newHigh, newLow, newClose, title='SubCandle', color=color.new(nColor, 80), bordercolor = nColor, wickcolor=nColor)
