手机版

新手快速学会怎样编写外汇EA

阅读 :
外_汇_邦 WaiHuiBang.com

 # 什么是MT4?

我就当你了解什么是MT4了
MT4就是META. TRADER 4

# 什么是EA?
我也当你了解什么是EA了
EA 就是 Expert Adviser
EA 就是智能交易系统
智能教育系统就是:
-软件自己判断下订单
-软件自己判断提成利润
-软件自己判断砍仓止输

# 什么是速成班?
就是你在很短很快的时间内就可以学会如何编写EA
是的,快则8个学习小时
慢则24个学习小时

# 什么叫“易学难精”?
就是你很快的就上手,难在成为厉害的高手。
这需要时间慢慢的去研究编写。
久而久之你就可以是EA编写高手了。

 

 

第一课::了解EA的形式


* 以下就是EA的编写形式//

#property copyright "外汇联盟 www.FXunion.com QQ群144033"

#property link      ""


//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+ 

 


#property copyright "外汇联盟 www.FXunion.com QQ群144033"

#property link      ""


写与不写都无所谓
我们也不用理他


* //+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
//以后可能有用到
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//以后可能有用到
//----
return(0);
}

这两个很少用到。高高手才会用
速成班的话,这个可以不用理他。
放在一边先,以后才学怎样用它。



* //+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----
//主程式
//----
return(0);
}
//+------------------------------------------------------------------+

这个就是EA开始操作的重要部分
以上的EA的编写形式我们改成如下:


#property copyright "外汇联盟 www.FXunion.com QQ群144033"

#property link      ""


//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
/以后可能有用到
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
/以后可能有用到
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----
//主程式
//----
return(0);
}
//+------------------------------------------------------------------+ 


* 什么是“.mq4”?
EA制作好了就save成.mq4的文件
这文件是可以修改的

* 什么是“.ex4”?
EA制作好了
按“编写”就会自动save成.ex4的文件
这文件是不可以被修改的
这文件是加密的
可以出售的文件。
 

 

 

第二课:EA重要部分




* 回到EA重要部分


//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----
//主程式
//----
return(0);
}
//+------------------------------------------------------------------+

* 我们要知道EA是由上面往下读的
所以我们在编写方面要有逻辑思考
以下是编写的形式

//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----

MoneyManagement();
macd();
ma();
zhangchang();
buyme();
sellme();
xianshi();
//----
return(0);
}
//+------------------------------------------------------------------+
void MoneyManagement() {
}
//+------------------------------------------------------------------+
void macd() {
}
//+------------------------------------------------------------------+
void ma() {
}
//+------------------------------------------------------------------+
void zhangchang() {
}
//+------------------------------------------------------------------+
void buyme() {
}
//+------------------------------------------------------------------+
void sellme() {
}
//+------------------------------------------------------------------+
void buyme() {
}
//+------------------------------------------------------------------+
void xianshi() {
}
//+------------------------------------------------------------------+
/////end


* // 这两划代表右边的不要读
也可以说是注解
注意:EA不会读中文符号。比如这个“”
这个就可以 ""



#property copyright "外汇联盟 www.FXunion.com QQ群144033"

#property link      ""


//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
//以后可能有用到
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//以后可能有用到
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----
//主程式
MoneyManagement();//资金管理
macd();//指标管理
ma();//指标管理
zhangchang();//战场管理
buyme();//买上
sellme();//买下
xianshi();//显示管理

//----
return(0);
}
//+------------------------------------------------------------------+
void MoneyManagement() {
//子程式
//这边要写一些东西
}
//+------------------------------------------------------------------+
void macd() {
//子程式
//这边要写一些东西
}
//+------------------------------------------------------------------+
void ma() {
//子程式
//这边要写一些东西
}
//+------------------------------------------------------------------+
void zhangchang() {
///子程式
//这边要写一些东西
}
//+------------------------------------------------------------------+
void buyme() {
//子程式
//这边要写一些东西
}
//+------------------------------------------------------------------+
void sellme() {
//子程式
//这边要写一些东西
}
//+------------------------------------------------------------------+
void buyme() {
//子程式
//这边要写一些东西
}
//+------------------------------------------------------------------+
void xianshi() {
//子程式
//这边要写一些东西
}
//+------------------------------------------------------------------+
/////end


* 如果你在上面加这个xianshi2();
下面就加这个

//////////////////////////
void xianshi2() {
//这边要写一些东西
}
////////////////////////////

可以任意无限的加。。。
 

 

第三课::加入指标


