资源加载中... loading...

SAR + 3SMMA 和 SL & TP

Author: 雨幕(youquant), Date: 2022-05-25 11:44:07
Tags: smaSAR

此脚本是SAR策略和3个平滑移动平均值的组合。

策略: 当所有3个SMMA都上升时,需要SAR多头。当所有3个SMMA都下降时,做空SAR。支持StopLoss和TakeProfit。

回测测试

img

img

img


/*backtest
start: 2021-12-01 00:00:00
end: 2022-05-24 00:00:00
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_CTP","currency":"FUTURES"}]
args: [["ContractType","i2209",360008]]
*/

//@version=5
//strategy(title="SAR + 3SMMA with SL & TP", overlay=true, calc_on_order_fills=false, calc_on_every_tick=false, default_qty_type=strategy.percent_of_equity, default_qty_value=100, currency=currency.USD, commission_type= strategy.commission.percent, commission_value=0.03)
start = input.float(0.02, step=0.01, group="SAR", title="开始")
increment = input.float(0.02, step=0.01, group="SAR", title="增量")
maximum = input.float(0.2, step=0.01, group="SAR", title="最大值")

//Take Profit Inputs     
take_profit = input.float(title="止盈 (%)", minval=0.0, step=0.1, defval = 0.1, group="止损和止盈", inline="TP") * 0.01

//Stop Loss Inputs
stop_loss = input.float(title="止损 (%)", minval=0.0, step=0.1, defval=1, group="止损和止盈", inline="SL") * 0.01

// Smooth Moving Average
fastSmmaLen = input.int(21, minval=1, title="快线周期", group = "SMA")
midSmmaLen = input.int(50, minval=1, title="中线周期", group = "SMA")
slowSmmaLen = input.int(200, minval=1, title="慢线周期", group = "SMA")

src = input(close, title="数据源", group = "SMA")

smma(ma, src, len) => 
    smma = 0.0
    smma := na(smma[1]) ? ma : (smma[1] * (len - 1) + src) / len
    smma

fastSma = ta.sma(src, fastSmmaLen)
midSma = ta.sma(src, midSmmaLen)
slowSma = ta.sma(src, slowSmmaLen)

fastSmma = smma(fastSma, src, fastSmmaLen)
midSmma = smma(midSma, src, midSmmaLen)
slowSmma = smma(slowSma, src, slowSmmaLen)

isSmmaUpward = ta.rising(fastSmma, 1) and ta.rising(midSmma, 1) and ta.rising(slowSmma, 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 := math.max(EP, high)
			EP := low
			AF := start
	else
		if SAR < high
			firstTrendBar := true
			uptrend := true
			SAR := math.min(EP, low)
			EP := high
			AF := start
	if not firstTrendBar
		if uptrend
			if high > EP
				EP := high
				AF := math.min(AF + increment, maximum)
		else
			if low < EP
				EP := low
				AF := math.min(AF + increment, maximum)
	if uptrend
		SAR := math.min(SAR, low[1])
		if bar_index > 1
			SAR := math.min(SAR, low[2])
	else
		SAR := math.max(SAR, high[1])
		if bar_index > 1
			SAR := math.max(SAR, high[2])
	nextBarSAR := SAR + AF * (EP - SAR)

sarIsUpTrend = uptrend ? true : false

sarFlippedDown = sarIsUpTrend and not sarIsUpTrend[1] ? true : false
sarFlippedUp = not sarIsUpTrend and sarIsUpTrend[1] ? true : false


longEntryCondition = isSmmaUpward and sarFlippedDown
shortEntryCondition = not isSmmaUpward and sarFlippedUp

if(longEntryCondition)
    strategy.entry("L", strategy.long, stop=nextBarSAR, comment="L")

if(shortEntryCondition)
    strategy.entry("S", strategy.short, stop=nextBarSAR, comment="S")


strategy.exit("CL", when=strategy.position_size > 0, limit=strategy.position_avg_price * (1+take_profit), stop=strategy.position_avg_price*(1-stop_loss))
strategy.exit("CS", when=strategy.position_size < 0, limit=strategy.position_avg_price * (1-take_profit), stop=strategy.position_avg_price*(1+stop_loss))


plot(SAR, style=plot.style_cross, linewidth=1, color=color.orange)
plot(nextBarSAR, style=plot.style_cross, linewidth=1, color=color.aqua)
plot(series = fastSmma, title="fastSmma", linewidth=1)
plot(series = midSmma, title="midSmma", linewidth=2)
plot(series = slowSmma, title="slowSmma", linewidth=3)

// plotchar(series=isSmmaUpward, title="isSmmaUpward", char='')
// plotchar(series=sarIsUpTrend, title="sarIsUpTrend", char='')
// plotchar(series=sarFlippedUp, title="sarFlippedUp", char='')
// plotchar(series=sarFlippedDown, title="sarFlippedDown", char='')
// plotchar(series=longEntryCondition, title="longEntryCondition", char='')
// plotchar(series=shortEntryCondition, title="shortEntryCondition", char='')
// plotchar(series=strategy.position_size > 0, title="inLong", char='')
// plotchar(series=strategy.position_size < 0, title="inShort", char='')


//plot(strategy.equity, title="equity", color=color.red, linewidth=2, style=plot.style_areabr)



相关内容

更多内容