GMH : ATH 200d 
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © gmhfund
//@version=5

strategy(title='ATH Strategy',overlay=true ,default_qty_type=strategy.percent_of_equity ,initial_capital=10000, default_qty_value=100 ,currency=currency.USD)

// Backtest Period
on_customPeriod = input.bool(true, "Backtesting custom dates")
start_date = input.time(timestamp("1 Jan 2019"), "From")
end_date = input.time(timestamp("1 Jan 2025"), "to")


checkPeriod() =>
    time >= start_date and time <= end_date ? true : false
testPeriod = checkPeriod()
isPeriod = on_customPeriod == true ? testPeriod : true


//Input

bar_ath = input.int(200,"ATH Period",minval=5,maxval=500,step=1)                    // ATH days

sl = input.int(10,"%Stoploss",minval=0,maxval=100,step=1)                           // Stoploss %

on_percent = input.bool(true,"Close order 1/2 position when reach profit target ")  // Profit Target Mode : ON
pt1 = input.int(50,"%Profit Target 1",minval=0,maxval=2000,step=1)
pt2 = input.int(100,"%Profit Target 2",minval=0,maxval=2000,step=1)
pt3 = input.int(300,"%Profit Target 3",minval=0,maxval=2000,step=1)

on_cross = input.bool(true,"Close order all position when cross under SMA")         // CrossUnder Mode : ON
bar_sma = input.int(90,"sma_length",minval=0,maxval=300,step=1)                     // SMA

var target_price = 0.0
var target1 = 0.0
var target2 = 0.0
var target3 = 0.0
var stock_share = 0.0



ath = ta.highest(bar_ath)[1]  // Seeking price of highest bar , exclude current one 
plot(ath,color=color.white)

sma = ta.sma(close,bar_sma)[1]
plot(sma)


// Price //

if close > ath and strategy.position_size == 0        // Update target price when ATH condition occurred
    stock_share := math.floor(strategy.equity/close)
    target1 := close*(1+pt1/100)
    target2 := close*(1+pt2/100)
    target3 := close*(1+pt3/100)


string pt_label = ""

if strategy.position_size >= stock_share             // Hold full position
    target_price := target1                            
    pt_label := str.tostring(pt1)
else if strategy.position_size >= stock_share/2      // Remain half position
    target_price := target2
    pt_label := str.tostring(pt2)
else if strategy.position_size >= stock_share/4      // Remain 1/4 position
    target_price := target3
    pt_label := str.tostring(pt3)
    
//  Long Condition
strategy.entry("long", strategy.long, stock_share, when = close > ath, comment = "buy" + " @" + str.tostring(close))

//  Close Condition
// 1) by % stoploss
strategy.close("long", when = close < strategy.position_avg_price*(1-sl/100) , comment = "stoploss" + " @" + str.tostring(close) , qty_percent = 100)

// 2) by target price
strategy.close("long", when = on_percent == true and close > target_price, comment = "hit target"+ pt_label + "%" + " @" + str.tostring(close) , qty_percent = 50)

// 3) by sma crossunder
strategy.close("long", when = on_cross == true and ta.crossunder(close,sma) == true, comment = "sma cross" + " @" + str.tostring(close) , qty_percent = 100)

// END


if not isPeriod
    strategy.cancel_all()
    strategy.close_all()