* //////////////////////////////
void ma()
{
ma1now = iMA(NULL,0,ma1,0,MODE_EMA,PRICE_CLOSE,0);
ma1now2 = iMA(NULL,0,ma1,0,MODE_EMA,PRICE_CLOSE,1);

ma2now = iMA(NULL,0,ma2,0,MODE_EMA,PRICE_CLOSE,0);
ma2now2 = iMA(NULL,0,ma2,0,MODE_EMA,PRICE_CLOSE,1);

ma1bnow = iMA(NULL,0,ma1,0,MODE_EMA,PRICE_OPEN,0);
ma2bnow = iMA(NULL,0,ma2,0,MODE_EMA,PRICE_OPEN,0);
}
////////////////////////

ma1now是代表的字
你可以写成什么都可以
PRICE_CLOSE是ma最新的参数
PRICE_OPEN是ma开始的参数

ma1now = iMA(NULL,0,ma1,0,MODE_EMA,PRICE_CLOSE,0);
这个整句是死的。
你只要copy&press就可以用了

ma1是ma的参数号码或bar
如果这样写法,他人不能更改参数
double ma1=1,ma2=2;
double ma1now,ma1now2,ma2now,ma2now2,ma1bnow,ma2bnow;


这样写法,他人可以更改参数
extern double ma1=1,ma2=2;
这个是要放在#property上面那边的


* 其它指标的写法
macd1now=iMACD(NULL,0,macd1,macd1*2,1,PRICE_CLOSE, MODE_MAIN,0);
zigzag1now=iCustom(NULL,0,"ZigZag",2,deviation,bac kstep,0,0);
Highest1now=High[iHighest(NULL,0,MODE_HIGH,Highest1,0)];
Lowest1now=Low[iLowest(NULL,0,MODE_LOW,Lowest1,0)];
iTrend1now=iCustom(NULL,0,"iTrend",2,0,0,iTrend1,0 ,iTrend1,0,0,1);
wpr1now = iWPR(Symbol(),0,wpr1,0);

指标写法是不用记住的
网上可以找到很多指标的写法


* 有了指标
我们可以更改我们的EA了
更改如下:
注意看看
刚刚的指标加在什么地方
什么位置

#property copyright "外汇联盟 www.FXunion.com QQ群144033"

#property link      ""

extern double ma1=1,ma2=2;
double ma1now,ma1now2,ma2now,ma2now2,ma1bnow,ma2bnow;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----

MoneyManagement();//资金管理
macd();//指标管理
ma();//指标管理
zhangchang();//战场管理
buyme();//买上
sellme();//买下
xianshi();//显示管理
//----
return(0);
}
//+------------------------------------------------------------------
+void MoneyManagement() {
//这边要写一些东西
}
//+------------------------------------------------------------------+
void macd() {
//这边要写一些东西
}
//+------------------------------------------------------------------+
void ma()
{
ma1now = iMA(NULL,0,ma1,0,MODE_EMA,PRICE_CLOSE,0);
ma1now2 = iMA(NULL,0,ma10,0,MODE_EMA,PRICE_CLOSE,1);

ma2now = iMA(NULL,0,ma2,0,MODE_EMA,PRICE_CLOSE,0);
ma2now2 = iMA(NULL,0,ma2,0,MODE_EMA,PRICE_CLOSE,1);

ma1bnow = iMA(NULL,0,ma1,0,MODE_EMA,PRICE_OPEN,0);
ma2bnow = iMA(NULL,0,ma2,0,MODE_EMA,PRICE_OPEN,0);

}
//+------------------------------------------------------------------+
void zhangchang() {
//这边要写一些东西
}
//+------------------------------------------------------------------+
void buyme() {
//这边要写一些东西
}
//+------------------------------------------------------------------+
void sellme() {
//这边要写一些东西
}
//+------------------------------------------------------------------+
void buyme() {
//这边要写一些东西
}
//+------------------------------------------------------------------+
void xianshi() {
//这边要写一些东西
}
//+------------------------------------------------------------------+
/////end


* 注意这个{},不要糊涂的删除了也不知道
会出错的
注意这个可以写成这样
效果一样
double ma1=1;
double ma2=2;
double ma1now;
double ma1now2;
double ma2now;
double ma2now2;
double ma1bnow;
double ma2bnow;


* extern double ma1=1;
extern double ma2=2;
这样写可以显示在EA开始的修改处
 

 

第四课-资金管理



* 这个将放在#property上面那边
////////////////////////
extern string _资金管理_ ="任选一个";
extern bool zijiguanli1 = true;//false true、、
extern bool zijiguanli2= false;//false true、、
extern bool zijiguanli3= false;//false true、、
///////////////////////////////////
true=要
false =不要用
我提供了三个资金管理作为参考
你可以自己增加自己喜欢的资金管理方案

