Forex bot full strategy with risk management
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © exlux99

//@version=4

strategy("Scalping FOrex full strategy with risk management",overlay=true,initial_capital = 10000, default_qty_type= strategy.percent_of_equity, default_qty_value = 100, calc_on_order_fills=false, slippage=0,commission_type=strategy.commission.cash_per_contract ,commission_value=0.00005)

//VOLUME o
shortlen = input(1, minval=1, title = "Short Length OBV")
longlen = input(8, minval=1, title = "Long Length OBV")
upColor = #2196F3//input(#2196F3, "Color Up")
dnColor = #6B1599//input(#6B1599, "Color Down")
f_normGradientColor(_series, _crossesZero, _colorNormLen, _dnColor, _upColor) =>
    _dnValue = _crossesZero?-100:0
    _mult = 0.0
    _lowest = lowest(_colorNormLen)
    _highest = highest(_colorNormLen)
    _diff1 = close - _lowest
    _diff2 = _highest - _lowest
    if _diff2 > 0
        _mult := _diff1 / _diff2 * 100
    color.from_gradient(sign(_series) * _mult, _dnValue, 100, _dnColor, _upColor)
shorta = ema(volume, shortlen)
longa = ema(volume, longlen)
osc = 100 * (shorta - longa) / longa

start = input(0.1, title="PSAR START")
increment = input(0.05,title="PSAR INC")
maximum = input(0.3, title="PSAR MAX")
// multitp=input(1)
// multisl=input(1)
var bool uptrend = na
var float EP = na
var float SAR = na
var float AF = start
var float nextBarSAR = na
if bar_index > 0
    firstTrendBar = false
    SAR := nextBarSAR
    if bar_index == 1
        float prevSAR = na
        float prevEP = na
        lowPrev = low[1]
        highPrev = high[1]
        closeCur = close
        closePrev = close[1]
        if closeCur > closePrev
            uptrend := true
            EP := high
            prevSAR := lowPrev
            prevEP := high
        else
            uptrend := false
            EP := low
            prevSAR := highPrev
            prevEP := low
        firstTrendBar := true
        SAR := prevSAR + start * (prevEP - prevSAR)
    if uptrend
        if SAR > low
            firstTrendBar := true
            uptrend := false
            SAR := max(EP, high)
            EP := low
            AF := start
    else
        if SAR < high
            firstTrendBar := true
            uptrend := true
            SAR := min(EP, low)
            EP := high
            AF := start
    if not firstTrendBar
        if uptrend
            if high > EP
                EP := high
                AF := min(AF + increment, maximum)
        else
            if low < EP
                EP := low
                AF := min(AF + increment, maximum)
    if uptrend
        SAR := min(SAR, low[1])
        if bar_index > 1
            SAR := min(SAR, low[2])
    else
        SAR := max(SAR, high[1])
        if bar_index > 1
            SAR := max(SAR, high[2])
    nextBarSAR := SAR + AF * (EP - SAR)
//    if barstate.isconfirmed
    
//         if uptrend
//             strategy.entry("ParSE", strategy.short, stop=nextBarSAR, comment="ParSE")
//             strategy.cancel("ParLE")
//         else
//             strategy.entry("ParLE", strategy.long, stop=nextBarSAR, comment="ParLE")
//             strategy.cancel("ParSE")
//plot(SAR, style=plot.style_cross, linewidth=3, color=color.orange)


psarshort = close- SAR
psarlong= SAR-close

 


lena = input(200, minval=1, title="Length EMA")
srca = input(close, title="Source")
out = ema(srca, lena)

fast_length = input(title="Fast Length MACD", type=input.integer, defval=12)
slow_length = input(title="Slow Length MACD", type=input.integer, defval=25)
src = input(title="Source", type=input.source, defval=close)
signal_length = input(title="Signal Smoothing MACD", type=input.integer, minval = 1, maxval = 50, defval = 9)
sma_source = input(title="Simple MA (Oscillator)", type=input.bool, defval=true)
sma_signal = input(title="Simple MA (Signal Line)", type=input.bool, defval=true)
// Calculating
fast_ma = sma_source ? sma(src, fast_length) : ema(src, fast_length)
slow_ma = sma_source ? sma(src, slow_length) : ema(src, slow_length)
macd = fast_ma - slow_ma
signal = sma_signal ? sma(macd, signal_length) : ema(macd, signal_length)
hist = macd - signal


long = hist[1]<0 and hist > 0 and close > out and uptrend and osc < 0
short = hist[1]>0 and hist< 0 and close < out and not uptrend and osc >0

// ------------------------- Strategy Logic --------------------------------- //
var longOpeneds = false
var shortOpeneds = false
var int timeOfBuys = na
var float tpLong = na
var float slLong = na
var int entrys = na

 

longConditions = long and not longOpeneds and entrys<100

if longConditions
    longOpeneds := true
    timeOfBuys := time
    tpLong := close+ (psarshort) //* multitp)
    slLong := close- (psarshort)//*multisl)
    entrys:=entrys+1


tpLongTrigger = (longOpeneds[1] and (crossover(close, tpLong) or crossover( high,tpLong)))
slLongTrigger = (longOpeneds[1] and (crossunder(close, slLong) or crossunder( low,slLong)))

longExitSignals =  slLongTrigger or tpLongTrigger  or short
exitLongConditions = longOpeneds[1] and longExitSignals

if exitLongConditions
    longOpeneds := false
    timeOfBuys := na
    tpLong := na
    slLong := na
    
if(short)
    entrys:=0

//short
// ------------------------- Strategy Logic --------------------------------- //
var longOpenedss = false
// var shortOpeneds = false
var int timeOfBuyss = na
var float tpLongs = na
var float slLongs = na
var int entry = na

 

longConditionss = short and not longOpenedss and entry<100

if longConditionss
    longOpenedss := true
    timeOfBuyss := time
    tpLongs := close- (psarlong)//*multitp )
    slLongs := close+ (psarlong)//*multisl)
    entry:=1

 

tpLongTriggers = (longOpenedss[1] and ( crossunder(close, tpLongs) or crossunder( low,tpLongs)))
slLongTriggers = (longOpenedss[1] and (crossover(close, slLongs) or crossover( high,slLongs)))

longExitSignalss =  slLongTriggers or tpLongTriggers  or long
exitLongConditionss = longOpenedss[1] and longExitSignalss

if exitLongConditionss
    longOpenedss := false
    timeOfBuyss := na
    tpLongs := na
    slLongs := na

if(long)
    entry:=0
    
longEntry=input(true)
shortEntry=input(true)

if(longEntry)
    strategy.entry("long",1,when=longConditions)
    strategy.close('long',when=exitLongConditions)

if(shortEntry)
    strategy.entry("short",0,when=longConditionss)
    strategy.close("short",when=exitLongConditionss)
