- 显示:
- 47
- 等级:
- 已发布:
-
需要基于此代码的EA交易或指标吗?请在自由职业者服务中订购 进入自由职业者服务
#property copyright "Samson Mwita 2025" #property link "Samson Mwita 2025" #property version "1.00" #property description "Advanced price action-based dynamic exit strategy" #property description "Monitors Fibonacci, market structure, candlestick patterns" #property description "and behavioral patterns for optimal exit timing" //--- 输入参数 input double ProfitActivationPercent = 70.0; // 当交易距离目标 X% 时开始监控 input bool UseRSIReversal = true; // 使用 RSI 进行反转检测 input int RSIPeriod = 14; // RSI 周期 input double RSIOverbought = 70.0; // RSI 超买水平 input double RSI_Oversold = 30.0; // RSI 超卖水平 input bool UseCandlestickPatterns = true; // 使用蜡烛图形态 input bool UseMovingAverageCross = false; // 使用 MA 十字星进行确认 input int FastMA_Period = 5; // 快速 MA 周期 input int SlowMA_Period = 10; // 慢速 MA 周期 input int CheckEveryXSeconds = 10; // 检查频率(秒) //+------------------------------------------------------------------+ //| 脚本程序启动功能| //+------------------------------------------------------------------+ void OnStart() { //--- 显示脚本启动信息 Print("Dynamic Exit Protector Started - Monitoring Trades..."); //--- 创建一个用于持续监控的计时器 EventSetTimer(CheckEveryXSeconds); } //+------------------------------------------------------------------+ //| 定时器功能--每 X 秒运行一次 //+------------------------------------------------------------------+ void OnTimer() { CheckAndProtectTrades(); } //+------------------------------------------------------------------+ //| 检查和保护交易的主要功能 //+------------------------------------------------------------------+ void CheckAndProtectTrades() { int total = PositionsTotal(); for(int i = total-1; i >= 0; i--) { ulong ticket = PositionGetTicket(i); if(ticket > 0) { string symbol = PositionGetString(POSITION_SYMBOL); double volume = PositionGetDouble(POSITION_VOLUME); double openPrice = PositionGetDouble(POSITION_PRICE_OPEN); double currentProfit = PositionGetDouble(POSITION_PROFIT); ulong type = PositionGetInteger(POSITION_TYPE); double sl = PositionGetDouble(POSITION_SL); double tp = PositionGetDouble(POSITION_TP); //--- 如果未设置止盈,则跳过 if(tp == 0) continue; double currentPrice = (type == POSITION_TYPE_BUY) ? SymbolInfoDouble(symbol, SYMBOL_BID) : SymbolInfoDouble(symbol, SYMBOL_ASK); //-- 计算我们离 TP 有多近(百分比) double distanceToTP = 0; double progressToTP = 0; if(type == POSITION_TYPE_BUY) { distanceToTP = tp - openPrice; progressToTP = (currentPrice - openPrice) / distanceToTP * 100; } else // 卖出位置 { distanceToTP = openPrice - tp; progressToTP = (openPrice - currentPrice) / distanceToTP * 100; } //--- 如果我们足够接近 TP,检查反转情况 if(progressToTP >= ProfitActivationPercent) { bool shouldClose = false; string reason = ""; //--- 检查 RSI 反转(如果启用) if(UseRSIReversal && CheckRSI_Reversal(symbol, type)) { shouldClose = true; reason = "RSI Reversal Signal"; } //--- 检查烛台形态(如果启用) if(UseCandlestickPatterns && CheckCandlestickReversal(symbol, type)) { shouldClose = true; reason = "Candlestick Reversal Pattern"; } //--- 检查 MA Cross(如果启用) if(UseMovingAverageCross && CheckMA_Cross(symbol)) { shouldClose = true; reason = "Moving Average Cross"; } //--- 附加安全性:如果利润开始大幅下降 if(IsProfitDecreasing(symbol, ticket, currentProfit)) { shouldClose = true; reason = "Profit Retracement Detected"; } //--- 如果满足任何反转条件,则关闭交易 if(shouldClose) { if(ClosePosition(ticket, symbol, volume, type)) { Print("Position #", ticket, " closed. Reason: ", reason, " | Progress to TP: ", DoubleToString(progressToTP, 1), "%", " | Final Profit: ", DoubleToString(currentProfit, 2)); } } } } } } //+------------------------------------------------------------------+ //| 检查 RSI 是否有反转信号| //+------------------------------------------------------------------+ bool CheckRSI_Reversal(string symbol, ulong positionType) { double rsi[]; ArraySetAsSeries(rsi, true); int handle = iRSI(symbol, PERIOD_CURRENT, RSIPeriod, PRICE_CLOSE); if(CopyBuffer(handle, 0, 0, 3, rsi) > 0) { // 买入头寸:关注 RSI 是否进入超买并转为下跌 if(positionType == POSITION_TYPE_BUY) { if(rsi[0] < rsi[1] && rsi[1] > RSIOverbought) return true; } // 卖出头寸:关注 RSI 是否进入超卖并转为上升 else { if(rsi[0] > rsi[1] && rsi[1] < RSI_Oversold) return true; } } return false; } //+------------------------------------------------------------------+ //| 检查蜡烛图反转形态 //+------------------------------------------------------------------+ bool CheckCandlestickReversal(string symbol, ulong positionType) { MqlRates rates[]; ArraySetAsSeries(rates, true); if(CopyRates(symbol, PERIOD_CURRENT, 0, 3, rates) > 0) { // 用于买入头寸的简单看跌反转形态 if(positionType == POSITION_TYPE_BUY) { // 检查看跌吞没或射击之星 if((rates[1].close > rates[1].open && rates[0].close < rates[0].open && rates[0].close < rates[1].open) || // 看跌吞没 (rates[0].high - MathMax(rates[0].open, rates[0].close) > MathAbs(rates[0].close - rates[0].open) * 2)) // 流星 return true; } // 卖出仓位的简单看涨反转形态 else { // 检查看涨吞没或锤形 if((rates[1].close < rates[1].open && rates[0].close > rates[0].open && rates[0].close > rates[1].open) || // 看涨吞没 (MathMin(rates[0].open, rates[0].close) - rates[0].low > MathAbs(rates[0].close - rates[0].open) * 2)) // 锤子 return true; } } return false; } //+------------------------------------------------------------------+ //| 检查移动平均线交叉| //+------------------------------------------------------------------+ bool CheckMA_Cross(string symbol) { double fastMA[], slowMA[]; ArraySetAsSeries(fastMA, true); ArraySetAsSeries(slowMA, true); int fast_handle = iMA(symbol, PERIOD_CURRENT, FastMA_Period, 0, MODE_SMA, PRICE_CLOSE); int slow_handle = iMA(symbol, PERIOD_CURRENT, SlowMA_Period, 0, MODE_SMA, PRICE_CLOSE); if(CopyBuffer(fast_handle, 0, 0, 2, fastMA) > 0 && CopyBuffer(slow_handle, 0, 0, 2, slowMA) > 0) { // 检查看跌交叉(快速 MA 穿过慢速 MA 下方) if(fastMA[1] > slowMA[1] && fastMA[0] < slowMA[0]) return true; } return false; } //+------------------------------------------------------------------+ //| 检查利润是否大幅下降 //+------------------------------------------------------------------+ bool IsProfitDecreasing(string symbol, ulong ticket, double currentProfit) { // 在实际执行中,您可能需要跟踪利润历史记录 // 这是一个简化版本--你可以存储以前的利润值 // 并比较利润是否减少了一定百分比 // 目前,我们将使用一种基于价格走势的简单方法 static double lastProfit = 0; static ulong lastTicket = 0; if(lastTicket != ticket) { lastProfit = currentProfit; lastTicket = ticket; return false; } // 如果利润从峰值下降 20% 以上 if(currentProfit < lastProfit * 0.8 && currentProfit > 0) { return true; } // 如果当前利润较高,则更新上次利润 if(currentProfit > lastProfit) { lastProfit = currentProfit; } return false; } //+------------------------------------------------------------------+ //| 关闭位置功能| //+------------------------------------------------------------------+ bool ClosePosition(ulong ticket, string symbol, double volume, ulong type) { MqlTradeRequest request = {1}; MqlTradeResult result = {0}; request.action = TRADE_ACTION_DEAL; request.position = ticket; request.symbol = symbol; request.volume = volume; request.deviation = 10; request.comment = "Dynamic Exit"; if(type == POSITION_TYPE_BUY) { request.type = ORDER_TYPE_SELL; request.price = SymbolInfoDouble(symbol, SYMBOL_BID); } else { request.type = ORDER_TYPE_BUY; request.price = SymbolInfoDouble(symbol, SYMBOL_ASK); } return OrderSend(request, result); } //+------------------------------------------------------------------+ //| 退出初始化函数| //+------------------------------------------------------------------+ //+------------------------------------------------------------------+
由MetaQuotes Ltd译自英文
原代码: https://www.mql5.com/en/code/66130
VR Locker Lite - 基于正向锁的交易策略
通过正向锁进行操作,交易机器人创建一个正向锁,交易者自行决定如何处理它。
交易策略正面或反面 (Heads or Tails)
经典版本的正面或反面交易策略与分析信号块代码。
iCrosshair - Real-Time Candle Metrics on Hover
将鼠标悬停在任何蜡烛上,查看 MT5 不显示的内容:范围大小、蜡烛体百分比、蜡烛芯比率。智能、快速、可定制。
离散
离散技术指标由价格和交易量的变化决定。
