您当前的位置: > GTC 学习中心 > 帮助中心
联系我们
服务热线:15388665518

外汇EA程序详细注解汇总

时间:2025-12-03  来源:  作者:泽汇资本gtc

以下是注解

// ------------------------------------------------------------------

//| Moving Average.mq4 |

//| http://funion.com

// ------------------------------------------------------------------

#define MAGICMA 20050610 //定义本EA操作的订单的唯一标识号码

extern double Lots = 0.1;//每单的交易量

extern double MaximumRisk = 0.02;//作者定义的最大风险参数

extern double DecreaseFactor = 3;//作者定义的参数,作用要看程序中的用法

extern double MovingPeriod = 10;//EA中使用的均线的周期

extern double MovingShift =3;泽汇资本gtc//EA中使用的均线向左的K线偏移量

// ------------------------------------------------------------------

//| Calculate open positions http://funion.com

// ------------------------------------------------------------------

int CalculateCurrentOrders(string symbol)//函数作用,计算当前持仓订单的数量

{

int buys=0,sells=0;//定义两个临时变量,准备用于后面的多空订单的个数计算

//----

for(int i=0;i

{

if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;//挑出持仓单的每一个订单位置

if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA)//根据订单位置,比较是否是当前K线商品 以及订单唯一标识号是否和本程序设置的一致(用于避免EA误操作其他程序控制的持仓单)

{

if(OrderType()==OP_BUY) buys ;//找到符合条件的持仓单后,如果是多单,则临时变量buys增加1

if(OrderType()==OP_SELL) sells ;//找到符合条件的持仓单后,如果是空单,则临时变量sells增加1

}

}

//---- return orders volume

if(buys0) return(buys);

else return(-sells);//本函数返回查询计算结束时的持仓单的个数。

}

// ------------------------------------------------------------------

//| Calculate optimal lot size |http://funion.com

// ------------------------------------------------------------------

double LotsOptimized()//函数目的,根据要求 计算出订单交易量

{

double lot=Lots;

int orders=HistoryTotal(); // history orders total 历史出场订单的个数

int losses=0; // number of losses orders without a break

//---- select lot size

lot=NormalizeDouble(AccountFreeMargin()*MaximumRisk/1000.0,1);//通过风险系数的计算获得当前入场单应该采用的交易量

//---- calcuulate number of losses orders without a break

if(DecreaseFactor0)

{

for(int i=orders-1;i=0;i--)

{

if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) { Print("Error in history!"); break; }//循环查询出场单队列

if(OrderSymbol()!=Symbol() || OrderType()OP_SELL) continue;//

//----

if(OrderProfit()0) break;

if(OrderProfit()<0) losses ;//循环计算所有出场亏损单的亏损总和

}

if(losses1) lot=NormalizeDouble(lot-lot*losses/DecreaseFactor,1);//如果亏损额大于1,则下一入场单的交易量修正为新的计算结果。

}

//---- return lot size

if(lot<0.1) lot=0.1;//如果计算出的交易量小于帐户最小手数0.1,则下一入场单的交易手数使用0.1作为交易量

return(lot);

}

// ------------------------------------------------------------------

//| Check for open order conditions | http://funion.com

// ------------------------------------------------------------------

void CheckForOpen()//检查入场条件的情况并作处理

{

double ma;

int res;

//---- go trading only for first tiks of new bar

if(Volume[0]1) return;//如果当前K线持仓量大于1,说明不是K线的开盘时间点,则直接返回 否则是K线第一个价格,则继续下面的过程

//---- get Moving Average

ma=iMA(NULL,0,MovingPeriod,MovingShift,MODE_SMA,PRICE_CLOSE,0);//获得当前的均线数值

//---- sell conditions

if(Open[1]ma && Close[1]

{

res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,0,0,"",MAGICMA,0,Red);

return;

}

//---- buy conditions

if(Open[1]

{

res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,0,0,"",MAGICMA,0,Blue);

return;

}

//----

}

// ------------------------------------------------------------------

//| Check for close order conditions | http://funion.com

// ------------------------------------------------------------------

void CheckForClose()//检查出场条件的情况并作处理

{

double ma;

//---- go trading only for first tiks of new bar

if(Volume[0]1) return;

//---- get Moving Average

ma=iMA(NULL,0,MovingPeriod,MovingShift,MODE_SMA,PRICE_CLOSE,0);

//----

for(int i=0;i

{

if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;

if(OrderMagicNumber()!=MAGICMA || OrderSymbol()!=Symbol()) continue;

//---- check order type

if(OrderType()==OP_BUY)

{

if(Open[1]ma && Close[1]

break;

}

if(OrderType()==OP_SELL)

{

if(Open[1]

break;

}

}

//----

}

// ------------------------------------------------------------------

//| Start function | http://funion.com

// ------------------------------------------------------------------

void start()//主循环过程

{

//---- check for history and trading

if(Bars<100 || IsTradeAllowed()==false) return;

//---- calculate open orders by current symbol

if(CalculateCurrentOrders(Symbol())==0) CheckForOpen();

else CheckForClose();

//----

}

// ------------------------------------------------------------------