如果使用止损或不使用止损,该代码块均可工作。
要求
#include <Trade\Trade.mqh> // <<------------------------------------------ 加入此 "Trade.mqh "以访问 CTrade 类
input double Traling_Step = 3.0;
//+------------------------------------------------------------------+ //| 专家初始化函数| //+------------------------------------------------------------------+ int OnInit() { //--- 创建计时器 EventSetTimer(60); //--- return(INIT_SUCCEEDED); } CTrade trade; // <<------------------------------------------ 声明 "CTrade "调用。您可以将 "trade "替换为任何您想要的名称。
void OnTick() { if(PositionsTotal() > 0) 如果有/正在运行的头寸,则 // 调用每个跳动点的移动止损函数。 { Check_TralingStop(); } }
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
对数移动平均法连续计算一段时间内最高价和最低价的对数平均值。