EA(原码)学习

阅读 :
欢迎访问 外 汇 邦 WWW.WaiHuiBang.com

//+------------------------------------------------------------------+
#property copyright "2009"
#property link " "

 

int MagicNumber = 0;
string EAname ="顺势";
int MAGICMA = 2000;


//定义资金管理--------------------------------------------------------+
extern string _资金管理_ ="任选一个";
extern bool zijiguanli1= true;//false true、、
extern bool zijiguanli2= false;//false true、、
extern bool zijiguanli3= false;//false true、、

// 定义各参数--------------------------------------------------------+
extern int FastEMA = 12;
extern int SlowEMA = 26;
extern int SignalSMA = 9;

//定义指标函数--------------------------------------------------------+
   double ema120;
   double sma20,sma21;
   double ema15,ema16;

//定义止赢止损,滑点---------------------------------------------------+
   double TP=0,TPP=0,SL=0,SLL=0; //赚120点出场,亏20点出场
   int slip=6;
//定义移动止损-------------------------------------------------------+
   int cnt;
   extern double TrailingStop=20;//移动止损为20点
//+------------------------------------------------------------------+
   double mode=0;
   double LastOrderType;
   double LastPricesell;
   double LastPricebuy;
   double LastPrice;
   double OpenSells;
   double OpenSellStops;
   double OpenBuys;
   double OpenBuyStops;
   double SLauto;
   string text="";

//定义下单信息--------------------------------------------------------+  
   extern double cheng=1;
   double aa=0;
   int s=0,b=0,i,panduan=0;
   double CurrentBasket=0;
   double LotMM=0;
   double lotstart=0;
   double LotsMax=100;
   double LotsMin=0.1;
   extern double lotab=1;
   extern double Stoploss=0,no60=60;
   double lots=0;

//关于保证金-----------------------------------------------------------+
double BaoZhenJing;

//+------------------------------------------------------------------+

int 判断=0,buy1=1,buy2=1,buy3=1,buy4=1,sell1=1,sell2=1,sell3=1,sell4=1;

//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//以后可能有用到
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//以后可能有用到
return(0);
}
//+------------------------------------------------------------------+
//| expert start 主程式
//+------------------------------------------------------------------+
int start()
{
MoneyManagement();//资金管理
ma();//指标管理
zhangchang();//战场管理
jiancha();//检查
TrailingStop();//移动止损
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<LotsMin){LotMM=LotsMin;}
if(LotMM>LotsMax){LotMM=LotsMax;}
}//end 资金管理

//指标管理MA--------------------------------------------------------+
void ma() {

ema120 = iMA(NULL, PERIOD_M5, 120, 0, MODE_EMA, PRICE_CLOSE, 0);

sma20 = iMA(NULL, PERIOD_M5, 20, 0, MODE_SMA, PRICE_CLOSE, 0);
sma21 = iMA(NULL, PERIOD_M5, 20, 0, MODE_SMA, PRICE_CLOSE, 0+1);

ema15 = iMA(NULL, PERIOD_M5, 15, 0, MODE_EMA, PRICE_CLOSE, 0);
ema16 = iMA(NULL, PERIOD_M5, 15, 0, MODE_EMA, PRICE_CLOSE, 0+1);
 

}

