有您的持续关注
我们会做得更好

一套智能交易EA编程模板



//+------------------------------------------------------------------+
 //| EA编程模板.mq4 |
 //| Copyright 2014, MetaQuotes Software Corp. |
 //| http://www.mql5.com |
 //+------------------------------------------------------------------+
 #property copyright "www.125808047.com"
 #property link "http://www.125808047.com/"
 #property version "1.00"
 #property strict
int id_1=123;
 int id_2=321;
 int bars;
 int n,m;
 int ticket_buy_1,ticket_sell_1;
 extern double lots=0.1;
 extern int sl = 30;
 extern int tp =40;
 extern int o =20;
 double point ;
 double stoploss,takeprofit;
int b1,s1;
 //+------------------------------------------------------------------+
 //| Expert initialization function |
 //+------------------------------------------------------------------+
 int OnInit()
 {
 //---
 bars=Bars; // 控制一根k线只开一张单
 if(Digits==5||Digits==3)
 point=Point*10;
 else
 point=Point;
 //---
 return(INIT_SUCCEEDED);
 }
 //+------------------------------------------------------------------+
 //| Expert deinitialization function |
 //+------------------------------------------------------------------+
 void OnDeinit(const int reason)
 {
 //---
}
 //+------------------------------------------------------------------+
 //| Expert tick function |
 //+------------------------------------------------------------------+
 void OnTick()
 {
 //---
 if(OrdersTotal()==0)
 {b1=0;s1=0;}
 myordersTotal_buy();
 myordersTotal_sell();
if( b1==0) // 无在仓单buy单
 {
 if(buy_Rule_1()== true && bars !=Bars)
 {
 ticket_buy_1=OrderSend(Symbol(),OP_BUY,lots,Ask,3,0,0,"订单1",id_1,0,Green);
 if(ticket_buy_1>0)
 {
 Set_sl_tp_buy();
 bars=Bars;
m=0; // 多单移动止损初始化
 // bp1=Ask; 记录下开单价格 如果后面要写加码策略这个得要
}
}
}
if( b1!=0) // 有在仓单buy单
 {
 set_on_loss_buy(); //多单有在仓单保护止损
 TLS_buy(); // 有在仓单后调用移动止损
 }
 if(Exit_buy()==true)
 {
 close_buy();
}
//+------------------------------------------------------------------+
if(s1==0) // 无在仓单sell单
 {
 if(sell_Rule_1()== true && bars !=Bars)
 {
 ticket_sell_1=OrderSend(Symbol(),OP_SELL,lots,Bid,3,0,0,"订单1",id_2,0,Green);
 if(ticket_sell_1>0)
 {
 Set_sl_tp_sell();
 bars=Bars;
 n=0; // 空单移动止损初始化
}
}
 }
if( s1!=0)// 有在仓单sell单
 {
 set_on_loss_sell(); //多单有在仓单保护止损
 TLS_sell(); // 有在仓单后调用移动止损
 }
 if(Exit_sell()==true)
 {
 close_sell();
}
}
 //+------------------------------------------------------------------+
 //
 // 开仓检测
 //
 //+------------------------------------------------------------------+
 void myordersTotal_buy()
 {
b1=0;
 for (int i = 0; i < OrdersTotal(); i++)
 {
 if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if (OrderSymbol()==Symbol() && OrderType()==OP_BUY)
 {
if(OrderMagicNumber()==id_1)
 {b1=OrderTicket(); }
}
 }
 }
void myordersTotal_sell()
 {
s1=0;
 for (int i = 0; i < OrdersTotal(); i++) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break; if (OrderSymbol()==Symbol() && OrderType()==OP_SELL ) { if(OrderMagicNumber()==id_2) {s1=OrderTicket(); } } } } //+------------------------------------------------------------------+ // // 下单条件 // //+------------------------------------------------------------------+ bool buy_Rule_1() // 多单下单条件 { double ma10=iMA(NULL,0,10,0,MODE_EMA,PRICE_CLOSE,1); if(Close[1]>ma10&&Open[1]<ma10)
 return(true);
 else
 return(false);
}
bool sell_Rule_1() // 空单下单条件
 {
 double ma10=iMA(NULL,0,10,0,MODE_EMA,PRICE_CLOSE,1);
 if(Close[1]<ma10&&Open[1]>ma10)
 return(true);
 else
 return(false);
}
//+------------------------------------------------------------------+
 //
 // 修改止损止盈
 //
 //+------------------------------------------------------------------+
void Set_sl_tp_buy() // 多单下单成功后修改 止损 止盈
 {
 for(int z=0;z<OrdersTotal();z++) { if(OrderSelect(z,SELECT_BY_POS,MODE_TRADES)==false) break; if(OrderSymbol() == Symbol()&& OrderType() ==OP_BUY && OrderMagicNumber()==id_1) { if(sl>0)
 stoploss=OrderOpenPrice()-sl*point;
 else
 stoploss=OrderStopLoss();
if(tp>0)
 takeprofit=OrderOpenPrice()+tp*point;
 else
 takeprofit=OrderOpenPrice();
if(!OrderModify(OrderTicket(),OrderOpenPrice(),stoploss,takeprofit,0,Blue))
 Print("OrderModify error ",GetLastError());
 return;
 }
}
}
void Set_sl_tp_sell() // 空单下单成功后修改 止损 止盈
 {
 for(int z=0;z<OrdersTotal();z++) { if(OrderSelect(z,SELECT_BY_POS,MODE_TRADES)==false) break; if(OrderSymbol() == Symbol()&& OrderType() ==OP_SELL && OrderMagicNumber()==id_2) { if(sl>0)
 stoploss=OrderOpenPrice()+sl*point;
 else
 stoploss=OrderStopLoss();
if(tp>0)
 takeprofit=OrderOpenPrice()-tp*point;
 else
 takeprofit=OrderOpenPrice();
if(!OrderModify(OrderTicket(),OrderOpenPrice(),stoploss,takeprofit,0,Red))
 Print("OrderModify error ",GetLastError());
 return;
}
}
}
 //+------------------------------------------------------------------+
 //
 // 条件平仓
 //
 //+------------------------------------------------------------------+
