Pattern Forecast (Expo)
// This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) https://creativecommons.org/licenses/by-nc-sa/4.0/
// © Zeiierman

//@version=5
indicator("Pattern Forecast (Expo)",overlay=true,max_bars_back=5000,max_boxes_count=500,max_lines_count=500)
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}

//~~ Tooltips {
string t1 = "The amount of Forecast candles that should be displayed in the future."
string t2 = "Background color divider between price and forecast."
string t3 = "Displays the current event found"
string t4 = "Forecast placement"
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}

//~~ Inputs {
pattern = input.string("Doji","Pattern",["OnNeckBearish",
 "RisingWindowBullish",
 "FallingWindowBearish",
 "FallingThreeMethodsBullish",
 "FallingThreeMethodsBearish",
 "TweezerBottomBullish",
 "TweezerTopBearish",
 "DarkCloudCoverBearish",
 "UpsideTasukiGapBullish",
 "DownsideTasukiGapBearish",
 "EveningDojiStarBearish",
 "Doji",
 "DojiStarBullish",
 "DojiStarBearish",
 "MorningDojiStarBullish",
 "PiercingBullish",
 "HammerBullish",
 "HangingManBearish",
 "ShootingStarBearish",      
 "InvertedHammerBullish",     
 "MorningStarBullish",     
 "EveningStarBearish",        
 "MarubozuWhiteBullish",       
 "MarubozuBlackBearish",      
 "GravestoneDojiBearish",      
 "DragonflyDojiBullish",     
 "HaramiCrossBullish",      
 "HaramiCrossBearish",        
 "HaramiBullish",        
 "HaramiBearish",             
 "LongLowerShadowBullish",     
 "LongUpperShadowBearish",    
 "SpinningTopWhite",    
 "SpinningTopBlack",          
 "ThreeWhiteSoldiersBullish",  
 "ThreeBlackCrowsBearish", 
 "EngulfingBullish",   
 "EngulfingBearish",          
 "AbandonedBabyBullish",       
 "AbandonedBabyBearish",      
 "TriStarBullish",      
 "TriStarBearish",            
 "KickingBullish",           
 "KickingBearish"])

Forecast  = input.int(50,"Event Candles",1,158,tooltip=t1)
Divider   = input.bool(true,"Event Divider",t2)
Display   = input.bool(true,"Display Event",t3)
Placement = input.int(5,"Placement",1,25,tooltip=t4)
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}

//~~ Types {
type Event
    box currentEvent
    box pastEvent
    box prediction
    array<box>  candle
    array<line> wick
type Data
    array<int>   b 
    array<int>   d
    array<float> o
    array<float> h
    array<float> l
    array<float> c
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}

//~~ Variables & Arrays {
b = bar_index
var data  = Data.new(array.new<int>(),
                     array.new<int>(),
                     array.new<float>(),
                     array.new<float>(),
                     array.new<float>(),
                     array.new<float>())
var event = Event.new(box.new(na,na,na,na,chart.fg_color,border_style=line.style_dashed,bgcolor=color(na)),
                     box.new(na,na,na,na,chart.fg_color,border_style=line.style_dashed,bgcolor=color(na)),
                     box.new(na,na,na,na,chart.fg_color,border_style=line.style_dotted,
                      bgcolor=color.new(color.teal,75)),
                     array.new<box>(Forecast),
                     array.new<line>(Forecast))
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}

