|
下面是一个一键平仓脚本,默认的是“all”平掉所有的持仓单,如果希望平掉多单,空单,盈利单,亏损单,只需要修改onstart函数中iCloseOrders中的类型。
- //+------------------------------------------------------------------+
- //| close01.mq4 |
- //| geekquant |
- //| https://www.geekquant.com |
- //+------------------------------------------------------------------+
- #property copyright "geekquant"
- #property link "https://www.geekquant.com"
- #property version "1.00"
- #property strict
- #property show_inputs
- //+------------------------------------------------------------------+
- //| Script program start function |
- //+------------------------------------------------------------------+
- void OnStart()
- {
- iCloseOrders("All");
-
- }
- //+------------------------------------------------------------------+
- void iCloseOrders(string myType)
- {
- int i;
- for (i=OrdersTotal()-1; i>=0; i--)
- {
-
- if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)
- continue;
- else
- {
- //--------------平掉所有单----------------------+
- if(myType=="All")
- {
- OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),0);
- }
- //--------------平掉多单----------------------+
- if(myType=="Buy")//平掉所有多单
- {
- if(OrderType()==OP_BUY)
- OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),0);
- }
- //--------------平掉空单----------------------+
- if(myType=="Sell")//平掉所有空单
- {
-
- if(OrderType()==OP_SELL)
- OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),0);
- }
- //--------------平掉盈利单----------------------+
- if(myType=="Profit")//平掉所有盈利单
- {
- if(OrderProfit()>0)
- OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),0);
- }
- }
- //--------------平掉亏损单----------------------+
- if(myType=="Loss")//平掉所亏损单
- {
- if(OrderProfit()<0)
- OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),0);
- }
- }
- }
-
-
复制代码
|
|