- 显示:
- 219
- 等级:
- 已发布:
-
需要基于此代码的EA交易或指标吗?请在自由职业者服务中订购 进入自由职业者服务
如果使用止损或不使用止损,该代码块均可工作。
要求
- 您需要包含 "Trade.mqh " 以访问 CTrade 类,该类允许您处理头寸和订单。
#include <Trade\Trade.mqh> // <<------------------------------------------ 加入此 "Trade.mqh "以访问 CTrade 类
- 您需要设置一个输入参数,以便根据需要调整追踪距离。这不是必须的,但为了方便起见。
input double Traling_Step = 3.0;
- 您需要为 CTrade 类定义实例。最好在OnInt 事件处理程序之后定义。
- 然后,您需要创建一个if 语句 来检查当前是否有任何仓位在运行。该语句调用Check_TrailingStop(); 函数来处理每一个跳动点。这一点很重要,因为 EA 的跟踪应该清晰流畅。请注意将此语句放在 OnTick 事件处理程序的顶部,以便正常工作。
//+------------------------------------------------------------------+ //| 专家初始化函数| //+------------------------------------------------------------------+ int OnInit() { //--- 创建计时器 EventSetTimer(60); //--- return(INIT_SUCCEEDED); } CTrade trade; // <<------------------------------------------ 声明 "CTrade "调用。您可以将 "trade "替换为任何您想要的名称。
void OnTick() { if(PositionsTotal() > 0) 如果有/正在运行的头寸,则 // 调用每个跳动点的移动止损函数。 { Check_TralingStop(); } }
- 您需要声明一个名为Check_TrailingStop();( 在本例中) 的自定义函数来完成其余工作。您可以使用任意名称。
- 该自定义函数会在所有已打开的位置 中循环,并按照所需的距离跟踪它们。
void Check_TralingStop() { int totalPositions = PositionsTotal(); for(int count =0; count < totalPositions; count++) { ulong TicketNo = PositionGetTicket(count); // 使用位置的 "索引 "获取位置票编号。 if(PositionSelectByTicket(TicketNo)) // 使用票号选择位置(我们已经选择了票号) { if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY) // 检查位置类型。 { double openPrice = PositionGetDouble(POSITION_PRICE_OPEN); double stopLoss = PositionGetDouble(POSITION_SL); // <<-------------------Get Position Current Stop Loss double takeProfit = PositionGetDouble(POSITION_TP); double bidPrice = SymbolInfoDouble(_Symbol,SYMBOL_BID); ulong ticket = PositionGetTicket(count); double trailingLevel = NormalizeDouble(bidPrice - (Traling_Step * Point()),_Digits); if(stopLoss < openPrice) // 没有止损为真。 { if(bidPrice > openPrice && trailingLevel > openPrice) // 每个位置只运行一次。设置第一个 SL。 trade.PositionModify(ticket,trailingLevel,takeProfit); // 使用 "CTrade.trade "修改尾随止损 } if(bidPrice > openPrice && trailingLevel > stopLoss) // 检查后置水平是否高于前置水平。 { trade.PositionModify(ticket,trailingLevel,takeProfit); // 使用 "CTrade.trade "修改尾随止损 } } if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL) { double openPrice = PositionGetDouble(POSITION_PRICE_OPEN); double stopLoss = PositionGetDouble(POSITION_SL); double takeProfit = PositionGetDouble(POSITION_TP); double bidPrice = SymbolInfoDouble(_Symbol,SYMBOL_BID); double askPrice = SymbolInfoDouble(_Symbol,SYMBOL_ASK); ulong ticket = PositionGetTicket(count); double trailingLevel = NormalizeDouble(askPrice + (Traling_Step * Point()),_Digits); if(stopLoss < openPrice) // 没有止损为真。 { if(askPrice < openPrice && trailingLevel < openPrice) // 每个位置只运行一次。设置第一个 SL。 trade.PositionModify(ticket,trailingLevel,takeProfit); // 使用 "CTrade.trade "修改尾随止损 } if(askPrice < openPrice && trailingLevel < stopLoss) // 检查后置水平是否高于前置水平。 { trade.PositionModify(ticket,trailingLevel,takeProfit); // 使用 "CTrade.trade "修改尾随止损 } } } } }
由MetaQuotes Ltd译自英文
原代码: https://www.mql5.com/en/code/49021
Simple Code for Detect A "New Bar or New Candle " Received
该代码块在收到新条形图或新蜡烛图时进行检测。
Logarithmic Moving Average
对数移动平均法连续计算一段时间内最高价和最低价的对数平均值。