TradingWolfLibary
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © TradingWolf
 
//@version=5
library("TradingWolfLibary")

// @function Gets a Moving Average based on type
// @param int length The MA period
// @param string maType The type of MA
// @returns A moving average with the given parameters
export getMA(string maType, simple int length) =>
    switch maType
        "EMA" => ta.ema(close, length)
        "SMA" => ta.sma(close, length)
        "HMA" => ta.hma(close, length)
        "WMA" => ta.wma(close, length)
        "VWMA" => ta.vwma(close, length)
        "VWAP" => ta.vwap
        => 
            e1 = ta.ema(close, length)
            e2 = ta.ema(e1, length)
            2 * e1 - e2

// @function Calculates and returns Minimum stop loss
// @param float entry price (Close if calculating on the entry candle)
// @param simple int Calculate how many bars back to look at swings
// @param float Minimum Stop Loss allowed (Should be x 0.01) if input
// @param string Direciton of trade either "Long" or "Short"
// @returns Stop Loss Value
export minStop(float _entry, simple int _swingDistance, float _minStop, string _direction) =>
    if _direction == "Long"
        swingLow    = ta.lowest(low, _swingDistance)
        distance    = ((_entry - swingLow)/swingLow)
        stop        = _minStop > distance ? _entry*(1-_minStop) : swingLow
    else if _direction == "Short"
        swingHigh   = ta.highest(high,_swingDistance)
        distance    = ((swingHigh-_entry)/_entry)
        stop        = _minStop > distance ? _entry*(1+_minStop) : swingHigh
         
// @function Calculates and returns Minimum/Maximum stop loss
// @param float entry price (Close if calculating on the entry candle)
// @param simple int Calculate how many bars back to look at swings
// @param float Minimum Stop Loss allowed (Should be x 0.01) if input
// @param float Maximum Stop Loss allowed (Should be x 0.01) if input
// @param string Direciton of trade either "Long" or "Short" 
// @returns Stop Loss Value 
export minmaxStop(float _entry,  float _swing, float _minStop, float _maxStop, string _direction) =>
    if _direction == "Long"
        distance    = ((_entry - _swing)/_swing)
        stop        = _minStop > distance ? _entry*(1-_minStop) : _maxStop < distance ? _entry*(1-_maxStop) : _swing
    else if _direction == "Short"
        distance    = ((_swing-_entry)/_entry)
        stop        = _minStop > distance ? _entry*(1+_minStop) : _maxStop < distance ? _entry*(1+_maxStop) : _swing   


// @function Calculates the take profit value
// @param float entry price (Close if calculating on the entry candle)
// @param float stop would be the variable calculated just before your TP, you can use the minmaxStop
// @param float ratio is a reward multiplier
// @param string direction is the direction of the trade 
// @returns Take proit value
export tpCalc(float _entry, float _stop, float _ratio, string _direction) =>
    if _direction == "Long"
        take = ((_entry - _stop)*_ratio) + _entry
    else if _direction == "Short"
        take = _entry - ((_stop - _entry)*_ratio) 
    




// @function Calculates the Stop Loss using ATR
// @param float entry price (Close if calculating on the entry candle)
// @param float ATR Risk
// @param string direction is the direction of the trade 
// @returns Take proit value
export atrStop(float _entry, float _risk, string _direction)=>
    atr = ta.rma(ta.tr(true), 14)
    if _direction == "Long"
        stop = _entry - (atr*_risk)
    else if _direction == "Short"
        stop = _entry + (atr*_risk)

// @function Calculates the Take profitusing ATR
// @param float entry price (Close if calculating on the entry candle)
// @param float ATR Reward
// @param string direction is the direction of the trade 
// @returns Take proit value
export atrTake(float _entry, float _reward, string _direction)=>
    atr = ta.rma(ta.tr(true), 14)
    if _direction == "Long"
        take = _entry + (atr*_reward)
    else if _direction == "Short"
        take = _entry - (atr*_reward)