Asiri Colored Candle
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © asiri8

//@version=5
indicator("Asiri Colored Candle", overlay=true, max_labels_count=500, max_lines_count = 1)


len_changed = (timeframe.period == 'W' ? 14 : timeframe.period == 'D' ? 14 : timeframe.period == '240' ? 14 : timeframe.period == '60' ? 14 : 
      timeframe.period == '30' ? 14 : timeframe.period == '15' ? 14 : timeframe.period == '10' ? 14 : timeframe.period == '5' ? 14 : 14)

//Volume Moving Average
SMA_Volume = ta.sma(volume, 20)


// MFI 
MFIlength = input.int(title="Length", defval=14, minval=1, maxval=2000)
src = hlc3
mf = ta.mfi(src, MFIlength)

// White Candle, current volume greater than previous volume, and also greater than volume moving average     SMA_Volume = ta.sma(volume, 20)
//Elasticity_Green_Candle = ((close>open) and (close>close[1]) and (volume>volume[1]) and ((((volume-volume[1])/volume[1])/((close-close[1])/close[1]))>0)) // شمعة المرونة
Elasticity_Green_Candle_with_Volume = ( (volume>volume[1]) and (close>open) and (close>close[1]) and (volume > SMA_Volume) )
plotcandle(Elasticity_Green_Candle_with_Volume ? open: na, Elasticity_Green_Candle_with_Volume ? high : na, 
  Elasticity_Green_Candle_with_Volume ? low : na, Elasticity_Green_Candle_with_Volume ? close : na, 
  title="Elasticity_Green_Candle_with_Volume", color=color.white, wickcolor=color.white, bordercolor=color.white)



// Green Candle, current volume greater than previous volume, and smaller than volume moving average
Elasticity_Green_Candle_No_Volume = ((volume>volume[1]) and (close>open) and (close>close[1]) and  (volume < SMA_Volume))
plotcandle(Elasticity_Green_Candle_No_Volume ? open: na, Elasticity_Green_Candle_No_Volume ? high : na, Elasticity_Green_Candle_No_Volume ? low : na, 
  Elasticity_Green_Candle_No_Volume ? close : na, title="Elasticity_Green_Candle_No_Volume", color=color.lime, 
  wickcolor=color.lime, bordercolor=color.lime)

// Use this for condition
data = ( (volume>volume[1]) and (close>open) and (close>close[1]) and (volume > 1.5*(SMA_Volume)) )
//data = ( ( (close>open)  and (volume > 1.5*(SMA_Volume)) ) )
//data = ( ((close>open) or (close==open)) and (volume > 1.5*(SMA_Volume)) )
//data1 = ( ((close==open) or (close>open)) and ((close == close[1]) or (close<close[1])) and (volume > 1.5*(SMA_Volume)) )

data1 =(close==open and (volume > 1.5*(SMA_Volume)))

data2 = (close==open and (volume > 1.5*(SMA_Volume))) 
data3 = (close>open and (close<close[1]and ((volume>volume[1]) or (volume<volume[1]) or (volume==volume[1]))) and (volume > 1.5*(SMA_Volume)))
data4 = (close>open and (close>close[1]and ((volume<volume[1]) or (volume==volume[1]))) and (volume > 1.5*(SMA_Volume)))
data5 = (close>open and (close==close[1]and ((volume>volume[1]) or (volume<volume[1]) or (volume==volume[1]))) and (volume > 1.5*(SMA_Volume)))
//data = ( (volume>volume[1]) and (close>open) and (close>close[1]) and (volume > (SMA_Volume + (1.5*(SMA_Volume)))) )
//data = ( (volume>volume[1]) and (close>open) and (close>close[1]) and (volume > 1.5*(SMA_Volume)) )
//data1 = ( ((close>open) and (volume <volume[1]) and (volume > 1.5*(SMA_Volume))) )
//data2 = ( ((close==open) and ((close == close[1]) or (close>close[1]))) and (volume > 1.5*(SMA_Volume)) )
//data3 = ( ((close==open) and (close == close[1]) and (volume > 1.5*(SMA_Volume))) or ((close==open) and (close > close[1]) and (volume > 1.5*(SMA_Volume))) )
//data4 = ( ((close==open) and (volume > 1.5*(SMA_Volume))) )

//plotchar(data, location = location.abovebar, color = color.white)
//plotchar(data, location = location.abovebar, color = color.white, text="X")
//plotchar(data, char='❄', location = location.abovebar, color = color.white)

label volumelabel1 = na
// This condition to put the volume Multipliers 
if (data)
    highvolume = volume /(SMA_Volume)
    volumelabel1 := label.new(bar_index, high, text=str.tostring(highvolume,'#'), size = size.small, textcolor = color.white ,color = color.white, yloc=yloc.abovebar, style=label.style_none)
    //volumelabel1 := label.new(bar_index, high, text=str.tostring(highvolume,'#.#'), size = size.small, textcolor = color.white ,color = color.white, yloc=yloc.abovebar, style=label.style_none)

 // This condition to put the volume Multipliers  
if (data2 or data3 or data4 or data5)
    highvolume = volume /(SMA_Volume)
    volumelabel1 := label.new(bar_index, high, text=str.tostring(highvolume,'#'), size = size.small, textcolor = color.yellow ,color = color.white, yloc=yloc.abovebar, style=label.style_none)
    //volumelabel1 := label.new(bar_index, high, text=str.tostring(highvolume,'#.#'), size = size.small, textcolor = color.yellow ,color = color.white, yloc=yloc.abovebar, style=label.style_none)
//if (data4)
    //highvolume = volume /(SMA_Volume)
    //volumelabel1 := label.new(bar_index, high, text=str.tostring(highvolume,'#'), size = size.small, textcolor = color.yellow ,color = color.white, yloc=yloc.abovebar, style=label.style_none)
    //

