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

//@version=5

// @description TODO: add library description here
library("OfekIndicatorsLib")


///--------ICHI CLOUDS--------//
donchian(len) => math.avg(ta.lowest(len), ta.highest(len))
export ichiClouds(int conversionPeriods,int basePeriods,int laggingSpan2Periods) =>
	conversionLine = donchian(conversionPeriods)
	baseLine = donchian(basePeriods)
	leadLine1 = math.avg(conversionLine, baseLine)
	leadLine2 = donchian(laggingSpan2Periods)
	[conversionLine,baseLine,leadLine1,leadLine2]


//-------------TRAMA--------------//
export trama(series float tramaSrc,int tramaLength) => 
    ama = 0.
    hh = math.max(math.sign(ta.change(ta.highest(tramaLength))),0)
    ll = math.max(math.sign(ta.change(ta.lowest(tramaLength))*-1),0)
    tc = math.pow(ta.sma(hh or ll ? 1 : 0,tramaLength),2)
    ama := nz(ama[1]+tc*(tramaSrc-ama[1]),tramaSrc)
    result = ama


//-------------KDJ--------------//
bcwsma(s, l, m) =>
    _bcwsma = float(na)
    _s = s
    _l = l
    _m = m
    _bcwsma := (_m * _s + (_l - _m) * nz(_bcwsma[1])) / _l
    result = _bcwsma
    
export kdj(int ilong,int isig,int startFrom = 0) => 
    c = close[startFrom]
    h = ta.highest(high[startFrom], ilong)
    l = ta.lowest(low[startFrom],ilong)
    RSV = 100*((c-l)/(h-l))
    pK = bcwsma(RSV, isig, 1)
    pD = bcwsma(pK, isig, 1)
    pJ = 3 * pK-2 * pD
    [pK,pD,pJ]



 
/// FSVZO - 
// default inputs - 
// src0  = input.source (close , 'Source'                 )
// len   = input.int    (45    , 'VZO Length'   , minval=1)
// malen = input.int    (7     , 'MA Length'    , minval=1)
// flen  = input.int    (7     , 'Fisher Length', minval=1)
zone(_src, _len,malen) =>
    vol = volume
    src = ta.wma(2 * ta.wma(_src, malen / 2) - ta.wma(_src, malen), math.round(math.sqrt(malen)))
    vp = src > src[1] ? vol : src < src[1] ? -vol : src == src[1] ? 0 : 0
    z = 100 * (ta.ema(vp, _len) / ta.ema(vol, _len))


export FSVZO(series float src0 = close,simple int len = 45,simple int malen = 7,simple int flen = 7) =>
    // CALC FISHER //---------------------------------------------------------------------------------
    fsrc = zone(_src = src0,_len = len,malen = malen)
    MaxH = ta.highest (fsrc , flen)
    MinL = ta.lowest  (fsrc , flen)
    var nValue1 = 0.0
    var nFish = 0.0


    nValue1 := 0.33 * 2 * ((fsrc  - MinL) / (MaxH - MinL) - 0.5) + 0.67 * nz(nValue1[1])
    nValue2  = (nValue1 > 0.99 ?  0.999 : (nValue1 < -0.99 ? -0.999: nValue1))
    nFish   := 0.5 * math.log((1 + nValue2) / (1 - nValue2)) + 0.5 * nz(nFish[1])

    f1 = nFish 
    f2 = nz(nFish[1])

    fzvzo_up = f1 > f2 
    [fzvzo_up,f1,f2]

// puell muell-
// default inputs - 
// top = input.float(0.6, step=0.1)
// bottom = input.float(4, step=0.01)
// miningRevenue = request.security('QUANDL:BCHAIN/MIREV', 'D', close[1], barmerge.gaps_off, barmerge.lookahead_on)
// ma365 = request.security('QUANDL:BCHAIN/MIREV', 'D', ta.sma(close, 365)[1], barmerge.gaps_off, barmerge.lookahead_on)
export puellMultiple(float top = 0.6,float bottom = 4,series float miningRevenue, series float ma365) => 
    puellMultiple = miningRevenue / ma365
    smonadnu = puellMultiple < top 
    smonavrhu = puellMultiple > bottom
    [puellMultiple,smonadnu,smonavrhu]

// BB % 
// default inputs -
// src = input(close, title="Source")
// mult = input.float(2.0, minval=0.001, maxval=50, title="StdDev")
// length = input.int(20, minval=1)
export BB(series float src = close,simple float mult = 2.0, simple int length = 20) => 
    basis = ta.sma(src, length)
    dev = mult * ta.stdev(src, length)
    upper = basis + dev
    lower = basis - dev
    bbr = (src - lower)/(upper - lower)
    [upper,lower,bbr]