//~~ Methods {
method Pattern(Data x,i)=>
    //Get Candles
    o0 = x.o.get(i) 
    h0 = x.h.get(i)
    l0 = x.l.get(i)
    c0 = x.c.get(i)
    o1 = x.o.get(i+1) 
    h1 = x.h.get(i+1)
    l1 = x.l.get(i+1)
    c1 = x.c.get(i+1)
    o2 = x.o.get(i+2) 
    h2 = x.h.get(i+2)
    l2 = x.l.get(i+2)
    c2 = x.c.get(i+2)
    o3 = x.o.get(i+3) 
    h3 = x.h.get(i+3)
    l3 = x.l.get(i+3)
    c3 = x.c.get(i+3)
    o4 = x.o.get(i+4) 
    h4 = x.h.get(i+4)
    l4 = x.l.get(i+4)
    c4 = x.c.get(i+4)
    o5 = x.o.get(i+5) 
    h5 = x.h.get(i+5)
    l5 = x.l.get(i+5)
    c5 = x.c.get(i+5)
    o6 = x.o.get(i+6) 
    h6 = x.h.get(i+6)
    l6 = x.l.get(i+6)
    c6 = x.c.get(i+6)
    o7 = x.o.get(i+7) 
    h7 = x.h.get(i+7)
    l7 = x.l.get(i+7)
    c7 = x.c.get(i+7)
    o8 = x.o.get(i+8) 
    h8 = x.h.get(i+8)
    l8 = x.l.get(i+8)
    c8 = x.c.get(i+8)

    //Candle Calculation
    C_BodyHi  = math.max(c0,o0) 
    C_BodyLo  = math.min(c0,o0)
    C_BodyHi1 = math.max(c1,o1) 
    C_BodyLo1 = math.min(c1,o1)
    C_BodyHi2 = math.max(c2,o1) 
    C_BodyLo2 = math.min(c2,o2)
    C_BodyHi3 = math.max(c3,o3) 
    C_BodyLo3 = math.min(c3,o3)
    C_BodyHi4 = math.max(c4,o4) 
    C_BodyLo4 = math.min(c4,o4)

    C_Body  = C_BodyHi  - C_BodyLo
    C_Body1 = C_BodyHi1 - C_BodyLo1
    C_Body2 = C_BodyHi2 - C_BodyLo2
    C_Body3 = C_BodyHi3 - C_BodyLo3
    C_Body4 = C_BodyHi4 - C_BodyLo4

    C_BodyAvg  = math.avg(math.abs(o0-c0),math.abs(o1-c1),math.abs(o2-c2),math.abs(o3-c3),math.abs(o4-c4))
    C_BodyAvg1 = math.avg(math.abs(o1-c1),math.abs(o2-c2),math.abs(o3-c3),math.abs(o4-c4),math.abs(o5-c5))
    C_BodyAvg2 = math.avg(math.abs(o2-c2),math.abs(o3-c3),math.abs(o4-c4),math.abs(o5-c5),math.abs(o6-c6))
    C_BodyAvg3 = math.avg(math.abs(o3-c3),math.abs(o4-c4),math.abs(o5-c5),math.abs(o6-c6),math.abs(o7-c7))
    C_BodyAvg4 = math.avg(math.abs(o4-c4),math.abs(o5-c5),math.abs(o6-c6),math.abs(o7-c7),math.abs(o8-c8))

    C_SmallBody  = C_Body  < C_BodyAvg
    C_SmallBody1 = C_Body1 < C_BodyAvg1
    C_SmallBody2 = C_Body2 < C_BodyAvg2
    C_SmallBody3 = C_Body3 < C_BodyAvg3
    C_SmallBody4 = C_Body4 < C_BodyAvg4

    C_LongBody  = C_Body  > C_BodyAvg
    C_LongBody1 = C_Body1 > C_BodyAvg1
    C_LongBody2 = C_Body2 > C_BodyAvg2
    C_LongBody3 = C_Body3 > C_BodyAvg3
    C_LongBody4 = C_Body4 > C_BodyAvg4

    C_UpShadow    = h0 - C_BodyHi
    C_UpShadow1   = h1 - C_BodyHi1
    C_UpShadow2   = h2 - C_BodyHi2
    C_DnShadow    = C_BodyLo - l0
    C_DnShadow1   = C_BodyLo1 - l1
    C_DnShadow2   = C_BodyLo2 - l2
    C_HasUpShadow = C_UpShadow > 5.0 / 100 * C_Body
    C_HasDnShadow = C_DnShadow > 5.0 / 100 * C_Body

    C_WhiteBody  = o0 < c0
    C_WhiteBody1 = o1 < c1
    C_WhiteBody2 = o2 < c2
    C_WhiteBody3 = o3 < c3
    C_WhiteBody4 = o4 < c4
    C_BlackBody  = o0 > c0
    C_BlackBody1 = o1 > c1
    C_BlackBody2 = o2 > c2
    C_BlackBody3 = o3 > c3
    C_BlackBody4 = o4 > c4

    C_Range  = h0-l0
    C_Range1 = h1-l1
    C_Range2 = h2-l2

    C_IsInsideBar = C_BodyHi1 > C_BodyHi and C_BodyLo1 < C_BodyLo
    C_BodyMiddle  = C_Body / 2 + C_BodyLo
    C_BodyMiddle1 = C_Body1 / 2 + C_BodyLo1
    C_BodyMiddle2 = C_Body2 / 2 + C_BodyLo2

    C_ShadowEquals  = C_UpShadow == C_DnShadow or (math.abs(C_UpShadow - C_DnShadow) / C_DnShadow * 100) < 100.0 and (math.abs(C_DnShadow - C_UpShadow) / C_UpShadow * 100) < 100.0
    C_ShadowEquals1 = C_UpShadow1 == C_DnShadow1 or (math.abs(C_UpShadow1 - C_DnShadow1) / C_DnShadow1 * 100) < 100.0 and (math.abs(C_DnShadow1 - C_UpShadow1) / C_UpShadow1 * 100) < 100.0
    C_ShadowEquals2 = C_UpShadow2 == C_DnShadow2 or (math.abs(C_UpShadow2 - C_DnShadow2) / C_DnShadow2 * 100) < 100.0 and (math.abs(C_DnShadow2 - C_UpShadow2) / C_UpShadow2 * 100) < 100.0
    
    C_IsDojiBody  = C_Range > 0 and C_Body <= C_Range * 5.0 / 100
    C_IsDojiBody1 = C_Range1 > 0 and C_Body1 <= C_Range1 * 5.0 / 100
    C_IsDojiBody2 = C_Range2 > 0 and C_Body2 <= C_Range2 * 5.0 / 100
    
    C_Doji  = C_IsDojiBody and C_ShadowEquals
    C_Doji1 = C_IsDojiBody1 and C_ShadowEquals1
    C_Doji2 = C_IsDojiBody2 and C_ShadowEquals2
    
    C_3WSld_HaveNotUpShadow  = C_Range * 5.0 / 100 > C_UpShadow
    C_3WSld_HaveNotUpShadow1 = C_Range1 * 5.0 / 100 > C_UpShadow1
    C_3WSld_HaveNotUpShadow2 = C_Range2 * 5.0 / 100 > C_UpShadow2
    C_3BCrw_HaveNotDnShadow  = C_Range * 5.0 / 100 > C_DnShadow
    C_3BCrw_HaveNotDnShadow1 = C_Range1 * 5.0 / 100 > C_DnShadow1
    C_3BCrw_HaveNotDnShadow2 = C_Range2 * 5.0 / 100 > C_DnShadow2

    C_BodyGapUpBullish  = C_BodyHi1 < C_BodyLo
    C_BodyGapDnBullish  = C_BodyLo1 > C_BodyHi
    C_BodyGapDnBullish1 = C_BodyLo2 > C_BodyHi1

    C_BodyGapUp  = C_BodyHi1 < C_BodyLo
    C_BodyGapUp1 = C_BodyHi2 < C_BodyLo1
    C_BodyGapDn  = C_BodyLo1 > C_BodyHi

    C_Marubozu                = C_LongBody and C_UpShadow <= 5.0/100*C_Body and C_DnShadow <= 5.0/100*C_Body
    C_Marubozu1               = C_LongBody1 and C_UpShadow1 <= 5.0/100*C_Body1 and C_DnShadow1 <= 5.0/100*C_Body1
    C_MarubozuBlackBullish    = C_Marubozu and C_BlackBody
    C_MarubozuBlackBullish1   = C_Marubozu1 and C_BlackBody1
    C_MarubozuBearishKicking  = C_LongBody and C_UpShadow <= 5.0/100*C_Body and C_DnShadow <= 5.0/100*C_Body
    C_MarubozuBearishKicking1 = C_LongBody1 and C_UpShadow1 <= 5.0/100*C_Body1 and C_DnShadow1 <= 5.0/100*C_Body1
    C_MarubozuWhiteBearish    = C_MarubozuBearishKicking and C_WhiteBody
    C_MarubozuWhiteBearish1   = C_MarubozuBearishKicking1 and C_WhiteBody1

    //Switch Function
    output = switch pattern
        "OnNeckBearish"              => C_BlackBody1 and C_LongBody1 and C_WhiteBody and o0 < c1 and C_SmallBody and C_Range!=0 and math.abs(c0-l1)<=C_BodyAvg*0.05
        "RisingWindowBullish"        => (C_Range!=0 and C_Range1!=0) and l0 > h1
        "FallingWindowBearish"       => (C_Range!=0 and C_Range1!=0) and h0 < l1
        "FallingThreeMethodsBullish" => (C_LongBody4 and C_WhiteBody4) and (C_SmallBody3 and C_BlackBody3 and o3<h4 and c3>l4) and (C_SmallBody2 and C_BlackBody2 and o2<h4 and c2>l4) and (C_SmallBody1 and C_BlackBody1 and o1<h4 and c1>l4) and (C_LongBody and C_WhiteBody and c0>c4)
        "FallingThreeMethodsBearish" => (C_LongBody4 and C_BlackBody4) and (C_SmallBody3 and C_WhiteBody3 and o3>l4 and c3<h4) and (C_SmallBody2 and C_WhiteBody2 and o2>l4 and c2<h4) and (C_SmallBody1 and C_WhiteBody1 and o1>l4 and c1<h4) and (C_LongBody and C_BlackBody and c0<c4)
        "TweezerBottomBullish"       => (not C_IsDojiBody or (C_HasUpShadow and C_HasDnShadow)) and math.abs(l0-l1) <= C_BodyAvg*0.05 and C_BlackBody1 and C_WhiteBody and C_LongBody1
        "TweezerTopBearish"          => (not C_IsDojiBody or (C_HasUpShadow and C_HasDnShadow)) and math.abs(h0-h1) <= C_BodyAvg*0.05 and C_WhiteBody1 and C_BlackBody and C_LongBody1
        "DarkCloudCoverBearish"      => (C_WhiteBody1 and C_LongBody1) and (C_BlackBody and o0 >= h1 and c0 < C_BodyMiddle1 and c0 > o1)
        "UpsideTasukiGapBullish"     => C_SmallBody1 and C_WhiteBody2 and C_BodyLo1 > C_BodyHi2 and C_WhiteBody1 and C_BlackBody and C_BodyLo >= C_BodyHi2 and C_BodyLo <= C_BodyLo1
        "DownsideTasukiGapBearish"   => C_SmallBody1 and C_BlackBody2 and C_BodyHi1 < C_BodyLo2 and C_BlackBody1 and C_WhiteBody and C_BodyHi <= C_BodyLo2 and C_BodyHi >= C_BodyHi1
        "EveningDojiStarBearish"     => C_LongBody2 and C_IsDojiBody1 and C_LongBody and C_WhiteBody2 and C_BodyLo1 > C_BodyHi2 and C_BlackBody and C_BodyLo <= C_BodyMiddle2 and C_BodyLo > C_BodyLo2 and C_BodyLo1 > C_BodyHi
        "Doji"                       => C_Doji
        "DojiStarBullish"            => C_BlackBody1 and C_LongBody1 and C_IsDojiBody and C_BodyHi < C_BodyLo1
        "DojiStarBearish"            => C_WhiteBody1 and C_LongBody1 and C_IsDojiBody and C_BodyLo > C_BodyHi1
        "MorningDojiStarBullish"     => C_LongBody2 and C_IsDojiBody1 and C_LongBody and C_BlackBody2 and C_BodyHi1 < C_BodyLo2 and C_WhiteBody and C_BodyHi >= C_BodyMiddle2 and C_BodyHi < C_BodyHi2 and C_BodyHi1 < C_BodyLo
        "PiercingBullish"            => (C_BlackBody1 and C_LongBody1) and (C_WhiteBody and o0 <= l1 and c0 > C_BodyMiddle1 and c0 < o1)
        "HammerBullish"              => C_SmallBody and C_Body > 0 and C_BodyLo > (h0+l0)/2 and C_DnShadow >= 2.0 * C_Body and not C_HasUpShadow
        "HangingManBearish"          => C_SmallBody and C_Body > 0 and C_BodyLo > (h0+l0)/2 and C_DnShadow >= 2.0 * C_Body and not C_HasUpShadow
        "ShootingStarBearish"        => C_SmallBody and C_Body > 0 and C_BodyHi < (h0+l0)/2 and C_UpShadow >= 2.0 * C_Body and not C_HasDnShadow
        "InvertedHammerBullish"      => C_SmallBody and C_Body > 0 and C_BodyHi < (h0+l0)/2 and C_UpShadow >= 2.0 * C_Body and not C_HasDnShadow
        "MorningStarBullish"         => C_LongBody2 and C_SmallBody1 and C_LongBody and C_BlackBody2 and C_BodyHi1 < C_BodyLo2 and C_WhiteBody and C_BodyHi >= C_BodyMiddle2 and C_BodyHi < C_BodyHi2 and C_BodyHi1 < C_BodyLo
        "EveningStarBearish"         => C_LongBody2 and C_SmallBody1 and C_LongBody and C_WhiteBody2 and C_BodyLo1 > C_BodyHi2 and C_BlackBody and C_BodyLo <= C_BodyMiddle2 and C_BodyLo > C_BodyLo2 and C_BodyLo1 > C_BodyHi
        "MarubozuWhiteBullish"       => C_WhiteBody and C_LongBody and C_UpShadow <= 5.0/100*C_Body and C_DnShadow <= 5.0/100*C_Body and C_WhiteBody
        "MarubozuBlackBearish"       => C_BlackBody and C_LongBody and C_UpShadow <= 5.0/100*C_Body and C_DnShadow <= 5.0/100*C_Body and C_BlackBody
        "GravestoneDojiBearish"      => C_IsDojiBody and C_DnShadow <= C_Body
        "DragonflyDojiBullish"       => C_IsDojiBody and C_UpShadow <= C_Body
        "HaramiCrossBullish"         => C_LongBody1 and C_BlackBody1 and C_IsDojiBody and h0 <= C_BodyHi1 and l0 >= C_BodyLo1
        "HaramiCrossBearish"         => C_LongBody1 and C_WhiteBody1 and C_IsDojiBody and h0 <= C_BodyHi1 and l0 >= C_BodyLo1
        "HaramiBullish"              => C_LongBody1 and C_BlackBody1 and C_WhiteBody and C_SmallBody and h0 <= C_BodyHi1 and l0 >= C_BodyLo1
        "HaramiBearish"              => C_LongBody1 and C_WhiteBody1 and C_BlackBody and C_SmallBody and h0 <= C_BodyHi1 and l0 >= C_BodyLo1
        "LongLowerShadowBullish"     => C_DnShadow > C_Range/100*75.0
        "LongUpperShadowBearish"     => C_UpShadow > C_Range/100*75.0
        "SpinningTopWhite"           => C_DnShadow >= C_Range / 100 * 34.0 and C_UpShadow >= C_Range / 100 * 34.0 and not C_IsDojiBody and C_WhiteBody
        "SpinningTopBlack"           => C_DnShadow >= C_Range / 100 * 34.0 and C_UpShadow >= C_Range / 100 * 34.0 and not C_IsDojiBody and C_BlackBody
        "ThreeWhiteSoldiersBullish"  => C_LongBody and C_LongBody1 and C_LongBody2 and C_WhiteBody and C_WhiteBody1 and C_WhiteBody2 and c0 > c1 and c1 > c2 and o0 < c1 and o0 > o1 and o1 < c2 and o1 > o2 and C_3WSld_HaveNotUpShadow and C_3WSld_HaveNotUpShadow1 and C_3WSld_HaveNotUpShadow2
        "ThreeBlackCrowsBearish"     => C_LongBody and C_LongBody1 and C_LongBody2 and C_BlackBody and C_BlackBody1 and C_BlackBody2 and c0 < c1 and c1 < c2 and o0 > c1 and o0 < o1 and o1 > c2 and o1 < o2 and C_3BCrw_HaveNotDnShadow and C_3BCrw_HaveNotDnShadow1 and C_3BCrw_HaveNotDnShadow2
        "EngulfingBullish"           => C_WhiteBody and C_LongBody and C_BlackBody1 and C_SmallBody1 and c0 >= o1 and o0 <= c1 and ( c0 > o1 or o0 < c1 )
        "EngulfingBearish"           => C_BlackBody and C_LongBody and C_WhiteBody1 and C_SmallBody1 and c0 <= o1 and o0 >= c1 and ( c0 < o1 or o0 > c1 )
        "AbandonedBabyBullish"       => C_BlackBody2 and C_IsDojiBody1 and l2 > h1 and C_WhiteBody and h1 < l0
        "AbandonedBabyBearish"       => C_WhiteBody2 and C_IsDojiBody1 and h2 < l1 and C_BlackBody and l1 > h0
        "TriStarBullish"             => C_Doji2 and C_Doji1 and C_Doji and C_BodyGapDnBullish1 and C_BodyGapUpBullish
        "TriStarBearish"             => C_Doji2 and C_Doji1 and C_Doji and C_BodyGapUp1 and C_BodyGapDn
        "KickingBullish"             => C_MarubozuBlackBullish1 and C_Marubozu and C_WhiteBody and h1 < l0
        "KickingBearish"             => C_MarubozuWhiteBearish1 and C_MarubozuBearishKicking and C_BlackBody and l1 > h0