* /////////////////////////////////////
void MoneyManagement() {
lotstart=(AccountBalance()*lotab/10000);
lots=lotstart;
if(cheng==0){cheng=1;}
CurrentBasket=AccountEquity()-AccountBalance();
/////////////////////////////////////////////////////////////////
if(zijiguanli1){ LotMM = lots;}
/////////////////////////////////////////////////////////////////
if(zijiguanli2){
if(CurrentBasket<0 ) {LotMM=NormalizeDouble(lots*(OrdersTotal()*cheng)+ lotstart,2); }
if(CurrentBasket==0 ) {LotMM=NormalizeDouble(LotMM*0,2); }
}
/////////////////////////////////////////////////////////////////
if(zijiguanli3){
if(s+b==0){aa=1;}
if(s+b==1){aa=3;}
if(s+b==2){aa=7;}
if(s+b==3){aa=11;}
if(s+b==4){aa=20;}
if(s+b==5){aa=31;}
if(s+b==6){aa=42;}
if(s+b==7){aa=54;}
if(s+b==8){aa=66;}
if(s+b==9){aa=76;}
if(s+b>=10){aa=66;}
if(CurrentBasket<0 ) {LotMM=NormalizeDouble(lotstart*aa,2); }
if(CurrentBasket==0 ) {LotMM=NormalizeDouble(LotMM*0,2); }
}
/////////////////////////////////////////////////////////////////
if(LotMM<lots){LotMM=0.01;}
if(LotMM>LotsMax){LotMM=LotsMax;}
}//end zijiguanli
//////////////////////////////////////////

注意:这个资金管理加了很多新的词句
所以我们要加上去#property上面那边
不然会出错


* 新的词句:
extern double cheng=1;
double aa=0;
int s=0,b=0;
intdouble CurrentBasket=0;
double LotMM=0;
double lotstart=0;
double LotsMax=100;
extern double lotab=0.1;
double lots=0;

* 注意:
double 是有小数点的
int 是没有小数点的

* 注意:
if(){}
很容易用,比如:
if(LotMM<lots){LotMM=0.01;}
意思就是:
如果LotMM小过lots,LotMM就等于0.01。

if(LotMM>LotsMax){LotMM=LotsMax;}
意思就是:
如果LotMM大过LotsMax,LotMM就等于LotsMax。

不会的话,不要紧
copy&press就可以用了
把void MoneyManagement() { }
copy&press到我们EA模板去
结果如下://

#property copyright "外汇联盟 www.FXunion.com QQ群144033"

#property link      ""

extern double ma1=1,ma2=2;
double ma1now,ma1now2,ma2now,ma2now2,ma1bnow,ma2bnow;
////////////////////////
extern string _资金管理_ ="任选一个";
extern bool zijiguanli1= true;//false true、、
extern bool zijiguanli2= false;//false true、、
extern bool zijiguanli3= false;//false true、、
///////////////////////////////////
extern double cheng=1;
double aa=0;
int s=0,b=0;
int double CurrentBasket=0;
double LotMM=0;
double lotstart=0;
double LotsMax=100;
extern double lotab=0.1;
double lots=0;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----

MoneyManagement();//资金管理
macd();//指标管理
ma();//指标管理
zhangchang();//战场管理
buyme();//买上
sellme();//买下
xianshi();//显示管理
//----
return(0);
}
//+------------------------------------------------------------------+
void MoneyManagement() {
lotstart=(AccountBalance()*lotab/10000);
lots=lotstart;
if(cheng==0){cheng=1;}
CurrentBasket=AccountEquity()-AccountBalance();
/////////////////////////////////////////////////////////////////
if(zijiguanli1){ LotMM = lots;}
/////////////////////////////////////////////////////////////////
if(zijiguanli2){
if(CurrentBasket<0 ) {LotMM=NormalizeDouble(lots*(OrdersTotal()*cheng)+ lotstart,2); }
if(CurrentBasket==0 ) {LotMM=NormalizeDouble(LotMM*0,2); }
}
/////////////////////////////////////////////////////////////////
if(zijiguanli3){
if(s+b==0){aa=1;}
if(s+b==1){aa=3;}
if(s+b==2){aa=7;}
if(s+b==3){aa=11;}
if(s+b==4){aa=20;}
if(s+b==5){aa=31;}
if(s+b==6){aa=42;}
if(s+b==7){aa=54;}
if(s+b==8){aa=66;}
if(s+b==9){aa=76;}
if(s+b>=10){aa=66;}
if(CurrentBasket<0 ) {LotMM=NormalizeDouble(lotstart*aa,2); }
if(CurrentBasket==0 ) {LotMM=NormalizeDouble(LotMM*0,2); }
}
/////////////////////////////////////////////////////////////////
if(LotMM<lots){LotMM=lots;}
if(LotMM>LotsMax){LotMM=LotsMax;}
}//end zijiguanli
//+------------------------------------------------------------------+
void macd() {
//这边要写一些东西
}
//+------------------------------------------------------------------+
void ma()
{
ma1now = iMA(NULL,0,ma1,0,MODE_EMA,PRICE_CLOSE,0);
ma1now2 = iMA(NULL,0,ma10,0,MODE_EMA,PRICE_CLOSE,1);

ma2now = iMA(NULL,0,ma2,0,MODE_EMA,PRICE_CLOSE,0);
ma2now2 = iMA(NULL,0,ma2,0,MODE_EMA,PRICE_CLOSE,1);

ma1bnow = iMA(NULL,0,ma1,0,MODE_EMA,PRICE_OPEN,0);
ma2bnow = iMA(NULL,0,ma2,0,MODE_EMA,PRICE_OPEN,0);

}
//+------------------------------------------------------------------+
void zhangchang() {
//这边要写一些东西
}
//+------------------------------------------------------------------+
void buyme() {
//这边要写一些东西
}
//+------------------------------------------------------------------+
void sellme() {
//这边要写一些东西
}
//+------------------------------------------------------------------+
void buyme() {
//这边要写一些东西
}
//+------------------------------------------------------------------+
void xianshi() {
//这边要写一些东西
}
//+------------------------------------------------------------------+
/////end


