该函数执行多单或空单到达指定盈利数值全部平仓。
- extern double yingli=0;//设定盈利值
- //+------------------------------------------------------------------+
- //| Expert initialization function |
- //+------------------------------------------------------------------+
- int onInit()
- {
- //---
- //---
- return(INIT_SUCCEEDED);
- }
- //+------------------------------------------------------------------+
- //| Expert deinitialization function |
- //+------------------------------------------------------------------+
- void onDeinit(const int reason)
- {
- //---
- }
- //+------------------------------------------------------------------+
- //| Expert tick function |
- //+------------------------------------------------------------------+
- void onTick()
- {
- //---
- if (Profit_total_buy() >= yingli) iCloseOrders("Buy");
- if (Profit_total_sell()>= yingli) iCloseOrders("Sell");
- }
- //+------------------------------------------------------------------+
- //---
- double Profit_total_buy()
- {
- double Profit=0;
- for(int i=0;i<=OrdersTotal();i++)
- {
- if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
- if( OrderSymbol()!=Symbol()) continue;
- if(OrderType()==OP_BUY)
- Profit=Profit+OrderProfit();
- }
- return(Profit);
- }
- //---
- double Profit_total_sell()
- {
- double Profit=0;
- for(int i=0;i<=OrdersTotal();i++)
- {
- if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
- if( OrderSymbol()!=Symbol()) continue;
- if(OrderType()==OP_SELL)
- Profit=Profit+OrderProfit();
- }
- return(Profit);
- }
- //---
- void iCloseOrders(string myType)
- {
- if(OrderSelect(OrdersTotal()-1,SELECT_BY_POS,MODE_TRADES)==false) return;
- //-----------------------
- if (myType=="Buy")//平掉所有多头订单
- {
- for(int i=OrdersTotal()-1;i>=0;i--)
- {
- if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
- if( OrderSymbol()!=Symbol()) continue;
- //--- check order type
- if(OrderType()==OP_BUY)
- {
- if(!OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,White))
- Print("OrderClose error ",GetLastError());
- continue;
- }
- }
- }
- //-----------------------
- if (myType=="Sell")//平掉所有空头订单
- {
- for(int i=OrdersTotal()-1;i>=0;i--)
- {
- if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
- if( OrderSymbol()!=Symbol()) continue;
- //--- check order type
- if(OrderType()==OP_SELL)
- {
- if(!OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,Red))
- Print("OrderClose error ",GetLastError());
- continue;
- }
- }
- }
- }
|