//战场管理---------------------------------------------------------+
void zhangchang() {
//--- 如果开仓保证金超过上限,则返还不交易。 ------------------
if( AccountMargin() / (AccountMargin() + AccountFreeMargin()) >= 1 ) { return(0); }
BaoZhenJing = MarketInfo( Symbol(), MODE_MARGINREQUIRED); // 一个标准手需保证金金额。
BaoZhenJing = BaoZhenJing * LotMM ; // 下单需用的保证金
if( (AccountMargin()+BaoZhenJing) / (AccountMargin()+AccountFreeMargin()) >= 1 ) { return(0); }
//+-------------------------------------------------------------------+

if(s+b==111){buyme();sellme();CloseSell();CloseBuy();closeall();}

datetime Time0 = 0;
if (Time0 != Time[0]){
Time0 = Time[0];


if(
(b==0 && ema120<sma21 && sma20>ema15 && sma21<ema16){判断=1;buyme();}
if(
(s==0 && ema120>sma21 && sma20<ema15 && sma21>ema16)){判断=2;sellme();}


}
if(判断==2 && b>0){CloseBuy();}
if(判断==1 && s>0){CloseSell();}
//if(s+b>0 && AccountProfit()>30){closeall();}

if(sma20>ema15 b>0){CloseBuy();}
if(sma20<ema15 && s>0){CloseSell();}
   return(0);
}


//下单后没成交 撤单--------------------------------------------------------------+
//if (OrderSend==OP_BUY)
//if((OrderOpenPrice-OrderClosePrice)/Point > 100)
//OrderDelete(OrderTicket)};
//if (OrderSend==OP_SELL)
//if((OrderClosePrice-OrderOpenPrice)/Point > 100)
//OrderDelete(OrderTicket)};


//买入开单--------------------------------------------------------------+
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 CloseBuy()
{
if (OrdersTotal( ) > 0 )  
{
for(i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)     break;
if(OrderType()==OP_BUY)
{
    OrderClose(OrderTicket(),OrderLots(),Bid,3,White);
    Sleep(5000);
} }}}

//平仓持有的卖单--------------------------------------------------------+
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)
    {
    OrderClose(OrderTicket(),OrderLots(),Ask,3,White);
    Sleep(5000);
    } }}}

//平仓所有--------------------------------------------------------+
void closeall()
{
if (OrdersTotal( ) > 0 )  
{
for(i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)     break;
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)     break;
if(OrderType()==OP_SELL)
if(OrderType()==OP_BUY)
    {
    OrderClose(OrderTicket(),OrderLots(),Ask,3,White);
    OrderClose(OrderTicket(),OrderLots(),Bid,3,White);
    Sleep(5000);
    } }}}


//移动止损-----------------------------------------------------------+

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+Point*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);
} } } } } }

}
//检 查--------------------------------------------------------------+
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++;}
}
}
}

//显示数据-----------------------------------------------------------+
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);

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);
ObjectSetText(name_8,""+DoubleToStr( AccountBalance(), 2)+"::账面资金", 13, "Arial", Lime);
ObjectSet(name_8, OBJPROP_CORNER, 1);
ObjectSet(name_8, OBJPROP_XDISTANCE, 15);
ObjectSet(name_8, OBJPROP_YDISTANCE, 70);

name_8 = "LableC5rice6";
ObjectDelete(name_8);
ObjectCreate(name_8, OBJ_LABEL, 0, 0, 0);
ObjectSetText(name_8,""+DoubleToStr( AccountEquity(), 2)+"::净值资金", 13, "Arial", Lime);
ObjectSet(name_8, OBJPROP_CORNER, 1);
ObjectSet(name_8, OBJPROP_XDISTANCE, 15);
ObjectSet(name_8, OBJPROP_YDISTANCE, 90);

name_8 = "Labl456rice6";
ObjectDelete(name_8);
ObjectCreate(name_8, OBJ_LABEL, 0, 0, 0);
ObjectSetText(name_8,""+DoubleToStr( AccountMargin(), 2)+"::保证资金", 13, "Arial", Lime);
ObjectSet(name_8, OBJPROP_CORNER, 1);
ObjectSet(name_8, OBJPROP_XDISTANCE, 15);
ObjectSet(name_8, OBJPROP_YDISTANCE, 110);

name_8 = "Labl4hcjce6";
ObjectDelete(name_8);
ObjectCreate(name_8, OBJ_LABEL, 0, 0, 0);
ObjectSetText(name_8,""+DoubleToStr( AccountFreeMargin(), 2)+"::可用资金", 13, "Arial", Lime);
ObjectSet(name_8, OBJPROP_CORNER, 1);
ObjectSet(name_8, OBJPROP_XDISTANCE, 15);
ObjectSet(name_8, OBJPROP_YDISTANCE, 130);

