《MT4EA编程速成教程》中第33页习题
第一题:将交易信号改成均线交叉,金叉做多,死叉做空。
金叉死叉,需要获取小均线两个点,大均线两个点,然后比较四个点的大小变化,就能判断金叉和死叉。
当黄色为小均线,红色为大均线 当 A>B 并且 C>D 就是金叉。
当黄色为大均线,红色为小均线 当 A>B 并且 C>D 就是死叉。
在之前一单一结的EA基础上,只需要修改signal()这个信号函数。
代码修改如下:
int signal(){int res=0;double smallma1=iMA(NULL,0,8,0,MODE_SMA,PRICE_CLOSE,1);double bigma1=iMA(NULL,0,26,0,MODE_SMA,PRICE_CLOSE,1);double smallma2=iMA(NULL,0,8,0,MODE_SMA,PRICE_CLOSE,2);double bigma2=iMA(NULL,0,26,0,MODE_SMA,PRICE_CLOSE,2);if(smallma1>bigma1 && smallma2<bigma2){res=1;}if(smallma1<bigma1 && smallma2>bigma2){res=-1;}return(res);}
完整代码如下:
//+------------------------------------------------------------------+
//| Test_EA_05_1.mq4 |
//| 云开 |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "http://www.forexmt4.cn"
#property link "http://www.forexmt4.cn"#property description "【漆天编程】 习题EA1"
#property description " "
#property description "这是一款测试EA,作者QQ:80364276"
#property description " "
#property description "发布时间:2021.10.26"
#property strict
#property icon "//Images//sea.ico"input double lots=0.1; //交易手数
input int SL=600; //止损点数
input int TP=200; //止盈点数//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit(){
//---
//---return(INIT_SUCCEEDED);}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason){
//---}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick(){int buys=0; //多单持仓有几笔int sells=0; //空单持仓有几笔int signal=signal();for(int i=0; i<OrdersTotal(); i++){if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){if(OrderSymbol()==Symbol() && OrderMagicNumber()==16384 && OrderType()==OP_BUY){buys++;if(signal<0){bool res=OrderClose(OrderTicket(),OrderLots(),Bid,0);if(res){Print("订单平仓成功");}return;}if(OrderStopLoss()==0){bool res=OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-SL*Point,OrderOpenPrice()+TP*Point,0);if(res)Print("订单修改成功");}}if(OrderSymbol()==Symbol() && OrderMagicNumber()==16384 && OrderType()==OP_SELL){sells++;if(OrderStopLoss()==0){bool res=OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+SL*Point,OrderOpenPrice()-TP*Point,0);if(res)Print("订单修改成功");}if(signal>0){bool res=OrderClose(OrderTicket(),OrderLots(),Ask,0);if(res){Print("订单平仓成功");}return;}}}}
//---if(signal>0 && buys==0){int ticket=OrderSend(Symbol(),OP_BUY,lots,Ask,3,0,0,"My buy order",16384,0,clrGreen);if(ticket<0){Print("OrderSend failed with error #",GetLastError());}else{Print("OrderSend placed successfully");}}if(signal<0 && sells==0){int ticket=OrderSend(Symbol(),OP_SELL,lots,Bid,3,0,0,"My sell order",16384,0,clrRed);if(ticket<0){Print("OrderSend failed with error #",GetLastError());}else{Print("OrderSend placed successfully");}}}
//+------------------------------------------------------------------+
int signal(){int res=0;double smallma1=iMA(NULL,0,8,0,MODE_SMA,PRICE_CLOSE,1);double bigma1=iMA(NULL,0,26,0,MODE_SMA,PRICE_CLOSE,1);double smallma2=iMA(NULL,0,8,0,MODE_SMA,PRICE_CLOSE,2);double bigma2=iMA(NULL,0,26,0,MODE_SMA,PRICE_CLOSE,2);if(smallma1>bigma1 && smallma2<bigma2){res=1;}if(smallma1<bigma1 && smallma2>bigma2){res=-1;}return(res);}
//+------------------------------------------------------------------+