* 资金管理有三个方案:
资金管理1=平注
资金管理2=1,2,3,4,5...
可以乘,如果你把cheng改成2或3或0.2
资金管理3是固定式,1,3,7,11.。。
 

 

第五课:策略:战场



* 这里是整个EA最重要的地方
这里策略变化无穷
高深莫测就是这里作怪


* 在这里你要想:。。。
你的EA要怎样下注
什么时候买上?
什么时候买下?
怎样买上?
怎样买下?
有买上又买下?
什么时候离场?
条件满足是很重要的


* 在这里我给个简单的策略
-ma1黄金交叉ma2,EA买上
-ma1死亡交叉ma2,EA买下
-有赚25点就出场
-如果亏的时候亏50点就出场

* 是这样编写的:
ma1now>ma2now
这样一句是不够条件的

ma1now>ma2now
&& ma1now2<ma2now2
这个就是黄金交叉的编写法

* 反过来
ma1now<ma2now
&& ma1now2>ma2now2
这个就是死亡交叉的编写法

* 但是条件还是不够的
再加
ma1now>ma2now
&& ma1now2<ma2now2
&& s+b==0

s+b==0 意思就是 买上+买下等于没有
没有下注,如果遇到条件符合就可以下注的意思

&& 的意思就是“再加”什么什么的条件

那么整句是这样编写
if(什么什么){就什么什么}
就是用这个

if(ma1now>ma2now
&& ma1now2<ma2now2
&& s+b==0
){buyme();}
if(ma1now<ma2now
&& ma1now2>ma2now2
&& s+b==0
){sellme();}

* 现在我们就把这两句copy&press到战场去

注意:
现在又多了两个新的东西
buyme()和sellme()
你就copy&press buyme()和sellme()的编写法就可以了。

* 结果如下:

#property copyright "外汇联盟 www.FXunion.com QQ群144033"

#property link      ""

extern double ma1=1,ma2=2;
double ma1now,ma1now2,ma2now,ma2now2,ma1bnow,ma2bnow;
////////////////////////
extern string _资金管理_ ="任选一个";
extern bool zijiguanli1= true;//false true、、
extern bool zijiguanli2= false;//false true、、
extern bool zijiguanli3= false;//false true、、
///////////////////////////////////
extern double cheng=1;
double aa=0;
int s=0,b=0;
intdouble CurrentBasket=0;
double LotMM=0;
double lotstart=0;
double LotsMax=100;
extern double lotab=0.1;
double lots=0;
double TP=0,TPP=0,SL=0,SLL=0;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----

