给出源码,你们可以看看
- //+------------------------------------------------------------------+
- //| 净值监控V1.mq4 |
- //| Copyright 2012, Goddz Corp. |
- //+------------------------------------------------------------------+
- #property copyright "Copyright 2012, Goddz Corp."
- extern int 参考净值 = 0; //当净值小于此值时全平全撤 或 警报提醒
- extern bool 全平全撤 = true; //true 表示净值达到后全平,false表示净值达到后只报警提示
- int preEquity = 0;
- int iniEquity = 0;
- bool isalertOn = true;
- datetime closetime = 0;
- bool isCloseOk = false;
- int id = 0;
- //+------------------------------------------------------------------+
- //| expert initialization function |
- //+------------------------------------------------------------------+
- int init()
- {
- //----
- alert("***------------------------。");
- alert("***------------------------。");
-
- if (参考净值 > AccountEquity())
- {
- alert("***参考净值不能大于当前净值。");
- 参考净值 = 0;
- return(0);
- }
- if (preEquity != 参考净值)
- {
- alert("***修改参考净值为:", 参考净值);
- closetime = 0;
- preEquity = 参考净值;
- }
- iniEquity = AccountEquity();
- //----
- return(0);
- }
- //+------------------------------------------------------------------+
- //| expert deinitialization function |
- //+------------------------------------------------------------------+
- int deinit()
- {
- //----
-
- //----
- return(0);
- }
- //+------------------------------------------------------------------+
- //| expert start function |
- //+------------------------------------------------------------------+
- int start()
- {
- //----
- if (参考净值 > 0 && AccountEquity() < 参考净值 && closetime == 0) //iniEquity >= 参考净值 &&
- {
- closetime = TimeCurrent(); //记录信息出现时间
- isCloseOk = false;
- Print("***净值达到设定值***", AccountEquity());
- id = 0;
- }
-
- if (closetime > 0)
- {
- if (全平全撤)
- {
- if (!isCloseOk)
- {
- CloseAndDeleteAll();
- if (OrdersTotal() == 0)
- {
- isCloseOk = true;
- }
- }
- if (isCloseOk)
- {
- id++;
- alert("***净值达到设定值,请重新初始化参考净值*** ", id);
- }
- }
- else
- {
- id++;
- alert("***净值达到设定值***", id);
- }
- }
- //----
- return(0);
- }
- //+------------------------------------------------------------------+
- void CloseAndDeleteAll()
- {
- for (int i = OrdersTotal() - 1; i >= 0; i--)
- {
- if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
- {
- int ticket = OrderTicket();
- bool ret = true;
- if (OrderType() == OP_BUY)
- {
- ret = OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 1000, CLR_NONE);
- }
- else if (OrderType() == OP_SELL)
- {
- ret = OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 1000, CLR_NONE);
- }
- else if (OrderType() > OP_SELL)
- {
- ret = OrderDelete( OrderTicket() );
- }//挂单
- if (ret)
- {
- break;
- }
- //失败
- IsContinueByErrcode( GetLastError(), ticket);
- }
- }//for
- RefreshRates();
- }
- bool IsContinueByErrcode( int errCode, int ticket)
- {
- if (isalertOn)
- {
- switch ( errCode )
- {
- case 129: if (isalertOn) alert(Symbol() + "-错误:无效的价格,交易失败.", " 定单号:" + ticket);break;
- case 131: if (isalertOn) alert(Symbol() + "-错误:无效的交易量,交易失败.", " 定单号:" + ticket);break;
- case 134: if (isalertOn) alert(Symbol() + "-错误:资金不足,交易失败.", " 定单号:" + ticket);break;
- case 130: if (isalertOn) alert(Symbol() + "-错误:无效的止损,止赢或挂单价离市价近.", " 定单号:" + ticket);break;
- case 138: if (isalertOn) alert(Symbol() + "-错误:价格过期,交易失败.", " 定单号:" + ticket);break;
- case 148: if (isalertOn) alert(Symbol() + "-错误:帐户定单过多,交易失败.", " 定单号:" + ticket);break;
- default :
- {
- if (isalertOn) alert(Symbol() + "-错误:其它错误#", errCode, " 定单号:" + ticket);break;
- }
- break;
- }
- }
-
- return (false);
- }
|