Volume Based Ichimoku Cloud
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// 
// This coded is based on Ichimoku indicator, the volume weighded donchian is selfmade production
// © pascorp

//@version=5
indicator(title='Volume Based Ichimoku Cloud', shorttitle='Ichi VW', overlay=true)

// ——————————————————————————————————————————————————————————————
// >>>>>>>>>>>>>>>>>>>>>>>>>  Inputs  <<<<<<<<<<<<<<<<<<<<<<<<<<<
// ——————————————————————————————————————————————————————————————
conversionPeriods   = input.int(defval=7,  minval=1,     title='Tenkan Sen Periods',    group="Ichimoku")
basePeriods         = input.int(defval=22, minval=1,     title='Kijun Sen Periods',     group="Ichimoku")
laggingSpan2Periods = input.int(defval=44, minval=1,     title='Senkou Span B Periods', group="Ichimoku")
displacement        = input.int(defval=22, minval=1,     title='Displacement',          group="Ichimoku")
typeDon             = input.string(defval="Volume Based", title="Donchian type", options=["Classic", "Volume Based"])
shwCond             = input(true, title="Show Ichimoku Condition")
shwRev              = input(true, title="Show Aggressive Reversal")

// ——————————————————————————————————————————————————————————————
// >>>>>>>>>>>>>>>>>>>>>>>  Functions  <<<<<<<<<<<<<<<<<<<<<<<<<<
// ——————————————————————————————————————————————————————————————
donchian(len) =>
    math.avg(ta.lowest(len), ta.highest(len))
    
donchianVW(len) =>
    num1 = math.sum(low*volume, len)
    den1 = math.sum(volume, len)
    lo = num1/den1
    num2 = math.sum(high*volume, len)
    den2 = math.sum(volume, len)
    hi = num2/den2
    math.avg(ta.lowest(lo,len), ta.highest(hi,len))
    
DONCHIAN(t, l) =>
    switch t 
        "Classic"       => donchian(l)
        "Volume Based"  => donchianVW(l)

// ——————————————————————————————————————————————————————————————
// >>>>>>>>>>>>>>>>>>>>>>>>>  Core   <<<<<<<<<<<<<<<<<<<<<<<<<<<<
// ——————————————————————————————————————————————————————————————
conversionLine = DONCHIAN(typeDon, conversionPeriods)
baseLine = DONCHIAN(typeDon, basePeriods)
leadLine1 = math.avg(conversionLine, baseLine)
leadLine2 = DONCHIAN(typeDon, laggingSpan2Periods)
SenkouSpanH = math.max(leadLine1[displacement - 1], leadLine2[displacement - 1])
SenkouSpanL = math.min(leadLine1[displacement - 1], leadLine2[displacement - 1])

