MT4自帶的EA,高手做的詳細注解 [未知]
//+------------------------------------------------------------------+
//| Moving Average.mq4 |
//| http://www.imt4.com |
//+------------------------------------------------------------------+
#define MAGICMA 20050610
extern double Lots = 0.1;
extern double MaximumRisk = 0.02;
extern double DecreaseFactor = 3;
extern double MovingPeriod = 10;
extern double MovingShift =3;
//+------------------------------------------------------------------+
//| Calculate open positions |
//+------------------------------------------------------------------+
int CalculateCurrentOrders(string symbol)
{
int buys=0,sells=0;
//----
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA)
{
if(OrderType()==OP_BUY) buys++;
if(OrderType()==OP_SELL) sells++;
}
}
//---- return orders volume
if(buys>0) return(buys);
else return(-sells);
}
//+------------------------------------------------------------------+
//| Calculate optimal lot size |
//+------------------------------------------------------------------+
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(DecreaseFactor>0)
{
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(losses>1) lot=NormalizeDouble(lot-lot*losses/DecreaseFactor,1);
}
//---- return lot size
if(lot<0.1) lot=0.1;
return(lot);
}
//+------------------------------------------------------------------+
//| Check for open order conditions |
//+------------------------------------------------------------------+
void CheckForOpen()
{
double ma;
int res;
//---- 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);
//---- sell conditions
if(Open[1]>ma && Close[1]<ma)
{
res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,0,0,"",MAGICMA,0,Red);
return;
}
//---- buy conditions
if(Open[1]<ma && Close[1]>ma)
{
res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,0,0,"",MAGICMA,0,Blue);
return;
}
//----
}
//+------------------------------------------------------------------+
//| Check for close order conditions |
//+------------------------------------------------------------------+
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<OrdersTotal();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]<ma) OrderClose(OrderTicket(),OrderLots(),Bid,3,White);
break;
}
if(OrderType()==OP_SELL)
{
if(Open[1]<ma && Close[1]>ma) OrderClose(OrderTicket(),OrderLots(),Ask,3,White);
break;
}
}
//----
}
//+------------------------------------------------------------------+
//| Start function |
//+------------------------------------------------------------------+
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();
//----
}
//+------------------------------------------------------------------+
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
以下是注解
//+------------------------------------------------------------------+
//| Moving Average.mq4 |
//| http://www.imt4.com |
//+------------------------------------------------------------------+
#define MAGICMA 20050610 //定義本EA操作的訂單的唯一標識號碼
extern double Lots = 0.1;//每單的交易量
extern double MaximumRisk = 0.02;//作者定義的最大風險參數(shù)
extern double DecreaseFactor = 3;//作者定義的參數(shù),作用要看程序中的用法
extern double MovingPeriod = 10;//EA中使用的均線的周期
extern double MovingShift =3;//EA中使用的均線向左的K線偏移量
//+------------------------------------------------------------------+
//| Calculate open positions |
//+------------------------------------------------------------------+
int CalculateCurrentOrders(string symbol)//函數(shù)作用,計算當前持倉訂單的數(shù)量
{
int buys=0,sells=0;//定義兩個臨時變量,準備用于后面的多空訂單的個數(shù)計算
//----
for(int i=0;i<OrdersTotal();i++)//循環(huán)檢測當前的訂單隊列
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;//挑出持倉單的每一個訂單位置
if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA)//根據(jù)訂單位置,比較是否是當前K線商品 以及訂單唯一標識號是否和本程序設置的一致(用于避免EA誤操作其他程序控制的持倉單)
{
if(OrderType()==OP_BUY) buys++;//找到符合條件的持倉單后,如果是多單,則臨時變量buys增加1
if(OrderType()==OP_SELL) sells++;//找到符合條件的持倉單后,如果是空單,則臨時變量sells增加1
}
}
//---- return orders volume
if(buys>0) return(buys);
else return(-sells);//本函數(shù)返回查詢計算結(jié)束時的持倉單的個數(shù)。
}
//+------------------------------------------------------------------+
//| Calculate optimal lot size |
//+------------------------------------------------------------------+
double LotsOptimized()//函數(shù)目的,根據(jù)要求 計算出訂單交易量
{
double lot=Lots;
int orders=HistoryTotal(); // history orders total 歷史出場訂單的個數(shù)
int losses=0; // number of losses orders without a break
//---- select lot size
lot=NormalizeDouble(AccountFreeMargin()*MaximumRisk/1000.0,1);//通過風險系數(shù)的計算獲得當前入場單應該采用的交易量
//---- calcuulate number of losses orders without a break
if(DecreaseFactor>0)
{
for(int i=orders-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) { Print("Error in history!"); break; }//循環(huán)查詢出場單隊列
if(OrderSymbol()!=Symbol() || OrderType()>OP_SELL) continue;//
//----
if(OrderProfit()>0) break;
if(OrderProfit()<0) losses++;//循環(huán)計算所有出場虧損單的虧損總和
}
if(losses>1) lot=NormalizeDouble(lot-lot*losses/DecreaseFactor,1);//如果虧損額大于1,則下一入場單的交易量修正為新的計算結(jié)果。
}
//---- return lot size
if(lot<0.1) lot=0.1;//如果計算出的交易量小于帳戶最小手數(shù)0.1,則下一入場單的交易手數(shù)使用0.1作為交易量
return(lot);
}
//+------------------------------------------------------------------+
//| Check for open order conditions |
//+------------------------------------------------------------------+
void CheckForOpen()//檢查入場條件的情況并作處理
{
double ma;
int res;
//---- go trading only for first tiks of new bar
if(Volume[0]>1) return;//如果當前K線持倉量大于1,說明不是K線的開盤時間點,則直接返回 否則是K線第一個價格,則繼續(xù)下面的過程
//---- get Moving Average
ma=iMA(NULL,0,MovingPeriod,MovingShift,MODE_SMA,PRICE_CLOSE,0);//獲得當前的均線數(shù)值
//---- sell conditions
if(Open[1]>ma && Close[1]<ma) //如當前K開盤價大于均線,而前一K收盤價小于均線,則發(fā)出入場多單
{
res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,0,0,"",MAGICMA,0,Red);
return;
}
//---- buy conditions
if(Open[1]<ma && Close[1]>ma) //如當前K開盤價小于均線,而前一K收盤價大于均線,則發(fā)出入場空單
{
res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,0,0,"",MAGICMA,0,Blue);
return;
}
//----
}
//+------------------------------------------------------------------+
//| Check for close order conditions |
//+------------------------------------------------------------------+
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<OrdersTotal();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]<ma) OrderClose(OrderTicket(),OrderLots(),Bid,3,White);//如果持倉是多單,則當當前K開盤價小于均線,而前一K收盤價大于均線,則發(fā)出平倉指令
break;
}
if(OrderType()==OP_SELL)
{
if(Open[1]<ma && Close[1]>ma) OrderClose(OrderTicket(),OrderLots(),Ask,3,White););//如果持倉是空單,則當當前K開盤價大于均線,而前一K收盤價小于均線,則發(fā)出平倉指令
break;
}
}
//----
}
//+------------------------------------------------------------------+
//| Start function |
//+------------------------------------------------------------------+
void start()//主循環(huán)過程
{
//---- check for history and trading
if(Bars<100 || IsTradeAllowed()==false) return;
//---- calculate open orders by current symbol
if(CalculateCurrentOrders(Symbol())==0) CheckForOpen();
else CheckForClose();
//----
}
//+------------------------------------------------------------------+
本文來源于http://www.ea80.com ,原文地址:http://www.ea800.com/bbsx/thread-5228-1-1.html
- 上一篇:MT4編程入門(一):MT4自定義指標的基本…
- 下一篇:沒有了!
相關(guān)文章
-
沒有相關(guān)內(nèi)容