//Store Data
method Store(Data x)=>
    int   B = b
    int bin = close>open?1:close<open?-1:0
    float O = open
    float H = high
    float L = low
    float C = close
    x.b.unshift(B)
    x.d.unshift(bin)
    x.o.unshift(O)
    x.h.unshift(H)
    x.l.unshift(L)
    x.c.unshift(C)

//Candle Plots
method Candle(Event e,x,i)=>
    int dist   = Placement
    float prev = x.c.get(i)
    float diff = ((close-prev)/prev)+1
    for j=i-1 to i-Forecast
        idx = j-i+Forecast
        if j<0
            break
        else
            pos = x.d.get(j)
            top = (pos>0?x.c.get(j):x.o.get(j))*diff
            bot = (pos>0?x.o.get(j):x.c.get(j))*diff
            hi  = (x.h.get(j))*diff
            lo  = (x.l.get(j))*diff
            col = pos==1?#26a69a:pos==-1?#ef5350:chart.fg_color
            candle = e.candle.get(idx)
            if na(candle)
                e.candle.set(idx,box.new(b+dist,top,b+dist+2,bot,na,bgcolor=col))
                e.wick.set(idx,line.new(b+dist+1,hi,b+dist+1,lo,color=col))
            else
                box.set_lefttop(e.candle.get(idx),b+dist,top)
                box.set_rightbottom(e.candle.get(idx),b+dist+2,bot)
                box.set_bgcolor(e.candle.get(idx),col)
                line.set_xy1(e.wick.get(idx),b+dist+1,hi)
                line.set_xy2(e.wick.get(idx),b+dist+1,lo)
                line.set_color(e.wick.get(idx),col)
        dist += 3