// ——————————————————————————————————————————————————————————————
// >>>>>>>>>>>>>>>>>>>>>>>>>  Plots  <<<<<<<<<<<<<<<<<<<<<<<<<<<<
// ——————————————————————————————————————————————————————————————
SpA = plot(leadLine1, offset=displacement - 1, color=color.new(#A5D6A7, 100),   title='Leading Span A')
SpB = plot(leadLine2, offset=displacement - 1, color=color.new(#EF9A9A, 100),   title='Leading Span B')
plot(conversionLine, color=color.new(color.blue, 0),                         title='Tenkan Sen',     linewidth=2)
plot(baseLine, color=color.new(color.red, 0),                                title='Kijun Sen',      linewidth=2)
plot(close, offset=-displacement + 1, color=color.new(color.green, 80),        title='Chikou Span',    linewidth=1)

fill(SpA, SpB, color=leadLine1 > leadLine2 ? color.rgb(127, 255, 212, 70) : color.rgb(229, 43, 80, 70))



// ——————————————————————————————————————————————————————————————
// >>>>>>>>>>>>>>>>>>>>>>>  Conditions  <<<<<<<<<<<<<<<<<<<<<<<<<
// ——————————————————————————————————————————————————————————————
condSell = shwCond and close < conversionLine and close < close[displacement - 1] and conversionLine < baseLine and close < leadLine2[displacement - 1] and open < leadLine2[displacement - 1] and open < conversionLine and conversionLine < leadLine2[displacement - 1] and baseLine < leadLine2[displacement - 1] and conversionLine < leadLine1[displacement - 1] and baseLine < leadLine1[displacement - 1] and close < leadLine2[displacement * 2 - 1]
plotshape(condSell, style=shape.square, location=location.bottom, color=color.new(color.red, 0), size=size.tiny, title='Dots Sell')

condBuy = shwCond and close > conversionLine and close > close[displacement - 1] and conversionLine > baseLine and close > leadLine2[displacement - 1] and open > leadLine2[displacement - 1] and open > conversionLine and conversionLine > leadLine2[displacement - 1] and baseLine > leadLine2[displacement - 1] and conversionLine > leadLine1[displacement - 1] and baseLine > leadLine1[displacement - 1] and close > leadLine2[displacement * 2 - 1]
plotshape(condBuy, style=shape.square, location=location.bottom, color=color.new(color.green, 0), size=size.tiny, title='Dots Sell')

condRevBuy = shwRev and close > conversionLine and conversionLine > baseLine and baseLine > close[displacement - 1] and close < leadLine2[displacement - 1]
plotshape(condRevBuy, style=shape.triangleup, location=location.bottom, color=color.new(color.aqua, 0), size=size.tiny, title='reversal Buy')

condRevSell = shwRev and close < conversionLine and conversionLine < baseLine and baseLine < close[displacement - 1] and close > leadLine2[displacement - 1]
plotshape(condRevSell, style=shape.triangledown, location=location.bottom, color=color.new(color.purple, 0), size=size.tiny, title='reversal Sell')


// ——————————————————————————————————————————————————————————————
// >>>>>>>>>>>>>>>>>>>>>>>>>  Alerts <<<<<<<<<<<<<<<<<<<<<<<<<<<<
// ——————————————————————————————————————————————————————————————
tup     = condBuy and not condBuy[1] and shwCond
tdown   = condSell and not condSell[1] and shwCond
tRup    = condRevBuy and not condRevBuy[1] and shwRev
tRdown  = condRevSell and not condRevSell[1] and shwRev

if tdown
    alert("Regular SELL Ichimoku", alert.freq_once_per_bar_close)
if tup
    alert("Regular BUY Ichimoku", alert.freq_once_per_bar_close)   
if tRup
    alert("Reversal BUY Ichimoku", alert.freq_once_per_bar_close)  
if tRdown
    alert("Reversal SELL Ichimoku", alert.freq_once_per_bar_close)  







// ══════════════════════════════════════════════════════════════════════════════════════════════════ //
//# * ══════════════════════════════════════════════════════════════════════════════════════════════
//# *
//# * Study       : Trades Framework
//# * Author      : © pascorp
//# * Purpose     : Ability to optimize a study and observe trade simulation statistics accordingly  
//# *
//# * Revision History
//# *  Release    : Nov 30, 2021  : Initial Release
//# *  
//# *
//# * ══════════════════════════════════════════════════════════════════════════════════════════════
// ══════════════════════════════════════════════════════════════════════════════════════════════════ //

// ——————————————————————————————————————————————————————————————
// >>>>>>>>>>>>>>>>>>>>>>>>>  Inputs  <<<<<<<<<<<<<<<<<<<<<<<<<<<
// ——————————————————————————————————————————————————————————————
shwLines = input(true, title="Show trade lines", group="Trades", inline="Lines")
shwXbars = input(5, title="    per n bars", group="Trades", inline="Lines")

gap = input.float(defval=0.1, step=0.05,title="Enter Gap", group="Trades")
TP = input.float(defval=0.35, step=0.05,title="TakeProfit 1", group="Trades")
TP2 = input.float(defval=0.7, step=0.05,title="TakeProfit 2", group="Trades")
//BreakEven = input.float(defval=0.2, step=0.05,title="Break Even", group="Trades")
SL = input.float(defval=0.35, step=0.05,title="StopLoss", group="Trades")




// ——————————————————————————————————————————————————————————————
// >>>>>>>>>>>>>>>>>>>>  State Definition  <<<<<<<<<<<<<<<<<<<<<<
// ——————————————————————————————————————————————————————————————

// trend 
trendUp     = conversionLine > baseLine
trendDown   = conversionLine < baseLine


// Trigger exit fascia
trigger     = tup or tdown or tRup or tRdown


shwUp = false
shwDown = false
var enterUp = 0.0
var enterDown = 0.0

if trigger
    if trendDown
        enterDown :=  low*(1.0-gap/100)
        shwDown:=true
    if trendUp
        enterUp :=  high*(1.0+gap/100)
        shwUp:=true
        
        
plotshape(shwUp?enterUp:na, location=location.absolute, style=shape.diamond, color = color.green)
plotshape(shwDown?enterDown:na, location=location.absolute, style=shape.diamond, color = color.red)

shwTotalUp=false
for i = 1 to shwXbars
    shwTotalUp:= (shwTotalUp or shwUp[i]) and trendUp

shwTotalDown = false
for i = 1 to shwXbars
    shwTotalDown := (shwTotalDown or shwDown[i])  and trendDown

plot(enterUp, color =shwTotalUp and shwLines ? color.gray : color.rgb(100,100,100,100))
plot(enterUp*(1.0+TP/100), color =shwTotalUp and shwLines ? color.green : color.rgb(100,100,100,100))
plot(enterUp*(1.0+TP2/100), color =shwTotalUp and shwLines ? color.yellow : color.rgb(100,100,100,100))
plot(enterUp*(1.0-SL/100), color =shwTotalUp and shwLines ? color.purple : color.rgb(100,100,100,100))

plot(enterDown, color =shwTotalDown and shwLines ? color.gray : color.rgb(100,100,100,100))
plot(enterDown*(1.0-TP/100), color =shwTotalDown and shwLines ? color.green : color.rgb(100,100,100,100))
plot(enterDown*(1.0-TP2/100), color =shwTotalDown and shwLines ? color.yellow : color.rgb(100,100,100,100))
plot(enterDown*(1.0+SL/100), color =shwTotalDown and shwLines ? color.purple : color.rgb(100,100,100,100))

