//+——————————————————————+
//| MACD升级版交易系统的改进版0.mq4 |
//| Copyright 2012, MetaQuotes Software Corp. |
//|
//+——————————————————————+
#property copyright “Copyright 2012, MetaQuotes Software Corp.”
#define MAGICMA 20121213 //MAGIC数字,为区别不同的EA
extern int whichmethod=2; //1是仅开仓,2是有损无盈,3是有盈无损,4是有损有盈
extern double TakeProfit=600; //止盈
extern double StopLoss=200; //止损
extern int RiskValue=3; //风险系数
extern int MaxOpen=4; //最多开仓次数限制!
extern double MaxLots=0.5; //最大的单仓持仓量限制!
extern double bb=0;//当价格向心想的方向移动时,BB》0,开始移动止损
extern double TrailingStop=100; //移动止损值
int i,res;
double Lots;
datetime lasttime;
//+——————————————————————+
//| expert initialization function |
//+——————————————————————+
int init()
{
Lots=0.1;//初始值
lasttime=NULL;//初始值
//—-
return(0);
}
//+——————————————————————+
//| expert deinitialization function |
//+——————————————————————+
int deinit()
{
//—-
//—-
return(0);
}
//+——————————————————————+
//| expert start function |
//+——————————————————————+
int start()
{
if(OrdersTotal()<=MaxOpen){
CheckForOpen();//平仓,开仓条件检查和操作!
if(buyorsell()==1)
{ bb=Bid-OrderOpenPrice();//
if(bb>0) CTP();
}
if(buyorsell()==-1){ bb=OrderOpenPrice()-Ask;
if(bb>0) CTP();} //
}
//—-
return(0);
}
//+——————————————————————+
double LotsOptimized(){//确定开仓量,资金管理!
Lots=NormalizeDouble(((AccountBalance()*RiskValue/100)/MarketInfo(Symbol(),MODE_MARGINREQUIRED)),2);
double minlot=MarketInfo(Symbol(),MODE_MINLOT);
double maxlot=MarketInfo(Symbol(),MODE_MAXLOT);
double lotstep=MarketInfo(Symbol(),MODE_LOTSTEP);
if(Lots<minlot){Lots=0rint("保证金余额不足!");}
if(Lots>MaxLots){Lots=MaxLots;}
OrderSelect(OrdersHistoryTotal()-1,SELECT_BY_POS,MODE_HISTORY);
if(OrderProfit()<0) {Lots=OrderLots()/2;}
return(Lots);}
void CloseBuy()
{
if (OrdersTotal( ) > 0 )
{
for(i=OrdersTotal()-1;i>=0;i–)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) continue;
if((OrderType()==OP_BUY)&&(OrderSymbol()==Symbol())&&(OrderMagicNumber()==MAGICMA))
{
OrderClose(OrderTicket(),OrderLots(),Bid,3,White);
Sleep(2000);
}
}
}
}
void CloseSell()
{
if (OrdersTotal( ) > 0 )
{
for(i=OrdersTotal()-1;i>=0;i–)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderType()==OP_SELL&&(OrderSymbol()==Symbol())&&(OrderMagicNumber()==MAGICMA))
{
OrderClose(OrderTicket(),OrderLots(),Ask,3,White);
Sleep(2000);
}
}
}
}
int buyorsell()//判断买或者卖
{
double s0= iCustom(NULL,0,”MACD”,0,iBarShift(NULL,0,iTime(NULL,0,0)));
double s1=iCustom(NULL,0,”MACD”,0,iBarShift(NULL,0,iTime(NULL,0,1)));
double macd0= iCustom(NULL,0,”MACD”,1,iBarShift(NULL,0,iTime(NULL,0,0)));
double macd1=iCustom(NULL,0,”MACD”,1,iBarShift(NULL,0,iTime(NULL,0,1)));
double s2= iCustom(NULL,0,”MACD”,0,iBarShift(NULL,0,iTime(NULL,0,2)));
double s3=iCustom(NULL,0,”MACD”,0,iBarShift(NULL,0,iTime(NULL,0,3)));
double macd2= iCustom(NULL,0,”MACD”,1,iBarShift(NULL,0,iTime(NULL,0,2)));
double macd3=iCustom(NULL,0,”MACD”,1,iBarShift(NULL,0,iTime(NULL,0,3)));
double jaws2=iCustom(NULL,0,”鳄鱼三线”,0,iBarShift(NULL,0,iTime(NULL,0,2)));//索引线为0
double jaws1=iCustom(NULL,0,”鳄鱼三线”,0,iBarShift(NULL,0,iTime(NULL,0,1)));
double teeth2=iCustom(NULL,0,”鳄鱼三线”,1,iBarShift(NULL,0,iTime(NULL,0,2)));//索引线为1
double teeth1=iCustom(NULL,0,”鳄鱼三线”,1,iBarShift(NULL,0,iTime(NULL,0,1)));
double lips2=iCustom(NULL,0,”鳄鱼三线”,2,iBarShift(NULL,0,iTime(NULL,0,2)));//索引线为2
double lips1=iCustom(NULL,0,”鳄鱼三线”,2,iBarShift(NULL,0,iTime(NULL,0,1)));
if((macd0<s0<0&&macd1<s1<0&&macd2<s2<0&&s3<macd3<0)&&(0<macd0<s0&&0<macd1<s1&&0<macd2<s2&&0<s3<macd3)) return(1);//改为并,是大阳线形成的穿越!
if((macd0>s0>0&&macd1>s1>0&&macd2>s2>0&&s3>macd3>0)&&(0>macd0>s0&&0>macd1>s1&&0>macd2>s2&&0>s3>macd3)) return(-1);//改为并,是大阴线形成的穿越!
if((macd0>s0)&&(jaws1>teeth1>lips1)) return(-1);
if((s0>macd0)&&(lips1>teeth1>jaws1)) return(1);
return(0);//0代表不交易
}
int nowbuyorsell=0;
void CheckForOpen()
{
if(Time[0]==lasttime)return;//每时间周期检查一次,时间控制!
lasttime=Time[0];
nowbuyorsell=buyorsell();//获取买卖信号
if(nowbuyorsell==1) CloseSell();//先平卖出的单,
if(nowbuyorsell==-1) CloseBuy();//先平买进的单,
if(nowbuyorsell==0) return;//0代表不交易,直接RETURN
if(OrdersTotal()>=MaxOpen) return;//当大于等于最大开仓量时,也直接RETURN
if (TimeDayOfWeek(TimeCurrent()) == 1)
{
if (TimeHour(TimeCurrent()) < 3 ) return; //周一早8点前不做 具体决定于你的时区和服务器的时区 时间控制
}
if (TimeDayOfWeek(TimeCurrent()) == 5)
{
if (TimeHour(TimeCurrent()) > 19 ) return; //周五晚11点后不做
}
TradeOK();
}
void TradeOK() //去下单交易
{
int error ;
if (nowbuyorsell == 1) //买
{
switch (whichmethod)
{
case 1: res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,30,0,0,””,MAGICMA,0,Blue);break;
case 2: res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,30,Ask-StopLoss*Point,0,””,MAGICMA,0,Blue); break;
case 3: res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,30,0,Ask+TakeProfit*Point,””,MAGICMA,0,Blue);break;
case 4: res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,30,Ask-StopLoss*Point,Ask+TakeProfit*Point,””,MAGICMA,0,Blue);break;
default : res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,30,0,0,””,MAGICMA,0,Blue);break;
}
if (res <=0)
{
error=GetLastError();
if(error==134)Print(“Received 134 Error after OrderSend() !! “); // not enough money
if(error==135) RefreshRates(); // prices have changed
}
Sleep(5000);
return ;
}
if (nowbuyorsell == -1) //卖
{
switch (whichmethod)
{
case 1: res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,30,0,0,””,MAGICMA,0,Red); break;
case 2: res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,30,Bid+StopLoss*Point,0,””,MAGICMA,0,Red); break;
case 3: res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,30,0,Bid-TakeProfit*Point,””,MAGICMA,0,Red); break;
case 4: res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,30,Bid+StopLoss*Point,Bid-TakeProfit*Point,””,MAGICMA,0,Red); break;
default : res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,30,0,0,””,MAGICMA,0,Red); break;
}
if (res <=0)
{
error=GetLastError();
if(error==134) Print(“Received 134 Error after OrderSend() !! “); // not enough money
if(error==135) RefreshRates(); // prices have changed
}
Sleep(5000);
return ;
}
}
void CTP() //跟踪止赢
{
bool bs = false;
for (int i = 0; i < OrdersTotal(); i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if (OrderType() == OP_BUY)
{
if ((Bid – OrderOpenPrice()) > (TrailingStop * MarketInfo(OrderSymbol(), MODE_POINT))) //开仓价格 当前止损和当前价格比较判断是否要修改跟踪止赢设置
{
if (OrderStopLoss() < Bid – TrailingStop * MarketInfo(OrderSymbol(), MODE_POINT))
{
bs = OrderModify(OrderTicket(), OrderOpenPrice(), Bid – TrailingStop * MarketInfo(OrderSymbol(), MODE_POINT), OrderTakeProfit(),0, Green);
}
}
}
else if (OrderType() == OP_SELL)
{
if ((OrderOpenPrice() – Ask) > (TrailingStop * MarketInfo(OrderSymbol(), MODE_POINT))) //开仓价格 当前止损和当前价格比较判断是否要修改跟踪止赢设置
{
if ((OrderStopLoss()) > (Ask + TrailingStop * MarketInfo(OrderSymbol(), MODE_POINT)))
{
bs = OrderModify(OrderTicket(), OrderOpenPrice(),Ask + TrailingStop * MarketInfo(OrderSymbol(), MODE_POINT), OrderTakeProfit(),0, Tan);
}
}
}
}
}
为什么这个EA总不下单?
请登录之后再进行评论
登录