MoneyManagement();//资金管理
macd();//指标管理
ma();//指标管理
zhangchang();//战场管理
buyme();//买上
sellme();//买下
xianshi();//显示管理
//----
return(0);
}
//+------------------------------------------------------------------+
void MoneyManagement() {
lotstart=(AccountBalance()*lotab/10000);
lots=lotstart;
if(cheng==0){cheng=1;}
CurrentBasket=AccountEquity()-AccountBalance();
/////////////////////////////////////////////////////////////////
if(zijiguanli1){ LotMM = lots;}
/////////////////////////////////////////////////////////////////
if(zijiguanli2){
if(CurrentBasket<0 ) {LotMM=NormalizeDouble(lots*(OrdersTotal()*cheng)+ lotstart,2); }
if(CurrentBasket==0 ) {LotMM=NormalizeDouble(LotMM*0,2); }
}
/////////////////////////////////////////////////////////////////
if(zijiguanli3){
if(s+b==0){aa=1;}
if(s+b==1){aa=3;}
if(s+b==2){aa=7;}
if(s+b==3){aa=11;}
if(s+b==4){aa=20;}
if(s+b==5){aa=31;}
if(s+b==6){aa=42;}
if(s+b==7){aa=54;}
if(s+b==8){aa=66;}
if(s+b==9){aa=76;}
if(s+b>=10){aa=66;}
if(CurrentBasket<0 ) {LotMM=NormalizeDouble(lotstart*aa,2); }
if(CurrentBasket==0 ) {LotMM=NormalizeDouble(LotMM*0,2); }
}
/////////////////////////////////////////////////////////////////
if(LotMM<lots){LotMM=lots;}
if(LotMM>LotsMax){LotMM=LotsMax;}
}//end zijiguanli
//+------------------------------------------------------------------+
void macd() {
//这边要写一些东西
}
//+------------------------------------------------------------------+
void ma()
{
ma1now = iMA(NULL,0,ma1,0,MODE_EMA,PRICE_CLOSE,0);
ma1now2 = iMA(NULL,0,ma10,0,MODE_EMA,PRICE_CLOSE,1);

ma2now = iMA(NULL,0,ma2,0,MODE_EMA,PRICE_CLOSE,0);
ma2now2 = iMA(NULL,0,ma2,0,MODE_EMA,PRICE_CLOSE,1);

ma1bnow = iMA(NULL,0,ma1,0,MODE_EMA,PRICE_OPEN,0);
ma2bnow = iMA(NULL,0,ma2,0,MODE_EMA,PRICE_OPEN,0);

}
//+------------------------------------------------------------------+
void zhangchang() {
//这边要写一些东西

if(ma1now>ma2now
&& ma1now2<ma2now2
&& s+b==0
){buyme();}

if(ma1now<ma2now
&& ma1now2>ma2now2
&& s+b==0
){sellme();}

}
//+------------------------------------------------------------------+
void buyme(){
if (TP==0) { TPP=0; } if(TP>0) { TPP=Ask+TP*Point; }
if (SL==0) { SLL=0; } if(SL>0) { SLL=Ask-SL*Point; }
OrderSend(Symbol(),OP_BUY,LotMM,Ask,slip,SLL,TPP," M"+Period()+"",0,0,Violet);

return(0); }
//+------------------------------------------------------------------+
void sellme(){
if (TP==0) { TPP=0; } if(TP>0) { TPP=Bid-(TP*Point); }
if (SL==0) { SLL=0; } if(SL>0) { SLL=Bid+(SL*Point); }
ticket=OrderSend(Symbol(),OP_SELL,LotMM,Bid,2,SLL, TPP,"M"+Period()+"",0,0,GreenYellow);
return(0); }
//+------------------------------------------------------------------+
void buyme() {
//这边要写一些东西
}
//+------------------------------------------------------------------
+void xianshi() {
//这边要写一些东西
}
//+------------------------------------------------------------------+/////end



* 在buyme()和sellme()
我们又发现新的词句
TP,TPP,SL,SLL

我们必须在#property上面加上他们
不然会出错

我已经加上去了上面的结果了
你注意到了吗?

* ok,这个EA可以用了
现在你copy&press 去你的METAEDITOR
save as 一个新的名字就可以了

* 试一试,用一用
告诉你,它肯定是“垃圾”EA
因为是亏钱的EA
 

 

 

第六课-解决出错问题



* 如果你有做功课
你会知道以上的EA还是不能用
因为出错了
出错是常事
没有什么了不起
没有什么大事

* 当你按在METAEDITOR按“编写”时
最下方会告诉你出错了
按一按下方的出错
他会告诉你在那里出错了
第一:intdouble CurrentBasket=0;
多了 int
删掉 int 就可以了
//int 是表示要“整数”比如:1,2,3,4...
//double 表示要有小数点,比如:1.1,7.987.56.897.....

* 之后你在按一下“编写”
他告诉你
第二:ma1now2 = iMA(NULL,0,ma10,0,MODE_EMA,PRICE_CLOSE,1);
ma10,意思就是没有ma10这个新词句
其实我们没有用到ma10
是编写错误
不是ma10,是多了个0
应该是ma1
删掉 0 就可以了

* 之后你在按一下“编写”
他告诉你
第三:OrderSend(Symbol(),OP_BUY,LotMM,Ask,slip,SLL,TP P,"M"+Period()+"",0,0,Violet);
问题是slip
应该是新的词句
slip 是什么?
听他们讲是滑点?管他
因为我们不可以删掉slip
所以在上方加上新的词句吧!
比如:
int slip=6;
就可以了

* 再来。。。
ticket=OrderSend(Symbol(),OP_SELL,LotMM,Bid,2,SLL, TPP,"M"+Period()+"",0,0,GreenYellow);
ticket?
我们没有用到ticket
所以可以删掉 ticket= 就可以了

* 再来。。。他告诉你多了这个

void buyme() {
//这边要写一些东西
}

因为已经有了的,哈哈和。。。
是糊涂多了一个
删掉 整句多余的buyme() 就可以了


* 再来。。。
0错误 0警告
哈哈和。。。
成功了
恭喜你已经学会编写EA了

编写EA就是解决问题

 

第七课-填写盈亏

