1,EA程式構成
對於一個智慧交易系統EA程式來說:主要由三個函式構成分別是:
init():初始化函式,負責程式變數及資料初始輸入;只在程式調入時執行一次,一般不用重寫內容。
deinit():反初始化函式,負責程式退出時,將資料從記憶體中清除;只在程式退出時,執行一次,一般不用重寫內容。
start():開始函式,也即程式的主函式,負責EA程式 的全部交易執行過程,實際上他是一個EA的交易管理與執行函式。每隔一定時間,一般幾秒之內,執行一次,就是迴圈執行,起到程式退出時終止。
執行流程:啟動EA後,程式的INTI()開始執行一次,——>然後 START()迴圈執行——->最後退出EA時deinit()執行一次
2,交易函式
2.1開倉函式
int OrderSend( string symbol, int cmd, double volume, double price, int slippage, double stoploss, double takeprofit, void comment, void magic, void expiration, void arrow_color)
這個功能主要應用於開倉位置和掛單交易,參量:
symbol - 交易貨幣對。
cmd - 購買方式。
volume - 購買手數。
price - 收盤價格。
slippage - 最大允許滑點數。
stoploss - 止損水平。
takeprofit - 贏利水平。
comment - 註解文字。
magic - 訂單指定碼。可以作為使用者指定識別碼使用。
expiration - 訂單有效時間(只限掛單)。
arrow_color - 圖表上箭頭顏色。如果參量丟失或存在CLR_NONE價格值不會在圖表中畫出
2。2平倉函式
bool OrderClose( int ticket, double lots, double price, int slippage, void Color)
對訂單進行平倉操作。如果函式成功,返回的值是真實的。如果函式失敗,返回的值是假的。獲得詳細錯誤資訊,請檢視GetLastError()函式。參量:
ticket - 訂單編號。
lots - 手數。
price - 收盤價格。
slippage - 最高劃點數。
Color - 圖表中標記顏色。如果參量丟失,CLR_NONE值將不會在圖表中畫出。
2。3訂單修改函式
bool OrderModify( int ticket, double price, double stoploss, double takeprofit, datetime expiration, void arrow_color)
對於先前的開倉或掛單進行特性修改。如果函式成功,返回的值為 TRUE。如果函式失敗,返回的值為FALSE。獲得詳細的錯誤資訊,檢視 GetLastError()函式。參量:
ticket -訂單編號。
price - 收盤價格。
stoploss - 新止損水平。
takeprofit - 新盈利水平。
expiration - 掛單有效時間。
arrow_color - 在圖表中允許對止損/贏利顏色進行修改。如果參量丟失或存在CLR_NONE 值,在圖表中將不會顯示。
3,交易流程
下面的原始碼是一個基於移動平均線的智慧交易系統的程式碼 ,整個程式非常簡潔但EA的功能又非常齊全,實現了完全由電腦自動下單和平倉,整個程式只用了一個START() 函式來實現 。
//+——————————————————————————————————+
//|modifiedEA。mq4 |
//| Copyright?2008, MetaQuotes SoftwareCorp。 |
//+——————————————————————————————————+
#propertylinkhttp://new。qzone。qq。com/809690298//—— input parameters
extern double TakeProfit = 20;
extern double StopLoss = 30;
extern double Lots = 2;
extern double TrailingStop = 50;
extern int ShortEma = 5;
extern int LongEma = 60;
//+——————————————————————————————————+
//| expertinitializationfunction |
//+——————————————————————————————————+
intinit()
{
//——
//——
return(0);
}
//+——————————————————————————————————+
//| expertdeinitializationfunction |
//+——————————————————————————————————+
intdeinit()
{
//——
//——
return(0);
}
//+——————————————————————————————————+
//| expert startfunction |
//+——————————————————————————————————+
intstart()
{
intcnt, ticket, total;
doubleSEma, LEma;
//——
if(Bars
{
Print(“barsless than 100”);
return(0);
}
//——
if(TakeProfit
{
Print(“TakeProfitless than 10”);
return(0); // check TakeProfit
}
//——
SEma= iMA(NULL, 0, ShortEma, 0, MODE_EMA, PRICE_CLOSE, 0);
LEma= iMA(NULL, 0, LongEma, 0, MODE_EMA, PRICE_CLOSE, 0);
//——
staticint isCrossed = 0;
isCrossed= Crossed(LEma, SEma);
//——
total= OrdersTotal();
if(total
{
if(isCrossed == 1) //滿足空倉條件,開空倉
{
ticket= OrderSend(Symbol(), OP_SELL, Lots, Bid, 3, Bid + StopLoss * Point,
Bid- TakeProfit * Point, “EMA_CROSS”, 12345, 0, Green);
if(ticket > 0)
{
if(OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES))
Print(“SELLorder opened : ”, OrderOpenPrice());
}else
Print(“Erroropening SELL order : ”, GetLastError());
return(0);
}
if(isCrossed == 2) //滿足多倉條件,開多倉
{
ticket= OrderSend(Symbol(), OP_BUY, Lots, Ask, 3, Ask - StopLoss * Point,
Ask+ TakeProfit * Point, “EMA_CROSS”, 12345, 0, Red);
if(ticket > 0)
{
if(OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES))
Print(“BUYorder opened : ”, OrderOpenPrice());
}else
Print(“Erroropening BUY order : ”, GetLastError());
return(0);
}
return(0);
}
//——訂單修改,實現動態止盈止損跟蹤
for(cnt = 0; cnt
{
OrderSelect(cnt,SELECT_BY_POS, MODE_TRADES);
if(OrderType()
{
if(OrderType() == OP_SELL) // long position is opened
{
// check fortrailing stop
if(TrailingStop > 0)
{
if(Bid - OrderOpenPrice() > Point * TrailingStop)
{
if(OrderStopLoss()
{
OrderModify(OrderTicket(),OrderOpenPrice(),
Bid- Point * TrailingStop,
OrderTakeProfit(),0, Green);
return(0);
}
}
}
}else // go to short position
{
// check fortrailing stop
if(TrailingStop > 0)
{
if((OrderOpenPrice() - Ask) > (Point * TrailingStop))
{
if((OrderStopLoss() > (Ask + Point * TrailingStop)))
{
OrderModify(OrderTicket(),OrderOpenPrice(),
Ask+ Point * TrailingStop,
OrderTakeProfit(),0, Red);
return(0);
}
}
}
}
}
}
//——
return(0);
}
//+——————————————————————————————————+
//移動平均線多空條件判斷,
intCrossed(double line1, double line2)
{
staticint last_direction = 0;
staticint current_direction = 0;
//Don‘t work inthe first load, wait for the first cross!
staticbool first_time = true;
if(first_time == true)
{
first_time= false;
return(0);
}
//——
if(line1 > line2)
current_direction= 2; //up多頭市場 上穿做多
if(line1
current_direction= 1; //down空頭市場 下穿做空
//——
if(current_direction != last_direction) //changed多空改變 {
last_direction= current_direction;
return(last_direction);
else return(0); //not changed
}