Moving Average profit targets with var size
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © dongyun

//@version=4
strategy("利润目标止损的移动平均线", overlay=true)

period = input(80,'')
ptper = input(252,'')
ptfactor = input(12,'')
sizeper = input(20, '')

trend = 0.0
signal = 0
size = 1.0
investment = 100000
atrange = 0.0
ptrange = 0.0
stoph = 0.0
stopl = 0.0


if sizeper != 0
	atrange := atr(sizeper)

if atrange == 0 or sizeper == 0 
	size := 1
else
	size := investment/atrange * 0.1

trend := sma(close,period)


if signal != 1 and nz(trend[1]) < nz(trend[2]) and trend > nz(trend[1])
	strategy.entry('long',strategy.long, comment='open_long')
	signal := 1
else
    signal := nz(signal[1])
    
if signal != -1 and nz(trend[1]) > nz(trend[2]) and trend < nz(trend[1])
	strategy.entry('short',strategy.short, comment='open_short')
	signal := -1
else
    if signal == 0
        signal := nz(signal[1])

ptrange := atr(ptper)

if strategy.position_size > 0
	strategy.exit("exit_long", "long", qty = strategy.position_size, limit = close + ptfactor*ptrange , comment='trail_long') 
else
	if strategy.position_size < 0
		strategy.exit("exit_short", "short", qty = abs(strategy.position_size), limit = close - ptfactor*ptrange, comment='trail_short')
