
本策略基于短期、中期和长期三条不同周期的指数移动平均线(EMA)进行交易信号生成。其中,短期EMA周期为5天,中期EMA周期为8天,长期EMA周期为13天。当短期EMA上穿中期和长期EMA时,做多;当短期EMA下穿中期和长期EMA时,做空。
该策略通过计算不同周期的EMA来判断市场趋势。短期EMA反映最近几天的平均价格,中长期EMA反映较长时间内的平均价格。短期EMA上穿中长期EMA代表价格开始向上突破,因此做多;短期EMA下穿中长期EMA代表价格开始向下突破,因此做空。
具体来说,该策略同时计算5天、8天和13天三条EMA。当5天EMA上穿8天和13天EMA时生成做多信号;当5天EMA下穿8天和13天EMA时生成做空信号。做多后,如果5天EMA重新下穿13天EMA,则平仓。做空后,如果5天EMA重新上穿13天EMA,则平仓。
可以通过以下方法优化:
本策略通过计算短中长三个周期EMA并比较其交叉情况来判断市场趋势转折,属于典型的突破系统。其优点是交易信号简单清晰,容易操作;缺点是EMA指标本身滞后,无法区分真正趋势和短期调整。未来可以考虑辅以其他技术指标判断,或结合自适应参数调整优化该策略。
/*backtest
start: 2023-11-16 00:00:00
end: 2023-11-23 00:00:00
period: 30m
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=4
//
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © gregoirejohnb
// @It is modified by ttsaadet.
// Moving average crossover systems measure drift in the market. They are great strategies for time-limited people.
// So, why don't more people use them?
//
//
strategy(title="EMA Crossover Strategy", shorttitle="EMA-5-8-13 COS by TTS", overlay=true, pyramiding=0, default_qty_type=strategy.percent_of_equity, default_qty_value=100, currency=currency.TRY,commission_type=strategy.commission.percent,commission_value=0.04, process_orders_on_close = true, initial_capital = 100000)
// === GENERAL INPUTS ===
//strategy start date
start_year = input(defval=2020, title="Backtest Start Year")
// === LOGIC ===
short_period = input(type=input.integer,defval=5,minval=1,title="Length")
mid_period = input(type=input.integer,defval=8,minval=1,title="Length")
long_period = input(type=input.integer,defval=13,minval=1,title="Length")
longOnly = input(type=input.bool,defval=false,title="Long Only")
shortEma = ema(hl2,short_period)
midEma = ema(hl2,mid_period)
longEma = ema(hl2,long_period)
plot(shortEma,linewidth=2,color=color.red,title="Fast")
plot(midEma,linewidth=2,color=color.orange,title="Fast")
plot(longEma,linewidth=2,color=color.blue,title="Slow")
longEntry = ((shortEma > midEma) and crossover(shortEma,longEma)) or ((shortEma > longEma) and crossover(shortEma,midEma))
shortEntry =((shortEma < midEma) and crossunder(shortEma,longEma)) or ((shortEma < longEma) and crossunder(shortEma,midEma))
plotshape(longEntry ? close : na,style=shape.triangleup,color=color.green,location=location.belowbar,size=size.small,title="Long Triangle")
plotshape(shortEntry and not longOnly ? close : na,style=shape.triangledown,color=color.red,location=location.abovebar,size=size.small,title="Short Triangle")
plotshape(shortEntry and longOnly ? close : na,style=shape.xcross,color=color.black,location=location.abovebar,size=size.small,title="Exit Sign")
// === STRATEGY - LONG POSITION EXECUTION ===
enterLong() =>
longEntry
exitLong() =>
crossunder(shortEma,longEma)
strategy.entry(id="Long", long=strategy.long, when=enterLong())
strategy.close(id="Long", when=exitLong())
// === STRATEGY - SHORT POSITION EXECUTION ===
enterShort() =>
not longOnly and shortEntry
exitShort() =>
crossover(shortEma,longEma)
strategy.entry(id="Short", long=strategy.short, when=enterShort())
strategy.close(id="Short", when=exitShort())