bool Exit_buy() // 多单平仓条件
 {
 // double ma10=iMA(NULL,0,10,0,MODE_SMMA,PRICE_MEDIAN,1);
 double buling=iBands(NULL,0,20,2,0,PRICE_CLOSE,MODE_UPPER,0);
// if(Close[1]ma10)
 if(Bid>buling)
 return(true);
 else
 return(false);
}
bool Exit_sell() // 空单平仓条件
 {
 // double ma10=iMA(NULL,0,10,0,MODE_SMMA,PRICE_MEDIAN,1);
double buling=iBands(NULL,0,20,2,0,PRICE_CLOSE,MODE_LOWER,0);
 // if(Close[1]>ma10 && Open[1]<ma10)
 if(Ask=0;z--)
 {
 if(OrderSelect(z,SELECT_BY_POS,MODE_TRADES)==false) break;
 if(OrderSymbol() == Symbol()&& OrderType() ==OP_BUY && OrderMagicNumber()==id_1)
 {
 if(!OrderClose(OrderTicket(),OrderLots(),Bid,3,White))
 Print("OrderClose error ",GetLastError());
 }
 break;
 }
}
void close_sell() // 空单平仓
 {
 for(int z=OrdersTotal()-1;z>=0;z--)
 {
 if(OrderSelect(z,SELECT_BY_POS,MODE_TRADES)==false) break;
 if(OrderSymbol() == Symbol()&& OrderType() ==OP_SELL && OrderMagicNumber()==id_2)
 {
 if(!OrderClose(OrderTicket(),OrderLots(),Ask,3,White))
 Print("OrderClose error ",GetLastError());
 }
 break;
 }
}
 //+------------------------------------------------------------------+
 //
 // 保护止损
 //
 //+------------------------------------------------------------------+
void set_on_loss_buy() // 多单保护止损
 {
 for(int z=0;z<OrdersTotal();z++)
 {
 if(OrderSelect(z,SELECT_BY_POS,MODE_TRADES)==false) break;
 if( OrderSymbol() == Symbol()&& OrderType() ==OP_BUY && OrderMagicNumber() ==id_1 )
 if( OrderStopLoss() ==0 || OrderStopLoss() < OrderOpenPrice() ) if(Bid - OrderOpenPrice()>= o*point )
 {
 if(!OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+10*point,OrderTakeProfit(),0,Blue))
 Print("OrderModify error ",GetLastError());
 return;
 }
}
}
void set_on_loss_sell() // 空单保护止损
 {
 for(int z=0;z<OrdersTotal();z++) { if(OrderSelect(z,SELECT_BY_POS,MODE_TRADES)==false) break; if( OrderSymbol() == Symbol()&& OrderType() ==OP_SELL && OrderMagicNumber() ==id_2 ) if( OrderStopLoss() ==0 || OrderStopLoss() > OrderOpenPrice() )
 if(OrderOpenPrice() - Ask >= o*point )
 {
 if(!OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-10*point,OrderTakeProfit(),0,Blue))
 Print("OrderModify error ",GetLastError());
 return;
 }
}
}
 //+------------------------------------------------------------------+
 //
 // 移动止损
 //
 //+------------------------------------------------------------------+
void TLS_buy() // 多单移动止损
{
 for(int z=0;z<OrdersTotal();z++) { if(OrderSelect(z,SELECT_BY_POS,MODE_TRADES)==false) break; if( OrderSymbol() == Symbol()&& OrderType() ==OP_BUY && OrderMagicNumber() ==id_1 ) if( OrderStopLoss() !=0 || OrderStopLoss() >= OrderOpenPrice() )
 if(Bid - OrderOpenPrice()>= (20+n)*point )
 {
 if(!OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+10*point,OrderTakeProfit(),0,Blue))
 Print("OrderModify error ",GetLastError());
n=n+(20-10);
 return;
}
 }
}
void TLS_sell() // 空单移动止损
{
 for(int z=0;z<OrdersTotal();z++) { if(OrderSelect(z,SELECT_BY_POS,MODE_TRADES)==false) break; if( OrderSymbol() == Symbol()&& OrderType() ==OP_SELL && OrderMagicNumber() ==id_2 ) if( OrderStopLoss() !=0 ) if( OrderOpenPrice() - Ask >= (20+m)*point )
 {
 if(! OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+10*point,OrderTakeProfit(),0,Blue))
 Print("OrderModify error ",GetLastError());
m=m+(20-10);
 return;
}
 }
}

如您喜欢此文章请点下面分享按钮↴峰汇在线 » 一套智能交易EA编程模板
上一篇:
下一篇:
分享到:更多 ()