这是一个基于两个不同时间周期的指数移动平均线交叉做多空的自动交易策略。它使用简单的技术指标,非常适合新手学习和实践。
该策略使用两个指数移动平均线,一个是大时间周期的均线,一个是当前周期的均线。当当前周期的均线上穿大周期的均线时,做多;当当前周期的均线下穿大周期的均线时,做空。
具体来说,策略首先定义两个均线参数:
然后分别计算出两个EMA:
最后,进入交易逻辑:
这样,通过不同时间周期均线的交叉来判断趋势方向,进行自动交易。
该策略具有以下优势:
该策略也存在一些风险:
可以通过设置止损,优化参数组合,或加入其它指标等方式来降低风险。
该策略可以从以下几个方面进行优化:
该指数移动平均交叉策略运用简单指标捕捉趋势,适合新手学习实践。优化空间较大,可以引入更多技术指标和模型进行改进,开发出具有更强效果的量化交易策略。
/*backtest
start: 2023-09-16 00:00:00
end: 2023-10-16 00:00:00
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=3
strategy("Noro's Singapore Strategy", shorttitle = "Singapore str", overlay = true, default_qty_type = strategy.percent_of_equity, default_qty_value = 100, pyramiding = 0)
//Settings
needlong = input(true, defval = true, title = "Long")
needshort = input(true, defval = true, title = "Short")
capital = input(100, defval = 100, minval = 1, maxval = 10000, title = "Lot")
tf = input("D", title = "Big Timeframe")
len = input(3, minval = 1, title = "MA length")
src = input(close, title = "MA Source")
fromyear = input(1900, defval = 1900, minval = 1900, maxval = 2100, title = "From Year")
toyear = input(2100, defval = 2100, minval = 1900, maxval = 2100, title = "To Year")
frommonth = input(01, defval = 01, minval = 01, maxval = 12, title = "From Month")
tomonth = input(12, defval = 12, minval = 01, maxval = 12, title = "To Month")
fromday = input(01, defval = 01, minval = 01, maxval = 31, title = "From day")
today = input(31, defval = 31, minval = 01, maxval = 31, title = "To day")
//MAs
ma1 = request.security(syminfo.tickerid, tf, sma(src, len))
ma2 = sma(src, len)
plot(ma1, linewidth = 2, color = blue, title = "Big TF MA")
plot(ma2, linewidth = 2, color = red, title = "MA")
//Trading
size = strategy.position_size
lot = 0.0
lot := size != size[1] ? strategy.equity / close * capital / 100 : lot[1]
if ma2 > ma1
strategy.entry("L", strategy.long, needlong ? lot : 0, when = (time > timestamp(fromyear, frommonth, fromday, 00, 00) and time < timestamp(toyear, tomonth, today, 23, 59)))
if ma2 < ma1
strategy.entry("S", strategy.short, needshort ? lot : 0, when = (time > timestamp(fromyear, frommonth, fromday, 00, 00) and time < timestamp(toyear, tomonth, today, 23, 59)))
if time > timestamp(toyear, tomonth, today, 23, 59)
strategy.close_all()