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

//@version=4

strategy("polynomic_stop", overlay=true, initial_capital=1000, commission_value=0.1, default_qty_type=strategy.percent_of_equity, default_qty_value=100)



D = input(0.1, minval = 0.0001, title = 'decrement')
S = input(2, minval = 1.0, title = 'polynomial degree ')



MA = input(20, title = 'period SMA')
MN = input(20, title = 'period MIN_for')



SMA = sma(close, MA)
MIN = lowest(low, MN)




var stop = 0.0
var num = 0
if strategy.opentrades[1] == 0 and strategy.opentrades != 0
    stop := MIN


    
if  strategy.opentrades != 0
    num := num + 1 
    
if  strategy.opentrades == 0
    num := 0
    stop := MIN


    
hl = stop + D * pow(num, S)


plot(hl)
plot(SMA, color = color.red)



strategy.entry("buy", true, when = close[1] < SMA[1] and close > SMA)

strategy.close("buy", when = crossover(hl, close))