上一回我们讲到
-有赚25点就出场
-如果亏的时候亏50点就出场

这个还没有加上去
编写方法就是
Take Profit=25
Stop Loss=50

我把他们简写成
TP=25;
SL=50;

你想到了什么吗?
对,这字眼很熟吧!
记得吗?
double TP=0,TPP=0,SL=0,SLL=0;

我们改成
double TP=25,TPP=0,SL=50,SLL=0;
你发现吗?
不过下注方面还是有问题!

其实是这EA没有判断
什么是B
什么是S

B是buy
S是sell
速成班不必学会怎样写
你只要copy&prss下面的子程式
放进去就可以了

我把它称为检查

//////////////////////////////////////////
void jiancha()
{

b=0;s=0;
for (int cnt=0; cnt<=OrdersTotal(); cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol()==Symbol() )
{
mode=OrderType();LastOrderType=OrderType();
if (OrderType() == OP_SELL){ s++; LastPricesell=OrderOpenPrice();}

if (OrderType() == OP_BUY ){ b++; LastPricebuy=OrderOpenPrice();}
LastPrice=OrderOpenPrice();
if (OrderType()==OP_SELL) {OpenSells++;}
if (OrderType()==OP_SELLSTOP) {OpenSellStops++;}
if (OrderType()==OP_BUY) {OpenBuys++;}
if (OrderType()==OP_BUYSTOP) {OpenBuyStops++;}
}
}
}//end jiancha
//////////////////////////////////////////////////

要记得:在start那边也加入
jiancha();
这样就可以了。
当我们按“编写”
会出错
因为有了新的词句
我们没有加在#property上面那边
新的词句:

mode
LastOrderType
LastPricesell
LastPricebuy
LastPrice
OpenSells
OpenSellStops
OpenBuys
OpenBuyStops

我们更正如下:

double mode=0;
double LastOrderType;
double LastPricesell;
double LastPricebuy;
double LastPrice;
double OpenSells;
double OpenSellStops;
double OpenBuys;
double OpenBuyStops;

按“编写”。。。
0错误 0警告
ok了
按“终端”试一试效果怎样
还是不行
还是buy sell buy sell buy sell
你知道什么事情发生了吗?
跟你讲一个逻辑
什么是主程式
就是主要的程式
什么是子程式
就是不是主要的程式
了解了这些
现在一个问题,这些子程式

MoneyManagement();//资金管理
macd();//指标管理
ma();//指标管理
zhangchang();//战场管理
buyme();//买上
sellme();//买下
xianshi();//显示管理

那些是不需要一直用的就不可以在主程式这里
对啦!

buyme();//买上
sellme();//买下

是有时用到,有时没有用
所以我们删掉它

现在这个EA可以正常的操作了
其编写程式如下:
注意颜色部分的位置

#property copyright "外汇联盟 www.FXunion.com QQ群144033"

#property link      ""

extern double ma1=1,ma2=2;
double ma1now,ma1now2,ma2now,ma2now2,ma1bnow,ma2bnow;
////////////////////////
extern string _资金管理_ ="任选一个";
extern bool zijiguanli1= true;//false true、、
extern bool zijiguanli2= false;//false true、、
extern bool zijiguanli3= false;//false true、、
///////////////////////////////////
extern double cheng=1;
double aa=0;
int s=0,b=0;
double CurrentBasket=0;
double LotMM=0;
double lotstart=0;
double LotsMax=100;
extern double lotab=0.1;
double lots=0;
double TP=25,TPP=0,SL=50,SLL=0;
int slip=6;
///////////////////////////////////////
double mode=0;
double LastOrderType;
double LastPricesell;
double LastPricebuy;
double LastPrice;
double OpenSells;
double OpenSellStops;
double OpenBuys;
double OpenBuyStops;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}

//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----
jiancha();//检查判断
MoneyManagement();//资金管理
macd();//指标管理
ma();//指标管理
zhangchang();//战场管理
xianshi();//显示管理
//----
return(0);
}
//+------------------------------------------------------------------+
void jiancha()
{

b=0;s=0;
for (int cnt=0; cnt<=OrdersTotal(); cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol()==Symbol() )
{
mode=OrderType();LastOrderType=OrderType();
if (OrderType() == OP_SELL){ s++; LastPricesell=OrderOpenPrice();}

if (OrderType() == OP_BUY ){ b++; LastPricebuy=OrderOpenPrice();}
LastPrice=OrderOpenPrice();
if (OrderType()==OP_SELL) {OpenSells++;}
if (OrderType()==OP_SELLSTOP) {OpenSellStops++;}
if (OrderType()==OP_BUY) {OpenBuys++;}
if (OrderType()==OP_BUYSTOP) {OpenBuyStops++;}
}
}
}//end jiancha
//+------------------------------------------------------------------+

