彩色的移动平均指标EMA/SMA

Author: 雨幕(youquant), Date: 2022-05-27 14:04:40
Tags: EMAsma

策略回测

img

img

img


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

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


//@version=5
indicator('Moving Average Colored EMA/SMA', overlay=true, precision=2)
gr1 = "Moving Average Colored EMA/SMA"
emaplot = input(true, title='显示EMA在图表上',group=gr1)
len = input.int(13, minval=1, title='Ema周期',group=gr1)
src = close
out = ta.ema(src, len)
up = out > out[1]
down = out < out[1]
mycolor = up ? color.green : down ? color.red : color.blue
plot(out and emaplot ? out : na, title='EMA', color=mycolor, linewidth=3)


smaplot = input(true, title='显示SMA在图表上',group=gr1)
len2 = input.int(15, minval=1, title='Sma周期',group=gr1)
src2 = close
out2 = ta.sma(src2, len2)
up2 = out2 > out2[1]
down2 = out2 < out2[1]
mycolor2 = up2 ? color.green : down2 ? color.red : color.blue
plot(out2 and smaplot ? out2 : na, title='SMA', color=mycolor2, linewidth=1)


//===================== Holders Sentiment ==================================================

gr2 = 'Holders Sentiment'
length = input.int(20,title="周期",group=gr2)
src3 = input(close, title='数据源',group=gr2)

zeroline = 0
// hzplot = plot(zeroline, color=color.new(color.black, 0))
difabs = (src3[0] - src3[length]) / src3[length] * 100
plotColour = difabs > 0 ? color.green : color.red
// changeplot = plot(difabs, color=plotColour, linewidth=2)
// fill(plot1=changeplot, plot2=hzplot, color=color.new(color.blue, 90))

//================ Supertrend ================================================

gr3 = "Supertrend"
tf = input.timeframe('D', "引用数据的K线周期",group=gr3)
atrPeriod = input(10, "ATR周期",group=gr3)
factor = input.float(3.0, "系数", step = 0.01,group=gr3)

[supertrend, direction] = request.security(syminfo.tickerid, tf, ta.supertrend(factor, atrPeriod), barmerge.gaps_off, barmerge.lookahead_off)

bodyMiddle = plot((open + close) / 2, display=display.none)
upTrend = plot(direction < 0 ? supertrend : na, "Up Trend", color = color.green, style=plot.style_linebr)
downTrend = plot(direction < 0? na : supertrend, "Down Trend", color = color.red, style=plot.style_linebr)

//fill(bodyMiddle, upTrend, color.new(color.green, 90), fillgaps=false)
//fill(bodyMiddle, downTrend, color.new(color.red, 90), fillgaps=false)

//=================================================================================

bs1 = ta.crossover(out,out2) ? 1 : ta.crossunder(out,out2) ? -1 : 0
bs2 = difabs > 0 ? 1 : -1
bs3 = direction < 0 ? 1 : -1
bs = 0
bs := bs1 == 1 and bs2 == 1 and bs3 == 1 ? 1 : bs1 == -1 and bs2 == -1 and bs3 == -1 ? -1 : 0

buy = bs == 1 and nz(bs[1]) !=1
sell = bs == -1 and nz(bs[1]) != -1

//bgcolor(buy ? color.new(#00ff00, 50) : na)
//if buy
//    label.new(bar_index, low, yloc = yloc.belowbar, text="Buy", textcolor = #00ff00, size = size.large, style=label.style_none)
//
//bgcolor(sell ? color.new(#b92ad8, 80) : na)
//if sell
//    label.new(bar_index,high, yloc = yloc.abovebar, text="Sell", textcolor = #b92ad8, size = size.large, style=label.style_none)
    

alertcondition(buy, title='Buy Alert', message='Buy')
alertcondition(sell, title='sell Alert', message='Sell')


if buy
    strategy.entry("Enter Long", strategy.long)
else if sell
    strategy.entry("Enter Short", strategy.short)

相关内容

更多内容