Rate_of_Candle_Change = (((close - close[1])/close[1]) * 100)
Elasticity_Green_Candle_with_change =  input.bool(false)
Elasticity_Green_Candle_with_change_1 =  input.bool(false)
Elasticity_Green_Candle_1 =  input.bool(false)

// Bullish candle, its change 3% and higher, and the volume greater than previous, no matter for volume moving average, 
// this candleو Often they appear in high Time Frame
// 
Purchaseing_Candle = ( (close>open) and (volume<volume[1]) and (Rate_of_Candle_Change >= 3) )
Minutes_Purchaseing_Candle  = input.bool(false)

// Bearish candle, its change 3%, and this candleو Often they appear in high Time Frame
Selling_Candle = ((Rate_of_Candle_Change < -3) and (close<open))
Minutes_Selling_Candle = input.bool(false)

if(timeframe.isminutes)
    Minutes_Purchaseing_Candle := ( (close>open) and (volume<volume[1]) and (Rate_of_Candle_Change >= 1.3) )
    Minutes_Selling_Candle := ((Rate_of_Candle_Change < -1.3) and (close<open))
barcolor(Elasticity_Green_Candle_with_Volume? color.white :Elasticity_Green_Candle_No_Volume? color.lime :Purchaseing_Candle? color.rgb(92, 90, 248):
 Minutes_Purchaseing_Candle? color.rgb(92, 90, 248):Selling_Candle? color.yellow: Minutes_Selling_Candle? color.yellow : na) 

// Set the color of the candles
barcolor(Elasticity_Green_Candle_with_Volume? color.white :Elasticity_Green_Candle_No_Volume? color.lime: Purchaseing_Candle? color.rgb(92, 90, 248):  
  Selling_Candle? color.yellow: na)  //Purchaseing_Candle? color.rgb(92, 90, 248)


plotcandle(Purchaseing_Candle ? open: na, Purchaseing_Candle ? high : na, Purchaseing_Candle ? low : na, 
  Purchaseing_Candle ? close : na, title="Purchaseing_Candle", color=color.rgb(92, 90, 248), 
  wickcolor=color.rgb(92, 90, 248), bordercolor=color.rgb(92, 90, 248))

plotcandle(Minutes_Purchaseing_Candle ? open: na, Minutes_Purchaseing_Candle ? high : na, Minutes_Purchaseing_Candle ? low : na, 
  Minutes_Purchaseing_Candle ? close : na, title="Minutes_Purchaseing_Candle", color=color.rgb(92, 90, 248), 
  wickcolor=color.rgb(92, 90, 248), bordercolor=color.rgb(92, 90, 248))





LowestValueAndBar(source, lenght) =>
    MFIValue = mf 
    MFIIndex = bar_index 
    
    //Loop e.g. 100 times and see if the other previous values are smaller. Then update the "minValue" and "minIndex"
    for i = 1 to lenght
        if source[i] < MFIValue
            MFIValue := source[i]
            MFIIndex := bar_index[i]
    [MFIValue, MFIIndex]

[value, index] = LowestValueAndBar(mf, 14)
//label.new(index, value, text="Hello, world!", style=label.style_circle, size = size.tiny)


// To put the lowest MFI vlaue under the candle
label label1 = na
//lowestrsi = 0.0
lowestrsi = ta.lowest(mf, 14)
//bar = ta.barssince(ta.lowest(mf, 14))
//lo = ta.barssince(ta.lowest(mf, 14))
Buy_M =  input.bool(false)

if timeframe.isminutes
    if (mf < 30) 
        
        Buy_M := (mf < 30)
        //label1 = label.new(bar_index , low - 0.05, text="B", size = size.normal, textcolor = color.black ,color = color.rgb(255, 255, 255), style=label.style_label_up)
        label1 := label.new(index, value, text=str.tostring(value,'#'), size = size.small, textcolor = color.white ,color = color.white, yloc=yloc.belowbar, style=label.style_none)
        //label1 := label.new(bar_index , low - 0.3, text=str.tostring(rsi,'#'), size = size.normal, textcolor = color.white ,color = color.rgb(255, 255, 255), style=label.style_none)
        //label1 := label.new(bar_index, na, text=str.tostring(rsi,'#'), size = size.small, textcolor = color.black ,color = color.white, yloc=yloc.belowbar, style=label.style_label_up)
        label.delete(label1[1])

        //label.new(bar_index, low - 0.1, "Buy", textcolor=color.rgb(255, 251, 2), style=label.style_none, size=size.small)
//plotchar(Buy_M, char='B', location = location.belowbar, color = color.white, size = size.tiny)


Buy_D =  input.bool(false)
if timeframe.isdaily
    if (mf < 30) 
        Buy_D := (mf < 30)
        //label1 := label.new(bar_index , na, text=str.tostring(rsi,'#'), size = size.small, textcolor = color.black ,color = color.white, yloc=yloc.belowbar,style=label.style_label_up)
        label1 := label.new(index, value, text=str.tostring(value,'#'), size = size.small, textcolor = color.white ,color = color.white, yloc=yloc.belowbar, style=label.style_none)
        //label1 := label.new(bar_index , low - 1, text=str.tostring(rsi,'#'), size = size.normal, textcolor = color.white ,color = color.rgb(255, 255, 255), style=label.style_none)
        label.delete(label1[1])

Buy_Mo =  input.bool(false)
if timeframe.isweekly
    if (mf < 30) 
        Buy_Mo := (mf < 30)
        label1 := label.new(index, value, text=str.tostring(value,'#'), size = size.small, textcolor = color.white ,color = color.white, yloc=yloc.belowbar, style=label.style_none)
        label.delete(label1[1])

// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