void MoneyManagement() {
lotstart=(AccountBalance()*lotab/10000);
lots=lotstart;
if(cheng==0){cheng=1;}
CurrentBasket=AccountEquity()-AccountBalance();
/////////////////////////////////////////////////////////////////
if(zijiguanli1){ LotMM = lots;}
/////////////////////////////////////////////////////////////////
if(zijiguanli2){
if(CurrentBasket<0 ) {LotMM=NormalizeDouble(lots*(OrdersTotal()*cheng)+ lotstart,2); }
if(CurrentBasket==0 ) {LotMM=NormalizeDouble(LotMM*0,2); }
}
/////////////////////////////////////////////////////////////////
if(zijiguanli3){
if(s+b==0){aa=1;}
if(s+b==1){aa=3;}
if(s+b==2){aa=7;}
if(s+b==3){aa=11;}
if(s+b==4){aa=20;}
if(s+b==5){aa=31;}
if(s+b==6){aa=42;}
if(s+b==7){aa=54;}
if(s+b==8){aa=66;}
if(s+b==9){aa=76;}
if(s+b>=10){aa=66;}
if(CurrentBasket<0 ) {LotMM=NormalizeDouble(lotstart*aa,2); }
if(CurrentBasket==0 ) {LotMM=NormalizeDouble(LotMM*0,2); }
}
/////////////////////////////////////////////////////////////////
if(LotMM<lots){LotMM=lots;}
if(LotMM>LotsMax){LotMM=LotsMax;}
}//end zijiguanli
//+------------------------------------------------------------------+
void macd() {
//这边要写一些东西
}
//+------------------------------------------------------------------+
void ma()
{
ma1now = iMA(NULL,0,ma1,0,MODE_EMA,PRICE_CLOSE,0);
ma1now2 = iMA(NULL,0,ma1,0,MODE_EMA,PRICE_CLOSE,1);

ma2now = iMA(NULL,0,ma2,0,MODE_EMA,PRICE_CLOSE,0);
ma2now2 = iMA(NULL,0,ma2,0,MODE_EMA,PRICE_CLOSE,1);

ma1bnow = iMA(NULL,0,ma1,0,MODE_EMA,PRICE_OPEN,0);
ma2bnow = iMA(NULL,0,ma2,0,MODE_EMA,PRICE_OPEN,0);

}
//+------------------------------------------------------------------+
void zhangchang() {
//这边要写一些东西

if(ma1now>ma2now
&& ma1now2<ma2now2
&& s+b==0
){buyme();}

if(ma1now<ma2now
&& ma1now2>ma2now2
&& s+b==0
){sellme();}

}
//+------------------------------------------------------------------+
void buyme(){
if (TP==0) { TPP=0; } if(TP>0) { TPP=Ask+TP*Point; }
if (SL==0) { SLL=0; } if(SL>0) { SLL=Ask-SL*Point; }
OrderSend(Symbol(),OP_BUY,LotMM,Ask,slip,SLL,TPP," M"+Period()+"",0,0,Violet);

return(0); }
//+------------------------------------------------------------------+
void sellme(){
if (TP==0) { TPP=0; } if(TP>0) { TPP=Bid-(TP*Point); }
if (SL==0) { SLL=0; } if(SL>0) { SLL=Bid+(SL*Point); }
OrderSend(Symbol(),OP_SELL,LotMM,Bid,2,SLL,TPP,"M" +Period()+"",0,0,GreenYellow);
return(0); }
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
void xianshi() {
//这边要写一些东西
}
//+------------------------------------------------------------------+

///////////////////////////////end


恭喜!恭喜,这EA可以正常操作了!

 

 

第八课-移动止赢


移动止赢就是赢到尽头的意思

不必学会怎样写Traingstop
copy&Press就可以了
子程式如下:


///////////////////////
void TrailingStop()
{
for(cnt=OrdersTotal();cnt>=0;cnt--)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol() == Symbol() )//&& OrderMagicNumber()==MAGICMA)
{
if (OrderType()==OP_SELL)
{
if(TrailingStop>0)
{
if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
{
if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))
{
OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Poi nt*TrailingStop,OrderTakeProfit(),0,Red);
return(0);
}
}
}
}

if (OrderType()==OP_BUY)
{
if(TrailingStop>0)
{
if(Bid-OrderOpenPrice()>Point*TrailingStop)
{
if(OrderStopLoss()<Bid-Point*TrailingStop)
{
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green);
return(0);
}
}
}
}
}
}
}//end TrailingStop
////////////////////////////////////


这TrailingStop是可以一直用的
所以我们要加在start那边加
TrailingStop();

发现新的词句
cnt
TrailingStop

我们更正如下:
int cnt;
double TrailingStop=0;

按“编写”
0错误 0警告
厉害,哈哈和。。。
加入成功

把double TrailingStop=0;
0改成10,就可以操作了

改成这样也可以
extern double TrailingStop=10;