name_8 = "Accvf7lance";
ObjectDelete(name_8);
ObjectCreate(name_8, OBJ_LABEL, 0, 0, 0);
ObjectSetText(name_8,""+DoubleToStr( AccountProfit(), 2)+"::账面盈亏", 13, "Arial", Lime);
ObjectSet(name_8, OBJPROP_CORNER, 1);
ObjectSet(name_8, OBJPROP_XDISTANCE, 15);
ObjectSet(name_8, OBJPROP_YDISTANCE, 150);
aa=s+b;
name_8 = "Accvfee7lance";
ObjectDelete(name_8);
ObjectCreate(name_8, OBJ_LABEL, 0, 0, 0);
ObjectSetText(name_8,""+DoubleToStr( aa, 0)+"::s+b", 13, "Arial", Lime);
ObjectSet(name_8, OBJPROP_CORNER, 1);
ObjectSet(name_8, OBJPROP_XDISTANCE, 15);
ObjectSet(name_8, OBJPROP_YDISTANCE, 170);
name_8 = "Acccvfee7lance";
ObjectDelete(name_8);
ObjectCreate(name_8, OBJ_LABEL, 0, 0, 0);
ObjectSetText(name_8,""+b+"::buy", 13, "Arial", Lime);
ObjectSet(name_8, OBJPROP_CORNER, 1);
ObjectSet(name_8, OBJPROP_XDISTANCE, 15);
ObjectSet(name_8, OBJPROP_YDISTANCE, 190);
name_8 = "Acccwvfee7lance";
ObjectDelete(name_8);
ObjectCreate(name_8, OBJ_LABEL, 0, 0, 0);
ObjectSetText(name_8,""+s+"::sell", 13, "Arial", Lime);
ObjectSet(name_8, OBJPROP_CORNER, 1);
ObjectSet(name_8, OBJPROP_XDISTANCE, 15);
ObjectSet(name_8, OBJPROP_YDISTANCE, 210);

return(0);
}

欢迎访问 外 汇 邦 WWW.WaiHuiBang.com
本文标题:EA(原码)学习 - MT4平台MQL4编程学习
本文地址:http://www.ea666.cn/fxschool/autotrading/mql4/40684.html

相关文章

  • 外汇EA编程教程——MQL语法

    什么是MQL?MetaQuotes Language 4 (MQL4)语言是MT4软件为了编写出执行交易策略的内置编程语言。我们可以通过编写EA(Expert Advisors)来控制MT4客户端按照我们的预先定义好的交易策略来执行外汇交易。另外,我们可以...

    MQL4编程学习
  • 外汇MT4 EA设计策略80条

      请仔细研究大腕们使用的80条外汇交易策略,并记住你只需要四样工具:柱图,均线背离,枢轴点和趋势线分析。不需要更多,简简单单。不要受人误导而相信别的。这个世界到处是"怀疑的托马斯",他们指手划脚却从未赚到过一文...

    MQL4编程学习
  • MT4平台的优势在哪里

    我们在外汇交易的时候,选择一个最好的外汇平台就像是在战场上选择最好的武器一样的重要,有了利器,交易者才能在交易当中获得成功,在外汇在线交易的市场当中,交易者有很多的交易平台可以使用,但是在所有的外汇平台当中,MT4...

    MQL4编程学习
  • 挂外汇EA需要什么样配置的电脑或服务器

    由于外汇EA需要24小时运行,所以需要稳定性强的电脑,不要运行着、运行着在关键时刻就死机了。 想想如果到了开仓信号出现时,电脑却死机了。或者是需要平仓时,电脑系统崩溃了。这是什么样的损失。 电脑的配置不要求高,但必...

    MQL4编程学习
  • 高盛600名交易员如今只剩2人,如何看待EA交易?

    高盛纽约总部交易大厅曾同时容纳600多位交易员,而现在已是人去楼空。2000年顶峰时期,高盛在纽约总部的美国现金股票交易柜台就雇佣了600名交易员,替投行金主的大额订单进行股票买卖操作。但时至如今,这里...

    MQL4编程学习
你可能感兴趣