#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link "http://www.mql5.com"
#property version "1.00"
#property strict
#define MAGICMA 2017303
input double Lots = 0.1;
input int LossRate = 2;
input double myLossStopB = 5;
input double myTakeProfitB = 6;
input double mycloseorderfrofitB = 7;
int myLossStop = 0;
int myTakeProfit = 0;
int mycloseorderfrofit = 0;
double mySPREAD = 0;
double myMINLOT = 0;
double myMAXLOT = 0;
double myiatr = 0;
int OnInit()
{
EventSetTimer(60);
mySPREAD = MarketInfo(Symbol(),MODE_SPREAD);
myMINLOT = MarketInfo(Symbol(),MODE_MINLOT);
myMAXLOT = MarketInfo(Symbol(),MODE_MAXLOT);
Print("------------------------------------------------>start");
string strcom = "交易公司名称:["+AccountCompany()+"] 商品名称:["+Symbol()+"]商品点差:["+DoubleToStr(MarketInfo(Symbol(),MODE_SPREAD),5)+"] "+" 平仓点差:["+DoubleToStr(MarketInfo(Symbol(),MODE_STOPLEVEL),3)+"]杠杆比例:["+AccountLeverage()+"] "+" 最";
Print(strcom);
iSetLable("lablecom",strcom,5,40,10,"Verdana",Olive);
string strlot = "杠杆比例:["+AccountLeverage()+"] "+" 最小允许标准手数["+DoubleToStr(myMINLOT,4)+"] "+" 最大允许标准手数:["+DoubleToStr(myMAXLOT,4)+"]";
iSetLable("lablelot",strlot,5,40,10,"Verdana",Olive);
mycloseorderfrofit = mySPREAD*mycloseorderfrofitB;
if(myMAXLOT/myMINLOT>=1000){
Print(strlot);
}else{
string msg = "手数空间太小,不建议使用本EA。";
string title = "交易提示窗口";
int MsgBoxInfo=MessageBox(msg,title,MB_OK|MB_ICONWARNING);
PlaySound("alert.wav");
printf("message:"+MsgBoxInfo);
return (INIT_FAILED);
}
myiatr = iATR(NULL,0,28,0);
string s = "mySPREAD*Point*10/Close[0]:["+DoubleToStr(mySPREAD*Point/Close[0]*10,4)+"] < iATR/Close[0]:["+DoubleToStr(myiatr/Close[0],4)+"]";
iSetLable("lable20b",s,5,100,10,"Verdana",Olive);
if(myiatr>mySPREAD*Point*10){
Print(s);
}
else{
string str = "波幅太小,不建议使用本EA";
string title = "交易提示窗口";
int MsgBoxInfo=MessageBox(str,title,MB_OK|MB_ICONWARNING);
Print(str);
return (INIT_FAILED);
}
return(INIT_SUCCEEDED);
}
double i=0.1;
string style = "Sell";
double oldOrderProfit = 0;
int oldOrderID = -1;
double bmyLossStop = 0;
double bmyTakeProfit = 0;
void OnTick()
{
ObjectDelete("Askline");
OrderSelect(OrdersTotal()-1,SELECT_BY_POS);
iSetLable("lable0","星期"+DayOfWeek()+" 市场时间:"+Year()+"-"+Month()+"-"+Day()+" "+ Hour()+":"+Minute()+":"+Seconds(),200,0,9,"Verdana",Red);
iSetLable("lable1","当前订单盈亏:"+DoubleToStr(OrderProfit(),2),5,20,10,"Verdana",Olive);
iSetLable("lable2","当前价格:"+DoubleToStr(Close[0],4),5,40,10,"Verdana",Olive);
string s = "当前点差:"+DoubleToStr(mySPREAD,4)+" 价差:"+DoubleToStr(mySPREAD*Point,4)+"小数位数:"+DoubleToStr(MarketInfo(Symbol(),MODE_DIGITS));
iSetLable("lable20",s,5,60,10,"Verdana",Olive);
myiatr = iATR(NULL,0,14,0);
s = "mySPREAD*Point*10/Close[0]:["+DoubleToStr(mySPREAD*Point/Close[0]*10,4)+"] < iATR/Close[0]:["+DoubleToStr(myiatr/Close[0],4)+"]";
iSetLable("lable20b",s,5,100,10,"Verdana",Olive);
Print(s);
string HLineBarString="Askline";
ObjectCreate(HLineBarString,OBJ_HLINE,0,Time[0],Ask);
ObjectSet(HLineBarString,OBJPROP_COLOR,Red);
ObjectSet(HLineBarString,OBJPROP_BACK,false);
if(Bars<100 || IsTradeAllowed()==false)
return;
int myHistoryOrderTotal=OrdersHistoryTotal();
int myOrdersTotal = OrdersTotal();
int myOrders = 0;
double myOldOrderProfit = 0;
double myOldLot = myMINLOT;
if(myOrdersTotal==0) myOrders = 0;
else if(myOrdersTotal>0){
for(int MSLCnt=myOrdersTotal-1;MSLCnt>=0;MSLCnt--){
if (OrderSelect(MSLCnt,SELECT_BY_POS)==true||OrderMagicNumber()==MAGICMA || OrderSymbol()==Symbol()){
myOrders++;
}
}
}
if(myOrders==0){
if(MathRand()%2==0){
style = "Buy";
}else style = "Sell";
if(myHistoryOrderTotal>0){
for(int MSLCnt=myHistoryOrderTotal-1;MSLCnt>=0;MSLCnt--){
if (OrderSelect(MSLCnt,SELECT_BY_POS,MODE_HISTORY)==true && OrderMagicNumber()==MAGICMA && OrderSymbol()==Symbol()){
oldOrderID = MSLCnt;
myOldOrderProfit = OrderProfit();
myOldLot = OrderLots();
break;
}
}
}
if (myOldOrderProfit>=0) i=myMINLOT;
else i = myOldLot*LossRate;
bmyLossStop = DoubleToStr(myiatr,4);
bmyTakeProfit = DoubleToStr(myiatr*1.2,4);
if(i<myMAXLOT){
iOpenOrdersByATR(style,i,bmyLossStop,bmyTakeProfit);
}
else{
double ii=i;
for(ii;ii>myMAXLOT;ii=ii-myMAXLOT-1){
iOpenOrdersByATR(style,myMAXLOT-1,bmyLossStop,bmyTakeProfit);
}
iOpenOrdersByATR(style,ii,bmyLossStop,bmyTakeProfit);
}
}else if(myOrders>0 && MathAbs(Close[0]-OrderOpenPrice())>mySPREAD*Point*10){
iCloseOrders("All");
}
}
void iSetLable(string LableName,string LableDoc,int LableX,int LableY, int DocSize,string DocStyle,color DocColor) {
ObjectCreate(LableName, OBJ_LABEL, 0, 0, 0);
ObjectSetText(LableName,LableDoc,DocSize,DocStyle,DocColor);
ObjectSet(LableName, OBJPROP_XDISTANCE, LableX);
ObjectSet(LableName, OBJPROP_YDISTANCE, LableY);
}
void iOpenOrders(string myType,double myLots,int myLossStop,int myTakeProfit) {
int mySPREAD=MarketInfo(Symbol(),MODE_SPREAD);
double BuyLossStop=Ask-myLossStop*Point;
double BuyTakeProfit=Ask+myTakeProfit*Point;
double SellLossStop=Bid+myLossStop*Point;
double SellTakeProfit=Bid-myTakeProfit*Point;
if (myLossStop<=0){
BuyLossStop=0;
SellLossStop=0;
} if (myTakeProfit<=0){
BuyTakeProfit=0;
SellTakeProfit=0;
} if (myType=="Buy"){
OrderSend(Symbol(),OP_BUY,myLots,Ask,mySPREAD,BuyLossStop,BuyTakeProfit,NULL,MAGICMA);
}
if (myType=="Sell"){
OrderSend(Symbol(),OP_SELL,myLots,Bid,mySPREAD,SellLossStop,SellTakeProfit,NULL,MAGICMA);
}
}
void iOpenOrdersByProsion(string myType,double myLots,double myLossStop,double myTakeProfit) {
int mySPREAD=MarketInfo(Symbol(),MODE_SPREAD);
double BuyLossStop=Ask*(1-myLossStop);
double BuyTakeProfit=Ask*(1+myTakeProfit);
double SellLossStop=Bid*(1+myLossStop);
double SellTakeProfit=Bid*(1-myTakeProfit);
if (myLossStop<=0){
BuyLossStop=0;
SellLossStop=0;
} if (myTakeProfit<=0){
BuyTakeProfit=0;
SellTakeProfit=0;
} if (myType=="Buy"){
OrderSend(Symbol(),OP_BUY,myLots,Ask,mySPREAD,BuyLossStop,BuyTakeProfit,NULL,MAGICMA);
}
if (myType=="Sell"){
OrderSend(Symbol(),OP_SELL,myLots,Bid,mySPREAD,SellLossStop,SellTakeProfit,NULL,MAGICMA);
}
}
void iOpenOrdersByATR(string myType,double myLots,double myLossStop,double myTakeProfit){
int mySPREAD=MarketInfo(Symbol(),MODE_SPREAD);
double BuyLossStop=Ask-myLossStop;
double BuyTakeProfit=Ask+myTakeProfit;
double SellLossStop=Bid+myLossStop;
double SellTakeProfit=Bid-myTakeProfit;
if (myLossStop<=0){
BuyLossStop=0;
SellLossStop=0;
} if (myTakeProfit<=0){
BuyTakeProfit=0;
SellTakeProfit=0;
} if (myType=="Buy"){
OrderSend(Symbol(),OP_BUY,myLots,Ask,mySPREAD,BuyLossStop,BuyTakeProfit,NULL,MAGICMA);
Print(Symbol()+"myLots:"+myLots+"Ask:"+Ask+" mySPREAD:"+mySPREAD+" BuyLossStop:"+BuyLossStop+" BuyTakeProfit:"+BuyTakeProfit);
}
if (myType=="Sell"){
OrderSend(Symbol(),OP_SELL,myLots,Bid,mySPREAD,SellLossStop,SellTakeProfit,NULL,MAGICMA);
Print(Symbol()+"myLots:"+myLots+"Bid:"+Bid+" mySPREAD:"+mySPREAD+" SellLossStop:"+SellLossStop+" SellTakeProfit:"+SellTakeProfit);
}
ShowError();
}
int CO_cnt;
void iCloseOrders(string myType) {
if (OrderSelect(OrdersTotal()-1,SELECT_BY_POS)==false) return;
if(OrderMagicNumber()!=MAGICMA || OrderSymbol()!=Symbol()) return;
if (myType=="All") {
for(CO_cnt=OrdersTotal();CO_cnt>=0;CO_cnt--) {
if(OrderSelect(CO_cnt,SELECT_BY_POS)==false) continue;
else OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),0);
}
oldOrderID = CO_cnt;
}
if (myType=="Buy"){
for(CO_cnt=OrdersTotal();CO_cnt>=0;CO_cnt--) {
if(OrderSelect(CO_cnt,SELECT_BY_POS)==false) continue;
else if (OrderType()==0) OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),0);
}
}
if (myType=="Sell"){
for(CO_cnt=OrdersTotal();CO_cnt>=0;CO_cnt--) {
if(OrderSelect(CO_cnt,SELECT_BY_POS)==false) continue;
else if (OrderType()==1) OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),0);
}
}
if (myType=="Profit"){
for(CO_cnt=OrdersTotal();CO_cnt>=0;CO_cnt--) {
if(OrderSelect(CO_cnt,SELECT_BY_POS)==false) continue;
else if (OrderProfit()>0) OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),0);
}
}
if (myType=="Loss") {
for(CO_cnt=OrdersTotal();CO_cnt>=0;CO_cnt--) {
if(OrderSelect(CO_cnt,SELECT_BY_POS)==false) continue;
else if (OrderProfit()<0) OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),0);
}
}
}
int LineNo=0;
void iDrawLine (int myFirstTime,double myFirstPrice,int mySecondTime,double mySecondPrice) {
string myObjectName="Line"+LineNo;
ObjectCreate(myObjectName,OBJ_TREND,0,myFirstTime,myFirstPrice,mySecondTime,mySecondPrice);
ObjectSet(myObjectName,OBJPROP_COLOR,Red);
ObjectSet(myObjectName,OBJPROP_STYLE,STYLE_DOT);
ObjectSet(myObjectName,OBJPROP_WIDTH, 1);
ObjectSet(myObjectName,OBJPROP_BACK,false);
ObjectSet(myObjectName,OBJPROP_RAY,false);
LineNo++;
}
void iDrawSign(string mySignal,double myPrice) {
if (mySignal=="Buy") {
ObjectCreate("BuyPoint-"+Time[0],OBJ_ARROW,0,Time[0],myPrice);
ObjectSet("BuyPoint-"+Time[0],OBJPROP_COLOR,Green);
ObjectSet("BuyPoint-"+Time[0],OBJPROP_ARROWCODE,241);
} if (mySignal=="Sell") {
ObjectCreate("SellPoint-"+Time[0],OBJ_ARROW,0,Time[0],myPrice);
ObjectSet("SellPoint-"+Time[0],OBJPROP_COLOR,Red);
ObjectSet("SellPoint-"+Time[0],OBJPROP_ARROWCODE,242);
} if (mySignal=="GreenMark") {
ObjectCreate("GreenMark-"+Time[0],OBJ_ARROW,0,Time[0],myPrice);
ObjectSet("GreenMark-"+Time[0],OBJPROP_COLOR,Green);
ObjectSet("GreenMark-"+Time[0],OBJPROP_ARROWCODE,162);
} if (mySignal=="RedMark") {
ObjectCreate("RedMark-"+Time[0],OBJ_ARROW,0,Time[0],myPrice);
ObjectSet("RedMark-"+Time[0],OBJPROP_COLOR,Red);
ObjectSet("RedMark-"+Time[0],OBJPROP_ARROWCODE,162);
}
}
void iMoveStopLoss(int myStopLoss){
int MSLCnt;
if (OrderSelect(OrdersTotal()-1,SELECT_BY_POS)==false) return;
if (OrdersTotal()>0){
for(MSLCnt=OrdersTotal();MSLCnt>=0;MSLCnt--){
if (OrderSelect(MSLCnt,SELECT_BY_POS)==false) continue;
else{
if (OrderProfit()>0 && OrderType()==0 && ((Close[0]-OrderStopLoss())>((2*myStopLoss)*Point))){
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*myStopLoss,OrderTakeProfit(),0);
Print("===========>");
}
if (OrderProfit()>0 && OrderType()==1 && ((OrderStopLoss()-Close[0])>((2*myStopLoss)*Point))){
OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*myStopLoss,OrderTakeProfit(),0);
Print("============>>");
}
}
}
}
}
void ShowError()
{
string description;
int err=GetLastError();
switch(err)
{
case 0: return;
case 1: description="没有错误返回"; break;
case 2: description="没有错误返回,但结果不明"; break;
case 3: description="一般错误"; break;
case 4: description="无效交易参量"; break;
case 5: description="交易服务器繁忙"; break;
case 6: description="客户终端旧版本"; break;
case 7: description="没有连接服务器"; break;
case 8: description="没有权限"; break;
case 9: description="请求国语频繁"; break;
case 64: description="交易运行故障"; break;
case 65: description="账户禁止"; break;
case 128: description="无效超时"; break;
case 129: description="无效价格"; break;
case 130: description="无效停止"; break;
case 131: description="无效交易量"; break;
case 132: description="市场关闭"; break;
case 133: description="交易被禁止"; break;
case 134: description="资金不足"; break;
case 135: description="价格改变"; break;
case 136: description="开价"; break;
case 137: description="经纪繁忙"; break;
case 138: description="重新开价"; break;
case 139: description="订单被锁定"; break;
case 140: description="只允许看涨仓位"; break;
case 141: description="过多请求"; break;
case 145: description="因为过于接近市场,修改被否定"; break;
case 146: description="交易文本已满"; break;
case 147: description="时间周期被经纪否定"; break;
default : description="默认错误"; break;
}
string field=" ";
string msg="错误码:#"+err+" "+description+field;
string title="下单错误"; if (AccountNumber()>0)title=AccountNumber()+": "+title;
MessageBox(msg,title,MB_OK|MB_ICONERROR);
}
void OnTimer()
{
}
double OnTester()
{
double ret=0.0;
return(ret);
}
void OnChartEvent(const int id,
const long &lparam,
const double &dparam,
const string &sparam)
{
}
void OnDeinit(const int reason)
{
EventKillTimer();
}