自己试一试看看效果如何?
怎样?容易吗?

 

 

第九课-显示画面


显示的子程式如下

//////////////////////////////////////////////
void xianshi()
{
string name_8 = "xianshi1";
ObjectDelete(name_8);
ObjectCreate(name_8, OBJ_LABEL, 0, 0, 0);
ObjectSetText(name_8, "伺服器时间::"+TimeToStr(CurTime( ))+"", 13, "Arial", White);
ObjectSet(name_8, OBJPROP_CORNER, 0);
ObjectSet(name_8, OBJPROP_XDISTANCE, 15);
ObjectSet(name_8, OBJPROP_YDISTANCE, 15);


ObjectDelete(name_8);
ObjectCreate(name_8, OBJ_LABEL, 0, 0, 0);
ObjectSetText(name_8, ""+text+"", 15, "Arial", Yellow);
ObjectSet(name_8, OBJPROP_CORNER, 1);
ObjectSet(name_8, OBJPROP_XDISTANCE, 15);
ObjectSet(name_8, OBJPROP_YDISTANCE, 5);

name_8 = "Lableyeddrice1";
ObjectDelete(name_8);
ObjectCreate(name_8, OBJ_LABEL, 0, 0, 0);
ObjectSetText(name_8, "M"+Period()+" 货币 "+Symbol()+"", 10, "Arial", Silver);
ObjectSet(name_8, OBJPROP_CORNER, 1);
ObjectSet(name_8, OBJPROP_XDISTANCE, 130);
ObjectSet(name_8, OBJPROP_YDISTANCE, 32);

//////这个显示上方
name_8 = "LableCurPrice2";
ObjectDelete(name_8);
ObjectCreate(name_8, OBJ_LABEL, 0, 0, 0);
ObjectSetText(name_8,""+DoubleToStr(Ask, Digits)+"::买上", 15, "Arial", Lime);
ObjectSet(name_8, OBJPROP_CORNER, 1);
ObjectSet(name_8, OBJPROP_XDISTANCE, 15);
ObjectSet(name_8, OBJPROP_YDISTANCE, 30);

//////这个显示下方
name_8 = "Lab345rPrice3";
ObjectDelete(name_8);
ObjectCreate(name_8, OBJ_LABEL, 0, 0, 0);
ObjectSetText(name_8,""+DoubleToStr(Bid, Digits)+"::卖下", 15, "Arial", Lime);
ObjectSet(name_8, OBJPROP_CORNER, 1);
ObjectSet(name_8, OBJPROP_XDISTANCE, 15);
ObjectSet(name_8, OBJPROP_YDISTANCE, 50);

name_8 = "Acc87lance";
ObjectDelete(name_8);
ObjectCreate(name_8, OBJ_LABEL, 0, 0, 0);

外_汇_邦 WaiHuiBang.com
本文标题:新手快速学会怎样编写外汇EA - MT4平台MQL4编程学习
本文地址:https://www.waihuibang.com/fxschool/autotrading/mql4/40645.html

相关文章

  • MT4软件中的Stochastic Oscillator(随机振荡指标)KD、KDJ使用简介
    MT4软件中的Stochastic Oscillator(随机振荡指标)KD、KDJ使用简介

    随机振荡指标(Stochastic Oscillator)中文名称是随机指数,最早起源于期货市场。是50年代由美国大师乔治.雷恩(George C. Lane)博士发明的,自创立以来,历经半世纪岁月,已被证明是令人膜拜的经典。...

    MQL4编程学习
  • MT4编程初级手册(3):自定义变量

    一、自定义变量在使用前,必须先声明(定义),向系统声明变量的类型和名称。 创建时可同时进行赋值(赋默认值),也可以不赋值。MQ语法中自定义变量的名称可以使用中文字符。 例子: int flag; 创建一个整型变量,变量...

    MQL4编程学习
  • 外汇EA智能交易软件是什么东西?

    一、外汇EA软件是什么? 答:智能交易系统软件的全称是Expert Advisor,简称EA,是一个全自动的交易工具,智能交易系统软件是用来对交易过程完全自动化,免除交易商连续对市场进行日常烦琐的监护,也免除交易员的盯盘操作。许多...

    MQL4编程学习
  • MT4中zigzag指标的用法

    zigzag指标作为外汇交易常用技术分析指标,并不是MT4自带的指标,投资人想要使用的话,必须要自己下载安装,下面是zigzag指标的下载和用法说明...

    MQL4编程学习
  • MQL4文件操作函数(三)

    本组函数用于处理文件。存放工作文件的三个目录(含子目录):/HISTORY/<当前经纪商> &#8211; 特别提供的FileOpenHistory函数: /EXPERTS/FILES &#8211; 公用目录; /TESTER/FILES &#8211; 测试专用。 禁止访...

    MQL4编程学习
你可能感兴趣