//Events Display
method Events(Event e,idx,h1,l1,h2,l2,fh,fl)=>
    int end   = idx
    e.pastEvent.set_lefttop(end,h2)
    e.pastEvent.set_rightbottom(end,l2)
    e.prediction.set_lefttop(end+1,fh.max())
    e.prediction.set_rightbottom(math.min(b,end+Forecast),fl.min())

//Current Event
method Series(Data x)=>
    data.Store()
    bool found = false
    if barstate.islast
        for i=1 to x.d.size()-9
            search = x.Pattern(i)
            if search
                found := true
                event.Candle(data,i)
                if Display
                    bar = x.b.get(i)
                    h1  = x.h.get(i)
                    l1  = x.l.get(i)
                    h2  = x.h.get(i)
                    l2  = x.l.get(i)
                    fh  = i-Forecast<=0?x.h.slice(0,i-1):x.h.slice(i-Forecast,i-1)
                    fl  = i-Forecast<=0?x.l.slice(0,i-1):x.l.slice(i-Forecast,i-1)
                    event.Events(bar,h1,l1,h2,l2,fh,fl)
                    label.new(x.b.get(i),x.h.get(i))
                break
    if barstate.islast and not found
        runtime.error("Sorry I couldn't find that specific pattern, try another one :)")
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}

//~~ Divider {
bgcolor(Divider and barstate.islast?color.new(chart.fg_color,90):na,1)
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}

//~~ Run Code {
data.Series()
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}

