EMA Trading Strategy


创建日期: 2023-09-11 12:02:56 最后修改: 2023-09-11 12:02:56
复制: 0 点击次数: 530
avatar of ChaoZhang ChaoZhang
1
关注
1361
关注者

EMA交易策略

该策略基于EMA均线进行判断,具体交易规则如下:

  • 如果前一天收盘价高于EMA均线,则第二天开盘做多入场。

  • 如果当前K线收盘价低于EMA均线,则平掉多单。

该策略的优点是:

  • 使用EMA判断趋势方向
  • 规则简单清晰,容易实现
  • 可自定义EMA周期进行优化

但该策略也存在一些问题:

  • 在盘整时容易产生错误信号
  • 入场时机较晚,容易被套
  • 没有止损设置,存在亏损风险
  • 没有考虑交易频率和资金管理

总体来说,EMA策略更适用于趋势行情,但需要谨慎使用,应加入止损和过滤条件来优化该策略。

策略源码
/*backtest
start: 2023-01-01 00:00:00
end: 2023-09-10 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Binance","currency":"BTC_USDT"}]
*/

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

//@version=5
strategy("EMA Strat", overlay=true, margin_long=100, margin_short=100)

// EMA Variables
emaInput = input(21, "Length")
ema = ta.ema(close, emaInput)

// Variable Declaration
p = 0

start = false

// Start Date
yearInput = input(2000, "Year")
if (time >= timestamp(2000,01,01,01,01))
    start := true


// Check first candle relative to EMA
if (close > ema and start == true)
    p += 1
    strategy.entry("Long", strategy.long, comment = "Entry")
    

// Candle close above EMA (p + 1, count reset to 0)
above = close[1] > ema[1]
if (above)
    p += 1



// Candle close below EMA (reset p to 0, count -1)
below = close < ema
if (below)
    p := 0
    strategy.close("Long", comment = "Flat")

// // Exit Position
// if (redCount == -2)
//     strategy.close("Long", comment = "Flat")
    
// goLong = p[1] == 0 and p == 1
// flatten = p == 0
    
// // Restablish long    
// if (goLong and start == true)
//     strategy.entry("Long", strategy.long, comment = "Entry")
    

plot(p)
plot(ema)