该策略是一个结合了指数移动平均线(EMA)和相对强弱指标(RSI)的动态趋势跟踪系统。它通过9周期和21周期的EMA交叉来识别趋势方向,并使用RSI作为趋势确认指标。该策略还包含了完整的资金管理系统,包括动态止损和获利目标的设置。
策略的核心逻辑基于以下几个关键要素: 1. 使用短期(9周期)和长期(21周期)EMA的交叉来捕捉趋势的变化 2. 通过14周期RSI指标进行趋势确认,要求RSI>50时才能做多,RSI<50时才能做空 3. 采用固定点数的止损设置(默认30点),并根据风险金额动态计算持仓规模 4. 利用资金管理参数动态计算获利目标价格 5. 在图表上实时显示入场标记、目标价位和止损位置
该策略通过结合EMA交叉和RSI确认建立了一个完整的趋势跟踪系统。它的主要优势在于将技术分析与风险管理有机结合,具有良好的可扩展性和适应性。虽然存在一些固有的风险,但通过持续优化和参数调整,该策略可以为交易者提供一个稳健的交易框架。
/*backtest
start: 2024-02-10 00:00:00
end: 2025-02-08 08:00:00
period: 1d
basePeriod: 1d
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Lukhi24
//@version=6
strategy("Lukhi EMA Crossover_TWL educational strategy", overlay=true)
// Input Parameters
capital = input.float(15000, title="Capital (₹)", tooltip="Total capital")
risk_per_trade = input.float(1000, title="Risk per Trade (₹)", tooltip="Risk per trade amount")
target_per_trade = input.float(5000, title="Take Profit per Trade (₹)", tooltip="Target profit per trade")
lot_size = input.int(1, title="Lot Size", tooltip="Nifty option lot size")
stop_loss_distance = input.float(30, title="Stop Loss Distance (Points)", tooltip="Fixed stop-loss in points")
// EMA Parameters
short_ema_length = input.int(9, title="Short EMA Length")
long_ema_length = input.int(21, title="Long EMA Length")
// RSI Parameters
rsi_length = input.int(14, title="RSI Length")
rsi_overbought = input.float(70, title="RSI Overbought Level")
rsi_oversold = input.float(30, title="RSI Oversold Level")
// Calculate EMAs and RSI
ema_short = ta.ema(close, short_ema_length)
ema_long = ta.ema(close, long_ema_length)
rsi = ta.rsi(close, rsi_length)
// Buy and Sell Signals
buy_signal = ta.crossover(ema_short, ema_long) and rsi > 50
sell_signal = ta.crossunder(ema_short, ema_long) and rsi < 50
// Plot EMAs
plot(ema_short, color=color.blue, title="EMA Short")
plot(ema_long, color=color.orange, title="EMA Long")
// Position Size Calculation
position_size = risk_per_trade / stop_loss_distance
// Stop Loss and Take Profit Levels
long_stop_loss = close - stop_loss_distance
long_take_profit = close + (target_per_trade / position_size)
short_stop_loss = close + stop_loss_distance
short_take_profit = close - (target_per_trade / position_size)
// Entry and Exit Logic
if buy_signal
strategy.entry("Buy", strategy.long, qty=lot_size)
strategy.exit("Exit Buy", "Buy", stop=long_stop_loss, limit=long_take_profit)
if sell_signal
strategy.entry("Sell", strategy.short, qty=lot_size)
strategy.exit("Exit Sell", "Sell", stop=short_stop_loss, limit=short_take_profit)
// Add Entry Signal Labels
var label long_label = na
var label short_label = na
if buy_signal
label.delete(long_label)
long_label := label.new(bar_index,close,text="BUY\nEntry: " + str.tostring(close, "#.##") + "\nTarget: " + str.tostring(long_take_profit, "#.##") + "\nSL: " + str.tostring(long_stop_loss, "#.##"),style=label.style_label_up,color=color.rgb(12, 90, 90, 73),textcolor=#010000)
if sell_signal
label.delete(short_label)
short_label := label.new(bar_index,close,text="SELL\nEntry: " + str.tostring(close, "#.##") + "\nTarget: " + str.tostring(short_take_profit, "#.##") + "\nSL: " + str.tostring(short_stop_loss, "#.##"),style=label.style_label_down,color=#5d371752,textcolor=#000000)
// Trade Failure Indicators
long_trade_loss = strategy.position_size > 0 and close <= long_stop_loss
short_trade_loss = strategy.position_size < 0 and close >= short_stop_loss
plotshape(long_trade_loss, location=location.belowbar, color=color.red, style=shape.cross, title="Long Trade Failed", text="SL Hit")
plotshape(short_trade_loss, location=location.abovebar, color=color.red, style=shape.cross, title="Short Trade Failed", text="SL Hit")